From f7d8288bd7b16c7f2e010aa8ddc241cf2ba8e0d5 Mon Sep 17 00:00:00 2001 From: Adam Wolf Date: Wed, 2 Mar 2022 08:44:51 -0800 Subject: [PATCH] Yield a Proxy for addresses without protocol Addresses https://github.com/bazelbuild/bazel/issues/14896 Closes #14897. PiperOrigin-RevId: 431949154 --- .../repository/downloader/ProxyHelper.java | 24 +++++++++++-------- .../downloader/ProxyHelperTest.java | 12 ++++------ 2 files changed, 18 insertions(+), 18 deletions(-) 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