Skip to content

Commit

Permalink
Fix slackapi#1129 Setting schema-less URL for proxy URLs results in N…
Browse files Browse the repository at this point in the history
…ullPointerException
  • Loading branch information
seratch committed Mar 27, 2023
1 parent dc788dc commit 7592445
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public static ProxyUrl parse(String proxyUrl) {
return null;
}
String[] proxyUrlElements = proxyUrl.split("://");
String schema = proxyUrlElements[0] + "://";
String urlWithoutSchema = proxyUrlElements[1];
boolean withSchema = proxyUrlElements.length == 2;
String schema = withSchema ? proxyUrlElements[0] + "://" : "http://";
String urlWithoutSchema = withSchema ? proxyUrlElements[1] : proxyUrlElements[0];
String[] urlWithUserAndPasswordIfTwoElements = urlWithoutSchema.split("@");
if (urlWithUserAndPasswordIfTwoElements.length == 2) {
String[] userAndPassword = urlWithUserAndPasswordIfTwoElements[0].split(":");
Expand All @@ -51,7 +52,7 @@ public static ProxyUrl parse(String proxyUrl) {
.port(hostAndPort.length == 2 ? Integer.parseInt(hostAndPort[1].replace("/", "")) : 80)
.build();
} else {
String[] hostAndPort = proxyUrl.split("://")[1].split(":");
String[] hostAndPort = (withSchema ? proxyUrlElements[1] : proxyUrlElements[0]).split(":");
return ProxyUrl.builder()
.schema(schema)
.host(hostAndPort[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,26 @@ public void parse_username_password() {
public void parse_invalid_format() {
ProxyUrlUtil.parse("http://foo:bar:baz@localhost:9000");
}

@Test
public void parse_no_schema() {
ProxyUrlUtil.ProxyUrl expected = ProxyUrlUtil.ProxyUrl.builder()
.schema("http://")
.host("localhost")
.port(9000)
.build();
assertThat(ProxyUrlUtil.parse("localhost:9000"), is(expected));
}

@Test
public void parse_username_password_no_schema() {
ProxyUrlUtil.ProxyUrl expected = ProxyUrlUtil.ProxyUrl.builder()
.schema("http://")
.host("localhost")
.username("user")
.password("password")
.port(9000)
.build();
assertThat(ProxyUrlUtil.parse("user:password@localhost:9000"), is(expected));
}
}

0 comments on commit 7592445

Please sign in to comment.