HTTPCLIENT-1092 If ClientPNames.VIRTUAL_HOST does not provide the port, derive it from the current request.

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1125566 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2011-05-20 20:51:24 +00:00
parent f3e7802196
commit 0e2b207489
5 changed files with 104 additions and 6 deletions

View File

@ -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>

View File

@ -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";

View File

@ -359,6 +359,14 @@ public class DefaultRequestDirector implements RequestDirector {
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);
boolean reuse = false;

View File

@ -611,6 +611,90 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}
@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();

View File

@ -144,10 +144,12 @@ httpclient.getConnectionManager().shutdown();
<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>