Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4666701

URI.resolve wrong when base path empty and given URI relative

XMLWordPrintable

    • generic, x86
    • generic, windows_98



      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)
      ======================================================================

            dfuchs Daniel Fuchs
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: