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.URI;
28  import java.net.URISyntaxException;
29  import java.nio.charset.Charset;
30  import java.nio.file.Files;
31  import java.nio.file.StandardCopyOption;
32  import java.util.Collections;
33  import java.util.Date;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.regex.Matcher;
37  import java.util.regex.Pattern;
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.HttpStatus;
45  import org.apache.http.auth.AuthSchemeProvider;
46  import org.apache.http.auth.AuthScope;
47  import org.apache.http.client.CredentialsProvider;
48  import org.apache.http.client.HttpRequestRetryHandler;
49  import org.apache.http.client.HttpResponseException;
50  import org.apache.http.client.config.AuthSchemes;
51  import org.apache.http.client.config.RequestConfig;
52  import org.apache.http.client.methods.CloseableHttpResponse;
53  import org.apache.http.client.methods.HttpGet;
54  import org.apache.http.client.methods.HttpHead;
55  import org.apache.http.client.methods.HttpOptions;
56  import org.apache.http.client.methods.HttpPut;
57  import org.apache.http.client.methods.HttpUriRequest;
58  import org.apache.http.client.utils.DateUtils;
59  import org.apache.http.client.utils.URIUtils;
60  import org.apache.http.config.Registry;
61  import org.apache.http.config.RegistryBuilder;
62  import org.apache.http.config.SocketConfig;
63  import org.apache.http.entity.AbstractHttpEntity;
64  import org.apache.http.entity.ByteArrayEntity;
65  import org.apache.http.impl.NoConnectionReuseStrategy;
66  import org.apache.http.impl.auth.BasicScheme;
67  import org.apache.http.impl.auth.BasicSchemeFactory;
68  import org.apache.http.impl.auth.DigestSchemeFactory;
69  import org.apache.http.impl.auth.KerberosSchemeFactory;
70  import org.apache.http.impl.auth.NTLMSchemeFactory;
71  import org.apache.http.impl.auth.SPNegoSchemeFactory;
72  import org.apache.http.impl.client.CloseableHttpClient;
73  import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
74  import org.apache.http.impl.client.HttpClientBuilder;
75  import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
76  import org.apache.http.util.EntityUtils;
77  import org.eclipse.aether.ConfigurationProperties;
78  import org.eclipse.aether.RepositorySystemSession;
79  import org.eclipse.aether.repository.AuthenticationContext;
80  import org.eclipse.aether.repository.Proxy;
81  import org.eclipse.aether.repository.RemoteRepository;
82  import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
83  import org.eclipse.aether.spi.connector.transport.GetTask;
84  import org.eclipse.aether.spi.connector.transport.PeekTask;
85  import org.eclipse.aether.spi.connector.transport.PutTask;
86  import org.eclipse.aether.spi.connector.transport.TransportTask;
87  import org.eclipse.aether.transfer.NoTransporterException;
88  import org.eclipse.aether.transfer.TransferCancelledException;
89  import org.eclipse.aether.util.ConfigUtils;
90  import org.eclipse.aether.util.FileUtils;
91  import org.slf4j.Logger;
92  import org.slf4j.LoggerFactory;
93  
94  import static java.util.Objects.requireNonNull;
95  
96  /**
97   * A transporter for HTTP/HTTPS.
98   */
99  final class HttpTransporter extends AbstractTransporter {
100 
101     static final String SUPPORT_WEBDAV = "aether.connector.http.supportWebDav";
102 
103     static final String PREEMPTIVE_PUT_AUTH = "aether.connector.http.preemptivePutAuth";
104 
105     static final String USE_SYSTEM_PROPERTIES = "aether.connector.http.useSystemProperties";
106 
107     static final String HTTP_RETRY_HANDLER_NAME = "aether.connector.http.retryHandler.name";
108 
109     private static final String HTTP_RETRY_HANDLER_NAME_STANDARD = "standard";
110 
111     private static final String HTTP_RETRY_HANDLER_NAME_DEFAULT = "default";
112 
113     static final String HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED =
114             "aether.connector.http.retryHandler.requestSentEnabled";
115 
116     private static final Pattern CONTENT_RANGE_PATTERN =
117             Pattern.compile("\\s*bytes\\s+([0-9]+)\\s*-\\s*([0-9]+)\\s*/.*");
118 
119     private static final Logger LOGGER = LoggerFactory.getLogger(HttpTransporter.class);
120 
121     private final Map<String, ChecksumExtractor> checksumExtractors;
122 
123     private final AuthenticationContext repoAuthContext;
124 
125     private final AuthenticationContext proxyAuthContext;
126 
127     private final URI baseUri;
128 
129     private final HttpHost server;
130 
131     private final HttpHost proxy;
132 
133     private final CloseableHttpClient client;
134 
135     private final Map<?, ?> headers;
136 
137     private final LocalState state;
138 
139     private final boolean preemptiveAuth;
140 
141     private final boolean preemptivePutAuth;
142 
143     private final boolean supportWebDav;
144 
145     HttpTransporter(
146             Map<String, ChecksumExtractor> checksumExtractors,
147             RemoteRepository repository,
148             RepositorySystemSession session)
149             throws NoTransporterException {
150         if (!"http".equalsIgnoreCase(repository.getProtocol()) && !"https".equalsIgnoreCase(repository.getProtocol())) {
151             throw new NoTransporterException(repository);
152         }
153         this.checksumExtractors = requireNonNull(checksumExtractors, "checksum extractors must not be null");
154         try {
155             this.baseUri = new URI(repository.getUrl()).parseServerAuthority();
156             if (baseUri.isOpaque()) {
157                 throw new URISyntaxException(repository.getUrl(), "URL must not be opaque");
158             }
159             this.server = URIUtils.extractHost(baseUri);
160             if (server == null) {
161                 throw new URISyntaxException(repository.getUrl(), "URL lacks host name");
162             }
163         } catch (URISyntaxException e) {
164             throw new NoTransporterException(repository, e.getMessage(), e);
165         }
166         this.proxy = toHost(repository.getProxy());
167 
168         this.repoAuthContext = AuthenticationContext.forRepository(session, repository);
169         this.proxyAuthContext = AuthenticationContext.forProxy(session, repository);
170 
171         String httpsSecurityMode = ConfigUtils.getString(
172                 session,
173                 ConfigurationProperties.HTTPS_SECURITY_MODE_DEFAULT,
174                 ConfigurationProperties.HTTPS_SECURITY_MODE + "." + repository.getId(),
175                 ConfigurationProperties.HTTPS_SECURITY_MODE);
176         final int connectionMaxTtlSeconds = ConfigUtils.getInteger(
177                 session,
178                 ConfigurationProperties.DEFAULT_HTTP_CONNECTION_MAX_TTL,
179                 ConfigurationProperties.HTTP_CONNECTION_MAX_TTL + "." + repository.getId(),
180                 ConfigurationProperties.HTTP_CONNECTION_MAX_TTL);
181         final int maxConnectionsPerRoute = ConfigUtils.getInteger(
182                 session,
183                 ConfigurationProperties.DEFAULT_HTTP_MAX_CONNECTIONS_PER_ROUTE,
184                 ConfigurationProperties.HTTP_MAX_CONNECTIONS_PER_ROUTE + "." + repository.getId(),
185                 ConfigurationProperties.HTTP_MAX_CONNECTIONS_PER_ROUTE);
186         this.state = new LocalState(
187                 session,
188                 repository,
189                 new ConnMgrConfig(
190                         session, repoAuthContext, httpsSecurityMode, connectionMaxTtlSeconds, maxConnectionsPerRoute));
191 
192         this.headers = ConfigUtils.getMap(
193                 session,
194                 Collections.emptyMap(),
195                 ConfigurationProperties.HTTP_HEADERS + "." + repository.getId(),
196                 ConfigurationProperties.HTTP_HEADERS);
197 
198         this.preemptiveAuth = ConfigUtils.getBoolean(
199                 session,
200                 ConfigurationProperties.DEFAULT_HTTP_PREEMPTIVE_AUTH,
201                 ConfigurationProperties.HTTP_PREEMPTIVE_AUTH + "." + repository.getId(),
202                 ConfigurationProperties.HTTP_PREEMPTIVE_AUTH);
203         this.preemptivePutAuth = // defaults to true: Wagon does same
204                 ConfigUtils.getBoolean(
205                         session, true, PREEMPTIVE_PUT_AUTH + "." + repository.getId(), PREEMPTIVE_PUT_AUTH);
206         this.supportWebDav = // defaults to false: who needs it will enable it
207                 ConfigUtils.getBoolean(session, false, SUPPORT_WEBDAV + "." + repository.getId(), SUPPORT_WEBDAV);
208         String credentialEncoding = ConfigUtils.getString(
209                 session,
210                 ConfigurationProperties.DEFAULT_HTTP_CREDENTIAL_ENCODING,
211                 ConfigurationProperties.HTTP_CREDENTIAL_ENCODING + "." + repository.getId(),
212                 ConfigurationProperties.HTTP_CREDENTIAL_ENCODING);
213         int connectTimeout = ConfigUtils.getInteger(
214                 session,
215                 ConfigurationProperties.DEFAULT_CONNECT_TIMEOUT,
216                 ConfigurationProperties.CONNECT_TIMEOUT + "." + repository.getId(),
217                 ConfigurationProperties.CONNECT_TIMEOUT);
218         int requestTimeout = ConfigUtils.getInteger(
219                 session,
220                 ConfigurationProperties.DEFAULT_REQUEST_TIMEOUT,
221                 ConfigurationProperties.REQUEST_TIMEOUT + "." + repository.getId(),
222                 ConfigurationProperties.REQUEST_TIMEOUT);
223         int retryCount = ConfigUtils.getInteger(
224                 session,
225                 ConfigurationProperties.DEFAULT_HTTP_RETRY_HANDLER_COUNT,
226                 ConfigurationProperties.HTTP_RETRY_HANDLER_COUNT + "." + repository.getId(),
227                 ConfigurationProperties.HTTP_RETRY_HANDLER_COUNT);
228         String retryHandlerName = ConfigUtils.getString(
229                 session,
230                 HTTP_RETRY_HANDLER_NAME_STANDARD,
231                 HTTP_RETRY_HANDLER_NAME + "." + repository.getId(),
232                 HTTP_RETRY_HANDLER_NAME);
233         boolean retryHandlerRequestSentEnabled = ConfigUtils.getBoolean(
234                 session,
235                 false,
236                 HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED + "." + repository.getId(),
237                 HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED);
238         String userAgent = ConfigUtils.getString(
239                 session, ConfigurationProperties.DEFAULT_USER_AGENT, ConfigurationProperties.USER_AGENT);
240 
241         Charset credentialsCharset = Charset.forName(credentialEncoding);
242         Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
243                 .register(AuthSchemes.BASIC, new BasicSchemeFactory(credentialsCharset))
244                 .register(AuthSchemes.DIGEST, new DigestSchemeFactory(credentialsCharset))
245                 .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
246                 .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
247                 .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
248                 .build();
249         SocketConfig socketConfig =
250                 SocketConfig.custom().setSoTimeout(requestTimeout).build();
251         RequestConfig requestConfig = RequestConfig.custom()
252                 .setConnectTimeout(connectTimeout)
253                 .setConnectionRequestTimeout(connectTimeout)
254                 .setSocketTimeout(requestTimeout)
255                 .build();
256 
257         HttpRequestRetryHandler retryHandler;
258         if (HTTP_RETRY_HANDLER_NAME_STANDARD.equals(retryHandlerName)) {
259             retryHandler = new StandardHttpRequestRetryHandler(retryCount, retryHandlerRequestSentEnabled);
260         } else if (HTTP_RETRY_HANDLER_NAME_DEFAULT.equals(retryHandlerName)) {
261             retryHandler = new DefaultHttpRequestRetryHandler(retryCount, retryHandlerRequestSentEnabled);
262         } else {
263             throw new IllegalArgumentException(
264                     "Unsupported parameter " + HTTP_RETRY_HANDLER_NAME + " value: " + retryHandlerName);
265         }
266 
267         HttpClientBuilder builder = HttpClientBuilder.create()
268                 .setUserAgent(userAgent)
269                 .setDefaultSocketConfig(socketConfig)
270                 .setDefaultRequestConfig(requestConfig)
271                 .setRetryHandler(retryHandler)
272                 .setDefaultAuthSchemeRegistry(authSchemeRegistry)
273                 .setConnectionManager(state.getConnectionManager())
274                 .setConnectionManagerShared(true)
275                 .setDefaultCredentialsProvider(toCredentialsProvider(server, repoAuthContext, proxy, proxyAuthContext))
276                 .setProxy(proxy);
277         final boolean useSystemProperties = ConfigUtils.getBoolean(
278                 session, false, USE_SYSTEM_PROPERTIES + "." + repository.getId(), USE_SYSTEM_PROPERTIES);
279         if (useSystemProperties) {
280             LOGGER.warn(
281                     "Transport used Apache HttpClient is instructed to use system properties: this may yield in unwanted side-effects!");
282             LOGGER.warn("Please use documented means to configure resolver transport.");
283             builder.useSystemProperties();
284         }
285 
286         final boolean reuseConnections = ConfigUtils.getBoolean(
287                 session,
288                 ConfigurationProperties.DEFAULT_HTTP_REUSE_CONNECTIONS,
289                 ConfigurationProperties.HTTP_REUSE_CONNECTIONS + "." + repository.getId(),
290                 ConfigurationProperties.HTTP_REUSE_CONNECTIONS);
291         if (!reuseConnections) {
292             builder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE);
293         }
294 
295         this.client = builder.build();
296     }
297 
298     private static HttpHost toHost(Proxy proxy) {
299         HttpHost host = null;
300         if (proxy != null) {
301             host = new HttpHost(proxy.getHost(), proxy.getPort());
302         }
303         return host;
304     }
305 
306     private static CredentialsProvider toCredentialsProvider(
307             HttpHost server, AuthenticationContext serverAuthCtx, HttpHost proxy, AuthenticationContext proxyAuthCtx) {
308         CredentialsProvider provider = toCredentialsProvider(server.getHostName(), AuthScope.ANY_PORT, serverAuthCtx);
309         if (proxy != null) {
310             CredentialsProvider p = toCredentialsProvider(proxy.getHostName(), proxy.getPort(), proxyAuthCtx);
311             provider = new DemuxCredentialsProvider(provider, p, proxy);
312         }
313         return provider;
314     }
315 
316     private static CredentialsProvider toCredentialsProvider(String host, int port, AuthenticationContext ctx) {
317         DeferredCredentialsProvider provider = new DeferredCredentialsProvider();
318         if (ctx != null) {
319             AuthScope basicScope = new AuthScope(host, port);
320             provider.setCredentials(basicScope, new DeferredCredentialsProvider.BasicFactory(ctx));
321 
322             AuthScope ntlmScope = new AuthScope(host, port, AuthScope.ANY_REALM, "ntlm");
323             provider.setCredentials(ntlmScope, new DeferredCredentialsProvider.NtlmFactory(ctx));
324         }
325         return provider;
326     }
327 
328     LocalState getState() {
329         return state;
330     }
331 
332     private URI resolve(TransportTask task) {
333         return UriUtils.resolve(baseUri, task.getLocation());
334     }
335 
336     @Override
337     public int classify(Throwable error) {
338         if (error instanceof HttpResponseException
339                 && ((HttpResponseException) error).getStatusCode() == HttpStatus.SC_NOT_FOUND) {
340             return ERROR_NOT_FOUND;
341         }
342         return ERROR_OTHER;
343     }
344 
345     @Override
346     protected void implPeek(PeekTask task) throws Exception {
347         HttpHead request = commonHeaders(new HttpHead(resolve(task)));
348         execute(request, null);
349     }
350 
351     @Override
352     protected void implGet(GetTask task) throws Exception {
353         boolean resume = true;
354         boolean applyChecksumExtractors = true;
355 
356         EntityGetter getter = new EntityGetter(task);
357         HttpGet request = commonHeaders(new HttpGet(resolve(task)));
358         while (true) {
359             try {
360                 if (resume) {
361                     resume(request, task);
362                 }
363                 if (applyChecksumExtractors) {
364                     for (ChecksumExtractor checksumExtractor : checksumExtractors.values()) {
365                         checksumExtractor.prepareRequest(request);
366                     }
367                 }
368                 execute(request, getter);
369                 break;
370             } catch (HttpResponseException e) {
371                 if (resume
372                         && e.getStatusCode() == HttpStatus.SC_PRECONDITION_FAILED
373                         && request.containsHeader(HttpHeaders.RANGE)) {
374                     request = commonHeaders(new HttpGet(resolve(task)));
375                     resume = false;
376                     continue;
377                 }
378                 if (applyChecksumExtractors) {
379                     boolean retryWithoutExtractors = false;
380                     for (ChecksumExtractor checksumExtractor : checksumExtractors.values()) {
381                         if (checksumExtractor.retryWithoutExtractor(e)) {
382                             retryWithoutExtractors = true;
383                             break;
384                         }
385                     }
386                     if (retryWithoutExtractors) {
387                         request = commonHeaders(new HttpGet(resolve(task)));
388                         applyChecksumExtractors = false;
389                         continue;
390                     }
391                 }
392                 throw e;
393             }
394         }
395     }
396 
397     @Override
398     protected void implPut(PutTask task) throws Exception {
399         PutTaskEntity entity = new PutTaskEntity(task);
400         HttpPut request = commonHeaders(entity(new HttpPut(resolve(task)), entity));
401         try {
402             execute(request, null);
403         } catch (HttpResponseException e) {
404             if (e.getStatusCode() == HttpStatus.SC_EXPECTATION_FAILED && request.containsHeader(HttpHeaders.EXPECT)) {
405                 state.setExpectContinue(false);
406                 request = commonHeaders(entity(new HttpPut(request.getURI()), entity));
407                 execute(request, null);
408                 return;
409             }
410             throw e;
411         }
412     }
413 
414     private void execute(HttpUriRequest request, EntityGetter getter) throws Exception {
415         try {
416             SharingHttpContext context = new SharingHttpContext(state);
417             prepare(request, context);
418             try (CloseableHttpResponse response = client.execute(server, request, context)) {
419                 try {
420                     context.close();
421                     handleStatus(response);
422                     if (getter != null) {
423                         getter.handle(response);
424                     }
425                 } finally {
426                     EntityUtils.consumeQuietly(response.getEntity());
427                 }
428             }
429         } catch (IOException e) {
430             if (e.getCause() instanceof TransferCancelledException) {
431                 throw (Exception) e.getCause();
432             }
433             throw e;
434         }
435     }
436 
437     private void prepare(HttpUriRequest request, SharingHttpContext context) {
438         final boolean put = HttpPut.METHOD_NAME.equalsIgnoreCase(request.getMethod());
439         if (preemptiveAuth || (preemptivePutAuth && put)) {
440             context.getAuthCache().put(server, new BasicScheme());
441         }
442         if (supportWebDav) {
443             if (state.getWebDav() == null && (put || isPayloadPresent(request))) {
444                 HttpOptions req = commonHeaders(new HttpOptions(request.getURI()));
445                 try (CloseableHttpResponse response = client.execute(server, req, context)) {
446                     state.setWebDav(response.containsHeader(HttpHeaders.DAV));
447                     EntityUtils.consumeQuietly(response.getEntity());
448                 } catch (IOException e) {
449                     LOGGER.debug("Failed to prepare HTTP context", e);
450                 }
451             }
452             if (put && Boolean.TRUE.equals(state.getWebDav())) {
453                 mkdirs(request.getURI(), context);
454             }
455         }
456     }
457 
458     @SuppressWarnings("checkstyle:magicnumber")
459     private void mkdirs(URI uri, SharingHttpContext context) {
460         List<URI> dirs = UriUtils.getDirectories(baseUri, uri);
461         int index = 0;
462         for (; index < dirs.size(); index++) {
463             try (CloseableHttpResponse response =
464                     client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
465                 try {
466                     int status = response.getStatusLine().getStatusCode();
467                     if (status < 300 || status == HttpStatus.SC_METHOD_NOT_ALLOWED) {
468                         break;
469                     } else if (status == HttpStatus.SC_CONFLICT) {
470                         continue;
471                     }
472                     handleStatus(response);
473                 } finally {
474                     EntityUtils.consumeQuietly(response.getEntity());
475                 }
476             } catch (IOException e) {
477                 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
478                 return;
479             }
480         }
481         for (index--; index >= 0; index--) {
482             try (CloseableHttpResponse response =
483                     client.execute(server, commonHeaders(new HttpMkCol(dirs.get(index))), context)) {
484                 try {
485                     handleStatus(response);
486                 } finally {
487                     EntityUtils.consumeQuietly(response.getEntity());
488                 }
489             } catch (IOException e) {
490                 LOGGER.debug("Failed to create parent directory {}", dirs.get(index), e);
491                 return;
492             }
493         }
494     }
495 
496     private <T extends HttpEntityEnclosingRequest> T entity(T request, HttpEntity entity) {
497         request.setEntity(entity);
498         return request;
499     }
500 
501     private boolean isPayloadPresent(HttpUriRequest request) {
502         if (request instanceof HttpEntityEnclosingRequest) {
503             HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
504             return entity != null && entity.getContentLength() != 0;
505         }
506         return false;
507     }
508 
509     private <T extends HttpUriRequest> T commonHeaders(T request) {
510         request.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store");
511         request.setHeader(HttpHeaders.PRAGMA, "no-cache");
512 
513         if (state.isExpectContinue() && isPayloadPresent(request)) {
514             request.setHeader(HttpHeaders.EXPECT, "100-continue");
515         }
516 
517         for (Map.Entry<?, ?> entry : headers.entrySet()) {
518             if (!(entry.getKey() instanceof String)) {
519                 continue;
520             }
521             if (entry.getValue() instanceof String) {
522                 request.setHeader(entry.getKey().toString(), entry.getValue().toString());
523             } else {
524                 request.removeHeaders(entry.getKey().toString());
525             }
526         }
527 
528         if (!state.isExpectContinue()) {
529             request.removeHeaders(HttpHeaders.EXPECT);
530         }
531 
532         return request;
533     }
534 
535     @SuppressWarnings("checkstyle:magicnumber")
536     private <T extends HttpUriRequest> T resume(T request, GetTask task) {
537         long resumeOffset = task.getResumeOffset();
538         if (resumeOffset > 0L && task.getDataFile() != null) {
539             request.setHeader(HttpHeaders.RANGE, "bytes=" + resumeOffset + '-');
540             request.setHeader(
541                     HttpHeaders.IF_UNMODIFIED_SINCE,
542                     DateUtils.formatDate(new Date(task.getDataFile().lastModified() - 60L * 1000L)));
543             request.setHeader(HttpHeaders.ACCEPT_ENCODING, "identity");
544         }
545         return request;
546     }
547 
548     @SuppressWarnings("checkstyle:magicnumber")
549     private void handleStatus(CloseableHttpResponse response) throws HttpResponseException {
550         int status = response.getStatusLine().getStatusCode();
551         if (status >= 300) {
552             throw new HttpResponseException(status, response.getStatusLine().getReasonPhrase() + " (" + status + ")");
553         }
554     }
555 
556     @Override
557     protected void implClose() {
558         try {
559             client.close();
560         } catch (IOException e) {
561             throw new UncheckedIOException(e);
562         }
563         AuthenticationContext.close(repoAuthContext);
564         AuthenticationContext.close(proxyAuthContext);
565         state.close();
566     }
567 
568     private class EntityGetter {
569 
570         private final GetTask task;
571 
572         EntityGetter(GetTask task) {
573             this.task = task;
574         }
575 
576         public void handle(CloseableHttpResponse response) throws IOException, TransferCancelledException {
577             HttpEntity entity = response.getEntity();
578             if (entity == null) {
579                 entity = new ByteArrayEntity(new byte[0]);
580             }
581 
582             long offset = 0L, length = entity.getContentLength();
583             Header rangeHeader = response.getFirstHeader(HttpHeaders.CONTENT_RANGE);
584             String range = rangeHeader != null ? rangeHeader.getValue() : null;
585             if (range != null) {
586                 Matcher m = CONTENT_RANGE_PATTERN.matcher(range);
587                 if (!m.matches()) {
588                     throw new IOException("Invalid Content-Range header for partial download: " + range);
589                 }
590                 offset = Long.parseLong(m.group(1));
591                 length = Long.parseLong(m.group(2)) + 1L;
592                 if (offset < 0L || offset >= length || (offset > 0L && offset != task.getResumeOffset())) {
593                     throw new IOException("Invalid Content-Range header for partial download from offset "
594                             + task.getResumeOffset() + ": " + range);
595                 }
596             }
597 
598             final boolean resume = offset > 0L;
599             final File dataFile = task.getDataFile();
600             if (dataFile == null) {
601                 try (InputStream is = entity.getContent()) {
602                     utilGet(task, is, true, length, resume);
603                     extractChecksums(response);
604                 }
605             } else {
606                 try (FileUtils.CollocatedTempFile tempFile = FileUtils.newTempFile(dataFile.toPath())) {
607                     task.setDataFile(tempFile.getPath().toFile(), resume);
608                     if (resume && Files.isRegularFile(dataFile.toPath())) {
609                         try (InputStream inputStream = Files.newInputStream(dataFile.toPath())) {
610                             Files.copy(inputStream, tempFile.getPath(), StandardCopyOption.REPLACE_EXISTING);
611                         }
612                     }
613                     try (InputStream is = entity.getContent()) {
614                         utilGet(task, is, true, length, resume);
615                     }
616                     tempFile.move();
617                 } finally {
618                     task.setDataFile(dataFile);
619                 }
620             }
621             extractChecksums(response);
622         }
623 
624         private void extractChecksums(CloseableHttpResponse response) {
625             for (Map.Entry<String, ChecksumExtractor> extractorEntry : checksumExtractors.entrySet()) {
626                 Map<String, String> checksums = extractorEntry.getValue().extractChecksums(response);
627                 if (checksums != null) {
628                     checksums.forEach(task::setChecksum);
629                     return;
630                 }
631             }
632         }
633     }
634 
635     private class PutTaskEntity extends AbstractHttpEntity {
636 
637         private final PutTask task;
638 
639         PutTaskEntity(PutTask task) {
640             this.task = task;
641         }
642 
643         @Override
644         public boolean isRepeatable() {
645             return true;
646         }
647 
648         @Override
649         public boolean isStreaming() {
650             return false;
651         }
652 
653         @Override
654         public long getContentLength() {
655             return task.getDataLength();
656         }
657 
658         @Override
659         public InputStream getContent() throws IOException {
660             return task.newInputStream();
661         }
662 
663         @Override
664         public void writeTo(OutputStream os) throws IOException {
665             try {
666                 utilPut(task, os, false);
667             } catch (TransferCancelledException e) {
668                 throw (IOException) new InterruptedIOException().initCause(e);
669             }
670         }
671     }
672 }