You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by sebb <se...@gmail.com> on 2010/12/06 21:39:47 UTC

HttpUriRequest.getURI() returns relative URI?

I'm following the example in the HttpClient documentation:

            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpget = new HttpGet("http://www.google.com/");
            HttpResponse response = httpclient.execute(httpget, localContext);
            HttpHost target = (HttpHost) localContext.getAttribute(
                    ExecutionContext.HTTP_TARGET_HOST);
            HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(
                    ExecutionContext.HTTP_REQUEST);
            System.out.println("Target host: " + target);
            System.out.println("Final request URI: " + req.getURI());

I see the following output:

Target host: http://www.google.co.uk
Final request URI: /

However, I would expect to see an absolute URI, as per the Javadoc for
HttpUriRequest#getURI().

Is this a bug in the Javadoc (and user guide?) or a bug in the code?

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


Re: HttpUriRequest.getURI() returns relative URI?

Posted by sebb <se...@gmail.com>.
On 7 December 2010 14:58, Oleg Kalnichevski <ol...@apache.org> wrote:
> On Tue, 2010-12-07 at 12:48 +0000, sebb wrote:
>> On 7 December 2010 09:35, Oleg Kalnichevski <ol...@apache.org> wrote:
>
> ...
>
>> > Hi Sebastian
>> >
>> > I could find any passage in javadocs or tutorial stating such
>> > requirement.
>>
>> Javadoc for getURI() starts:
>>
>>     /**
>>      * Returns the URI this request uses, such as
>>      * <code>http://example.org/path/to/file</code>.
>>      *
>>
>> which strongly suggests that the URI is absolute.
>>
>
> Honestly, I fail to see a strong suggestion here. The URI used as an
> example just happen to be absolute. That is it.

Well, "/path/to/file" makes no sense on its own as a request URI.
However the HttpUriRequest interface does not have any methods for
returning the host or scheme.

>> Also, section 1.1.1 has 2 examples which display absolute URIs.
>>
>
> Again, to me that does not have any implications other than two examples
> happen to use absolute URIs.
>
>> I've not found an example which shows a relative URI.
>>
>> > As far as I remember request URIs can be either absolute or
>> > relative.
>>
>> AFAICT, this is not documented.
>>
>
> http://download.oracle.com/javase/1.4.2/docs/api/java/net/URI.html

Sorry, I meant URIs returned by getURI() are not documented as being
relative or absolute.

>
>> Seems to me that user code is often going to need the absolute URI of
>> the request, so there should be a method to return the absolute URI.
>>
>> It's confusing that the URI returned from the execution context
>> appears to be always relative, even if the original request uses an
>> absolute URI.
>>
>
> The final HttpRequest object in the execution context always represents
> the state of the message _exactly_ as it was sent to the target server.
> Per default HTTP/1.0 and HTTP/1.1 use relative request URIs.

OK, this is worth documenting.

> If you feel that the javadocs need to be more specific / clear /
> unambiguous, by all of means just go ahead and improve them.

Will do.

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

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


Re: HttpUriRequest.getURI() returns relative URI?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2010-12-07 at 12:48 +0000, sebb wrote:
> On 7 December 2010 09:35, Oleg Kalnichevski <ol...@apache.org> wrote:

...

> > Hi Sebastian
> >
> > I could find any passage in javadocs or tutorial stating such
> > requirement.
> 
> Javadoc for getURI() starts:
> 
>     /**
>      * Returns the URI this request uses, such as
>      * <code>http://example.org/path/to/file</code>.
>      *
> 
> which strongly suggests that the URI is absolute.
> 

Honestly, I fail to see a strong suggestion here. The URI used as an
example just happen to be absolute. That is it.

> Also, section 1.1.1 has 2 examples which display absolute URIs.
> 

Again, to me that does not have any implications other than two examples
happen to use absolute URIs.

> I've not found an example which shows a relative URI.
> 
> > As far as I remember request URIs can be either absolute or
> > relative.
> 
> AFAICT, this is not documented.
> 

http://download.oracle.com/javase/1.4.2/docs/api/java/net/URI.html


> Seems to me that user code is often going to need the absolute URI of
> the request, so there should be a method to return the absolute URI.
> 
> It's confusing that the URI returned from the execution context
> appears to be always relative, even if the original request uses an
> absolute URI.
> 

The final HttpRequest object in the execution context always represents
the state of the message _exactly_ as it was sent to the target server.
Per default HTTP/1.0 and HTTP/1.1 use relative request URIs. 

If you feel that the javadocs need to be more specific / clear /
unambiguous, by all of means just go ahead and improve them.

Cheers

Oleg  


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


Re: HttpUriRequest.getURI() returns relative URI?

Posted by sebb <se...@gmail.com>.
On 7 December 2010 12:48, sebb <se...@gmail.com> wrote:
> On 7 December 2010 09:35, Oleg Kalnichevski <ol...@apache.org> wrote:
>> On Mon, 2010-12-06 at 20:39 +0000, sebb wrote:
>>> I'm following the example in the HttpClient documentation:
>>>
>>>             DefaultHttpClient httpclient = new DefaultHttpClient();
>>>             HttpContext localContext = new BasicHttpContext();
>>>             HttpGet httpget = new HttpGet("http://www.google.com/");
>>>             HttpResponse response = httpclient.execute(httpget, localContext);
>>>             HttpHost target = (HttpHost) localContext.getAttribute(
>>>                     ExecutionContext.HTTP_TARGET_HOST);
>>>             HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(
>>>                     ExecutionContext.HTTP_REQUEST);
>>>             System.out.println("Target host: " + target);
>>>             System.out.println("Final request URI: " + req.getURI());
>>>
>>> I see the following output:
>>>
>>> Target host: http://www.google.co.uk
>>> Final request URI: /
>>>
>>> However, I would expect to see an absolute URI, as per the Javadoc for
>>> HttpUriRequest#getURI().
>>>
>>> Is this a bug in the Javadoc (and user guide?) or a bug in the code?
>>>
>>
>> Hi Sebastian
>>
>> I could find any passage in javadocs or tutorial stating such
>> requirement.
>
> Javadoc for getURI() starts:
>
>    /**
>     * Returns the URI this request uses, such as
>     * <code>http://example.org/path/to/file</code>.
>     *
>
> which strongly suggests that the URI is absolute.
>
> Also, section 1.1.1 has 2 examples which display absolute URIs.
>
> I've not found an example which shows a relative URI.
>
>> As far as I remember request URIs can be either absolute or
>> relative.
>
> AFAICT, this is not documented.
>
>> In case of a relative request URI HttpClient attempts to
>> determine the target host based on request parameters. In case of a
>> absolute request URI HttpClient rewrites the request URI as relative and
>> adds a 'Host' header containing the target host.
>
> Seems to me that user code is often going to need the absolute URI of
> the request, so there should be a method to return the absolute URI.
>
> It's confusing that the URI returned from the execution context
> appears to be always relative, even if the original request uses an
> absolute URI.
>
> Combining the first example in section 1.1.1 with the execution
> context example in section 5.4 produces the following output:
>
> http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
> Target host: http://www.google.com
> Final request URI: /search?q=httpclient&btnG=Google+Search&aq=f&oq=

Just to be clear - I don't have a problem with the current implementation.

However, I do think the documentation needs to be clarified, which I'm
happy to do.

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

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


Re: HttpUriRequest.getURI() returns relative URI?

Posted by sebb <se...@gmail.com>.
On 7 December 2010 09:35, Oleg Kalnichevski <ol...@apache.org> wrote:
> On Mon, 2010-12-06 at 20:39 +0000, sebb wrote:
>> I'm following the example in the HttpClient documentation:
>>
>>             DefaultHttpClient httpclient = new DefaultHttpClient();
>>             HttpContext localContext = new BasicHttpContext();
>>             HttpGet httpget = new HttpGet("http://www.google.com/");
>>             HttpResponse response = httpclient.execute(httpget, localContext);
>>             HttpHost target = (HttpHost) localContext.getAttribute(
>>                     ExecutionContext.HTTP_TARGET_HOST);
>>             HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(
>>                     ExecutionContext.HTTP_REQUEST);
>>             System.out.println("Target host: " + target);
>>             System.out.println("Final request URI: " + req.getURI());
>>
>> I see the following output:
>>
>> Target host: http://www.google.co.uk
>> Final request URI: /
>>
>> However, I would expect to see an absolute URI, as per the Javadoc for
>> HttpUriRequest#getURI().
>>
>> Is this a bug in the Javadoc (and user guide?) or a bug in the code?
>>
>
> Hi Sebastian
>
> I could find any passage in javadocs or tutorial stating such
> requirement.

Javadoc for getURI() starts:

    /**
     * Returns the URI this request uses, such as
     * <code>http://example.org/path/to/file</code>.
     *

which strongly suggests that the URI is absolute.

Also, section 1.1.1 has 2 examples which display absolute URIs.

I've not found an example which shows a relative URI.

> As far as I remember request URIs can be either absolute or
> relative.

AFAICT, this is not documented.

> In case of a relative request URI HttpClient attempts to
> determine the target host based on request parameters. In case of a
> absolute request URI HttpClient rewrites the request URI as relative and
> adds a 'Host' header containing the target host.

Seems to me that user code is often going to need the absolute URI of
the request, so there should be a method to return the absolute URI.

It's confusing that the URI returned from the execution context
appears to be always relative, even if the original request uses an
absolute URI.

Combining the first example in section 1.1.1 with the execution
context example in section 5.4 produces the following output:

http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
Target host: http://www.google.com
Final request URI: /search?q=httpclient&btnG=Google+Search&aq=f&oq=


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

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


Re: HttpUriRequest.getURI() returns relative URI?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2010-12-06 at 20:39 +0000, sebb wrote:
> I'm following the example in the HttpClient documentation:
> 
>             DefaultHttpClient httpclient = new DefaultHttpClient();
>             HttpContext localContext = new BasicHttpContext();
>             HttpGet httpget = new HttpGet("http://www.google.com/");
>             HttpResponse response = httpclient.execute(httpget, localContext);
>             HttpHost target = (HttpHost) localContext.getAttribute(
>                     ExecutionContext.HTTP_TARGET_HOST);
>             HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(
>                     ExecutionContext.HTTP_REQUEST);
>             System.out.println("Target host: " + target);
>             System.out.println("Final request URI: " + req.getURI());
> 
> I see the following output:
> 
> Target host: http://www.google.co.uk
> Final request URI: /
> 
> However, I would expect to see an absolute URI, as per the Javadoc for
> HttpUriRequest#getURI().
> 
> Is this a bug in the Javadoc (and user guide?) or a bug in the code?
> 

Hi Sebastian

I could find any passage in javadocs or tutorial stating such
requirement. As far as I remember request URIs can be either absolute or
relative. In case of a relative request URI HttpClient attempts to
determine the target host based on request parameters. In case of a
absolute request URI HttpClient rewrites the request URI as relative and
adds a 'Host' header containing the target host.

Oleg  



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