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