Skip to content

Commit

Permalink
fix #126 Correctly use base hostname in HttpClientOptions even if proxy
Browse files Browse the repository at this point in the history
This commit fixes the HttpClientOptions to correctly derive relative
urls from the base url, even when a proxy is configured. It would
previously wrongly replace the hostname with `localhost`.
  • Loading branch information
simonbasle authored Jul 10, 2017
1 parent cbf0c1a commit 672b850
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/reactor/ipc/netty/http/client/HttpClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,25 @@ final String formatSchemeAndHost(String url, boolean ws) {
else {
schemeBuilder.append(isSecure() ? HttpClient.HTTPS_SCHEME : HttpClient.HTTP_SCHEME);
}

final String scheme = schemeBuilder.append("://").toString();
if (url.startsWith("/")) {
//consider relative URL, use the base hostname/port or fallback to localhost
SocketAddress remote = getAddress();

if (remote != null && !useProxy(remote) && remote instanceof InetSocketAddress) {
if (remote instanceof InetSocketAddress) {
InetSocketAddress inet = (InetSocketAddress) remote;

return scheme + inet.getHostName() + ":" + inet.getPort() + url;
}
return scheme + "localhost" + url;
else {
return scheme + "localhost" + url;
}
}
else {
//consider absolute URL
return scheme + url;
}
return scheme + url;
}
else {
return url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public void formatSchemeAndHostRelativeAddressSsl() throws Exception {
assertThat(test2).isEqualTo("wss://example:8080/foo");
}

@Test
public void formatSchemeAndHostRelativeAndProxy() {
String test = this.builder.proxy(proxyOptions)
.host("google.com")
.port(80)
.build()
.formatSchemeAndHost("/foo", false);

assertThat(test).isEqualTo("http://google.com:80/foo");
}

@Test
public void formatSchemeAndHostAbsoluteHttp() throws Exception {
String test1 = this.builder.build()
Expand Down

0 comments on commit 672b850

Please sign in to comment.