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.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  * A transporter for HTTP/HTTPS.
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                 // the time to establish connection (low level)
289                 SocketConfig.custom().setSoTimeout(requestTimeout).build();
290         RequestConfig requestConfig = RequestConfig.custom()
291                 .setMaxRedirects(maxRedirects)
292                 .setRedirectsEnabled(followRedirects)
293                 .setRelativeRedirectsAllowed(followRedirects)
294                 // the time waiting for data; max time between two data packets
295                 .setSocketTimeout(requestTimeout)
296                 // the time to establish the connection (high level)
297                 .setConnectTimeout(connectTimeout)
298                 // the time to wait for a connection from the connection manager/pool
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      * Returns non-null {@link InetAddress} if set in configuration, {@code null} otherwise.
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     @SuppressWarnings("checkstyle:magicnumber")
537     private void mkdirs(URI uri, SharingHttpContext context) throws Exception {
538         List<URI> dirs = UriUtils.getDirectories(baseUri, uri);
539         int index = 0;
540         for (; index < dirs.size(); index++) {
541             try (CloseableHttpResponse response =
542                     client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
543                 try {
544                     int status = response.getStatusLine().getStatusCode();
545                     if (status < 300 || status == HttpStatus.SC_METHOD_NOT_ALLOWED) {
546                         break;
547                     } else if (status == HttpStatus.SC_CONFLICT) {
548                         continue;
549                     }
550                     handleStatus(response);
551                 } finally {
552                     EntityUtils.consumeQuietly(response.getEntity());
553                 }
554             } catch (IOException e) {
555                 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
556                 return;
557             }
558         }
559         for (index--; index >= 0; index--) {
560             try (CloseableHttpResponse response =
561                     client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
562                 try {
563                     handleStatus(response);
564                 } finally {
565                     EntityUtils.consumeQuietly(response.getEntity());
566                 }
567             } catch (IOException e) {
568                 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
569                 return;
570             }
571         }
572     }
573 
574     private <T extends HttpEntityEnclosingRequest> T entity(T request, HttpEntity entity) {
575         request.setEntity(entity);
576         return request;
577     }
578 
579     private boolean isPayloadPresent(HttpUriRequest request) {
580         if (request instanceof HttpEntityEnclosingRequest) {
581             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
582             return entity != null && entity.getContentLength() != 0;
583         }
584         return false;
585     }
586 
587     private <T extends HttpUriRequest> T commonHeaders(T request) {
588         request.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store");
589         request.setHeader(HttpHeaders.PRAGMA, "no-cache");
590 
591         if (state.isExpectContinue() && isPayloadPresent(request)) {
592             request.setHeader(HttpHeaders.EXPECT, "100-continue");
593         }
594 
595         for (Map.Entry<?, ?> entry : headers.entrySet()) {
596             if (!(entry.getKey() instanceof String)) {
597                 continue;
598             }
599             if (entry.getValue() instanceof String) {
600                 request.setHeader(entry.getKey().toString(), entry.getValue().toString());
601             } else {
602                 request.removeHeaders(entry.getKey().toString());
603             }
604         }
605 
606         if (!state.isExpectContinue()) {
607             request.removeHeaders(HttpHeaders.EXPECT);
608         }
609 
610         return request;
611     }
612 
613     @SuppressWarnings("checkstyle:magicnumber")
614     private <T extends HttpUriRequest> void resume(T request, GetTask task) throws IOException {
615         long resumeOffset = task.getResumeOffset();
616         if (resumeOffset > 0L && task.getDataPath() != null) {
617             long lastModified = Files.getLastModifiedTime(task.getDataPath()).toMillis();
618             request.setHeader(HttpHeaders.RANGE, "bytes=" + resumeOffset + '-');
619             request.setHeader(
620                     HttpHeaders.IF_UNMODIFIED_SINCE, DateUtils.formatDate(new Date(lastModified - 60L * 1000L)));
621             request.setHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
622         }
623     }
624 
625     @SuppressWarnings("checkstyle:magicnumber")
626     private void handleStatus(CloseableHttpResponse response) throws Exception {
627         int status = response.getStatusLine().getStatusCode();
628         if (status >= 300) {
629             ApacheRFC9457Reporter.INSTANCE.generateException(response, (statusCode, reasonPhrase) -> {
630                 throw new HttpResponseException(statusCode, reasonPhrase + " (" + statusCode + ")");
631             });
632         }
633     }
634 
635     @Override
636     protected void implClose() {
637         try {
638             client.close();
639         } catch (IOException e) {
640             throw new UncheckedIOException(e);
641         }
642         AuthenticationContext.close(repoAuthContext);
643         AuthenticationContext.close(proxyAuthContext);
644         state.close();
645     }
646 
647     private class EntityGetter {
648 
649         private final GetTask task;
650 
651         EntityGetter(GetTask task) {
652             this.task = task;
653         }
654 
655         public void handle(CloseableHttpResponse response) throws IOException, TransferCancelledException {
656             HttpEntity entity = response.getEntity();
657             if (entity == null) {
658                 entity = new ByteArrayEntity(new byte[0]);
659             }
660 
661             long offset = 0L, length = entity.getContentLength();
662             Header rangeHeader = response.getFirstHeader(HttpHeaders.CONTENT_RANGE);
663             String range = rangeHeader != null ? rangeHeader.getValue() : null;
664             if (range != null) {
665                 Matcher m = CONTENT_RANGE_PATTERN.matcher(range);
666                 if (!m.matches()) {
667                     throw new IOException("Invalid Content-Range header for partial download: " + range);
668                 }
669                 offset = Long.parseLong(m.group(1));
670                 length = Long.parseLong(m.group(2)) + 1L;
671                 if (offset < 0L || offset >= length || (offset > 0L && offset != task.getResumeOffset())) {
672                     throw new IOException("Invalid Content-Range header for partial download from offset "
673                             + task.getResumeOffset() + ": " + range);
674                 }
675             }
676 
677             final boolean resume = offset > 0L;
678             final Path dataFile = task.getDataPath();
679             if (dataFile == null) {
680                 try (InputStream is = entity.getContent()) {
681                     utilGet(task, is, true, length, resume);
682                     extractChecksums(response);
683                 }
684             } else {
685                 try (FileUtils.CollocatedTempFile tempFile = FileUtils.newTempFile(dataFile)) {
686                     task.setDataPath(tempFile.getPath(), resume);
687                     if (resume && Files.isRegularFile(dataFile)) {
688                         try (InputStream inputStream = Files.newInputStream(dataFile)) {
689                             Files.copy(inputStream, tempFile.getPath(), StandardCopyOption.REPLACE_EXISTING);
690                         }
691                     }
692                     try (InputStream is = entity.getContent()) {
693                         utilGet(task, is, true, length, resume);
694                     }
695                     tempFile.move();
696                 } finally {
697                     task.setDataPath(dataFile);
698                 }
699             }
700             if (task.getDataPath() != null) {
701                 Header lastModifiedHeader =
702                         response.getFirstHeader(HttpHeaders.LAST_MODIFIED); // note: Wagon also does first not last
703                 if (lastModifiedHeader != null) {
704                     Date lastModified = DateUtils.parseDate(lastModifiedHeader.getValue());
705                     if (lastModified != null) {
706                         pathProcessor.setLastModified(task.getDataPath(), lastModified.getTime());
707                     }
708                 }
709             }
710             extractChecksums(response);
711         }
712 
713         private void extractChecksums(CloseableHttpResponse response) {
714             Map<String, String> checksums = checksumExtractor.extractChecksums(headerGetter(response));
715             if (checksums != null && !checksums.isEmpty()) {
716                 checksums.forEach(task::setChecksum);
717             }
718         }
719     }
720 
721     private static Function<String, String> headerGetter(CloseableHttpResponse closeableHttpResponse) {
722         return s -> {
723             Header header = closeableHttpResponse.getFirstHeader(s);
724             return header != null ? header.getValue() : null;
725         };
726     }
727 
728     private class PutTaskEntity extends AbstractHttpEntity {
729 
730         private final PutTask task;
731 
732         PutTaskEntity(PutTask task) {
733             this.task = task;
734         }
735 
736         @Override
737         public boolean isRepeatable() {
738             return true;
739         }
740 
741         @Override
742         public boolean isStreaming() {
743             return false;
744         }
745 
746         @Override
747         public long getContentLength() {
748             return task.getDataLength();
749         }
750 
751         @Override
752         public InputStream getContent() throws IOException {
753             return task.newInputStream();
754         }
755 
756         @Override
757         public void writeTo(OutputStream os) throws IOException {
758             try {
759                 utilPut(task, os, false);
760             } catch (TransferCancelledException e) {
761                 throw (IOException) new InterruptedIOException().initCause(e);
762             }
763         }
764     }
765 
766     private static class ResolverServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy {
767         private final int retryCount;
768 
769         private final long retryInterval;
770 
771         private final long retryIntervalMax;
772 
773         private final Set<Integer> serviceUnavailableHttpCodes;
774 
775         /**
776          * Ugly, but forced by HttpClient API {@link ServiceUnavailableRetryStrategy}: the calls for
777          * {@link #retryRequest(HttpResponse, int, HttpContext)} and {@link #getRetryInterval()} are done by same
778          * thread and are actually done from spot that are very close to each other (almost subsequent calls).
779          */
780         private static final ThreadLocal<Long> RETRY_INTERVAL_HOLDER = new ThreadLocal<>();
781 
782         private ResolverServiceUnavailableRetryStrategy(
783                 int retryCount, long retryInterval, long retryIntervalMax, Set<Integer> serviceUnavailableHttpCodes) {
784             if (retryCount < 0) {
785                 throw new IllegalArgumentException("retryCount must be >= 0");
786             }
787             if (retryInterval < 0L) {
788                 throw new IllegalArgumentException("retryInterval must be >= 0");
789             }
790             if (retryIntervalMax < 0L) {
791                 throw new IllegalArgumentException("retryIntervalMax must be >= 0");
792             }
793             this.retryCount = retryCount;
794             this.retryInterval = retryInterval;
795             this.retryIntervalMax = retryIntervalMax;
796             this.serviceUnavailableHttpCodes = requireNonNull(serviceUnavailableHttpCodes);
797         }
798 
799         @Override
800         public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
801             final boolean retry = executionCount <= retryCount
802                     && (serviceUnavailableHttpCodes.contains(
803                             response.getStatusLine().getStatusCode()));
804             if (retry) {
805                 Long retryInterval = retryInterval(response, executionCount, context);
806                 if (retryInterval != null) {
807                     RETRY_INTERVAL_HOLDER.set(retryInterval);
808                     return true;
809                 }
810             }
811             RETRY_INTERVAL_HOLDER.remove();
812             return false;
813         }
814 
815         /**
816          * Calculates retry interval in milliseconds. If {@link HttpHeaders#RETRY_AFTER} header present, it obeys it.
817          * Otherwise, it returns {@link this#retryInterval} long value multiplied with {@code executionCount} (starts
818          * from 1 and goes 2, 3,...).
819          *
820          * @return Long representing the retry interval as millis, or {@code null} if the request should be failed.
821          */
822         private Long retryInterval(HttpResponse httpResponse, int executionCount, HttpContext httpContext) {
823             Long result = null;
824             Header header = httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER);
825             if (header != null && header.getValue() != null) {
826                 String headerValue = header.getValue();
827                 if (headerValue.contains(":")) { // is date when to retry
828                     Date when = DateUtils.parseDate(headerValue); // presumably future
829                     if (when != null) {
830                         result = Math.max(when.getTime() - System.currentTimeMillis(), 0L);
831                     }
832                 } else {
833                     try {
834                         result = Long.parseLong(headerValue) * 1000L; // is in seconds
835                     } catch (NumberFormatException e) {
836                         // fall through
837                     }
838                 }
839             }
840             if (result == null) {
841                 result = executionCount * this.retryInterval;
842             }
843             if (result > retryIntervalMax) {
844                 return null;
845             }
846             return result;
847         }
848 
849         @Override
850         public long getRetryInterval() {
851             Long ri = RETRY_INTERVAL_HOLDER.get();
852             if (ri == null) {
853                 return 0L;
854             }
855             RETRY_INTERVAL_HOLDER.remove();
856             return ri;
857         }
858     }
859 }