On Thu, Jan 05, 2006 at 03:36:04PM +0100, Jasper van Zandbeek wrote:
> Hello,
> 
> The attached patch adds the possibility to specify the port number for 
> virtual hosts.
> 
> HttpClient currently uses the port number of the connection in the Host 
> request header if the port number is different from the default port number 
> for the protocol. Currently it isn't possible to specify a port number 
> different from the port number of the connection. This patch adds this 
> functionality.
> 
> According to the HTTP/1.1 specification, the port number specified in the 
> Host request header doesn't need to be the same as the port number of the 
> connection. Some web servers, e.g. Apache, ignore a different port number, 
> but e.g. the application server WebSphere allows the port number of the 
> connection and the port number in the Host header to be different.
> 
> This patch allows users of HttpClient to use a different port number for the 
> Host request header if the webserver they connect to supports this.
> 
> Here's a code snippet that uses the patched code:
> 
> HttpClient httpClient = new HttpClient();
> HttpMethod method = new GetMethod();
> HostConfiguration hostConfiguration = new HostConfiguration();
> hostConfiguration.setHost("localhost", 80, "http");
> HostParams params = new HostParams();
> params.setVirtualHost("localhost");
> params.setVirtualHostPort(100);
> hostConfiguration.setParams(params);
> httpClient.executeMethod(hostConfiguration, method);
> System.out.println(method.getResponseBodyAsString());
> method.releaseConnection();
> 
> Kind regards,
> 
> Jasper van Zandbeek
> 

What's wrong with this?

HttpClient httpClient = new HttpClient();
HttpMethod method = new GetMethod();
HostConfiguration hostConfiguration = new HostConfiguration();
hostConfiguration.setHost("localhost", 100, "http");
HostParams params = new HostParams();
params.setVirtualHost("localhost");
hostConfiguration.setParams(params);
httpClient.executeMethod(hostConfiguration, method);
System.out.println(method.getResponseBodyAsString());
method.releaseConnection();

Oleg

>  
>  <<patchfile.txt>> 

Content-Description: patchfile.txt
> --- org\apache\commons\httpclient\HttpMethodBase.java.orig    2006-01-05 
> 04:03:48.000000000 +0100
> +++ org\apache\commons\httpclient\HttpMethodBase.java 2006-01-05 
> 14:02:22.108177600 +0100
> @@ -1221,7 +1221,13 @@
>          } else {
>              host = conn.getHost();
>          }
> -        int port = conn.getPort();
> +        int port = this.params.getVirtualHostPort();
> +        if (port == -1) { 
> +            port = conn.getPort();
> +        }
> +        else {
> +            LOG.debug("Using virtual host port: " + port);
> +        }
>  
>          // Note: RFC 2616 uses the term "internet host name" for what goes 
> on the
>          // host line.  It would seem to imply that host should be blank if 
> the
> --- org\apache\commons\httpclient\params\HostParams.java.orig 2006-01-05 
> 04:03:48.000000000 +0100
> +++ org\apache\commons\httpclient\params\HostParams.java      2006-01-05 
> 14:01:37.857044800 +0100
> @@ -97,5 +97,23 @@
>      public String getVirtualHost() {
>          return (String) getParameter(HttpMethodParams.VIRTUAL_HOST);
>      }
> +   
> +    /**
> +     * Sets the virtual host port.
> +     * 
> +     * @param port The port
> +     */
> +    public void setVirtualHostPort(int port) {
> +         setIntParameter(HttpMethodParams.VIRTUAL_HOST_PORT, port);
> +    }
> +    
> +    /**
> +     * Returns the virtual host port.
> +     * 
> +     * @return The virtual host port
> +     */
> +    public int getVirtualHostPort() {
> +        return getIntParameter(HttpMethodParams.VIRTUAL_HOST_PORT, -1);
> +    }   
>          
>  }
> --- org\apache\commons\httpclient\params\HttpMethodParams.java.orig   
> 2006-01-05 04:03:48.000000000 +0100
> +++ org\apache\commons\httpclient\params\HttpMethodParams.java        
> 2006-01-05 14:10:34.334917000 +0100
> @@ -265,6 +265,14 @@
>      public static final String VIRTUAL_HOST = "http.virtual-host"; 
>  
>      /**
> +     * Defines the virtual host port.
> +     * <p>
> +     * This parameter expects a value of type (@link Integer).
> +     * </p>
> +     */
> +    public static final String VIRTUAL_HOST_PORT = "http.virtual-host-port";
> +
> +    /**
>       * Sets the value to use as the multipart boundary.
>       * <p>
>       * This parameter expects a value if type [EMAIL PROTECTED] String}.
> @@ -459,6 +467,24 @@
>          return (String) getParameter(VIRTUAL_HOST);
>      }
>      
> +    /**
> +     * Sets the virtual host port.
> +     * 
> +     * @param port The host port
> +     */
> +    public void setVirtualHostPort(int port) {
> +        setIntParameter(VIRTUAL_HOST_PORT, port);
> +    }
> +    
> +    /**
> +     * Returns the virtual host port.
> +     * 
> +     * @return The virutal host port.
> +     */
> +    public int getVirtualHostPort() {
> +        return getIntParameter(VIRTUAL_HOST_PORT, -1);
> +    }    
> +    
>      private static final String[] PROTOCOL_STRICTNESS_PARAMETERS = {
>          UNAMBIGUOUS_STATUS_LINE,
>          SINGLE_COOKIE_HEADER,

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to