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