Index: src/test/org/apache/commons/httpclient/TestVirtualHost.java =================================================================== --- src/test/org/apache/commons/httpclient/TestVirtualHost.java (revision 359204) +++ src/test/org/apache/commons/httpclient/TestVirtualHost.java (working copy) @@ -31,12 +31,19 @@ package org.apache.commons.httpclient; import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.params.HttpConnectionParams; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.httpclient.server.HttpService; +import org.apache.commons.httpclient.server.RequestLine; import org.apache.commons.httpclient.server.SimpleRequest; import org.apache.commons.httpclient.server.SimpleResponse; @@ -121,4 +128,94 @@ httpget.releaseConnection(); } } + + private class VirtualHostService implements HttpService { + + public VirtualHostService() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException { + RequestLine reqline = request.getRequestLine(); + HttpVersion ver = reqline.getHttpVersion(); + Header header = request.getFirstHeader("Host"); + if (header == null) { + response.setStatusLine(ver, HttpStatus.SC_BAD_REQUEST); + return true; + } + String host = header.getValue(); + if (host.equalsIgnoreCase("whatever.com")) { + response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY); + response.setHeader(new Header("Location", "testhttp://www.whatever.com/")); + return true; + } else if (host.equalsIgnoreCase("www.whatever.com")) { + response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY); + response.setHeader(new Header("Location", "testhttp://www.whatever.co.nz/")); + return true; + } else if (host.equalsIgnoreCase("www.whatever.co.nz")) { + response.setStatusLine(ver, HttpStatus.SC_OK); + return true; + } else { + response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND); + return true; + } + } + } + + private class VirtualSocketFactory implements ProtocolSocketFactory { + + private final String hostname; + private final int port; + + public VirtualSocketFactory(final String hostname, int port) { + super(); + this.hostname = hostname; + this.port = port; + } + + public Socket createSocket( + final String host, + int port, + final InetAddress localAddress, + int localPort, + final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { + return new Socket(this.hostname, this.port); + } + + public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException { + return new Socket(this.hostname, this.port); + } + + public Socket createSocket(String host, int port) throws IOException, UnknownHostException { + return new Socket(this.hostname, this.port); + } + + } + + public void testRedirectWithVirtualHost() throws IOException { + String host = this.server.getLocalAddress(); + int port = this.server.getLocalPort(); + + Protocol testhttp = new Protocol("http", new VirtualSocketFactory(host, port), port); + Protocol.registerProtocol("testhttp", testhttp); + try { + this.server.setHttpService(new VirtualHostService()); + this.client.getHostConfiguration().setHost(host, port, "testhttp"); + this.client.getHostConfiguration().getParams().setVirtualHost("whatever.com"); + GetMethod httpget = new GetMethod("/"); + httpget.setFollowRedirects(true); + try { + this.client.executeMethod(httpget); + assertEquals(HttpStatus.SC_OK, httpget.getStatusCode()); + assertEquals("http://www.whatever.co.nz/", httpget.getURI().toString()); + } finally { + httpget.releaseConnection(); + } + } finally { + Protocol.unregisterProtocol("testhttp"); + } + + } + } Index: src/java/org/apache/commons/httpclient/HttpMethodDirector.java =================================================================== --- src/java/org/apache/commons/httpclient/HttpMethodDirector.java (revision 359204) +++ src/java/org/apache/commons/httpclient/HttpMethodDirector.java (working copy) @@ -604,7 +604,10 @@ LOG.debug("Redirect URI is not absolute - parsing as relative"); redirectUri = new URI(currentUri, redirectUri); } - } + } else { + // Reset the default params + method.getParams().setDefaults(this.params); + } method.setURI(redirectUri); hostConfiguration.setHost(redirectUri); } catch (URIException e) {