1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.apache;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InterruptedIOException;
24 import java.io.OutputStream;
25 import java.io.UncheckedIOException;
26 import java.net.InetAddress;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.net.UnknownHostException;
30 import java.nio.charset.Charset;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.StandardCopyOption;
34 import java.util.Collections;
35 import java.util.Date;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40 import java.util.function.Function;
41 import java.util.regex.Matcher;
42
43 import org.apache.http.Header;
44 import org.apache.http.HttpEntity;
45 import org.apache.http.HttpEntityEnclosingRequest;
46 import org.apache.http.HttpHeaders;
47 import org.apache.http.HttpHost;
48 import org.apache.http.HttpResponse;
49 import org.apache.http.HttpStatus;
50 import org.apache.http.auth.AuthSchemeProvider;
51 import org.apache.http.auth.AuthScope;
52 import org.apache.http.client.CredentialsProvider;
53 import org.apache.http.client.HttpRequestRetryHandler;
54 import org.apache.http.client.HttpResponseException;
55 import org.apache.http.client.ServiceUnavailableRetryStrategy;
56 import org.apache.http.client.config.AuthSchemes;
57 import org.apache.http.client.config.CookieSpecs;
58 import org.apache.http.client.config.RequestConfig;
59 import org.apache.http.client.methods.CloseableHttpResponse;
60 import org.apache.http.client.methods.HttpGet;
61 import org.apache.http.client.methods.HttpHead;
62 import org.apache.http.client.methods.HttpOptions;
63 import org.apache.http.client.methods.HttpPut;
64 import org.apache.http.client.methods.HttpUriRequest;
65 import org.apache.http.client.utils.DateUtils;
66 import org.apache.http.client.utils.URIUtils;
67 import org.apache.http.config.Registry;
68 import org.apache.http.config.RegistryBuilder;
69 import org.apache.http.config.SocketConfig;
70 import org.apache.http.entity.AbstractHttpEntity;
71 import org.apache.http.entity.ByteArrayEntity;
72 import org.apache.http.impl.NoConnectionReuseStrategy;
73 import org.apache.http.impl.auth.BasicScheme;
74 import org.apache.http.impl.auth.BasicSchemeFactory;
75 import org.apache.http.impl.auth.DigestSchemeFactory;
76 import org.apache.http.impl.auth.KerberosSchemeFactory;
77 import org.apache.http.impl.auth.NTLMSchemeFactory;
78 import org.apache.http.impl.auth.SPNegoSchemeFactory;
79 import org.apache.http.impl.client.CloseableHttpClient;
80 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
81 import org.apache.http.impl.client.HttpClientBuilder;
82 import org.apache.http.impl.client.LaxRedirectStrategy;
83 import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
84 import org.apache.http.protocol.HttpContext;
85 import org.apache.http.util.EntityUtils;
86 import org.eclipse.aether.ConfigurationProperties;
87 import org.eclipse.aether.RepositorySystemSession;
88 import org.eclipse.aether.repository.AuthenticationContext;
89 import org.eclipse.aether.repository.Proxy;
90 import org.eclipse.aether.repository.RemoteRepository;
91 import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
92 import org.eclipse.aether.spi.connector.transport.GetTask;
93 import org.eclipse.aether.spi.connector.transport.PeekTask;
94 import org.eclipse.aether.spi.connector.transport.PutTask;
95 import org.eclipse.aether.spi.connector.transport.TransportTask;
96 import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractor;
97 import org.eclipse.aether.spi.connector.transport.http.HttpTransporter;
98 import org.eclipse.aether.spi.connector.transport.http.HttpTransporterException;
99 import org.eclipse.aether.spi.io.PathProcessor;
100 import org.eclipse.aether.transfer.NoTransporterException;
101 import org.eclipse.aether.transfer.TransferCancelledException;
102 import org.eclipse.aether.util.ConfigUtils;
103 import org.eclipse.aether.util.FileUtils;
104 import org.slf4j.Logger;
105 import org.slf4j.LoggerFactory;
106
107 import static java.util.Objects.requireNonNull;
108 import static org.eclipse.aether.spi.connector.transport.http.HttpConstants.CONTENT_RANGE_PATTERN;
109 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_FOLLOW_REDIRECTS;
110 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_HTTP_RETRY_HANDLER_NAME;
111 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED;
112 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_MAX_REDIRECTS;
113 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_USE_SYSTEM_PROPERTIES;
114 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_FOLLOW_REDIRECTS;
115 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED;
116 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_MAX_REDIRECTS;
117 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_USE_SYSTEM_PROPERTIES;
118 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.HTTP_RETRY_HANDLER_NAME_DEFAULT;
119 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.HTTP_RETRY_HANDLER_NAME_STANDARD;
120
121
122
123
124 final class ApacheTransporter extends AbstractTransporter implements HttpTransporter {
125 private static final Logger LOGGER = LoggerFactory.getLogger(ApacheTransporter.class);
126
127 private final ChecksumExtractor checksumExtractor;
128
129 private final PathProcessor pathProcessor;
130
131 private final AuthenticationContext repoAuthContext;
132
133 private final AuthenticationContext proxyAuthContext;
134
135 private final URI baseUri;
136
137 private final HttpHost server;
138
139 private final HttpHost proxy;
140
141 private final CloseableHttpClient client;
142
143 private final Map<?, ?> headers;
144
145 private final LocalState state;
146
147 private final boolean preemptiveAuth;
148
149 private final boolean preemptivePutAuth;
150
151 private final boolean supportWebDav;
152
153 @SuppressWarnings("checkstyle:methodlength")
154 ApacheTransporter(
155 RemoteRepository repository,
156 RepositorySystemSession session,
157 ChecksumExtractor checksumExtractor,
158 PathProcessor pathProcessor)
159 throws NoTransporterException {
160 this.checksumExtractor = checksumExtractor;
161 this.pathProcessor = pathProcessor;
162 try {
163 this.baseUri = new URI(repository.getUrl()).parseServerAuthority();
164 if (baseUri.isOpaque()) {
165 throw new URISyntaxException(repository.getUrl(), "URL must not be opaque");
166 }
167 this.server = URIUtils.extractHost(baseUri);
168 if (server == null) {
169 throw new URISyntaxException(repository.getUrl(), "URL lacks host name");
170 }
171 } catch (URISyntaxException e) {
172 throw new NoTransporterException(repository, e.getMessage(), e);
173 }
174 this.proxy = toHost(repository.getProxy());
175
176 this.repoAuthContext = AuthenticationContext.forRepository(session, repository);
177 this.proxyAuthContext = AuthenticationContext.forProxy(session, repository);
178
179 String httpsSecurityMode = ConfigUtils.getString(
180 session,
181 ConfigurationProperties.HTTPS_SECURITY_MODE_DEFAULT,
182 ConfigurationProperties.HTTPS_SECURITY_MODE + "." + repository.getId(),
183 ConfigurationProperties.HTTPS_SECURITY_MODE);
184 final int connectionMaxTtlSeconds = ConfigUtils.getInteger(
185 session,
186 ConfigurationProperties.DEFAULT_HTTP_CONNECTION_MAX_TTL,
187 ConfigurationProperties.HTTP_CONNECTION_MAX_TTL + "." + repository.getId(),
188 ConfigurationProperties.HTTP_CONNECTION_MAX_TTL);
189 final int maxConnectionsPerRoute = ConfigUtils.getInteger(
190 session,
191 ConfigurationProperties.DEFAULT_HTTP_MAX_CONNECTIONS_PER_ROUTE,
192 ConfigurationProperties.HTTP_MAX_CONNECTIONS_PER_ROUTE + "." + repository.getId(),
193 ConfigurationProperties.HTTP_MAX_CONNECTIONS_PER_ROUTE);
194 this.state = new LocalState(
195 session,
196 repository,
197 new ConnMgrConfig(
198 session, repoAuthContext, httpsSecurityMode, connectionMaxTtlSeconds, maxConnectionsPerRoute));
199
200 this.headers = ConfigUtils.getMap(
201 session,
202 Collections.emptyMap(),
203 ConfigurationProperties.HTTP_HEADERS + "." + repository.getId(),
204 ConfigurationProperties.HTTP_HEADERS);
205
206 this.preemptiveAuth = ConfigUtils.getBoolean(
207 session,
208 ConfigurationProperties.DEFAULT_HTTP_PREEMPTIVE_AUTH,
209 ConfigurationProperties.HTTP_PREEMPTIVE_AUTH + "." + repository.getId(),
210 ConfigurationProperties.HTTP_PREEMPTIVE_AUTH);
211 this.preemptivePutAuth = ConfigUtils.getBoolean(
212 session,
213 ConfigurationProperties.DEFAULT_HTTP_PREEMPTIVE_PUT_AUTH,
214 ConfigurationProperties.HTTP_PREEMPTIVE_PUT_AUTH + "." + repository.getId(),
215 ConfigurationProperties.HTTP_PREEMPTIVE_PUT_AUTH);
216 this.supportWebDav = ConfigUtils.getBoolean(
217 session,
218 ConfigurationProperties.DEFAULT_HTTP_SUPPORT_WEBDAV,
219 ConfigurationProperties.HTTP_SUPPORT_WEBDAV + "." + repository.getId(),
220 ConfigurationProperties.HTTP_SUPPORT_WEBDAV);
221 String credentialEncoding = ConfigUtils.getString(
222 session,
223 ConfigurationProperties.DEFAULT_HTTP_CREDENTIAL_ENCODING,
224 ConfigurationProperties.HTTP_CREDENTIAL_ENCODING + "." + repository.getId(),
225 ConfigurationProperties.HTTP_CREDENTIAL_ENCODING);
226 int connectTimeout = ConfigUtils.getInteger(
227 session,
228 ConfigurationProperties.DEFAULT_CONNECT_TIMEOUT,
229 ConfigurationProperties.CONNECT_TIMEOUT + "." + repository.getId(),
230 ConfigurationProperties.CONNECT_TIMEOUT);
231 int requestTimeout = ConfigUtils.getInteger(
232 session,
233 ConfigurationProperties.DEFAULT_REQUEST_TIMEOUT,
234 ConfigurationProperties.REQUEST_TIMEOUT + "." + repository.getId(),
235 ConfigurationProperties.REQUEST_TIMEOUT);
236 int retryCount = ConfigUtils.getInteger(
237 session,
238 ConfigurationProperties.DEFAULT_HTTP_RETRY_HANDLER_COUNT,
239 ConfigurationProperties.HTTP_RETRY_HANDLER_COUNT + "." + repository.getId(),
240 ConfigurationProperties.HTTP_RETRY_HANDLER_COUNT);
241 long retryInterval = ConfigUtils.getLong(
242 session,
243 ConfigurationProperties.DEFAULT_HTTP_RETRY_HANDLER_INTERVAL,
244 ConfigurationProperties.HTTP_RETRY_HANDLER_INTERVAL + "." + repository.getId(),
245 ConfigurationProperties.HTTP_RETRY_HANDLER_INTERVAL);
246 long retryIntervalMax = ConfigUtils.getLong(
247 session,
248 ConfigurationProperties.DEFAULT_HTTP_RETRY_HANDLER_INTERVAL_MAX,
249 ConfigurationProperties.HTTP_RETRY_HANDLER_INTERVAL_MAX + "." + repository.getId(),
250 ConfigurationProperties.HTTP_RETRY_HANDLER_INTERVAL_MAX);
251 String serviceUnavailableCodesString = ConfigUtils.getString(
252 session,
253 ConfigurationProperties.DEFAULT_HTTP_RETRY_HANDLER_SERVICE_UNAVAILABLE,
254 ConfigurationProperties.HTTP_RETRY_HANDLER_SERVICE_UNAVAILABLE + "." + repository.getId(),
255 ConfigurationProperties.HTTP_RETRY_HANDLER_SERVICE_UNAVAILABLE);
256 String retryHandlerName = ConfigUtils.getString(
257 session,
258 HTTP_RETRY_HANDLER_NAME_STANDARD,
259 CONFIG_PROP_HTTP_RETRY_HANDLER_NAME + "." + repository.getId(),
260 CONFIG_PROP_HTTP_RETRY_HANDLER_NAME);
261 boolean retryHandlerRequestSentEnabled = ConfigUtils.getBoolean(
262 session,
263 DEFAULT_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED,
264 CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED + "." + repository.getId(),
265 CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED);
266 int maxRedirects = ConfigUtils.getInteger(
267 session,
268 DEFAULT_MAX_REDIRECTS,
269 CONFIG_PROP_MAX_REDIRECTS + "." + repository.getId(),
270 CONFIG_PROP_MAX_REDIRECTS);
271 boolean followRedirects = ConfigUtils.getBoolean(
272 session,
273 DEFAULT_FOLLOW_REDIRECTS,
274 CONFIG_PROP_FOLLOW_REDIRECTS + "." + repository.getId(),
275 CONFIG_PROP_FOLLOW_REDIRECTS);
276 String userAgent = ConfigUtils.getString(
277 session, ConfigurationProperties.DEFAULT_USER_AGENT, ConfigurationProperties.USER_AGENT);
278
279 Charset credentialsCharset = Charset.forName(credentialEncoding);
280 Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
281 .register(AuthSchemes.BASIC, new BasicSchemeFactory(credentialsCharset))
282 .register(AuthSchemes.DIGEST, new DigestSchemeFactory(credentialsCharset))
283 .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
284 .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
285 .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
286 .build();
287 SocketConfig socketConfig =
288
289 SocketConfig.custom().setSoTimeout(requestTimeout).build();
290 RequestConfig requestConfig = RequestConfig.custom()
291 .setMaxRedirects(maxRedirects)
292 .setRedirectsEnabled(followRedirects)
293 .setRelativeRedirectsAllowed(followRedirects)
294
295 .setSocketTimeout(requestTimeout)
296
297 .setConnectTimeout(connectTimeout)
298
299 .setConnectionRequestTimeout(connectTimeout)
300 .setLocalAddress(getHttpLocalAddress(session, repository))
301 .setCookieSpec(CookieSpecs.STANDARD)
302 .build();
303
304 HttpRequestRetryHandler retryHandler;
305 if (HTTP_RETRY_HANDLER_NAME_STANDARD.equals(retryHandlerName)) {
306 retryHandler = new StandardHttpRequestRetryHandler(retryCount, retryHandlerRequestSentEnabled);
307 } else if (HTTP_RETRY_HANDLER_NAME_DEFAULT.equals(retryHandlerName)) {
308 retryHandler = new DefaultHttpRequestRetryHandler(retryCount, retryHandlerRequestSentEnabled);
309 } else {
310 throw new IllegalArgumentException(
311 "Unsupported parameter " + CONFIG_PROP_HTTP_RETRY_HANDLER_NAME + " value: " + retryHandlerName);
312 }
313 Set<Integer> serviceUnavailableCodes = new HashSet<>();
314 try {
315 for (String code : ConfigUtils.parseCommaSeparatedUniqueNames(serviceUnavailableCodesString)) {
316 serviceUnavailableCodes.add(Integer.parseInt(code));
317 }
318 } catch (NumberFormatException e) {
319 throw new IllegalArgumentException(
320 "Illegal HTTP codes for " + ConfigurationProperties.HTTP_RETRY_HANDLER_SERVICE_UNAVAILABLE
321 + " (list of integers): " + serviceUnavailableCodesString);
322 }
323 ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new ResolverServiceUnavailableRetryStrategy(
324 retryCount, retryInterval, retryIntervalMax, serviceUnavailableCodes);
325
326 HttpClientBuilder builder = HttpClientBuilder.create()
327 .setUserAgent(userAgent)
328 .setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
329 .setDefaultSocketConfig(socketConfig)
330 .setDefaultRequestConfig(requestConfig)
331 .setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
332 .setRetryHandler(retryHandler)
333 .setDefaultAuthSchemeRegistry(authSchemeRegistry)
334 .setConnectionManager(state.getConnectionManager())
335 .setConnectionManagerShared(true)
336 .setDefaultCredentialsProvider(toCredentialsProvider(server, repoAuthContext, proxy, proxyAuthContext))
337 .setProxy(proxy);
338 final boolean useSystemProperties = ConfigUtils.getBoolean(
339 session,
340 DEFAULT_USE_SYSTEM_PROPERTIES,
341 CONFIG_PROP_USE_SYSTEM_PROPERTIES + "." + repository.getId(),
342 CONFIG_PROP_USE_SYSTEM_PROPERTIES);
343 if (useSystemProperties) {
344 LOGGER.warn(
345 "Transport used Apache HttpClient is instructed to use system properties: this may yield in unwanted side-effects!");
346 LOGGER.warn("Please use documented means to configure resolver transport.");
347 builder.useSystemProperties();
348 }
349
350 final String expectContinue = ConfigUtils.getString(
351 session,
352 null,
353 ConfigurationProperties.HTTP_EXPECT_CONTINUE + "." + repository.getId(),
354 ConfigurationProperties.HTTP_EXPECT_CONTINUE);
355 if (expectContinue != null) {
356 state.setExpectContinue(Boolean.parseBoolean(expectContinue));
357 }
358
359 final boolean reuseConnections = ConfigUtils.getBoolean(
360 session,
361 ConfigurationProperties.DEFAULT_HTTP_REUSE_CONNECTIONS,
362 ConfigurationProperties.HTTP_REUSE_CONNECTIONS + "." + repository.getId(),
363 ConfigurationProperties.HTTP_REUSE_CONNECTIONS);
364 if (!reuseConnections) {
365 builder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE);
366 }
367
368 this.client = builder.build();
369 }
370
371
372
373
374 private InetAddress getHttpLocalAddress(RepositorySystemSession session, RemoteRepository repository) {
375 String bindAddress = ConfigUtils.getString(
376 session,
377 null,
378 ConfigurationProperties.HTTP_LOCAL_ADDRESS + "." + repository.getId(),
379 ConfigurationProperties.HTTP_LOCAL_ADDRESS);
380 if (bindAddress == null) {
381 return null;
382 }
383 try {
384 return InetAddress.getByName(bindAddress);
385 } catch (UnknownHostException uhe) {
386 throw new IllegalArgumentException(
387 "Given bind address (" + bindAddress + ") cannot be resolved for remote repository " + repository,
388 uhe);
389 }
390 }
391
392 private static HttpHost toHost(Proxy proxy) {
393 HttpHost host = null;
394 if (proxy != null) {
395 host = new HttpHost(proxy.getHost(), proxy.getPort());
396 }
397 return host;
398 }
399
400 private static CredentialsProvider toCredentialsProvider(
401 HttpHost server, AuthenticationContext serverAuthCtx, HttpHost proxy, AuthenticationContext proxyAuthCtx) {
402 CredentialsProvider provider = toCredentialsProvider(server.getHostName(), AuthScope.ANY_PORT, serverAuthCtx);
403 if (proxy != null) {
404 CredentialsProvider p = toCredentialsProvider(proxy.getHostName(), proxy.getPort(), proxyAuthCtx);
405 provider = new DemuxCredentialsProvider(provider, p, proxy);
406 }
407 return provider;
408 }
409
410 private static CredentialsProvider toCredentialsProvider(String host, int port, AuthenticationContext ctx) {
411 DeferredCredentialsProvider provider = new DeferredCredentialsProvider();
412 if (ctx != null) {
413 AuthScope basicScope = new AuthScope(host, port);
414 provider.setCredentials(basicScope, new DeferredCredentialsProvider.BasicFactory(ctx));
415
416 AuthScope ntlmScope = new AuthScope(host, port, AuthScope.ANY_REALM, "ntlm");
417 provider.setCredentials(ntlmScope, new DeferredCredentialsProvider.NtlmFactory(ctx));
418 }
419 return provider;
420 }
421
422 LocalState getState() {
423 return state;
424 }
425
426 private URI resolve(TransportTask task) {
427 return UriUtils.resolve(baseUri, task.getLocation());
428 }
429
430 @Override
431 public int classify(Throwable error) {
432 if (error instanceof HttpTransporterException
433 && ((HttpTransporterException) error).getStatusCode() == HttpStatus.SC_NOT_FOUND) {
434 return ERROR_NOT_FOUND;
435 }
436 return ERROR_OTHER;
437 }
438
439 @Override
440 protected void implPeek(PeekTask task) throws Exception {
441 HttpHead request = commonHeaders(new HttpHead(resolve(task)));
442 try {
443 execute(request, null);
444 } catch (HttpResponseException e) {
445 throw new HttpTransporterException(e.getStatusCode());
446 }
447 }
448
449 @Override
450 protected void implGet(GetTask task) throws Exception {
451 boolean resume = true;
452
453 EntityGetter getter = new EntityGetter(task);
454 HttpGet request = commonHeaders(new HttpGet(resolve(task)));
455 while (true) {
456 try {
457 if (resume) {
458 resume(request, task);
459 }
460 execute(request, getter);
461 break;
462 } catch (HttpResponseException e) {
463 if (resume
464 && e.getStatusCode() == HttpStatus.SC_PRECONDITION_FAILED
465 && request.containsHeader(HttpHeaders.RANGE)) {
466 request = commonHeaders(new HttpGet(resolve(task)));
467 resume = false;
468 continue;
469 }
470 throw new HttpTransporterException(e.getStatusCode());
471 }
472 }
473 }
474
475 @Override
476 protected void implPut(PutTask task) throws Exception {
477 PutTaskEntity entity = new PutTaskEntity(task);
478 HttpPut request = commonHeaders(entity(new HttpPut(resolve(task)), entity));
479 try {
480 execute(request, null);
481 } catch (HttpResponseException e) {
482 if (e.getStatusCode() == HttpStatus.SC_EXPECTATION_FAILED && request.containsHeader(HttpHeaders.EXPECT)) {
483 state.setExpectContinue(false);
484 request = commonHeaders(entity(new HttpPut(request.getURI()), entity));
485 execute(request, null);
486 return;
487 }
488 throw new HttpTransporterException(e.getStatusCode());
489 }
490 }
491
492 private void execute(HttpUriRequest request, EntityGetter getter) throws Exception {
493 try {
494 SharingHttpContext context = new SharingHttpContext(state);
495 prepare(request, context);
496 try (CloseableHttpResponse response = client.execute(server, request, context)) {
497 try {
498 context.close();
499 handleStatus(response);
500 if (getter != null) {
501 getter.handle(response);
502 }
503 } finally {
504 EntityUtils.consumeQuietly(response.getEntity());
505 }
506 }
507 } catch (IOException e) {
508 if (e.getCause() instanceof TransferCancelledException) {
509 throw (Exception) e.getCause();
510 }
511 throw e;
512 }
513 }
514
515 private void prepare(HttpUriRequest request, SharingHttpContext context) throws Exception {
516 final boolean put = HttpPut.METHOD_NAME.equalsIgnoreCase(request.getMethod());
517 if (preemptiveAuth || (preemptivePutAuth && put)) {
518 context.getAuthCache().put(server, new BasicScheme());
519 }
520 if (supportWebDav) {
521 if (state.getWebDav() == null && (put || isPayloadPresent(request))) {
522 HttpOptions req = commonHeaders(new HttpOptions(request.getURI()));
523 try (CloseableHttpResponse response = client.execute(server, req, context)) {
524 state.setWebDav(response.containsHeader(HttpHeaders.DAV));
525 EntityUtils.consumeQuietly(response.getEntity());
526 } catch (IOException e) {
527 LOGGER.debug("Failed to prepare HTTP context", e);
528 }
529 }
530 if (put && Boolean.TRUE.equals(state.getWebDav())) {
531 mkdirs(request.getURI(), context);
532 }
533 }
534 }
535
536 private void mkdirs(URI uri, SharingHttpContext context) throws Exception {
537 List<URI> dirs = UriUtils.getDirectories(baseUri, uri);
538 int index = 0;
539 for (; index < dirs.size(); index++) {
540 try (CloseableHttpResponse response =
541 client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
542 try {
543 int status = response.getStatusLine().getStatusCode();
544 if (status < 300 || status == HttpStatus.SC_METHOD_NOT_ALLOWED) {
545 break;
546 } else if (status == HttpStatus.SC_CONFLICT) {
547 continue;
548 }
549 handleStatus(response);
550 } finally {
551 EntityUtils.consumeQuietly(response.getEntity());
552 }
553 } catch (IOException e) {
554 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
555 return;
556 }
557 }
558 for (index--; index >= 0; index--) {
559 try (CloseableHttpResponse response =
560 client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
561 try {
562 handleStatus(response);
563 } finally {
564 EntityUtils.consumeQuietly(response.getEntity());
565 }
566 } catch (IOException e) {
567 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
568 return;
569 }
570 }
571 }
572
573 private <T extends HttpEntityEnclosingRequest> T entity(T request, HttpEntity entity) {
574 request.setEntity(entity);
575 return request;
576 }
577
578 private boolean isPayloadPresent(HttpUriRequest request) {
579 if (request instanceof HttpEntityEnclosingRequest) {
580 HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
581 return entity != null && entity.getContentLength() != 0;
582 }
583 return false;
584 }
585
586 private <T extends HttpUriRequest> T commonHeaders(T request) {
587 request.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store");
588 request.setHeader(HttpHeaders.PRAGMA, "no-cache");
589
590 if (state.isExpectContinue() && isPayloadPresent(request)) {
591 request.setHeader(HttpHeaders.EXPECT, "100-continue");
592 }
593
594 for (Map.Entry<?, ?> entry : headers.entrySet()) {
595 if (!(entry.getKey() instanceof String)) {
596 continue;
597 }
598 if (entry.getValue() instanceof String) {
599 request.setHeader(entry.getKey().toString(), entry.getValue().toString());
600 } else {
601 request.removeHeaders(entry.getKey().toString());
602 }
603 }
604
605 if (!state.isExpectContinue()) {
606 request.removeHeaders(HttpHeaders.EXPECT);
607 }
608
609 return request;
610 }
611
612 private <T extends HttpUriRequest> void resume(T request, GetTask task) throws IOException {
613 long resumeOffset = task.getResumeOffset();
614 if (resumeOffset > 0L && task.getDataPath() != null) {
615 long lastModified = Files.getLastModifiedTime(task.getDataPath()).toMillis();
616 request.setHeader(HttpHeaders.RANGE, "bytes=" + resumeOffset + '-');
617 request.setHeader(
618 HttpHeaders.IF_UNMODIFIED_SINCE, DateUtils.formatDate(new Date(lastModified - 60L * 1000L)));
619 request.setHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
620 }
621 }
622
623 private void handleStatus(CloseableHttpResponse response) throws Exception {
624 int status = response.getStatusLine().getStatusCode();
625 if (status >= 300) {
626 ApacheRFC9457Reporter.INSTANCE.generateException(response, (statusCode, reasonPhrase) -> {
627 throw new HttpResponseException(statusCode, reasonPhrase + " (" + statusCode + ")");
628 });
629 }
630 }
631
632 @Override
633 protected void implClose() {
634 try {
635 client.close();
636 } catch (IOException e) {
637 throw new UncheckedIOException(e);
638 }
639 AuthenticationContext.close(repoAuthContext);
640 AuthenticationContext.close(proxyAuthContext);
641 state.close();
642 }
643
644 private class EntityGetter {
645
646 private final GetTask task;
647
648 EntityGetter(GetTask task) {
649 this.task = task;
650 }
651
652 public void handle(CloseableHttpResponse response) throws IOException, TransferCancelledException {
653 HttpEntity entity = response.getEntity();
654 if (entity == null) {
655 entity = new ByteArrayEntity(new byte[0]);
656 }
657
658 long offset = 0L, length = entity.getContentLength();
659 Header rangeHeader = response.getFirstHeader(HttpHeaders.CONTENT_RANGE);
660 String range = rangeHeader != null ? rangeHeader.getValue() : null;
661 if (range != null) {
662 Matcher m = CONTENT_RANGE_PATTERN.matcher(range);
663 if (!m.matches()) {
664 throw new IOException("Invalid Content-Range header for partial download: " + range);
665 }
666 offset = Long.parseLong(m.group(1));
667 length = Long.parseLong(m.group(2)) + 1L;
668 if (offset < 0L || offset >= length || (offset > 0L && offset != task.getResumeOffset())) {
669 throw new IOException("Invalid Content-Range header for partial download from offset "
670 + task.getResumeOffset() + ": " + range);
671 }
672 }
673
674 final boolean resume = offset > 0L;
675 final Path dataFile = task.getDataPath();
676 if (dataFile == null) {
677 try (InputStream is = entity.getContent()) {
678 utilGet(task, is, true, length, resume);
679 extractChecksums(response);
680 }
681 } else {
682 try (FileUtils.CollocatedTempFile tempFile = FileUtils.newTempFile(dataFile)) {
683 task.setDataPath(tempFile.getPath(), resume);
684 if (resume && Files.isRegularFile(dataFile)) {
685 try (InputStream inputStream = Files.newInputStream(dataFile)) {
686 Files.copy(inputStream, tempFile.getPath(), StandardCopyOption.REPLACE_EXISTING);
687 }
688 }
689 try (InputStream is = entity.getContent()) {
690 utilGet(task, is, true, length, resume);
691 }
692 tempFile.move();
693 } finally {
694 task.setDataPath(dataFile);
695 }
696 }
697 if (task.getDataPath() != null) {
698 Header lastModifiedHeader =
699 response.getFirstHeader(HttpHeaders.LAST_MODIFIED);
700 if (lastModifiedHeader != null) {
701 Date lastModified = DateUtils.parseDate(lastModifiedHeader.getValue());
702 if (lastModified != null) {
703 pathProcessor.setLastModified(task.getDataPath(), lastModified.getTime());
704 }
705 }
706 }
707 extractChecksums(response);
708 }
709
710 private void extractChecksums(CloseableHttpResponse response) {
711 Map<String, String> checksums = checksumExtractor.extractChecksums(headerGetter(response));
712 if (checksums != null && !checksums.isEmpty()) {
713 checksums.forEach(task::setChecksum);
714 }
715 }
716 }
717
718 private static Function<String, String> headerGetter(CloseableHttpResponse closeableHttpResponse) {
719 return s -> {
720 Header header = closeableHttpResponse.getFirstHeader(s);
721 return header != null ? header.getValue() : null;
722 };
723 }
724
725 private class PutTaskEntity extends AbstractHttpEntity {
726
727 private final PutTask task;
728
729 PutTaskEntity(PutTask task) {
730 this.task = task;
731 }
732
733 @Override
734 public boolean isRepeatable() {
735 return true;
736 }
737
738 @Override
739 public boolean isStreaming() {
740 return false;
741 }
742
743 @Override
744 public long getContentLength() {
745 return task.getDataLength();
746 }
747
748 @Override
749 public InputStream getContent() throws IOException {
750 return task.newInputStream();
751 }
752
753 @Override
754 public void writeTo(OutputStream os) throws IOException {
755 try {
756 utilPut(task, os, false);
757 } catch (TransferCancelledException e) {
758 throw (IOException) new InterruptedIOException().initCause(e);
759 }
760 }
761 }
762
763 private static class ResolverServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy {
764 private final int retryCount;
765
766 private final long retryInterval;
767
768 private final long retryIntervalMax;
769
770 private final Set<Integer> serviceUnavailableHttpCodes;
771
772
773
774
775
776
777 private static final ThreadLocal<Long> RETRY_INTERVAL_HOLDER = new ThreadLocal<>();
778
779 private ResolverServiceUnavailableRetryStrategy(
780 int retryCount, long retryInterval, long retryIntervalMax, Set<Integer> serviceUnavailableHttpCodes) {
781 if (retryCount < 0) {
782 throw new IllegalArgumentException("retryCount must be >= 0");
783 }
784 if (retryInterval < 0L) {
785 throw new IllegalArgumentException("retryInterval must be >= 0");
786 }
787 if (retryIntervalMax < 0L) {
788 throw new IllegalArgumentException("retryIntervalMax must be >= 0");
789 }
790 this.retryCount = retryCount;
791 this.retryInterval = retryInterval;
792 this.retryIntervalMax = retryIntervalMax;
793 this.serviceUnavailableHttpCodes = requireNonNull(serviceUnavailableHttpCodes);
794 }
795
796 @Override
797 public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
798 final boolean retry = executionCount <= retryCount
799 && (serviceUnavailableHttpCodes.contains(
800 response.getStatusLine().getStatusCode()));
801 if (retry) {
802 Long retryInterval = retryInterval(response, executionCount, context);
803 if (retryInterval != null) {
804 RETRY_INTERVAL_HOLDER.set(retryInterval);
805 return true;
806 }
807 }
808 RETRY_INTERVAL_HOLDER.remove();
809 return false;
810 }
811
812
813
814
815
816
817
818
819 private Long retryInterval(HttpResponse httpResponse, int executionCount, HttpContext httpContext) {
820 Long result = null;
821 Header header = httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER);
822 if (header != null && header.getValue() != null) {
823 String headerValue = header.getValue();
824 if (headerValue.contains(":")) {
825 Date when = DateUtils.parseDate(headerValue);
826 if (when != null) {
827 result = Math.max(when.getTime() - System.currentTimeMillis(), 0L);
828 }
829 } else {
830 try {
831 result = Long.parseLong(headerValue) * 1000L;
832 } catch (NumberFormatException e) {
833
834 }
835 }
836 }
837 if (result == null) {
838 result = executionCount * this.retryInterval;
839 }
840 if (result > retryIntervalMax) {
841 return null;
842 }
843 return result;
844 }
845
846 @Override
847 public long getRetryInterval() {
848 Long ri = RETRY_INTERVAL_HOLDER.get();
849 if (ri == null) {
850 return 0L;
851 }
852 RETRY_INTERVAL_HOLDER.remove();
853 return ri;
854 }
855 }
856 }