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.URI;
27  import java.net.URISyntaxException;
28  import java.nio.charset.Charset;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.nio.file.StandardCopyOption;
32  import java.util.Date;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Set;
36  import java.util.function.Function;
37  import java.util.regex.Matcher;
38  
39  import org.apache.http.Header;
40  import org.apache.http.HttpEntity;
41  import org.apache.http.HttpEntityEnclosingRequest;
42  import org.apache.http.HttpHeaders;
43  import org.apache.http.HttpHost;
44  import org.apache.http.HttpResponse;
45  import org.apache.http.HttpStatus;
46  import org.apache.http.auth.AuthSchemeProvider;
47  import org.apache.http.auth.AuthScope;
48  import org.apache.http.client.AuthCache;
49  import org.apache.http.client.CredentialsProvider;
50  import org.apache.http.client.HttpRequestRetryHandler;
51  import org.apache.http.client.HttpResponseException;
52  import org.apache.http.client.ServiceUnavailableRetryStrategy;
53  import org.apache.http.client.config.AuthSchemes;
54  import org.apache.http.client.config.CookieSpecs;
55  import org.apache.http.client.config.RequestConfig;
56  import org.apache.http.client.methods.CloseableHttpResponse;
57  import org.apache.http.client.methods.HttpGet;
58  import org.apache.http.client.methods.HttpHead;
59  import org.apache.http.client.methods.HttpOptions;
60  import org.apache.http.client.methods.HttpPut;
61  import org.apache.http.client.methods.HttpUriRequest;
62  import org.apache.http.client.utils.DateUtils;
63  import org.apache.http.client.utils.URIUtils;
64  import org.apache.http.config.Registry;
65  import org.apache.http.config.RegistryBuilder;
66  import org.apache.http.config.SocketConfig;
67  import org.apache.http.entity.AbstractHttpEntity;
68  import org.apache.http.entity.ByteArrayEntity;
69  import org.apache.http.impl.NoConnectionReuseStrategy;
70  import org.apache.http.impl.auth.BasicScheme;
71  import org.apache.http.impl.auth.BasicSchemeFactory;
72  import org.apache.http.impl.auth.DigestSchemeFactory;
73  import org.apache.http.impl.auth.KerberosSchemeFactory;
74  import org.apache.http.impl.auth.NTLMSchemeFactory;
75  import org.apache.http.impl.auth.SPNegoSchemeFactory;
76  import org.apache.http.impl.client.BasicAuthCache;
77  import org.apache.http.impl.client.CloseableHttpClient;
78  import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
79  import org.apache.http.impl.client.HttpClientBuilder;
80  import org.apache.http.impl.client.LaxRedirectStrategy;
81  import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
82  import org.apache.http.protocol.HttpContext;
83  import org.apache.http.util.EntityUtils;
84  import org.eclipse.aether.RepositorySystemSession;
85  import org.eclipse.aether.repository.AuthenticationContext;
86  import org.eclipse.aether.repository.Proxy;
87  import org.eclipse.aether.repository.RemoteRepository;
88  import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
89  import org.eclipse.aether.spi.connector.transport.GetTask;
90  import org.eclipse.aether.spi.connector.transport.PeekTask;
91  import org.eclipse.aether.spi.connector.transport.PutTask;
92  import org.eclipse.aether.spi.connector.transport.TransportTask;
93  import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractor;
94  import org.eclipse.aether.spi.connector.transport.http.HttpTransporter;
95  import org.eclipse.aether.spi.connector.transport.http.HttpTransporterException;
96  import org.eclipse.aether.spi.io.PathProcessor;
97  import org.eclipse.aether.transfer.NoTransporterException;
98  import org.eclipse.aether.transfer.TransferCancelledException;
99  import org.eclipse.aether.util.ConfigUtils;
100 import org.eclipse.aether.util.StringDigestUtil;
101 import org.eclipse.aether.util.connector.transport.http.HttpTransporterUtils;
102 import org.slf4j.Logger;
103 import org.slf4j.LoggerFactory;
104 
105 import static java.util.Objects.requireNonNull;
106 import static org.eclipse.aether.spi.connector.transport.http.HttpConstants.CONTENT_RANGE_PATTERN;
107 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_FOLLOW_REDIRECTS;
108 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_HTTP_RETRY_HANDLER_NAME;
109 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED;
110 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_MAX_REDIRECTS;
111 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_USE_SYSTEM_PROPERTIES;
112 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_FOLLOW_REDIRECTS;
113 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED;
114 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_MAX_REDIRECTS;
115 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_USE_SYSTEM_PROPERTIES;
116 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.HTTP_RETRY_HANDLER_NAME_DEFAULT;
117 import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.HTTP_RETRY_HANDLER_NAME_STANDARD;
118 
119 /**
120  * A transporter for HTTP/HTTPS.
121  */
122 final class ApacheTransporter extends AbstractTransporter implements HttpTransporter {
123     private static final Logger LOGGER = LoggerFactory.getLogger(ApacheTransporter.class);
124 
125     private final ChecksumExtractor checksumExtractor;
126 
127     private final PathProcessor pathProcessor;
128 
129     private final AuthenticationContext repoAuthContext;
130 
131     private final AuthenticationContext proxyAuthContext;
132 
133     private final URI baseUri;
134 
135     private final HttpHost server;
136 
137     private final HttpHost proxy;
138 
139     private final CloseableHttpClient client;
140 
141     private final Map<?, ?> headers;
142 
143     private final LocalState state;
144 
145     private final boolean preemptiveAuth;
146 
147     private final boolean preemptivePutAuth;
148 
149     private final boolean supportWebDav;
150 
151     private final AuthCache authCache;
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 = HttpTransporterUtils.getHttpsSecurityMode(session, repository);
180         final int connectionMaxTtlSeconds = HttpTransporterUtils.getHttpConnectionMaxTtlSeconds(session, repository);
181         final int maxConnectionsPerRoute = HttpTransporterUtils.getHttpMaxConnectionsPerRoute(session, repository);
182         this.state = new LocalState(
183                 session,
184                 repository,
185                 new ConnMgrConfig(
186                         session, repoAuthContext, httpsSecurityMode, connectionMaxTtlSeconds, maxConnectionsPerRoute));
187 
188         this.headers = HttpTransporterUtils.getHttpHeaders(session, repository);
189         this.preemptiveAuth = HttpTransporterUtils.isHttpPreemptiveAuth(session, repository);
190         this.preemptivePutAuth = HttpTransporterUtils.isHttpPreemptivePutAuth(session, repository);
191         this.supportWebDav = HttpTransporterUtils.isHttpSupportWebDav(session, repository);
192         int connectTimeout = HttpTransporterUtils.getHttpConnectTimeout(session, repository);
193         int requestTimeout = HttpTransporterUtils.getHttpRequestTimeout(session, repository);
194         int retryCount = HttpTransporterUtils.getHttpRetryHandlerCount(session, repository);
195         long retryInterval = HttpTransporterUtils.getHttpRetryHandlerInterval(session, repository);
196         long retryIntervalMax = HttpTransporterUtils.getHttpRetryHandlerIntervalMax(session, repository);
197         String retryHandlerName = ConfigUtils.getString(
198                 session,
199                 HTTP_RETRY_HANDLER_NAME_STANDARD,
200                 CONFIG_PROP_HTTP_RETRY_HANDLER_NAME + "." + repository.getId(),
201                 CONFIG_PROP_HTTP_RETRY_HANDLER_NAME);
202         boolean retryHandlerRequestSentEnabled = ConfigUtils.getBoolean(
203                 session,
204                 DEFAULT_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED,
205                 CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED + "." + repository.getId(),
206                 CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED);
207         int maxRedirects = ConfigUtils.getInteger(
208                 session,
209                 DEFAULT_MAX_REDIRECTS,
210                 CONFIG_PROP_MAX_REDIRECTS + "." + repository.getId(),
211                 CONFIG_PROP_MAX_REDIRECTS);
212         boolean followRedirects = ConfigUtils.getBoolean(
213                 session,
214                 DEFAULT_FOLLOW_REDIRECTS,
215                 CONFIG_PROP_FOLLOW_REDIRECTS + "." + repository.getId(),
216                 CONFIG_PROP_FOLLOW_REDIRECTS);
217         String userAgent = HttpTransporterUtils.getUserAgent(session, repository);
218 
219         Charset credentialsCharset = HttpTransporterUtils.getHttpCredentialsEncoding(session, repository);
220         Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
221                 .register(AuthSchemes.BASIC, new BasicSchemeFactory(credentialsCharset))
222                 .register(AuthSchemes.DIGEST, new DigestSchemeFactory(credentialsCharset))
223                 .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
224                 .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
225                 .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
226                 .build();
227         SocketConfig socketConfig =
228                 // the time to establish connection (low level)
229                 SocketConfig.custom().setSoTimeout(requestTimeout).build();
230         RequestConfig requestConfig = RequestConfig.custom()
231                 .setMaxRedirects(maxRedirects)
232                 .setRedirectsEnabled(followRedirects)
233                 .setRelativeRedirectsAllowed(followRedirects)
234                 // the time waiting for data; max time between two data packets
235                 .setSocketTimeout(requestTimeout)
236                 // the time to establish the connection (high level)
237                 .setConnectTimeout(connectTimeout)
238                 // the time to wait for a connection from the connection manager/pool
239                 .setConnectionRequestTimeout(connectTimeout)
240                 .setLocalAddress(HttpTransporterUtils.getHttpLocalAddress(session, repository)
241                         .orElse(null))
242                 .setCookieSpec(CookieSpecs.STANDARD)
243                 .build();
244 
245         HttpRequestRetryHandler retryHandler;
246         if (HTTP_RETRY_HANDLER_NAME_STANDARD.equals(retryHandlerName)) {
247             retryHandler = new StandardHttpRequestRetryHandler(retryCount, retryHandlerRequestSentEnabled);
248         } else if (HTTP_RETRY_HANDLER_NAME_DEFAULT.equals(retryHandlerName)) {
249             retryHandler = new DefaultHttpRequestRetryHandler(retryCount, retryHandlerRequestSentEnabled);
250         } else {
251             throw new IllegalArgumentException(
252                     "Unsupported parameter " + CONFIG_PROP_HTTP_RETRY_HANDLER_NAME + " value: " + retryHandlerName);
253         }
254         ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new ResolverServiceUnavailableRetryStrategy(
255                 retryCount,
256                 retryInterval,
257                 retryIntervalMax,
258                 HttpTransporterUtils.getHttpServiceUnavailableCodes(session, repository));
259 
260         HttpClientBuilder builder = HttpClientBuilder.create()
261                 .setUserAgent(userAgent)
262                 .setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
263                 .setDefaultSocketConfig(socketConfig)
264                 .setDefaultRequestConfig(requestConfig)
265                 .setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
266                 .setRetryHandler(retryHandler)
267                 .setDefaultAuthSchemeRegistry(authSchemeRegistry)
268                 .setConnectionManager(state.getConnectionManager())
269                 .setConnectionManagerShared(true)
270                 .setDefaultCredentialsProvider(toCredentialsProvider(server, repoAuthContext, proxy, proxyAuthContext))
271                 .setProxy(proxy);
272         final boolean useSystemProperties = ConfigUtils.getBoolean(
273                 session,
274                 DEFAULT_USE_SYSTEM_PROPERTIES,
275                 CONFIG_PROP_USE_SYSTEM_PROPERTIES + "." + repository.getId(),
276                 CONFIG_PROP_USE_SYSTEM_PROPERTIES);
277         if (useSystemProperties) {
278             LOGGER.warn(
279                     "Transport used Apache HttpClient is instructed to use system properties: this may yield in unwanted side-effects!");
280             LOGGER.warn("Please use documented means to configure resolver transport.");
281             builder.useSystemProperties();
282         }
283 
284         HttpTransporterUtils.getHttpExpectContinue(session, repository).ifPresent(state::setExpectContinue);
285         if (!HttpTransporterUtils.isHttpReuseConnections(session, repository)) {
286             builder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE);
287         }
288 
289         if (session.getCache() != null) {
290             String authCacheKey = getClass().getSimpleName() + "-" + repository.getId() + "-"
291                     + StringDigestUtil.sha1(repository.toString());
292             this.authCache = (AuthCache) session.getCache().computeIfAbsent(session, authCacheKey, BasicAuthCache::new);
293         } else {
294             this.authCache = new BasicAuthCache();
295         }
296         this.client = builder.build();
297     }
298 
299     private static HttpHost toHost(Proxy proxy) {
300         HttpHost host = null;
301         if (proxy != null) {
302             // in Maven, the proxy.protocol is used for proxy matching against remote repository protocol; no TLS proxy
303             // support
304             // https://github.com/apache/maven/issues/2519
305             // https://github.com/apache/maven-resolver/issues/745
306             host = new HttpHost(proxy.getHost(), proxy.getPort());
307         }
308         return host;
309     }
310 
311     private static CredentialsProvider toCredentialsProvider(
312             HttpHost server, AuthenticationContext serverAuthCtx, HttpHost proxy, AuthenticationContext proxyAuthCtx) {
313         CredentialsProvider provider = toCredentialsProvider(server.getHostName(), AuthScope.ANY_PORT, serverAuthCtx);
314         if (proxy != null) {
315             CredentialsProvider p = toCredentialsProvider(proxy.getHostName(), proxy.getPort(), proxyAuthCtx);
316             provider = new DemuxCredentialsProvider(provider, p, proxy);
317         }
318         return provider;
319     }
320 
321     private static CredentialsProvider toCredentialsProvider(String host, int port, AuthenticationContext ctx) {
322         DeferredCredentialsProvider provider = new DeferredCredentialsProvider();
323         if (ctx != null) {
324             AuthScope basicScope = new AuthScope(host, port);
325             provider.setCredentials(basicScope, new DeferredCredentialsProvider.BasicFactory(ctx));
326 
327             AuthScope ntlmScope = new AuthScope(host, port, AuthScope.ANY_REALM, "ntlm");
328             provider.setCredentials(ntlmScope, new DeferredCredentialsProvider.NtlmFactory(ctx));
329         }
330         return provider;
331     }
332 
333     LocalState getState() {
334         return state;
335     }
336 
337     private URI resolve(TransportTask task) {
338         return UriUtils.resolve(baseUri, task.getLocation());
339     }
340 
341     @Override
342     protected void implPeek(PeekTask task) throws Exception {
343         HttpHead request = commonHeaders(new HttpHead(resolve(task)));
344         try {
345             execute(request, null);
346         } catch (HttpResponseException e) {
347             throw new HttpTransporterException(e.getStatusCode());
348         }
349     }
350 
351     @Override
352     protected void implGet(GetTask task) throws Exception {
353         boolean resume = true;
354 
355         EntityGetter getter = new EntityGetter(task);
356         HttpGet request = commonHeaders(new HttpGet(resolve(task)));
357         ApacheRFC9457Reporter.INSTANCE.prepareRequest(request);
358         while (true) {
359             try {
360                 if (resume) {
361                     resume(request, task);
362                 }
363                 execute(request, getter);
364                 break;
365             } catch (HttpResponseException e) {
366                 if (resume
367                         && e.getStatusCode() == HttpStatus.SC_PRECONDITION_FAILED
368                         && request.containsHeader(HttpHeaders.RANGE)) {
369                     request = commonHeaders(new HttpGet(resolve(task)));
370                     resume = false;
371                     continue;
372                 }
373                 throw new HttpTransporterException(e.getStatusCode());
374             }
375         }
376     }
377 
378     @Override
379     protected void implPut(PutTask task) throws Exception {
380         PutTaskEntity entity = new PutTaskEntity(task);
381         HttpPut request = commonHeaders(entity(new HttpPut(resolve(task)), entity));
382         ApacheRFC9457Reporter.INSTANCE.prepareRequest(request);
383         try {
384             execute(request, null);
385         } catch (HttpResponseException e) {
386             if (e.getStatusCode() == HttpStatus.SC_EXPECTATION_FAILED && request.containsHeader(HttpHeaders.EXPECT)) {
387                 state.setExpectContinue(false);
388                 request = commonHeaders(entity(new HttpPut(request.getURI()), entity));
389                 execute(request, null);
390                 return;
391             }
392             throw new HttpTransporterException(e.getStatusCode());
393         }
394     }
395 
396     private void execute(HttpUriRequest request, EntityGetter getter) throws Exception {
397         try {
398             SharingHttpContext context = new SharingHttpContext(state);
399             context.setAuthCache(authCache);
400             prepare(request, context);
401             try (CloseableHttpResponse response = client.execute(server, request, context)) {
402                 try {
403                     handleStatus(response);
404                     if (getter != null) {
405                         getter.handle(response);
406                     }
407                 } finally {
408                     EntityUtils.consumeQuietly(response.getEntity());
409                 }
410             }
411         } catch (IOException e) {
412             if (e.getCause() instanceof TransferCancelledException) {
413                 throw (Exception) e.getCause();
414             }
415             throw e;
416         }
417     }
418 
419     private void prepare(HttpUriRequest request, SharingHttpContext context) throws Exception {
420         final boolean put = HttpPut.METHOD_NAME.equalsIgnoreCase(request.getMethod());
421         if (preemptiveAuth || (preemptivePutAuth && put)) {
422             context.getAuthCache().put(server, new BasicScheme());
423         }
424         if (supportWebDav) {
425             if (state.getWebDav() == null && (put || isPayloadPresent(request))) {
426                 HttpOptions req = commonHeaders(new HttpOptions(request.getURI()));
427                 try (CloseableHttpResponse response = client.execute(server, req, context)) {
428                     state.setWebDav(response.containsHeader(HttpHeaders.DAV));
429                     EntityUtils.consumeQuietly(response.getEntity());
430                 } catch (IOException e) {
431                     LOGGER.debug("Failed to prepare HTTP context", e);
432                 }
433             }
434             if (put && Boolean.TRUE.equals(state.getWebDav())) {
435                 mkdirs(request.getURI(), context);
436             }
437         }
438     }
439 
440     private void mkdirs(URI uri, SharingHttpContext context) throws Exception {
441         List<URI> dirs = UriUtils.getDirectories(baseUri, uri);
442         int index = 0;
443         for (; index < dirs.size(); index++) {
444             try (CloseableHttpResponse response =
445                     client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
446                 try {
447                     int status = response.getStatusLine().getStatusCode();
448                     if (status < 300 || status == HttpStatus.SC_METHOD_NOT_ALLOWED) {
449                         break;
450                     } else if (status == HttpStatus.SC_CONFLICT) {
451                         continue;
452                     }
453                     handleStatus(response);
454                 } finally {
455                     EntityUtils.consumeQuietly(response.getEntity());
456                 }
457             } catch (IOException e) {
458                 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
459                 return;
460             }
461         }
462         for (index--; index >= 0; index--) {
463             try (CloseableHttpResponse response =
464                     client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
465                 try {
466                     handleStatus(response);
467                 } finally {
468                     EntityUtils.consumeQuietly(response.getEntity());
469                 }
470             } catch (IOException e) {
471                 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
472                 return;
473             }
474         }
475     }
476 
477     private <T extends HttpEntityEnclosingRequest> T entity(T request, HttpEntity entity) {
478         request.setEntity(entity);
479         return request;
480     }
481 
482     private boolean isPayloadPresent(HttpUriRequest request) {
483         if (request instanceof HttpEntityEnclosingRequest) {
484             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
485             return entity != null && entity.getContentLength() != 0;
486         }
487         return false;
488     }
489 
490     private <T extends HttpUriRequest> T commonHeaders(T request) {
491         request.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store");
492         request.setHeader(HttpHeaders.PRAGMA, "no-cache");
493 
494         if (state.isExpectContinue() && isPayloadPresent(request)) {
495             request.setHeader(HttpHeaders.EXPECT, "100-continue");
496         }
497 
498         for (Map.Entry<?, ?> entry : headers.entrySet()) {
499             if (!(entry.getKey() instanceof String)) {
500                 continue;
501             }
502             if (entry.getValue() instanceof String) {
503                 request.setHeader(entry.getKey().toString(), entry.getValue().toString());
504             } else {
505                 request.removeHeaders(entry.getKey().toString());
506             }
507         }
508 
509         if (!state.isExpectContinue()) {
510             request.removeHeaders(HttpHeaders.EXPECT);
511         }
512         return request;
513     }
514 
515     private <T extends HttpUriRequest> void resume(T request, GetTask task) throws IOException {
516         long resumeOffset = task.getResumeOffset();
517         if (resumeOffset > 0L && task.getDataPath() != null) {
518             long lastModified = Files.getLastModifiedTime(task.getDataPath()).toMillis();
519             request.setHeader(HttpHeaders.RANGE, "bytes=" + resumeOffset + '-');
520             request.setHeader(
521                     HttpHeaders.IF_UNMODIFIED_SINCE, DateUtils.formatDate(new Date(lastModified - 60L * 1000L)));
522             request.setHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
523         }
524     }
525 
526     private void handleStatus(CloseableHttpResponse response) throws Exception {
527         int status = response.getStatusLine().getStatusCode();
528         if (status >= 300) {
529             ApacheRFC9457Reporter.INSTANCE.generateException(response, (statusCode, reasonPhrase) -> {
530                 throw new HttpResponseException(statusCode, reasonPhrase + " (" + statusCode + ")");
531             });
532         }
533     }
534 
535     @Override
536     protected void implClose() {
537         try {
538             client.close();
539         } catch (IOException e) {
540             throw new UncheckedIOException(e);
541         }
542         AuthenticationContext.close(repoAuthContext);
543         AuthenticationContext.close(proxyAuthContext);
544         state.close();
545     }
546 
547     private class EntityGetter {
548 
549         private final GetTask task;
550 
551         EntityGetter(GetTask task) {
552             this.task = task;
553         }
554 
555         public void handle(CloseableHttpResponse response) throws IOException, TransferCancelledException {
556             HttpEntity entity = response.getEntity();
557             if (entity == null) {
558                 entity = new ByteArrayEntity(new byte[0]);
559             }
560 
561             long offset = 0L, length = entity.getContentLength();
562             Header rangeHeader = response.getFirstHeader(HttpHeaders.CONTENT_RANGE);
563             String range = rangeHeader != null ? rangeHeader.getValue() : null;
564             if (range != null) {
565                 Matcher m = CONTENT_RANGE_PATTERN.matcher(range);
566                 if (!m.matches()) {
567                     throw new IOException("Invalid Content-Range header for partial download: " + range);
568                 }
569                 offset = Long.parseLong(m.group(1));
570                 length = Long.parseLong(m.group(2)) + 1L;
571                 if (offset < 0L || offset >= length || (offset > 0L && offset != task.getResumeOffset())) {
572                     throw new IOException("Invalid Content-Range header for partial download from offset "
573                             + task.getResumeOffset() + ": " + range);
574                 }
575             }
576 
577             final boolean resume = offset > 0L;
578             final Path dataFile = task.getDataPath();
579             if (dataFile == null) {
580                 try (InputStream is = entity.getContent()) {
581                     utilGet(task, is, true, length, resume);
582                     extractChecksums(response);
583                 }
584             } else {
585                 try (PathProcessor.CollocatedTempFile tempFile = pathProcessor.newTempFile(dataFile)) {
586                     task.setDataPath(tempFile.getPath(), resume);
587                     if (resume && Files.isRegularFile(dataFile)) {
588                         try (InputStream inputStream = Files.newInputStream(dataFile)) {
589                             Files.copy(inputStream, tempFile.getPath(), StandardCopyOption.REPLACE_EXISTING);
590                         }
591                     }
592                     try (InputStream is = entity.getContent()) {
593                         utilGet(task, is, true, length, resume);
594                     }
595                     tempFile.move();
596                 } finally {
597                     task.setDataPath(dataFile);
598                 }
599             }
600             if (task.getDataPath() != null) {
601                 Header lastModifiedHeader =
602                         response.getFirstHeader(HttpHeaders.LAST_MODIFIED); // note: Wagon also does first not last
603                 if (lastModifiedHeader != null) {
604                     Date lastModified = DateUtils.parseDate(lastModifiedHeader.getValue());
605                     if (lastModified != null) {
606                         pathProcessor.setLastModified(task.getDataPath(), lastModified.getTime());
607                     }
608                 }
609             }
610             extractChecksums(response);
611         }
612 
613         private void extractChecksums(CloseableHttpResponse response) {
614             Map<String, String> checksums = checksumExtractor.extractChecksums(headerGetter(response));
615             if (checksums != null && !checksums.isEmpty()) {
616                 checksums.forEach(task::setChecksum);
617             }
618         }
619     }
620 
621     private static Function<String, String> headerGetter(CloseableHttpResponse closeableHttpResponse) {
622         return s -> {
623             Header header = closeableHttpResponse.getFirstHeader(s);
624             return header != null ? header.getValue() : null;
625         };
626     }
627 
628     private class PutTaskEntity extends AbstractHttpEntity {
629 
630         private final PutTask task;
631 
632         PutTaskEntity(PutTask task) {
633             this.task = task;
634         }
635 
636         @Override
637         public boolean isRepeatable() {
638             return true;
639         }
640 
641         @Override
642         public boolean isStreaming() {
643             return false;
644         }
645 
646         @Override
647         public long getContentLength() {
648             return task.getDataLength();
649         }
650 
651         @Override
652         public InputStream getContent() throws IOException {
653             return task.newInputStream();
654         }
655 
656         @Override
657         public void writeTo(OutputStream os) throws IOException {
658             try {
659                 utilPut(task, os, false);
660             } catch (TransferCancelledException e) {
661                 throw (IOException) new InterruptedIOException().initCause(e);
662             }
663         }
664     }
665 
666     private static class ResolverServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy {
667         private final int retryCount;
668 
669         private final long retryInterval;
670 
671         private final long retryIntervalMax;
672 
673         private final Set<Integer> serviceUnavailableHttpCodes;
674 
675         /**
676          * Ugly, but forced by HttpClient API {@link ServiceUnavailableRetryStrategy}: the calls for
677          * {@link #retryRequest(HttpResponse, int, HttpContext)} and {@link #getRetryInterval()} are done by same
678          * thread and are actually done from spot that are very close to each other (almost subsequent calls).
679          */
680         private static final ThreadLocal<Long> RETRY_INTERVAL_HOLDER = new ThreadLocal<>();
681 
682         private ResolverServiceUnavailableRetryStrategy(
683                 int retryCount, long retryInterval, long retryIntervalMax, Set<Integer> serviceUnavailableHttpCodes) {
684             if (retryCount < 0) {
685                 throw new IllegalArgumentException("retryCount must be >= 0");
686             }
687             if (retryInterval < 0L) {
688                 throw new IllegalArgumentException("retryInterval must be >= 0");
689             }
690             if (retryIntervalMax < 0L) {
691                 throw new IllegalArgumentException("retryIntervalMax must be >= 0");
692             }
693             this.retryCount = retryCount;
694             this.retryInterval = retryInterval;
695             this.retryIntervalMax = retryIntervalMax;
696             this.serviceUnavailableHttpCodes = requireNonNull(serviceUnavailableHttpCodes);
697         }
698 
699         @Override
700         public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
701             final boolean retry = executionCount <= retryCount
702                     && (serviceUnavailableHttpCodes.contains(
703                             response.getStatusLine().getStatusCode()));
704             if (retry) {
705                 Long retryInterval = retryInterval(response, executionCount, context);
706                 if (retryInterval != null) {
707                     RETRY_INTERVAL_HOLDER.set(retryInterval);
708                     return true;
709                 }
710             }
711             RETRY_INTERVAL_HOLDER.remove();
712             return false;
713         }
714 
715         /**
716          * Calculates retry interval in milliseconds. If {@link HttpHeaders#RETRY_AFTER} header present, it obeys it.
717          * Otherwise, it returns {@link this#retryInterval} long value multiplied with {@code executionCount} (starts
718          * from 1 and goes 2, 3,...).
719          *
720          * @return Long representing the retry interval as millis, or {@code null} if the request should be failed.
721          */
722         private Long retryInterval(HttpResponse httpResponse, int executionCount, HttpContext httpContext) {
723             Long result = null;
724             Header header = httpResponse.getFirstHeader(HttpHeaders.RETRY_AFTER);
725             if (header != null && header.getValue() != null) {
726                 String headerValue = header.getValue();
727                 if (headerValue.contains(":")) { // is date when to retry
728                     Date when = DateUtils.parseDate(headerValue); // presumably future
729                     if (when != null) {
730                         result = Math.max(when.getTime() - System.currentTimeMillis(), 0L);
731                     }
732                 } else {
733                     try {
734                         result = Long.parseLong(headerValue) * 1000L; // is in seconds
735                     } catch (NumberFormatException e) {
736                         // fall through
737                     }
738                 }
739             }
740             if (result == null) {
741                 result = executionCount * this.retryInterval;
742             }
743             if (result > retryIntervalMax) {
744                 return null;
745             }
746             return result;
747         }
748 
749         @Override
750         public long getRetryInterval() {
751             Long ri = RETRY_INTERVAL_HOLDER.get();
752             if (ri == null) {
753                 return 0L;
754             }
755             RETRY_INTERVAL_HOLDER.remove();
756             return ri;
757         }
758     }
759 }