From: <rb...@us...> - 2012-11-28 20:56:44
|
Revision: 7801 http://sourceforge.net/p/htmlunit/code/7801 Author: rbri Date: 2012-11-28 20:56:40 +0000 (Wed, 28 Nov 2012) Log Message: ----------- Fix URL manipulation (UrlUtils) for file url's containing drive letter. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Location.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAnchorElement.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/UrlUtils.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/util/UrlUtilsTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2012-11-28 18:31:51 UTC (rev 7800) +++ trunk/htmlunit/src/changes/changes.xml 2012-11-28 20:56:40 UTC (rev 7801) @@ -8,6 +8,9 @@ <body> <release version="2.12" date="???" description="Bugfixes"> + <action type="fix" dev="rbri"> + Fix URL manipulation (UrlUtils) for file url's containing drive letter. + </action> <action type="fix" dev="asashour"> JavaScript: fix handling relations of document type comment with <html> element (IE). </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Location.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Location.java 2012-11-28 18:31:51 UTC (rev 7800) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Location.java 2012-11-28 20:56:40 UTC (rev 7801) @@ -390,9 +390,8 @@ hostname = host; port = -1; } - final URL url1 = UrlUtils.getUrlWithNewHost(getUrl(), hostname); - final URL url2 = UrlUtils.getUrlWithNewPort(url1, port); - setUrl(url2); + final URL url = UrlUtils.getUrlWithNewHostAndPort(getUrl(), hostname, port); + setUrl(url); } /** Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAnchorElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAnchorElement.java 2012-11-28 18:31:51 UTC (rev 7800) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLAnchorElement.java 2012-11-28 20:56:40 UTC (rev 7801) @@ -44,6 +44,7 @@ * @author Ahmed Ashour * @author Sudhan Moghe * @author Daniel Gredler + * @author Ronald Brill */ @JsxClass(domClasses = HtmlAnchor.class) public class HTMLAnchorElement extends HTMLElement { @@ -277,9 +278,8 @@ hostname = host; port = -1; } - final URL url1 = UrlUtils.getUrlWithNewHost(getUrl(), hostname); - final URL url2 = UrlUtils.getUrlWithNewPort(url1, port); - setUrl(url2); + final URL url = UrlUtils.getUrlWithNewHostAndPort(getUrl(), hostname, port); + setUrl(url); } /** Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java 2012-11-28 18:31:51 UTC (rev 7800) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLDocument.java 2012-11-28 20:56:40 UTC (rev 7801) @@ -800,7 +800,7 @@ final boolean file = "file".equals(protocol); if (file) { try { - url = UrlUtils.getUrlWithNewPort(UrlUtils.getUrlWithNewHost(url, "LOCAL_FILESYSTEM"), 0); + url = UrlUtils.getUrlWithNewHostAndPort(url, "LOCAL_FILESYSTEM", 0); } catch (final MalformedURLException e) { throw new RuntimeException(e); Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/UrlUtils.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/UrlUtils.java 2012-11-28 18:31:51 UTC (rev 7800) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/util/UrlUtils.java 2012-11-28 20:56:40 UTC (rev 7801) @@ -34,6 +34,7 @@ * @author Sudhan Moghe * @author Marc Guillemot * @author Ahmed Ashour + * @author Ronald Brill */ public final class UrlUtils { private static final BitSet PATH_ALLOWED_CHARS = new BitSet(256); @@ -298,7 +299,7 @@ * @throws MalformedURLException if there is a problem creating the new URL */ public static URL getUrlWithNewProtocol(final URL u, final String newProtocol) throws MalformedURLException { - return createNewUrl(newProtocol, u.getHost(), u.getPort(), u.getPath(), u.getRef(), u.getQuery()); + return createNewUrl(newProtocol, u.getAuthority(), u.getPath(), u.getRef(), u.getQuery()); } /** @@ -308,11 +309,25 @@ * @return a new URL identical to the specified URL, except using the specified host * @throws MalformedURLException if there is a problem creating the new URL */ - public static URL getUrlWithNewHost(final URL u, final String newHost) throws MalformedURLException { + public static URL getUrlWithNewHost(final URL u, final String newHost) + throws MalformedURLException { return createNewUrl(u.getProtocol(), newHost, u.getPort(), u.getPath(), u.getRef(), u.getQuery()); } /** + * Creates and returns a new URL identical to the specified URL, except using the specified host. + * @param u the URL on which to base the returned URL + * @param newHost the new host to use in the returned URL + * @param newPort the new port to use in the returned URL + * @return a new URL identical to the specified URL, except using the specified host + * @throws MalformedURLException if there is a problem creating the new URL + */ + public static URL getUrlWithNewHostAndPort(final URL u, final String newHost, final int newPort) + throws MalformedURLException { + return createNewUrl(u.getProtocol(), newHost, newPort, u.getPath(), u.getRef(), u.getQuery()); + } + + /** * Creates and returns a new URL identical to the specified URL, except using the specified port. * @param u the URL on which to base the returned URL * @param newPort the new port to use in the returned URL @@ -331,7 +346,7 @@ * @throws MalformedURLException if there is a problem creating the new URL */ public static URL getUrlWithNewPath(final URL u, final String newPath) throws MalformedURLException { - return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(), newPath, u.getRef(), u.getQuery()); + return createNewUrl(u.getProtocol(), u.getAuthority(), newPath, u.getRef(), u.getQuery()); } /** @@ -342,7 +357,7 @@ * @throws MalformedURLException if there is a problem creating the new URL */ public static URL getUrlWithNewRef(final URL u, final String newRef) throws MalformedURLException { - return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(), u.getPath(), newRef, u.getQuery()); + return createNewUrl(u.getProtocol(), u.getAuthority(), u.getPath(), newRef, u.getQuery()); } /** @@ -353,7 +368,7 @@ * @throws MalformedURLException if there is a problem creating the new URL */ public static URL getUrlWithNewQuery(final URL u, final String newQuery) throws MalformedURLException { - return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(), u.getPath(), u.getRef(), newQuery); + return createNewUrl(u.getProtocol(), u.getAuthority(), u.getPath(), u.getRef(), newQuery); } /** @@ -391,11 +406,65 @@ } s.append(ref); } + final URL url = new URL(s.toString()); return url; } /** + * Creates a new URL based on the specified fragments. + * @param protocol the protocol to use (may not be <tt>null</tt>) + * @param authority the authority to use (may not be <tt>null</tt>) + * @param path the path to use (may be <tt>null</tt> and may omit the initial <tt>'/'</tt>) + * @param ref the reference to use (may be <tt>null</tt> and must not include the <tt>'#'</tt>) + * @param query the query to use (may be <tt>null</tt> and must not include the <tt>'?'</tt>) + * @return a new URL based on the specified fragments + * @throws MalformedURLException if there is a problem creating the new URL + */ + private static URL createNewUrl(final String protocol, final String authority, + final String path, final String ref, final String query) throws MalformedURLException { + + // pre-compute length of StringBuffer + int len = protocol.length() + 1; + if (authority != null && authority.length() > 0) { + len += 2 + authority.length(); + } + if (path != null) { + len += path.length(); + } + if (query != null) { + len += 1 + query.length(); + } + if (ref != null) { + len += 1 + ref.length(); + } + + final StringBuffer s = new StringBuffer(len); + s.append(protocol); + s.append(":"); + if (authority != null && authority.length() > 0) { + s.append("//"); + s.append(authority); + } + if (path != null) { + s.append(path); + } + if (query != null) { + s.append('?'); + s.append(query); + } + if (ref != null) { + if (!((ref.length() > 0) && ('#' == ref.charAt(0)))) { + s.append("#"); + } + s.append(ref); + } + + final URL url = new URL(s.toString()); + return url; + } + + /** * Resolves a given relative URL against a base URL. See * <a href="http://www.faqs.org/rfcs/rfc1808.html">RFC1808</a> * Section 4 for more details. Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/util/UrlUtilsTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/util/UrlUtilsTest.java 2012-11-28 18:31:51 UTC (rev 7800) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/util/UrlUtilsTest.java 2012-11-28 20:56:40 UTC (rev 7801) @@ -28,6 +28,7 @@ * @author Martin Tamme * @author Sudhan Moghe * @author Ahmed Ashour + * @author Ronald Brill */ public class UrlUtilsTest extends SimpleWebTestCase { @@ -55,10 +56,26 @@ * @throws Exception if the test fails */ @Test + public void getUrlWithNewHostAndPort() throws Exception { + final URL a = new URL("http://my.home.com/index.html?query#ref"); + URL b = UrlUtils.getUrlWithNewHostAndPort(a, "your.home.com", 4711); + assertEquals("http://your.home.com:4711/index.html?query#ref", b.toExternalForm()); + + b = UrlUtils.getUrlWithNewHostAndPort(a, "your.home.com", -1); + assertEquals("http://your.home.com/index.html?query#ref", b.toExternalForm()); + } + + /** + * @throws Exception if the test fails + */ + @Test public void getUrlWithNewPort() throws Exception { final URL a = new URL("http://my.home.com/index.html?query#ref"); - final URL b = UrlUtils.getUrlWithNewPort(a, 8080); + URL b = UrlUtils.getUrlWithNewPort(a, 8080); assertEquals("http://my.home.com:8080/index.html?query#ref", b.toExternalForm()); + + b = UrlUtils.getUrlWithNewPort(a, -1); + assertEquals("http://my.home.com/index.html?query#ref", b.toExternalForm()); } /** @@ -76,17 +93,30 @@ */ @Test public void getUrlWithNewRef() throws Exception { - final URL a = new URL("http://my.home.com/index.html?query#ref"); - final URL b = UrlUtils.getUrlWithNewRef(a, "abc"); + URL a = new URL("http://my.home.com/index.html?query#ref"); + URL b = UrlUtils.getUrlWithNewRef(a, "abc"); assertEquals("http://my.home.com/index.html?query#abc", b.toExternalForm()); - final URL c = new URL("http://my.home.com/#ref"); - final URL d = UrlUtils.getUrlWithNewRef(c, "xyz"); - assertEquals("http://my.home.com/#xyz", d.toExternalForm()); + a = new URL("http://my.home.com/#ref"); + b = UrlUtils.getUrlWithNewRef(a, "xyz"); + assertEquals("http://my.home.com/#xyz", b.toExternalForm()); - final URL e = new URL("http://my.home.com#ref"); - final URL f = UrlUtils.getUrlWithNewRef(e, "xyz"); - assertEquals("http://my.home.com#xyz", f.toExternalForm()); + a = new URL("http://my.home.com#ref"); + b = UrlUtils.getUrlWithNewRef(a, "xyz"); + assertEquals("http://my.home.com#xyz", b.toExternalForm()); + + a = new URL("http://my.home.com"); + b = UrlUtils.getUrlWithNewRef(a, "xyz"); + assertEquals("http://my.home.com#xyz", b.toExternalForm()); + + a = new URL("http://my.home.com"); + b = UrlUtils.getUrlWithNewRef(a, null); + assertEquals("http://my.home.com", b.toExternalForm()); + + a = new URL("http://my.home.com"); + b = UrlUtils.getUrlWithNewRef(a, ""); + assertEquals("http://my.home.com#", b.toExternalForm()); + } /** @@ -94,9 +124,18 @@ */ @Test public void getUrlWithNewQuery() throws Exception { - final URL a = new URL("http://my.home.com/index.html?query#ref"); - final URL b = UrlUtils.getUrlWithNewQuery(a, "xyz"); + URL a = new URL("http://my.home.com/index.html?query#ref"); + URL b = UrlUtils.getUrlWithNewQuery(a, "xyz"); assertEquals("http://my.home.com/index.html?xyz#ref", b.toExternalForm()); + + // DOS + a = new URL("file://c:/index.html?query"); + b = UrlUtils.getUrlWithNewQuery(a, "xyz"); + assertEquals("file://c:/index.html?xyz", b.toExternalForm()); + // UNIX + a = new URL("file:///index.html?query"); + b = UrlUtils.getUrlWithNewQuery(a, "xyz"); + assertEquals("file:/index.html?xyz", b.toExternalForm()); } /** |