From: <bra...@us...> - 2011-05-25 01:08:44
|
Revision: 3442 http://archive-access.svn.sourceforge.net/archive-access/?rev=3442&view=rev Author: bradtofel Date: 2011-05-25 01:08:38 +0000 (Wed, 25 May 2011) Log Message: ----------- BUGFIX: urlToPath was not handling ports correctly Modified Paths: -------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/url/UrlOperations.java trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/url/UrlOperationsTest.java Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/url/UrlOperations.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/url/UrlOperations.java 2011-05-25 01:04:11 UTC (rev 3441) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/url/UrlOperations.java 2011-05-25 01:08:38 UTC (rev 3442) @@ -220,25 +220,28 @@ * @return the path component of the URL, or "" if it contains no path. */ public static String getURLPath(String url) { - int portIdx = url.indexOf(UrlOperations.PORT_SEPARATOR); + url = stripURLScheme(url); int pathIdx = url.indexOf(UrlOperations.PATH_START); - if(portIdx == -1 && pathIdx == -1) { - return ""; - } - if(portIdx == -1) { - return url.substring(pathIdx); - } if(pathIdx == -1) { - return url.substring(portIdx); + return "/"; } - if(pathIdx > portIdx) { - return url.substring(portIdx); - } else { - return url.substring(pathIdx); - } + return url.substring(pathIdx); } - /** + * Attempt to extract the path component of a url String argument. + * @param url the URL which may contain a path, sans scheme. + * @return the path component of the URL, or "" if it contains no path. + */ + public static String stripURLScheme(String url) { + String lcUrl = url.toLowerCase(); + for(String scheme : ALL_SCHEMES) { + if(lcUrl.startsWith(scheme)) { + return url.substring(scheme.length()); + } + } + return url; + } + /** * Attempt to strip default ports out of URL strings. * @param url the original URL possibly including a port * @return the URL sans port, if the scheme was recognized and the default @@ -279,6 +282,11 @@ return sb.toString(); } + /** + * @param orig String containing a URL, possibly beginning with "http:/". + * @return original string if orig begins with "http://", or a new String + * with the extra slash, if orig only had one slash. + */ public static String fixupHTTPUrlWithOneSlash(String orig) { if(orig.startsWith("http:/") && ! orig.startsWith(HTTP_SCHEME)) { // very likely the IE "you must have meant 1 slash, not 2 bug: Modified: trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/url/UrlOperationsTest.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/url/UrlOperationsTest.java 2011-05-25 01:04:11 UTC (rev 3441) +++ trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/url/UrlOperationsTest.java 2011-05-25 01:08:38 UTC (rev 3442) @@ -205,7 +205,29 @@ } + public void testUrlPath() { + assertEquals("/",UrlOperations.getURLPath("http://foo.com")); + assertEquals("/",UrlOperations.getURLPath("http://foo.com/")); + assertEquals("/",UrlOperations.getURLPath("http://foo.com:80/")); + assertEquals("/blue",UrlOperations.getURLPath("http://foo.com:80/blue")); + assertEquals("/blue/red",UrlOperations.getURLPath("http://foo.com:80/blue/red")); + assertEquals("/blue/red:colon",UrlOperations.getURLPath("http://foo.com:80/blue/red:colon")); + assertEquals("/",UrlOperations.getURLPath("foo.com")); + assertEquals("/",UrlOperations.getURLPath("foo.com:80")); + assertEquals("/",UrlOperations.getURLPath("foo.com:8080")); + assertEquals("/",UrlOperations.getURLPath("foo.com/")); + assertEquals("/",UrlOperations.getURLPath("foo.com:80/")); + assertEquals("/",UrlOperations.getURLPath("foo.com:8080/")); + assertEquals("/bar",UrlOperations.getURLPath("foo.com/bar")); + assertEquals("/bar",UrlOperations.getURLPath("foo.com:80/bar")); + assertEquals("/bar",UrlOperations.getURLPath("foo.com:8080/bar")); + + assertEquals("/bar/baz",UrlOperations.getURLPath("foo.com/bar/baz")); + assertEquals("/bar/baz",UrlOperations.getURLPath("foo.com:80/bar/baz")); + assertEquals("/bar/baz",UrlOperations.getURLPath("foo.com:8080/bar/baz")); + + } public void testStripDefaultPort() { assertSDP("http://foo.com/","http://foo.com/"); assertSDP("http://foo.com","http://foo.com"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |