001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.transport.url;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.HttpURLConnection;
024import java.net.InetSocketAddress;
025import java.net.Proxy;
026import java.net.URI;
027import java.net.URISyntaxException;
028import java.nio.charset.Charset;
029import java.nio.file.Path;
030import java.util.ArrayList;
031import java.util.Base64;
032import java.util.Collections;
033import java.util.Map;
034import java.util.function.Consumer;
035import java.util.function.Function;
036import java.util.zip.GZIPInputStream;
037import java.util.zip.InflaterInputStream;
038
039import org.eclipse.aether.ConfigurationProperties.RedirectMode;
040import org.eclipse.aether.Keys;
041import org.eclipse.aether.RepositorySystemSession;
042import org.eclipse.aether.repository.AuthenticationContext;
043import org.eclipse.aether.repository.RemoteRepository;
044import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
045import org.eclipse.aether.spi.connector.transport.GetTask;
046import org.eclipse.aether.spi.connector.transport.PeekTask;
047import org.eclipse.aether.spi.connector.transport.PutTask;
048import org.eclipse.aether.spi.connector.transport.http.ChecksumExtractor;
049import org.eclipse.aether.spi.connector.transport.http.HttpConstants;
050import org.eclipse.aether.spi.connector.transport.http.HttpTransporter;
051import org.eclipse.aether.spi.connector.transport.http.HttpTransporterException;
052import org.eclipse.aether.spi.io.PathProcessor;
053import org.eclipse.aether.transfer.NoTransporterException;
054import org.eclipse.aether.util.ConfigUtils;
055import org.eclipse.aether.util.connector.transport.TransporterUtils;
056import org.eclipse.aether.util.connector.transport.http.HttpTransporterUtils;
057
058/**
059 * A special, "read only" and limited capability transport usable for bootstrapping. It provides HTTP with minimal
060 * support (only basic auth, only GET/HEAD). It is implemented using {@link java.net.HttpURLConnection} class.
061 *
062 * @since 2.0.21
063 */
064public class UrlTransporter extends AbstractTransporter implements HttpTransporter {
065
066    private static final String METHOD_GET = "GET";
067    private static final String METHOD_HEAD = "HEAD";
068    private static final String HEADER_LOCATION = "Location";
069    private static final String HEADER_AUTHORIZATION = "Authorization";
070    private static final String HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization";
071    private static final String AUTH_SCHEME_BASIC = "Basic";
072    private static final int HTTP_STATUS_TEMPORARY_REDIRECT = 307;
073    private static final int HTTP_STATUS_PERMANENT_REDIRECT = 308;
074
075    private final ChecksumExtractor checksumExtractor;
076    private final PathProcessor pathProcessor;
077    private final URI baseUri;
078    private final Map<String, String> headers;
079    private final String userAgent;
080    private final int connectTimeout;
081    private final int requestTimeout;
082    private final boolean preemptiveAuth;
083    private final Charset authEncoding;
084    private final String auth;
085    private final Proxy proxy;
086    private final String proxyAuth;
087    private final RedirectMode redirectMode;
088    private final boolean originScopedHeaders;
089    private final boolean redirectAllowDowngrade;
090    private final int maxRedirects;
091    private final boolean closeConnection;
092
093    private final Object authKey;
094    private final Object proxyAuthKey;
095    private final Function<Object, Boolean> cacheGetter;
096    private final Consumer<Object> cacheSetter;
097
098    @FunctionalInterface
099    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}