From 4a9f0a6ebb54707fd6f814fbdcef9aab701b97b3 Mon Sep 17 00:00:00 2001 From: Karl Wright Date: Thu, 10 Jan 2013 11:27:36 +0000 Subject: [PATCH] Second fix for HTTPCLIENT-1296. Include Oleg's suggestions as to a better way to obtain a port from the non-existent target. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1431285 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/client/DefaultRequestDirector.java | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java index d7f226e35..4b55468d6 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java @@ -380,6 +380,15 @@ public class DefaultRequestDirector implements RequestDirector { virtualHost = (HttpHost) origWrapper.getParams().getParameter(ClientPNames.VIRTUAL_HOST); + // HTTPCLIENT-1092 - add the port if necessary + if (virtualHost != null && virtualHost.getPort() == -1) { + HttpHost host = (target != null) ? target : origRoute.getTargetHost(); + int port = host.getPort(); + if (port != -1){ + virtualHost = new HttpHost(virtualHost.getHostName(), port, virtualHost.getSchemeName()); + } + } + RoutedRequest roureq = new RoutedRequest(origWrapper, origRoute); boolean reuse = false; @@ -449,27 +458,19 @@ public class DefaultRequestDirector implements RequestDirector { } // Get target. Even if there's virtual host, we may need the target to set the port. - URI requestURI = wrapper.getURI(); - if (requestURI.isAbsolute()) { - target = new HttpHost( - requestURI.getHost(), requestURI.getPort(), requestURI.getScheme()); + if (virtualHost != null) { + target = virtualHost; + } else { + URI requestURI = wrapper.getURI(); + if (requestURI.isAbsolute()) { + target = new HttpHost( + requestURI.getHost(), requestURI.getPort(), requestURI.getScheme()); + } } if (target == null) { target = route.getTargetHost(); } - // Override the target if the virtual host is present. But make sure the port is right. - if (virtualHost != null) { - // HTTPCLIENT-1092 - add the port if necessary - if (virtualHost.getPort() == -1 && target != null) { - int port = target.getPort(); - if (port != -1) { - virtualHost = new HttpHost(virtualHost.getHostName(), port, virtualHost.getSchemeName()); - } - } - target = virtualHost; - } - // Reset headers on the request wrapper wrapper.resetHeaders(); // Re-write request URI if needed