Index: java/org/apache/commons/httpclient/HostConfiguration.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v retrieving revision 1.8 diff -u -r1.8 HostConfiguration.java --- java/org/apache/commons/httpclient/HostConfiguration.java 28 Jan 2003 04:40:20 -0000 1.8 +++ java/org/apache/commons/httpclient/HostConfiguration.java 8 Apr 2003 02:02:39 -0000 @@ -69,6 +69,7 @@ * * @author Michael Becke * @author Mike Bowler + * @author Oleg Kalnichevski * @since 2.0 */ public class HostConfiguration implements Cloneable { @@ -76,6 +77,9 @@ /** The host to use. */ private String host; + /** The virtual host to use. */ + private String virtualHost; + /** The port to use. */ private int port; @@ -100,6 +104,7 @@ public HostConfiguration() { this.host = null; + this.virtualHost = null; this.port = -1; this.protocol = null; this.hostSet = false; @@ -121,6 +126,7 @@ // having to negotiate the monitor for each method call synchronized (hostConfiguration) { this.host = hostConfiguration.getHost(); + this.virtualHost = hostConfiguration.getVirtualHost(); this.port = hostConfiguration.getPort(); this.protocol = hostConfiguration.getProtocol(); this.hostSet = hostConfiguration.isHostSet(); @@ -153,10 +159,25 @@ public synchronized boolean hostEquals(HttpConnection connection) { if (hostSet) { - return (host.equalsIgnoreCase(connection.getHost()) - && port == connection.getPort() - && protocol.equals(connection.getProtocol())); - + if (!this.host.equalsIgnoreCase(connection.getHost())) { + return false; + } + if (this.virtualHost != null) { + if (!this.virtualHost.equalsIgnoreCase(connection.getVirtualHost())) { + return false; + } + } else { + if (connection.getVirtualHost() != null) { + return false; + } + } + if (this.port != connection.getPort()) { + return false; + } + if (!this.protocol.equals(connection.getProtocol())) { + return false; + } + return true; } else { return false; } @@ -195,23 +216,25 @@ } /** - * Set the host - * @param host The host. + * Set the given host, port and protocol + * + * @param host the host, IP or DNS name * @param port The port * @param protocol The protocol. */ public synchronized void setHost(String host, int port, String protocol) { - setHost(host, port, Protocol.getProtocol(protocol)); + setHost(host, null, port, Protocol.getProtocol(protocol)); } /** - * Sets this configuration's host infomation. + * Set the given host, virtual host, port and protocol. * * @param host the host, IP or DNS name + * @param virtualHost the virtual host name * @param port the host port or -1 to use protocol default * @param protocol the protocol */ - public synchronized void setHost(String host, int port, + public synchronized void setHost(String host, String virtualHost, int port, Protocol protocol) { if (host == null) { @@ -222,6 +245,7 @@ } this.host = host; + this.virtualHost = virtualHost; this.port = port == -1 ? protocol.getDefaultPort() : port; this.protocol = protocol; @@ -230,6 +254,38 @@ } /** + * Set the given host, port and protocol. + * + * @param host the host, IP or DNS name + * @param port The port + * @param protocol the protocol + */ + public synchronized void setHost(String host, int port, Protocol protocol) { + setHost(host, null, port, protocol); + } + + + /** + * Set the given host and port. Select default protocol. + * @param host the host, IP or DNS name + * @param port The port + */ + public synchronized void setHost(String host, int port) { + setHost(host, null, port, Protocol.getProtocol("http")); + } + + + /** + * Set the given host. Select default protocol and port. + * @param host The host. + */ + public synchronized void setHost(String host) { + Protocol defaultProtocol = Protocol.getProtocol("http"); + setHost(host, null, defaultProtocol.getDefaultPort(), defaultProtocol); + } + + + /** * Sets the protocol, host and port from the given URI. * @param uri the URI. */ @@ -273,6 +329,14 @@ } /** + * Returns the virtual host. + * @return String + */ + public synchronized String getVirtualHost() { + return virtualHost; + } + + /** * Returns the port. * @return int */ @@ -339,10 +403,22 @@ HostConfiguration config = (HostConfiguration) o; if (hostSet) { - if (!host.equalsIgnoreCase(config.getHost()) - || port != config.getPort() - || !protocol.equals(config.getProtocol())) { - // either host, port or protocol don't match + if (!host.equalsIgnoreCase(config.getHost())) { + return false; + } + if (virtualHost != null) { + if (!virtualHost.equalsIgnoreCase(config.getVirtualHost())) { + return false; + } + } else { + if (config.getVirtualHost() != null) { + return false; + } + } + if (port != config.getPort()) { + return false; + } + if (!protocol.equals(config.getProtocol())) { return false; } } else if (config.isHostSet()) { Index: java/org/apache/commons/httpclient/HttpClient.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v retrieving revision 1.71 diff -u -r1.71 HttpClient.java --- java/org/apache/commons/httpclient/HttpClient.java 25 Feb 2003 09:13:55 -0000 1.71 +++ java/org/apache/commons/httpclient/HttpClient.java 8 Apr 2003 02:02:40 -0000 @@ -81,7 +81,7 @@ * @author Rodney Waldhoff * @author Sean C. Sullivan * @author dIon Gillard - * @author Ortwin Gl� + * @author Ortwin Gl�ck * @author Michael Becke * @author Mike Bowler * @author Sam Maloney @@ -366,7 +366,7 @@ throw new IllegalStateException ("HttpURL or HttpsURL instance required"); } - this.hostConfiguration.setHost(host, port, protocol); + this.hostConfiguration.setHost(host, null, port, protocol); } /** @@ -390,7 +390,7 @@ int port = url.getPort(); Protocol protocol = Protocol.getProtocol(url.getProtocol()); - hostConfiguration.setHost(url.getHost(), port, protocol); + hostConfiguration.setHost(url.getHost(), null, port, protocol); } /** @@ -532,6 +532,7 @@ if (!methodConfiguration.isHostSet()) { methodConfiguration.setHost( defaultHostConfiguration.getHost(), + defaultHostConfiguration.getVirtualHost(), defaultHostConfiguration.getPort(), defaultHostConfiguration.getProtocol() ); Index: java/org/apache/commons/httpclient/HttpConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v retrieving revision 1.53 diff -u -r1.53 HttpConnection.java --- java/org/apache/commons/httpclient/HttpConnection.java 4 Apr 2003 02:37:02 -0000 1.53 +++ java/org/apache/commons/httpclient/HttpConnection.java 8 Apr 2003 02:02:43 -0000 @@ -147,7 +147,18 @@ * @param protocol the protocol to use */ public HttpConnection(String host, int port, Protocol protocol) { - this(null, -1, host, port, protocol); + this(null, -1, host, null, port, protocol); + } + + /** + * Constructor. + * + * @param host the host I should connect to + * @param port the port I should connect to + * @param protocol the protocol to use + */ + public HttpConnection(String host, String virtualHost, int port, Protocol protocol) { + this(null, -1, host, virtualHost, port, protocol); } /** @@ -186,7 +197,7 @@ String host, int port, boolean secure) { - this(proxyHost, proxyPort, host, port, + this(proxyHost, proxyPort, host, null, port, Protocol.getProtocol(secure ? "https" : "http")); } @@ -199,6 +210,7 @@ this(hostConfiguration.getProxyHost(), hostConfiguration.getProxyPort(), hostConfiguration.getHost(), + hostConfiguration.getVirtualHost(), hostConfiguration.getPort(), hostConfiguration.getProtocol()); } @@ -209,23 +221,18 @@ * @param proxyHost the host I should proxy via * @param proxyPort the port I should proxy via * @param host the host I should connect to. Parameter value must be non-null. + * @param virtualHost the virtual host I will be sending requests to * @param port the port I should connect to - * @param protocol The protocol to use. + * @param protocol The protocol to use. Parameter value must be non-null. */ public HttpConnection( String proxyHost, int proxyPort, String host, + String virtualHost, int port, Protocol protocol) { - if (LOG.isDebugEnabled()) { - LOG.debug( - "creating connection for " + host + ":" + port + " via " - + proxyHost + ":" + proxyPort + " using protocol: " + protocol - ); - } - if (host == null) { throw new IllegalArgumentException("host parameter is null"); } @@ -233,12 +240,38 @@ throw new IllegalArgumentException("protocol is null"); } + if (LOG.isDebugEnabled()) { + StringBuffer buffer = new StringBuffer(); + buffer.append("Creating connection for "); + buffer.append(host); + if (virtualHost != null) { + buffer.append("["); + buffer.append(virtualHost); + buffer.append("]"); + } + if (port < 0) { + buffer.append(":"); + buffer.append(port); + } + if (proxyHost != null) { + buffer.append(" via "); + buffer.append(proxyHost); + if (proxyPort < 0) { + buffer.append(":"); + buffer.append(proxyPort); + } + } + buffer.append(" using protocol "); + buffer.append(protocol.toString()); + LOG.debug(buffer.toString()); + } + proxyHostName = proxyHost; proxyPortNumber = proxyPort; hostName = host; + virtualName = virtualHost; portNumber = protocol.resolvePort(port); protocolInUse = protocol; - } // ------------------------------------------ Attribute Setters and Getters @@ -260,13 +293,36 @@ */ public void setHost(String host) throws IllegalStateException { if (host == null) { - throw new NullPointerException("host parameter is null"); + throw new IllegalArgumentException("host parameter is null"); } assertNotOpen(); hostName = host; } /** + * Return my virtual host name. + * + * @return my virtual host. + */ + public String getVirtualHost() { + return virtualName; + } + + /** + * Set my virtual host name. + * + * @param host the virtual host name that should be used instead of + * physical host name when sending HTTP requests. Virtual host + * name can be set to null if virtual host name is not + * to be used + * @throws IllegalStateException if I am already connected + */ + public void setVirtualHost(String host) throws IllegalStateException { + assertNotOpen(); + virtualName = host; + } + + /** * Return my port. * * If the port is -1 (or less than 0) the default port for @@ -1094,6 +1150,9 @@ /** My host. */ private String hostName = null; + + /** My virtual host. */ + private String virtualName = null; /** My port. */ private int portNumber = -1; Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.131 diff -u -r1.131 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 7 Apr 2003 19:23:36 -0000 1.131 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 8 Apr 2003 02:02:49 -0000 @@ -1431,7 +1431,12 @@ // applications to send the Host request-header. // TODO: Add the ability to disable the sending of this header for // HTTP/1.0 requests. - String host = conn.getHost(); + String host = conn.getVirtualHost(); + if (host != null) { + LOG.debug("Using virtual host name: " + host); + } else { + host = conn.getHost(); + } int port = conn.getPort(); if (getRequestHeader("host") != null) { @@ -1603,21 +1608,27 @@ Protocol protocol = connection.getProtocol(); buf.append(protocol.getScheme().toLowerCase()); buf.append("://"); - buf.append(connection.getHost()); + if (connection.getVirtualHost() != null) { + buf.append(connection.getVirtualHost()); + } else { + buf.append(connection.getHost()); + } if ((connection.getPort() != -1) && (connection.getPort() != protocol.getDefaultPort()) ) { buf.append(":"); buf.append(connection.getPort()); } + if (requestPath != null && !requestPath.startsWith("/")) { + // append a "/" as one is included by the path, but is needed + // to separate the path from the host + buf.append("/"); + } } // Append path, if any if (requestPath == null) { buf.append("/"); } else { - if (!connection.isTransparent() && !requestPath.startsWith("/")) { - buf.append("/"); - } buf.append(requestPath); } // Append query, if any Index: java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java,v retrieving revision 1.12 diff -u -r1.12 MultiThreadedHttpConnectionManager.java --- java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java 6 Mar 2003 07:49:03 -0000 1.12 +++ java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java 8 Apr 2003 02:02:50 -0000 @@ -324,8 +324,11 @@ SimpleHttpConnectionManager.finishLastResponse(conn); HostConfiguration connectionConfiguration = new HostConfiguration(); - connectionConfiguration.setHost(conn.getHost(), - conn.getPort(), conn.getProtocol()); + connectionConfiguration.setHost( + conn.getHost(), + conn.getVirtualHost(), + conn.getPort(), + conn.getProtocol()); if (conn.getProxyHost() != null) { connectionConfiguration.setProxy(conn.getProxyHost(), conn.getProxyPort()); } Index: java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v retrieving revision 1.10 diff -u -r1.10 SimpleHttpConnectionManager.java --- java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java 31 Jan 2003 00:33:36 -0000 1.10 +++ java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java 8 Apr 2003 02:02:51 -0000 @@ -77,6 +77,7 @@ * @author Michael Becke * @author Eric Johnson * @author Mike Bowler + * @author Oleg Kalnichevski * * @since 2.0 */ @@ -107,6 +108,7 @@ Protocol protocol = hostConfiguration.getProtocol(); String host = hostConfiguration.getHost(); + String virtualHost = hostConfiguration.getVirtualHost(); int port = hostConfiguration.getPort(); if (httpConnection == null) { @@ -116,11 +118,16 @@ hostConfiguration.getProxyHost(), hostConfiguration.getProxyPort(), host, + virtualHost, port, protocol ); } else { - httpConnection = new HttpConnection(host, port, protocol); + httpConnection = new HttpConnection( + host, + virtualHost, + port, + protocol); } } else { @@ -135,6 +142,7 @@ } httpConnection.setHost(host); + httpConnection.setVirtualHost(virtualHost); httpConnection.setPort(port); httpConnection.setProtocol(protocol); Index: test/org/apache/commons/httpclient/SimpleHttpConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java,v retrieving revision 1.11 diff -u -r1.11 SimpleHttpConnection.java --- test/org/apache/commons/httpclient/SimpleHttpConnection.java 13 Mar 2003 17:51:28 -0000 1.11 +++ test/org/apache/commons/httpclient/SimpleHttpConnection.java 8 Apr 2003 02:02:52 -0000 @@ -111,16 +111,17 @@ } public SimpleHttpConnection() { - super(null, -1, "localhost", 80, Protocol.getProtocol("http")); + super(null, -1, "localhost", null, 80, Protocol.getProtocol("http")); } public SimpleHttpConnection( String proxyHost, int proxyPort, String host, + String virtualHost, int port, Protocol protocol) { - super(proxyHost, proxyPort, host, port, protocol); + super(proxyHost, proxyPort, host, virtualHost, port, protocol); } public SimpleHttpConnection(String host, int port){ Index: test/org/apache/commons/httpclient/TestRequestLine.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestRequestLine.java,v retrieving revision 1.1 diff -u -r1.1 TestRequestLine.java --- test/org/apache/commons/httpclient/TestRequestLine.java 13 Mar 2003 17:51:28 -0000 1.1 +++ test/org/apache/commons/httpclient/TestRequestLine.java 8 Apr 2003 02:02:52 -0000 @@ -60,8 +60,11 @@ package org.apache.commons.httpclient; -import org.apache.commons.httpclient.protocol.Protocol; -import junit.framework.*; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.commons.httpclient.protocol.Protocol; /** * Simple tests for {@link StatusLine}. @@ -71,8 +74,6 @@ */ public class TestRequestLine extends TestCase { - private StatusLine statusLine = null; - // ------------------------------------------------------------ Constructor public TestRequestLine(String testName) { super(testName); @@ -99,7 +100,7 @@ SimpleHttpConnection conn = null; SimpleHttpMethod method = null; - conn = new SimpleHttpConnection(null, -1, "localhost", 80, Protocol.getProtocol("http")); + conn = new SimpleHttpConnection(null, -1, "localhost", null, 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple / HTTP/1.1\r\n", method.getTestRequestLine(conn)); @@ -107,7 +108,7 @@ method = new SimpleHttpMethod("stuff"); assertEquals("Simple stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); - conn = new SimpleHttpConnection("proxy", 8080, "localhost", 80, Protocol.getProtocol("http")); + conn = new SimpleHttpConnection("proxy", 8080, "localhost", null, 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); @@ -115,7 +116,7 @@ method = new SimpleHttpMethod("stuff"); assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); - conn = new SimpleHttpConnection("proxy", 8080, "localhost", -1, Protocol.getProtocol("http")); + conn = new SimpleHttpConnection("proxy", 8080, "localhost", null, -1, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple http://localhost/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); @@ -123,7 +124,7 @@ method = new SimpleHttpMethod("stuff"); assertEquals("Simple http://localhost/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); - conn = new SimpleHttpConnection("proxy", 8080, "localhost", 666, Protocol.getProtocol("http")); + conn = new SimpleHttpConnection("proxy", 8080, "localhost", null, 666, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); assertEquals("Simple http://localhost:666/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); @@ -132,11 +133,48 @@ assertEquals("Simple http://localhost:666/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); } + public void testRequestLineVirtualHost() throws Exception { + SimpleHttpConnection conn = null; + SimpleHttpMethod method = null; + + conn = new SimpleHttpConnection(null, -1, "localhost", "virtual", 80, Protocol.getProtocol("http")); + + method = new SimpleHttpMethod(); + assertEquals("Simple / HTTP/1.1\r\n", method.getTestRequestLine(conn)); + + method = new SimpleHttpMethod("stuff"); + assertEquals("Simple stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); + + conn = new SimpleHttpConnection("proxy", 8080, "localhost", "virtual", 80, Protocol.getProtocol("http")); + + method = new SimpleHttpMethod(); + assertEquals("Simple http://virtual/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); + + method = new SimpleHttpMethod("stuff"); + assertEquals("Simple http://virtual/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); + + conn = new SimpleHttpConnection("proxy", 8080, "localhost", "virtual", -1, Protocol.getProtocol("http")); + + method = new SimpleHttpMethod(); + assertEquals("Simple http://virtual/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); + + method = new SimpleHttpMethod("stuff"); + assertEquals("Simple http://virtual/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); + + conn = new SimpleHttpConnection("proxy", 8080, "localhost", "virtual", 666, Protocol.getProtocol("http")); + + method = new SimpleHttpMethod(); + assertEquals("Simple http://virtual:666/ HTTP/1.1\r\n", method.getTestRequestLine(conn)); + + method = new SimpleHttpMethod("stuff"); + assertEquals("Simple http://virtual:666/stuff HTTP/1.1\r\n", method.getTestRequestLine(conn)); + } + public void testRequestLineQuery() throws Exception { SimpleHttpConnection conn = null; SimpleHttpMethod method = null; - conn = new SimpleHttpConnection(null, -1, "localhost", 80, Protocol.getProtocol("http")); + conn = new SimpleHttpConnection(null, -1, "localhost", null, 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); method.setQueryString( new NameValuePair[] { @@ -151,7 +189,7 @@ SimpleHttpConnection conn = null; SimpleHttpMethod method = null; - conn = new SimpleHttpConnection(null, -1, "localhost", 80, Protocol.getProtocol("http")); + conn = new SimpleHttpConnection(null, -1, "localhost", null, 80, Protocol.getProtocol("http")); method = new SimpleHttpMethod(); method.setPath("/some%20stuff/");