diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelper.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelper.java index 559563cce5a76a..de90074c8ef727 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelper.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelper.java @@ -133,7 +133,7 @@ public static Proxy createProxy(@Nullable String proxyAddress) throws IOExceptio // Here there be dragons. Pattern urlPattern = - Pattern.compile("^(https?)://(([^:@]+?)(?::([^@]+?))?@)?([^:]+)(?::(\\d+))?/?$"); + Pattern.compile("^(https?://)?(([^:@]+?)(?::([^@]+?))?@)?([^:]+)(?::(\\d+))?/?$"); Matcher matcher = urlPattern.matcher(proxyAddress); if (!matcher.matches()) { throw new IOException("Proxy address " + proxyAddress + " is not a valid URL"); @@ -153,15 +153,19 @@ public static Proxy createProxy(@Nullable String proxyAddress) throws IOExceptio } boolean https; - switch (protocol) { - case "https": - https = true; - break; - case "http": - https = false; - break; - default: - throw new IOException("Invalid proxy protocol for " + cleanProxyAddress); + if (protocol == null) { + https = false; + } else { + switch (protocol) { + case "https://": + https = true; + break; + case "http://": + https = false; + break; + default: + throw new IOException("Invalid proxy protocol for " + cleanProxyAddress); + } } int port = https ? 443 : 80; // Default port numbers diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java index 2c5ce3019581ae..30a930ead7c924 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java @@ -204,18 +204,14 @@ public void testProxyExplicitPort() throws Exception { @Test public void testProxyNoProtocol() throws Exception { - IOException e = - assertThrows(IOException.class, () -> ProxyHelper.createProxy("my.example.com")); - assertThat(e).hasMessageThat().contains("Proxy address my.example.com is not a valid URL"); + Proxy proxy = ProxyHelper.createProxy("my.example.com"); + assertThat(proxy.toString()).endsWith(":80"); } @Test public void testProxyNoProtocolWithPort() throws Exception { - IOException e = - assertThrows(IOException.class, () -> ProxyHelper.createProxy("my.example.com:12345")); - assertThat(e) - .hasMessageThat() - .contains("Proxy address my.example.com:12345 is not a valid URL"); + Proxy proxy = ProxyHelper.createProxy("my.example.com:12345"); + assertThat(proxy.toString()).endsWith(":12345"); } @Test