diff options
author | Kristina Chodorow <kchodorow@google.com> | 2015-12-22 19:21:21 +0000 |
---|---|---|
committer | Dmitry Lomov <dslomov@google.com> | 2015-12-22 19:57:48 +0000 |
commit | 5fd83b28a267bbd4edbd01370e4b7905f5f9b0de (patch) | |
tree | 35953c905336be9cff614deb6b702553d20f31cc /src/main/java | |
parent | 3a837477b79599263dd0f6284806aed291c7dcfd (diff) |
Add support for downloading through proxies
Fixes #587.
--
MOS_MIGRATED_REVID=110785300
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java index e14edbb8f1..3f1495012f 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java @@ -14,6 +14,8 @@ package com.google.devtools.build.lib.bazel.repository; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; import com.google.common.hash.Hasher; import com.google.common.hash.Hashing; import com.google.devtools.build.lib.events.Event; @@ -30,6 +32,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.net.URL; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -175,12 +179,58 @@ public class HttpDownloader { } private InputStream getInputStream(URL url) throws IOException { - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + HttpURLConnection connection = (HttpURLConnection) url.openConnection( + createProxyIfNeeded(url.getProtocol())); connection.setInstanceFollowRedirects(true); connection.connect(); return connection.getInputStream(); } + private static Proxy createProxyIfNeeded(String protocol) throws IOException { + if (protocol.equals("https")) { + return createProxy(System.getenv("HTTPS_PROXY")); + } else if (protocol.equals("http")) { + return createProxy(System.getenv("HTTP_PROXY")); + } + return Proxy.NO_PROXY; + } + + @VisibleForTesting + static Proxy createProxy(String proxyAddress) throws IOException { + if (Strings.isNullOrEmpty(proxyAddress)) { + return Proxy.NO_PROXY; + } + + // Split the proxyAddress into the protocol, address, and optional port. This does not use the + // URL class to avoid the DNS resolution it performs on creation. + int protocolIndex = proxyAddress.indexOf(':'); + if (protocolIndex == -1) { + throw new IOException("No proxy protocol found for " + proxyAddress); + } + boolean https; + String protocol = proxyAddress.substring(0, protocolIndex); + if (protocol.equals("https")) { + https = true; + } else if (protocol.equals("http")) { + https = false; + } else { + throw new IOException("Invalid proxy protocol for " + proxyAddress); + } + int portIndex = proxyAddress.lastIndexOf(':'); + if (protocolIndex == portIndex) { + // No port set, just the http: colon. + return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyAddress, https ? 443 : 80)); + } + + try { + return new Proxy(Proxy.Type.HTTP, new InetSocketAddress( + proxyAddress.substring(0, portIndex), + Integer.parseInt(proxyAddress.substring(portIndex + 1)))); + } catch (NumberFormatException e) { + throw new IOException("Error parsing proxy port: " + proxyAddress); + } + } + public static String getHash(Hasher hasher, Path path) throws IOException { byte byteBuffer[] = new byte[BUFFER_SIZE]; try (InputStream stream = path.getInputStream()) { |