You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Johannes Koch <jo...@fit.fraunhofer.de> on 2008/02/28 11:08:59 UTC

URI.resolve() not woring properly?

Hi,

recently I discovered that java.net.URI.resolve(String str) does not 
work properly according to RFC 2396 (or 3986) when resolving an empty 
URI reference ("").

The result from resolving "" against the base URI "http://a/b/c/d;p?q" 
should be the same URI ("http://a/b/c/d;p?q"), but is "http://a/b/c/".



RFC 2396 section 5.2:
    2) If the path component is empty and the scheme, authority, and
       query components are undefined, then it is a reference to the
       current document and we are done.

and Appendix C:
Within an object with a well-defined base URI of

       http://a/b/c/d;p?q

    the relative URI would be resolved as follows:
with C.2
[...]
    An empty reference refers to the start of the current document.

       <>            =  (current document)



RFC 3986 section 5.2.2:
             if (R.path == "") then
                T.path = Base.path;
                if defined(R.query) then
                   T.query = R.query;
                else
                   T.query = Base.query;
                endif;

and section 5.4:
Within a representation with a well defined base URI of

       http://a/b/c/d;p?q

    a relative reference is transformed to its target URI as follows.
with section 5.4.1
[...]

       ""              =  "http://a/b/c/d;p?q"



new URL(URL context, String spec) works better in this case, but it 
keeps the context URL's fragment identifier if defined, which as I read 
the RFCs is not correct.

Does this have any relevance in HttpComponents?
-- 
Johannes Koch
BIKA Web Compliance Center - Fraunhofer FIT
Schloss Birlinghoven, D-53757 Sankt Augustin, Germany
Phone: +49-2241-142628    Fax: +49-2241-142065

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


Re: URI.resolve() not woring properly?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2008-02-28 at 13:11 +0100, Johannes Koch wrote:
> Hi Oleg
> 
> Oleg Kalnichevski schrieb:
> > Is java.net.URI.resolve(String str) being used anywhere in HttpClient?
> 
> Yep, o.a.h..impl.client.DefaultRedirectHandler method getLocationURI.
> 
> > If so, what do you suggest we do?
> 
> Create a resolve(URI, String) method in o.a.h.client.utils.URLUtils? See 
> attached patch.
> 

Johannes,

Could you please open a JIRA ticket and attach this patch to it? You
should also select 'Grant license to ASF for inclusion in ASF works'
checkbox, if it is okay with to have this code licensed as ASLv2.    

Oleg 


> <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535> suggests:
> 
> Specially check for this case in invoking code,
> replace "" by "#" and then remove "#" manually later.
> (Review ID: 148122)
> 
> plain text document attachment (URIresolve_patch_20080228.txt)
> Index: C:/Dokumente und Einstellungen/Koch/Eigene Dateien/koch/opt/eclipse/workspace_p/httpclient_trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java
> ===================================================================
> --- C:/Dokumente und Einstellungen/Koch/Eigene Dateien/koch/opt/eclipse/workspace_p/httpclient_trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java	(revision 630775)
> +++ C:/Dokumente und Einstellungen/Koch/Eigene Dateien/koch/opt/eclipse/workspace_p/httpclient_trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultRedirectHandler.java	(working copy)
> @@ -138,7 +138,7 @@
>              try {
>                  URI requestURI = new URI(request.getRequestLine().getUri());
>                  URI absoluteRequestURI = URLUtils.rewriteURI(requestURI, target, true);
> -                uri = absoluteRequestURI.resolve(uri); 
> +                uri = URLUtils.resolve(absoluteRequestURI, uri); 
>              } catch (URISyntaxException ex) {
>                  throw new ProtocolException(ex.getMessage(), ex);
>              }
> Index: C:/Dokumente und Einstellungen/Koch/Eigene Dateien/koch/opt/eclipse/workspace_p/httpclient_trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java
> ===================================================================
> --- C:/Dokumente und Einstellungen/Koch/Eigene Dateien/koch/opt/eclipse/workspace_p/httpclient_trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java	(revision 630775)
> +++ C:/Dokumente und Einstellungen/Koch/Eigene Dateien/koch/opt/eclipse/workspace_p/httpclient_trunk/module-client/src/main/java/org/apache/http/client/utils/URLUtils.java	(working copy)
> @@ -195,6 +195,45 @@
>      }
>      
>      /**
> +     * Resolves a URI reference aginast a base URI. Work-around for bug in
> +     * java.net.URI (<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535>)
> +     *  
> +     * @param baseURI the base URI
> +     * @param reference the URI reference
> +     * @return the resulting URI
> +     */
> +    public static URI resolve(URI baseURI, String reference)
> +    {
> +        return URLUtils.resolve(baseURI, URI.create(reference));
> +    }
> +
> +    /**
> +     * Resolves a URI reference aginast a base URI. Work-around for bug in
> +     * java.net.URI (<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535>)
> +     *  
> +     * @param baseURI the base URI
> +     * @param reference the URI reference
> +     * @return the resulting URI
> +     */
> +    public static URI resolve(URI baseURI, URI reference)
> +    {
> +        boolean emptyReference = "".equals(reference.toString());
> +        if (emptyReference)
> +        {
> +            reference = URI.create("#");
> +        }
> +        URI resolved = baseURI.resolve(reference);
> +        if (emptyReference)
> +        {
> +            String resolvedString = resolved.toString();
> +            resolved = URI.create(resolvedString.substring(0,
> +                resolvedString.indexOf('#')));
> +        }
> +        return resolved;
> +    }
> +
> +    
> +    /**
>       * This class should not be instantiated.
>       */
>      private URLUtils() {
> 
> ---------------------------------------------------------------------
> 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: URI.resolve() not woring properly?

Posted by Johannes Koch <jo...@fit.fraunhofer.de>.
Hi Oleg

Oleg Kalnichevski schrieb:
> Is java.net.URI.resolve(String str) being used anywhere in HttpClient?

Yep, o.a.h..impl.client.DefaultRedirectHandler method getLocationURI.

> If so, what do you suggest we do?

Create a resolve(URI, String) method in o.a.h.client.utils.URLUtils? See 
attached patch.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535> suggests:

Specially check for this case in invoking code,
replace "" by "#" and then remove "#" manually later.
(Review ID: 148122)

-- 
Johannes Koch
BIKA Web Compliance Center - Fraunhofer FIT
Schloss Birlinghoven, D-53757 Sankt Augustin, Germany
Phone: +49-2241-142628    Fax: +49-2241-142065

Re: URI.resolve() not woring properly?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2008-02-28 at 11:08 +0100, Johannes Koch wrote:
> Hi,
> 
> recently I discovered that java.net.URI.resolve(String str) does not 
> work properly according to RFC 2396 (or 3986) when resolving an empty 
> URI reference ("").
> 
> The result from resolving "" against the base URI "http://a/b/c/d;p?q" 
> should be the same URI ("http://a/b/c/d;p?q"), but is "http://a/b/c/".
> 
> 
> 
> RFC 2396 section 5.2:
>     2) If the path component is empty and the scheme, authority, and
>        query components are undefined, then it is a reference to the
>        current document and we are done.
> 
> and Appendix C:
> Within an object with a well-defined base URI of
> 
>        http://a/b/c/d;p?q
> 
>     the relative URI would be resolved as follows:
> with C.2
> [...]
>     An empty reference refers to the start of the current document.
> 
>        <>            =  (current document)
> 
> 
> 
> RFC 3986 section 5.2.2:
>              if (R.path == "") then
>                 T.path = Base.path;
>                 if defined(R.query) then
>                    T.query = R.query;
>                 else
>                    T.query = Base.query;
>                 endif;
> 
> and section 5.4:
> Within a representation with a well defined base URI of
> 
>        http://a/b/c/d;p?q
> 
>     a relative reference is transformed to its target URI as follows.
> with section 5.4.1
> [...]
> 
>        ""              =  "http://a/b/c/d;p?q"
> 
> 
> 
> new URL(URL context, String spec) works better in this case, but it 
> keeps the context URL's fragment identifier if defined, which as I read 
> the RFCs is not correct.
> 
> Does this have any relevance in HttpComponents?

Johannes,

Is java.net.URI.resolve(String str) being used anywhere in HttpClient?
If so, what do you suggest we do?

Oleg


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