Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yield a Proxy for addresses without protocol #14897

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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