JDK-4328499 : URL.toString() leaves out slash between host and file
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.2.2
  • Priority: P2
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2000-04-06
  • Updated: 2010-07-05
  • Resolved: 2010-07-05
Related Reports
Relates :  
Description

Name: skT45625			Date: 04/06/2000


java version "1.2.2-RC2"
Classic VM (build 1.2.2-RC2-K, green threads, javacomp)

This bug is related to previous bug report #4269444

Test code:
------------------------
import java.net.*;

class Test {
    public static final void main(String[] args)
	throws MalformedURLException
    {
	URL url = new URL("file", "localhost", "foobar");
	System.out.println(url);
    }
}
------------------------
Actual Output:
file://localhostfoobar

Expected Output:
file://localhost/foobar

Details:
The toString() and toExternalForm() methods on java.net.URL and
java.net.URLStreamHandler leave out the slash following the hostname in the
string form.  All are based on this implementation in URLStreamHandler.java:
-----------------------------
    /**
     * Converts a <code>URL</code> of a specific protocol to a
     * <code>String</code>.
     *
     * @param   u   the URL.
     * @return  a string representation of the <code>URL</code> argument.
     */
    protected String toExternalForm(URL u) {
	String result = u.getProtocol() + ":";
	if ((u.getHost() != null) && (u.getHost().length() > 0)) {
	    result = result + "//" + u.getHost();
	    if (u.getPort() != -1) {
		result += ":" + u.getPort();
	    }
	}
	result += u.getFile();
	if (u.getRef() != null) {
	    result += "#" + u.getRef();
	}
	return result;
    }
----------------------------
It seems that this line:
result += u.getFile();

should be changed to include the slash:
result += "/" + u.getFile();

HOWEVER, a previous bug (#4269444) mentioned that the slash should be left out
when there's no file in the URL.  So the correct code should test this case:

if ((u.getFile() != null) && (u.getFile() != ""))
  result += "/" + u.getFile();

[I'm not sure that the null case is necessary]
[Note the conditional changed from the original report]
(Review ID: 103388) 
======================================================================

Comments
EVALUATION This problem is easily worked around, and fixing it might break code that currently relies on such technically invalid URLs. So, am closing as will not fix for the moment
05-07-2010

WORK AROUND Use java.net.URI instead. It correctly rejects attempts to construct an absolute URI with a relative path.
05-11-2008

WORK AROUND Name: skT45625 Date: 04/06/2000 A simple work around is to prepend a slash to the filename: URL url = new URL("file", "localhost", "/foobar"); System.out.println(url); Output: file://localhost/foobar Note that this doesn't help when one uses methods like URLClassLoader.getResource(String): here, you're stuck with the bad URL. ======================================================================
11-06-2004

EVALUATION The real here is that the piece-wise component constructors don't do any real validation. This will be re-examined for tiger. alan.bateman@ireland 2001-02-12
12-02-2001