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.url;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.HttpURLConnection;
24  import java.net.InetSocketAddress;
25  import java.net.Proxy;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  import java.nio.charset.Charset;
29  import java.nio.file.Path;
30  import java.util.ArrayList;
31  import java.util.Base64;
32  import java.util.Collections;
33  import java.util.Map;
34  import java.util.function.Consumer;
35  import java.util.function.Function;
36  import java.util.zip.GZIPInputStream;
37  import java.util.zip.InflaterInputStream;
38  
39  import org.eclipse.aether.ConfigurationProperties.RedirectMode;
40  import org.eclipse.aether.Keys;
41  import org.eclipse.aether.RepositorySystemSession;
42  import org.eclipse.aether.repository.AuthenticationContext;
43  import org.eclipse.aether.repository.RemoteRepository;
44  import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
45  import org.eclipse.aether.spi.connector.transport.GetTask;
46  import org.eclipse.aether.spi.connector.transport.PeekTask;
47  import org.eclipse.aether.spi.connector.transport.PutTask;
48  import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractor;
49  import org.eclipse.aether.spi.connector.transport.http.HttpConstants;
50  import org.eclipse.aether.spi.connector.transport.http.HttpTransporter;
51  import org.eclipse.aether.spi.connector.transport.http.HttpTransporterException;
52  import org.eclipse.aether.spi.io.PathProcessor;
53  import org.eclipse.aether.transfer.NoTransporterException;
54  import org.eclipse.aether.util.ConfigUtils;
55  import org.eclipse.aether.util.connector.transport.TransporterUtils;
56  import org.eclipse.aether.util.connector.transport.http.HttpTransporterUtils;
57  
58  /**
59   * A special, "read only" and limited capability transport usable for bootstrapping. It provides HTTP with minimal
60   * support (only basic auth, only GET/HEAD). It is implemented using {@link java.net.HttpURLConnection} class.
61   *
62   * @since 2.0.21
63   */
64  public class UrlTransporter extends AbstractTransporter implements HttpTransporter {
65  
66      private static final String METHOD_GET = "GET";
67      private static final String METHOD_HEAD = "HEAD";
68      private static final String HEADER_LOCATION = "Location";
69      private static final String HEADER_AUTHORIZATION = "Authorization";
70      private static final String HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization";
71      private static final String AUTH_SCHEME_BASIC = "Basic";
72      private static final int HTTP_STATUS_TEMPORARY_REDIRECT = 307;
73      private static final int HTTP_STATUS_PERMANENT_REDIRECT = 308;
74  
75      private final ChecksumExtractor checksumExtractor;
76      private final PathProcessor pathProcessor;
77      private final URI baseUri;
78      private final Map<String, String> headers;
79      private final String userAgent;
80      private final int connectTimeout;
81      private final int requestTimeout;
82      private final boolean preemptiveAuth;
83      private final Charset authEncoding;
84      private final String auth;
85      private final Proxy proxy;
86      private final String proxyAuth;
87      private final RedirectMode redirectMode;
88      private final boolean originScopedHeaders;
89      private final boolean redirectAllowDowngrade;
90      private final int maxRedirects;
91      private final boolean closeConnection;
92  
93      private final Object authKey;
94      private final Object proxyAuthKey;
95      private final Function<Object, Boolean> cacheGetter;
96      private final Consumer<Object> cacheSetter;
97  
98      @FunctionalInterface
99      private interface IOSupplier<T> {
100         T get() throws IOException;
101     }
102 
103     @SuppressWarnings("deprecation")
104     public UrlTransporter(
105             RemoteRepository repository,
106             RepositorySystemSession session,
107             ChecksumExtractor checksumExtractor,
108             PathProcessor pathProcessor)
109             throws NoTransporterException {
110         this.checksumExtractor = checksumExtractor;
111         this.pathProcessor = pathProcessor;
112         try {
113             this.baseUri = HttpTransporterUtils.getBaseUri(repository);
114         } catch (URISyntaxException e) {
115             throw new NoTransporterException(repository, e.getMessage(), e);
116         }
117 
118         this.headers = HttpTransporterUtils.getHttpHeaders(session, repository);
119         this.userAgent = HttpTransporterUtils.getUserAgent(session, repository);
120         this.connectTimeout = HttpTransporterUtils.getHttpConnectTimeout(session, repository);
121         this.requestTimeout = HttpTransporterUtils.getHttpRequestTimeout(session, repository);
122         String authString = null;
123         try (AuthenticationContext repoAuthContext = AuthenticationContext.forRepository(session, repository)) {
124             if (repoAuthContext != null) {
125                 String username = repoAuthContext.get(AuthenticationContext.USERNAME);
126                 String password = repoAuthContext.get(AuthenticationContext.PASSWORD);
127                 if (username != null && password != null) {
128                     authString = username + ":" + password;
129                 }
130             }
131         }
132         this.authEncoding = HttpTransporterUtils.getHttpCredentialsEncoding(session, repository);
133         this.auth = authString;
134         this.preemptiveAuth = this.auth != null && HttpTransporterUtils.isHttpPreemptiveAuth(session, repository);
135         this.originScopedHeaders = HttpTransporterUtils.isOriginScopedHeaders(session, repository);
136 
137         org.eclipse.aether.repository.Proxy repoProxy = repository.getProxy();
138         this.proxy = repoProxy != null
139                 ? new Proxy(Proxy.Type.HTTP, new InetSocketAddress(repoProxy.getHost(), repoProxy.getPort()))
140                 : Proxy.NO_PROXY;
141         String proxyAuthString = null;
142         try (AuthenticationContext proxyAuthContext = AuthenticationContext.forProxy(session, repository)) {
143             if (proxyAuthContext != null) {
144                 String username = proxyAuthContext.get(AuthenticationContext.USERNAME);
145                 String password = proxyAuthContext.get(AuthenticationContext.PASSWORD);
146                 if (username != null && password != null) {
147                     proxyAuthString = username + ":" + password;
148                 }
149             }
150         }
151         this.proxyAuth = proxyAuthString;
152         this.redirectMode = HttpTransporterUtils.getRedirectMode(
153                 session, repository, UrlTransporterConfigurationKeys.CONFIG_PROP_REDIRECT_MODE);
154         this.redirectAllowDowngrade = HttpTransporterUtils.isFollowInsecureRedirect(
155                 session, repository, UrlTransporterConfigurationKeys.CONFIG_PROP_REDIRECT_ALLOW_DOWNGRADE);
156         this.maxRedirects = ConfigUtils.getInteger(
157                 session,
158                 UrlTransporterConfigurationKeys.DEFAULT_MAX_REDIRECT_COUNT,
159                 UrlTransporterConfigurationKeys.CONFIG_PROP_MAX_REDIRECT_COUNT + "." + repository.getId(),
160                 UrlTransporterConfigurationKeys.CONFIG_PROP_MAX_REDIRECT_COUNT);
161         this.closeConnection = ConfigUtils.getBoolean(
162                 session,
163                 UrlTransporterConfigurationKeys.DEFAULT_CLOSE_CONNECTION,
164                 UrlTransporterConfigurationKeys.CONFIG_PROP_CLOSE_CONNECTION + "." + repository.getId(),
165                 UrlTransporterConfigurationKeys.CONFIG_PROP_CLOSE_CONNECTION);
166 
167         this.authKey = Keys.of(UrlTransporter.class, repository, "auth");
168         this.proxyAuthKey = Keys.of(UrlTransporter.class, repository, "proxyAuth");
169         if (session.getCache() != null) {
170             this.cacheGetter = k -> {
171                 Boolean ret = (Boolean) session.getCache().get(session, k);
172                 if (ret == null) {
173                     return false;
174                 } else {
175                     return ret;
176                 }
177             };
178             this.cacheSetter = k -> session.getCache().put(session, k, Boolean.TRUE);
179         } else {
180             this.cacheGetter = k -> false;
181             this.cacheSetter = k -> {};
182         }
183     }
184 
185     @Override
186     protected void implPeek(PeekTask task) throws Exception {
187         HttpURLConnection con = perform(METHOD_HEAD, baseUri.resolve(task.getLocation()), null);
188         try {
189             int responseCode = con.getResponseCode();
190             if (HttpURLConnection.HTTP_OK != responseCode) {
191                 throw new HttpTransporterException(responseCode);
192             }
193         } finally {
194             con.disconnect();
195         }
196     }
197 
198     @Override
199     protected void implGet(GetTask task) throws Exception {
200         HttpURLConnection con = perform(METHOD_GET, baseUri.resolve(task.getLocation()), task);
201         try {
202             int responseCode = con.getResponseCode();
203             if (HttpURLConnection.HTTP_OK != responseCode) {
204                 throw new HttpTransporterException(responseCode);
205             }
206             IOSupplier<InputStream> inputStreamSupplier = () -> {
207                 String contentEncoding = con.getHeaderField("Content-Encoding");
208                 if (contentEncoding != null) {
209                     if ("gzip".equalsIgnoreCase(contentEncoding)) {
210                         return new GZIPInputStream(con.getInputStream());
211                     } else if ("deflate".equalsIgnoreCase(contentEncoding)) {
212                         return new InflaterInputStream(con.getInputStream());
213                     }
214                 }
215                 return con.getInputStream();
216             };
217             final Path dataFile = task.getDataPath();
218             if (dataFile == null) {
219                 try (InputStream is = inputStreamSupplier.get()) {
220                     utilGet(task, is, true, con.getContentLengthLong(), false);
221                 }
222             } else {
223                 try (PathProcessor.CollocatedTempFile tempFile = pathProcessor.newTempFile(dataFile)) {
224                     task.setDataPath(tempFile.getPath(), false);
225                     try (InputStream is = inputStreamSupplier.get()) {
226                         utilGet(task, is, true, con.getContentLengthLong(), false);
227                     }
228                     tempFile.move();
229                 } finally {
230                     task.setDataPath(dataFile);
231                 }
232             }
233             if (task.getDataPath() != null) {
234                 long lastModified = con.getLastModified();
235                 if (lastModified != 0) {
236                     pathProcessor.setLastModified(
237                             task.getDataPath(), HttpTransporterUtils.clampRemoteLastModified(lastModified));
238                 }
239             }
240         } finally {
241             con.disconnect();
242         }
243     }
244 
245     @Override
246     protected void implPut(PutTask task) {
247         throw new UnsupportedOperationException("PUT method unsupported");
248     }
249 
250     @Override
251     protected void implClose() {
252         // nothing
253     }
254 
255     private HttpURLConnection perform(String method, URI target, GetTask task) throws IOException {
256         String currAuth = preemptiveAuth ? auth : null;
257         String currProxyAuth = null;
258         if (cacheGetter.apply(authKey)) {
259             currAuth = this.auth;
260         }
261         if (cacheGetter.apply(proxyAuthKey)) {
262             currProxyAuth = this.proxyAuth;
263         }
264         return perform(method, new ArrayList<>(Collections.singletonList(target)), currAuth, currProxyAuth, task);
265     }
266 
267     private HttpURLConnection perform(
268             String method, ArrayList<URI> target, String currAuth, String currProxyAuth, GetTask task)
269             throws IOException {
270         if (target.size() - 1 > maxRedirects) {
271             throw new IOException("Too many redirects");
272         }
273         HttpURLConnection con = (HttpURLConnection) target.get(0).toURL().openConnection(proxy);
274         con.setConnectTimeout(connectTimeout);
275         con.setReadTimeout(requestTimeout);
276         con.setRequestMethod(method);
277         con.setUseCaches(false);
278         con.setInstanceFollowRedirects(false);
279         con.setRequestProperty(HttpConstants.ACCEPT_ENCODING, "gzip,deflate");
280         con.setRequestProperty(HttpConstants.CACHE_CONTROL, "no-cache, no-store");
281         con.setRequestProperty("Pragma", "no-cache");
282         con.setRequestProperty(HttpConstants.USER_AGENT, userAgent);
283         boolean sameOrigin = TransporterUtils.isSameOrigin(baseUri, target.get(0));
284         if (!originScopedHeaders || sameOrigin) {
285             headers.forEach(con::setRequestProperty);
286         }
287         if (closeConnection) {
288             con.setRequestProperty("Connection", "close");
289         }
290         if (currAuth != null && (!originScopedHeaders || sameOrigin)) {
291             con.setRequestProperty(HEADER_AUTHORIZATION, basicAuthorization(currAuth));
292         }
293         if (currProxyAuth != null) {
294             con.setRequestProperty(HEADER_PROXY_AUTHORIZATION, basicAuthorization(currProxyAuth));
295         }
296         int responseCode = con.getResponseCode();
297         if (responseCode == HttpURLConnection.HTTP_OK) {
298             if (task != null) {
299                 Map<String, String> checksums = checksumExtractor.extractChecksums(con::getHeaderField);
300                 if (checksums != null && !checksums.isEmpty()) {
301                     checksums.forEach(task::setChecksum);
302                 }
303             }
304         } else if (responseCode == HttpURLConnection.HTTP_MOVED_PERM
305                 || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
306                 || responseCode == HttpURLConnection.HTTP_SEE_OTHER
307                 || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT
308                 || responseCode == HTTP_STATUS_PERMANENT_REDIRECT) {
309             if (redirectMode == RedirectMode.NONE) {
310                 con.disconnect();
311                 throw new IOException("Refusing to follow redirects");
312             }
313             final String location = con.getHeaderField(HEADER_LOCATION);
314             if (location == null) {
315                 con.disconnect();
316                 throw new IOException("Redirect response missing Location header");
317             }
318             final URI currentUri;
319             final URI redirectUri;
320             try {
321                 currentUri = URI.create(con.getURL().toString());
322                 redirectUri = currentUri.resolve(location);
323             } catch (IllegalArgumentException e) {
324                 con.disconnect();
325                 throw new IOException("Redirect response has invalid Location header: " + location, e);
326             }
327             // ensure we are HTTP or HTTPS after redirect
328             if (!"http".equalsIgnoreCase(redirectUri.getScheme())
329                     && !"https".equalsIgnoreCase(redirectUri.getScheme())) {
330                 con.disconnect();
331                 throw new IOException("Unsupported redirect protocol: " + redirectUri.getScheme());
332             }
333             String currentAuthority = currentUri.getAuthority();
334             String redirectAuthority = redirectUri.getAuthority();
335             if (redirectMode == RedirectMode.SAME_AUTHORITY
336                     && !TransporterUtils.isSameAuthority(currentUri, redirectUri)) {
337                 con.disconnect();
338                 throw new IOException("Refusing to follow redirect to different authority: " + redirectAuthority);
339             }
340             if (originScopedHeaders && !TransporterUtils.isSameOrigin(baseUri, redirectUri)) {
341                 currAuth = null;
342             } else if (currentAuthority == null || !currentAuthority.equalsIgnoreCase(redirectAuthority)) {
343                 // reset auth if authority differs after redirect
344                 currAuth = null;
345             }
346             if ("https".equalsIgnoreCase(currentUri.getScheme())
347                     && "http".equalsIgnoreCase(redirectUri.getScheme())
348                     && !redirectAllowDowngrade) {
349                 // forbid HTTPS -> HTTP downgrade during redirect
350                 con.disconnect();
351                 throw new IOException("Refusing to downgrade from HTTPS to HTTP protocol");
352             }
353             target.add(0, redirectUri);
354             con.disconnect();
355             return perform(method, target, currAuth, currProxyAuth, task);
356         } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED
357                 && currAuth == null
358                 && this.auth != null
359                 && (!originScopedHeaders || TransporterUtils.isSameOrigin(baseUri, target.get(0)))) {
360             con.disconnect();
361             return perform(method, target, this.auth, currProxyAuth, task);
362         } else if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH
363                 && currProxyAuth == null
364                 && this.proxyAuth != null) {
365             con.disconnect();
366             return perform(method, target, currAuth, this.proxyAuth, task);
367         }
368         if (currAuth != null) {
369             cacheSetter.accept(authKey);
370         }
371         if (currProxyAuth != null) {
372             cacheSetter.accept(proxyAuthKey);
373         }
374         return con;
375     }
376 
377     private String basicAuthorization(String credentials) {
378         return AUTH_SCHEME_BASIC + " " + Base64.getEncoder().encodeToString(credentials.getBytes(authEncoding));
379     }
380 }