Skip to content

Commit

Permalink
Yield a Proxy for addresses without protocol (#14956)
Browse files Browse the repository at this point in the history
Addresses #14896

Closes #14897.

PiperOrigin-RevId: 431949154
(cherry picked from commit f7d8288)

Co-authored-by: Adam Wolf <[email protected]>
  • Loading branch information
brentleyjones and adamawolf authored Mar 4, 2022
1 parent 85d7ed6 commit a6a4305
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a6a4305

Please sign in to comment.