hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1125558 - in /httpcomponents/httpclient/branches/4.1.x: ./ httpclient/src/main/java/org/apache/http/client/params/ httpclient/src/main/java/org/apache/http/impl/client/ httpclient/src/test/java/org/apache/http/impl/client/ src/docbkx/
Date Fri, 20 May 2011 20:45:30 GMT
Author: sebb
Date: Fri May 20 20:45:29 2011
New Revision: 1125558

URL: http://svn.apache.org/viewvc?rev=1125558&view=rev
Log:
HTTPCLIENT-1092 If ClientPNames.VIRTUAL_HOST does not provide the port, derive it from the
current request.

Modified:
    httpcomponents/httpclient/branches/4.1.x/RELEASE_NOTES.txt
    httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java
    httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
    httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
    httpcomponents/httpclient/branches/4.1.x/src/docbkx/httpagent.xml

Modified: httpcomponents/httpclient/branches/4.1.x/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/RELEASE_NOTES.txt?rev=1125558&r1=1125557&r2=1125558&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/branches/4.1.x/RELEASE_NOTES.txt Fri May 20 20:45:29 2011
@@ -1,5 +1,8 @@
 Changes since 4.1.1
 
+* [HTTPCLIENT-1092] If ClientPNames.VIRTUAL_HOST does not provide the port, derive it from
the current request.
+  Contributed by Sebastian Bazley <sebb at apache.org>
+
 * [HTTPCLIENT-1087] NTLM proxy authentication fails on retry if the underlying connection
is closed
   as a result of a target authentication failure.
   Contributed by Oleg Kalnichevski <olegk at apache.org>

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java?rev=1125558&r1=1125557&r2=1125558&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java
(original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java
Fri May 20 20:45:29 2011
@@ -100,11 +100,12 @@ public interface ClientPNames {
     public static final String COOKIE_POLICY = "http.protocol.cookie-policy";
 
     /**
-     * Defines the virtual host name to be used in the <code>Host</code>
-     * request header instead of the physical host name.
+     * Defines the virtual host to be used in the <code>Host</code>
+     * request header instead of the physical host.
      * <p>
      * This parameter expects a value of type {@link org.apache.http.HttpHost}.
      * </p>
+     * If a port is not provided, it will be derived from the request URL.
      */
     public static final String VIRTUAL_HOST = "http.virtual-host";
 

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java?rev=1125558&r1=1125557&r2=1125558&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
(original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
Fri May 20 20:45:29 2011
@@ -359,6 +359,15 @@ public class DefaultRequestDirector impl
 
         virtualHost = (HttpHost) orig.getParams().getParameter(
                 ClientPNames.VIRTUAL_HOST);
+        
+        // HTTPCLIENT-1092 - add the port if necessary
+        if (virtualHost != null && virtualHost.getPort() == -1) 
+        {
+            int port = target.getPort();
+            if (port != -1){
+                virtualHost = new HttpHost(virtualHost.getHostName(), port, virtualHost.getSchemeName());
+            }
+        }
 
         RoutedRequest roureq = new RoutedRequest(origWrapper, origRoute);
 

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java?rev=1125558&r1=1125557&r2=1125558&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
(original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
Fri May 20 20:45:29 2011
@@ -612,6 +612,90 @@ public class TestDefaultClientRequestDir
     }
 
     @Test
+    public void testDefaultHostHeader() throws Exception {
+        int port = this.localServer.getServiceAddress().getPort();
+        this.localServer.register("*", new SimpleService());
+
+        HttpContext context = new BasicHttpContext();
+
+        String s = "http://localhost:" + port;
+        HttpGet httpget = new HttpGet(s);
+
+        DefaultHttpClient client = new DefaultHttpClient();
+        HttpResponse response = client.execute(getServerHttp(), httpget, context);
+        EntityUtils.consume(response.getEntity());
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        // Check that Host header is generated as expected
+        Header[] headers = reqWrapper.getHeaders("host");
+        Assert.assertNotNull(headers);
+        Assert.assertEquals(1, headers.length);
+        Assert.assertEquals("localhost:"+port,headers[0].getValue());
+    }
+
+    @Test
+    // HTTPCLIENT-1092
+    public void testVirtualHostHeader() throws Exception {
+        int port = this.localServer.getServiceAddress().getPort();
+        this.localServer.register("*", new SimpleService());
+
+        HttpContext context = new BasicHttpContext();
+
+        String s = "http://localhost:" + port;
+        HttpGet httpget = new HttpGet(s);
+
+        DefaultHttpClient client = new DefaultHttpClient();
+        String virtHost = "virtual";
+        httpget.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost(virtHost,
port));
+        HttpResponse response = client.execute(getServerHttp(), httpget, context);
+        EntityUtils.consume(response.getEntity());
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        // Check that Host header is generated as expected
+        Header[] headers = reqWrapper.getHeaders("host");
+        Assert.assertNotNull(headers);
+        Assert.assertEquals(1, headers.length);
+        Assert.assertEquals(virtHost+":"+port,headers[0].getValue());
+    }
+
+    @Test
+    // Test that virtual port is propagated if provided
+    // This is not expected to be used much, if ever
+    // HTTPCLIENT-1092
+    public void testVirtualHostPortHeader() throws Exception {
+        int port = this.localServer.getServiceAddress().getPort();
+        this.localServer.register("*", new SimpleService());
+
+        HttpContext context = new BasicHttpContext();
+
+        String s = "http://localhost:" + port;
+        HttpGet httpget = new HttpGet(s);
+
+        DefaultHttpClient client = new DefaultHttpClient();
+        String virtHost = "virtual";
+        int virtPort = 9876;
+        httpget.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost(virtHost,
virtPort));
+        HttpResponse response = client.execute(getServerHttp(), httpget, context);
+        EntityUtils.consume(response.getEntity());
+
+        HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
+                ExecutionContext.HTTP_REQUEST);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        // Check that Host header is generated as expected
+        Header[] headers = reqWrapper.getHeaders("host");
+        Assert.assertNotNull(headers);
+        Assert.assertEquals(1, headers.length);
+        Assert.assertEquals(virtHost+":"+virtPort,headers[0].getValue());
+    }
+
+    @Test
     public void testDefaultHostAtRequestLevel() throws Exception {
         int port = this.localServer.getServiceAddress().getPort();
         this.localServer.register("*", new SimpleService());

Modified: httpcomponents/httpclient/branches/4.1.x/src/docbkx/httpagent.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/src/docbkx/httpagent.xml?rev=1125558&r1=1125557&r2=1125558&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/src/docbkx/httpagent.xml (original)
+++ httpcomponents/httpclient/branches/4.1.x/src/docbkx/httpagent.xml Fri May 20 20:45:29
2011
@@ -144,10 +144,12 @@ httpclient.getConnectionManager().shutdo
             <listitem>
                 <formalpara>
                     <title><constant>ClientPNames.VIRTUAL_HOST</constant>='http.virtual-host':</title>
-                    <para>defines the virtual host name to be used in the <literal>Host</literal>
-                        header instead of the physical host name. This parameter expects
a value of
-                        type <classname>HttpHost</classname>. If this parameter
is not set, the name or
-                        IP address of the target host will be used.</para>
+                    <para>defines the virtual host settings to be used in the <literal>Host</literal>
+                        header instead of the physical host. This parameter expects a value
of
+                        type <classname>HttpHost</classname>. The HttpHost port
does not have to
+                        be specified as it will be derived from the target.
+                        If this parameter is not set, the name or
+                        IP address (and port if required) of the target host will be used.</para>
                 </formalpara>
             </listitem>
             <listitem>



Mime
View raw message