Name: nt126004 Date: 04/11/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
Windows 98 [Version 4.10.2222]
A DESCRIPTION OF THE PROBLEM :
URI.resolve(URI) produces incorrect results when the
base URI has an empty path and the given URI is relative.
EXPECTED VERSUS ACTUAL BEHAVIOR :
For example, given
URI uri = URI.create("http://java.sun.com")
then uri.resolve("relative/path.html") produces
"http://java.sun.comrelative/path.html".
That is, a path is concatenated to the authority,
which can't be right.
Obviously, the desired result is
"http://java.sun.com/relative/path.html".
This desired result is produced if "relative/path.html"
is resolved against "http://java.sun.com/" (note trailing
slash).
Moveover, URI.create("http://java.sun.com").getPath()
returns a 0-length String, whereas what we want is
the root path, "/".
This happens extremely frequently, as most people don't
even realize that a URI like "http://java.sun.com"
should more correctly be "http://java.sun.com".
Thus, the URI class should be modified to canonicalize
*hierarchical* URIs with empty paths to instead have
the path "/".
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.net.*;
public class URIBug {
public void test() {
// show concatenated authority and path
URI baseuri = URI.create("http://java.sun.com");
System.out.println("base URI path = "+baseuri.getPath());
System.out.println("relative base URI =
"+baseuri.resolve("relative/path.html"));
// with trailing slash, relative path resolved correctly
URI baseuri2 = URI.create("http://java.sun.com/");
System.out.println("relative base URI 2 =
"+baseuri2.resolve("relative/path.html"));
}
public static void main(String[] args) {
URIBug u = new URIBug();
u.test();
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
After create URI, if
!uri.isOpaque() and "".equals(uri.getPath()),
then override it with
uri.resolve("/")
(Review ID: 145090)
======================================================================
- duplicates
-
JDK-8272702 Resolving URI relative path with no / may lead to incorrect toString
- Closed