From d38ef0493d1666c58ae647e68ef69bc030dfa161 Mon Sep 17 00:00:00 2001 From: theli Date: Tue, 24 Oct 2006 05:22:54 +0000 Subject: [PATCH] *) be more tolerant against missing ports in url "http://yacy.net:/" is now interpreted as "http://yacy.net/" See: http://www.yacy-forum.de/viewtopic.php?p=27102 git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2852 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/net/URL.java | 10 ++++++---- test/de/anomic/net/URLTest.java | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/source/de/anomic/net/URL.java b/source/de/anomic/net/URL.java index 019eae042..f277c5ec7 100644 --- a/source/de/anomic/net/URL.java +++ b/source/de/anomic/net/URL.java @@ -371,13 +371,15 @@ public class URL { private void identPort(String inputURL) throws MalformedURLException { // identify ref in file - int r = host.indexOf(':'); + int r = this.host.indexOf(':'); if (r < 0) { this.port = -1; - } else { + } else { try { - this.port = Integer.parseInt(host.substring(r + 1)); - this.host = host.substring(0, r); + String portStr = this.host.substring(r + 1); + if (portStr.trim().length() > 0) this.port = Integer.parseInt(portStr); + else this.port = -1; + this.host = this.host.substring(0, r); } catch (NumberFormatException e) { throw new MalformedURLException("wrong port in host fragment '" + this.host + "' of input url '" + inputURL + "'"); } diff --git a/test/de/anomic/net/URLTest.java b/test/de/anomic/net/URLTest.java index 518d201b5..a6a349a0d 100644 --- a/test/de/anomic/net/URLTest.java +++ b/test/de/anomic/net/URLTest.java @@ -37,4 +37,27 @@ public class URLTest extends TestCase { } } + public void testIdentPort() throws MalformedURLException { + String[][] testStrings = new String[][] { + new String[]{"http://www.yacy.net:","http://www.yacy.net/"}, + new String[]{"http://www.yacy.net:-1","http://www.yacy.net/"}, + new String[]{"http://www.yacy.net:/","http://www.yacy.net/"}, + new String[]{"http://www.yacy.net: /","http://www.yacy.net/"} + }; + + for (int i=0; i < testStrings.length; i++) { + // desired conversion result + System.out.print("testIdentPort: " + testStrings[i][0]); + String shouldBe = testStrings[i][1]; + + // conversion result + String resolvedURL = (new URL(testStrings[i][0])).toString(); + + // test if equal + assertEquals(shouldBe,resolvedURL); + System.out.println(" -> " + resolvedURL); + + } + } + }