From e4f5663623ffe7f9f74e12c04771d95d8476686b Mon Sep 17 00:00:00 2001 From: Nitesh Kant Date: Tue, 28 Oct 2014 16:14:38 -0700 Subject: [PATCH] Fixes #258 (Add port to the host header) --- .../protocol/http/client/HttpClientImpl.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/rx-netty/src/main/java/io/reactivex/netty/protocol/http/client/HttpClientImpl.java b/rx-netty/src/main/java/io/reactivex/netty/protocol/http/client/HttpClientImpl.java index ce14abe8..fc0e2f1b 100644 --- a/rx-netty/src/main/java/io/reactivex/netty/protocol/http/client/HttpClientImpl.java +++ b/rx-netty/src/main/java/io/reactivex/netty/protocol/http/client/HttpClientImpl.java @@ -35,6 +35,8 @@ public class HttpClientImpl extends RxClientImpl, HttpClientResponse> implements HttpClient { + private final String hostHeaderValue; + public HttpClientImpl(String name, ServerInfo serverInfo, Bootstrap clientBootstrap, PipelineConfigurator, HttpClientRequest> pipelineConfigurator, ClientConfig clientConfig, @@ -44,6 +46,7 @@ public HttpClientImpl(String name, ServerInfo serverInfo, Bootstrap clientBootst MetricEventsSubject> eventsSubject) { super(name, serverInfo, clientBootstrap, pipelineConfigurator, clientConfig, channelFactory, connectionFactory, eventsSubject); + hostHeaderValue = prepareHostHeaderValue(); } public HttpClientImpl(String name, ServerInfo serverInfo, Bootstrap clientBootstrap, @@ -52,6 +55,7 @@ public HttpClientImpl(String name, ServerInfo serverInfo, Bootstrap clientBootst ConnectionPoolBuilder, HttpClientRequest> poolBuilder, MetricEventsSubject> eventsSubject) { super(name, serverInfo, clientBootstrap, pipelineConfigurator, clientConfig, poolBuilder, eventsSubject); + hostHeaderValue = prepareHostHeaderValue(); } @Override @@ -69,7 +73,7 @@ protected Observable> submit(final HttpClientRequest re return submit(request, connectionObservable, null == clientConfig ? HttpClientConfig.Builder.newDefaultConfig() : clientConfig); } - + protected Observable> submit(final HttpClientRequest request, final Observable, HttpClientRequest>> connectionObservable, final ClientConfig config) { @@ -133,7 +137,7 @@ private void enrichRequest(HttpClientRequest request, ClientConfig config) { request.setDynamicUriParts(serverInfo.getHost(), serverInfo.getPort(), false /*Set when we handle https*/); if(!request.getHeaders().contains(HttpHeaders.Names.HOST)) { - request.getHeaders().add(HttpHeaders.Names.HOST, serverInfo.getHost()); + request.getHeaders().add(HttpHeaders.Names.HOST, hostHeaderValue); } if (config instanceof HttpClientConfig) { @@ -143,4 +147,12 @@ private void enrichRequest(HttpClientRequest request, ClientConfig config) { } } } + + private String prepareHostHeaderValue() { + if (serverInfo.getPort() == 80 || serverInfo.getPort() == 443) { + return serverInfo.getHost(); + } + // Add port to the host header if the port is not standard port. Issue: https://github.com/ReactiveX/RxNetty/issues/258 + return serverInfo.getHost() + ':' + serverInfo.getPort(); + } } \ No newline at end of file