You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by alin vasile <al...@yahoo.com> on 2011/03/12 12:20:27 UTC

Trimming port 80 in request Host header

Hi,

   I noticed that when the default port is specified explicitly in the requested URL address, http-client 4.1  will use it in the 'Host' header. The HTTP 1.1 specification doesn't deny this, but the same scenario can't be reproduced with any modern browser. Would be any issues if httpclient will have the same behaviour?

Sample code:

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.ro:80/");   
HttpResponse response = httpclient.execute(httpget);

Log:
[DEBUG] DefaultClientConnection - Sending request: GET / HTTP/1.1
[DEBUG] headers - >> GET / HTTP/1.1
[DEBUG] headers - >> Host: www.google.ro:80
[DEBUG] headers - >> Connection: Keep-Alive
[DEBUG] headers - >> User-Agent: Apache-HttpClient/4.1 (java 1.5)

And some added test cases in org.apache.http.protocol.TestStandardInterceptors that currently fail:

public void testRequestTargetHostPort443Generated() throws Exception {
        HttpContext context = new BasicHttpContext(null);
        HttpHost host = new HttpHost("somehost", 443, "https");
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
        BasicHttpRequest request = new BasicHttpRequest("GET", "/");
        RequestTargetHost interceptor = new RequestTargetHost();
        interceptor.process(request, context);
        Header header = request.getFirstHeader(HTTP.TARGET_HOST);
        assertNotNull(header);
        assertEquals("somehost", header.getValue());
    }
    
    public void testRequestTargetHostPort80Generated() throws Exception {
        HttpContext context = new BasicHttpContext(null);
        HttpHost host = new HttpHost("somehost", 80, "http");
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
        BasicHttpRequest request = new BasicHttpRequest("GET", "/");
        RequestTargetHost interceptor = new RequestTargetHost();
        interceptor.process(request, context);
        Header header = request.getFirstHeader(HTTP.TARGET_HOST);
        assertNotNull(header);
        assertEquals("somehost", header.getValue());
    }

Regards,
Alin


      

Re: Trimming port 80 in request Host header

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Sat, 2011-03-12 at 03:20 -0800, alin vasile wrote:
> Hi,
> 
>    I noticed that when the default port is specified explicitly in the requested URL address, http-client 4.1  will use it in the 'Host' header. The HTTP 1.1 specification doesn't deny this, but the same scenario can't be reproduced with any modern browser. Would be any issues if httpclient will have the same behaviour?
> 
> Sample code:
> 
> DefaultHttpClient httpclient = new DefaultHttpClient();
> HttpGet httpget = new HttpGet("http://www.google.ro:80/");   
> HttpResponse response = httpclient.execute(httpget);
> 
> Log:
> [DEBUG] DefaultClientConnection - Sending request: GET / HTTP/1.1
> [DEBUG] headers - >> GET / HTTP/1.1
> [DEBUG] headers - >> Host: www.google.ro:80
> [DEBUG] headers - >> Connection: Keep-Alive
> [DEBUG] headers - >> User-Agent: Apache-HttpClient/4.1 (java 1.5)
> 
> And some added test cases in org.apache.http.protocol.TestStandardInterceptors that currently fail:
> 
> public void testRequestTargetHostPort443Generated() throws Exception {
>         HttpContext context = new BasicHttpContext(null);
>         HttpHost host = new HttpHost("somehost", 443, "https");
>         context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
>         BasicHttpRequest request = new BasicHttpRequest("GET", "/");
>         RequestTargetHost interceptor = new RequestTargetHost();
>         interceptor.process(request, context);
>         Header header = request.getFirstHeader(HTTP.TARGET_HOST);
>         assertNotNull(header);
>         assertEquals("somehost", header.getValue());
>     }
>     
>     public void testRequestTargetHostPort80Generated() throws Exception {
>         HttpContext context = new BasicHttpContext(null);
>         HttpHost host = new HttpHost("somehost", 80, "http");
>         context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
>         BasicHttpRequest request = new BasicHttpRequest("GET", "/");
>         RequestTargetHost interceptor = new RequestTargetHost();
>         interceptor.process(request, context);
>         Header header = request.getFirstHeader(HTTP.TARGET_HOST);
>         assertNotNull(header);
>         assertEquals("somehost", header.getValue());
>     }
> 
> Regards,
> Alin
> 
> 

Alin

I do not think that a core protocol interceptor is the right place for
such a tweak. The problem can be dealt with in a much better way by
overriding the AbstractHttpClient#execute(HttpUriRequest) method [1] and
stripping away default ports for known schemes when building HttpHost
instance from the request URI. 

Hope this helps

Oleg

[1]
http://hc.apache.org/httpcomponents-client-ga/httpclient/xref/org/apache/http/impl/client/AbstractHttpClient.html#745 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org