diff --git a/source/net/yacy/cora/protocol/Domains.java b/source/net/yacy/cora/protocol/Domains.java index 04155c589..211c4f3e9 100644 --- a/source/net/yacy/cora/protocol/Domains.java +++ b/source/net/yacy/cora/protocol/Domains.java @@ -817,24 +817,19 @@ public class Domains { // IPv4 / host heuristics p = target.lastIndexOf(':'); if ( p < 0 ) { - // may be IPv4 or IPv6, we chop off brackets if exist - if (target.charAt(0) == '[') target = target.substring(1); - if (target.charAt(target.length() - 1) == ']') target = target.substring(0, target.length() - 1); p = target.lastIndexOf('%'); if (p > 0) target = target.substring(0, p); return target; } // the ':' at pos p may be either a port divider or a part of an IPv6 address - if (target.charAt(p - 1) == ']') { - target = target.substring(1, p - 1); - p = target.lastIndexOf('%'); - if (p > 0) target = target.substring(0, p); - return target; + if ( p > target.lastIndexOf(']')) { // if after ] it's a port divider (not IPv6 part) + target = target.substring(0, p ); } - // the ':' must be a port divider - target = target.substring(0, p); + // may be IPv4 or IPv6, we chop off brackets if exist + if (target.charAt(0) == '[') target = target.substring(1); + if (target.charAt(target.length() - 1) == ']') target = target.substring(0, target.length() - 1); p = target.lastIndexOf('%'); if (p > 0) target = target.substring(0, p); return target; diff --git a/test/java/net/yacy/cora/protocol/DomainsTest.java b/test/java/net/yacy/cora/protocol/DomainsTest.java index a953117ce..26bea5fd1 100644 --- a/test/java/net/yacy/cora/protocol/DomainsTest.java +++ b/test/java/net/yacy/cora/protocol/DomainsTest.java @@ -35,12 +35,13 @@ public class DomainsTest { @Test public void testStripToPort() { Map testHost = new HashMap(); - + // key = teststring, value = expected port testHost.put("[3ffe:2a00:100:7031::1]:80", 80); testHost.put("https://[3ffe:2a00:100:7031::1]:80/test.html", 80); testHost.put("[3ffe:2a00:100:7031::1]/test.html", 80); testHost.put("http://[3ffe:2a00:100:7031::1]/test.html", 80); testHost.put("[3ffe:2a00:100:7031::1]:8090/test.html", 8090); + testHost.put("ftp://[3ffe:2a00:100:7031::1]/test.html", 21); for (String host : testHost.keySet()) { int port = Domains.stripToPort(host); @@ -49,4 +50,36 @@ public class DomainsTest { } } + + /** + * Test of stripToHostName method, of class Domains. + */ + @Test + public void testStripToHostName() { + Map testHost = new HashMap(); + // key = teststring, value = expected host + testHost.put("[3ffe:2a00:100:7031::1]:80", "3ffe:2a00:100:7031::1"); + testHost.put("https://[3ffe:2a00:100:7032::1]:80/test.html", "3ffe:2a00:100:7032::1"); + testHost.put("[3ffe:2a00:100:7033::1]/test.html", "3ffe:2a00:100:7033::1"); + testHost.put("http://[3ffe:2a00:100:7034::1]/test.html", "3ffe:2a00:100:7034::1"); + testHost.put("[3ffe:2a00:100:7035::1]:8090/test.html", "3ffe:2a00:100:7035::1"); + testHost.put("ftp://[3ffe:2a00:100:7036::1]/test.html", "3ffe:2a00:100:7036::1"); + + testHost.put("http://test1.org/test.html", "test1.org"); + testHost.put("http://test2.org:80/test.html", "test2.org"); + testHost.put("http://test3.org:7777/test.html", "test3.org"); + testHost.put("http://www.test4.org/test.html", "www.test4.org"); + testHost.put("http://www.test5.org:80/test.html", "www.test5.org"); + testHost.put("http://www.test6.org:7777/test.html", "www.test6.org"); + + testHost.put("test7.org/test.html", "test7.org"); + testHost.put("test8.org:80/test.html", "test8.org"); + testHost.put("test9.org:7777/test.html", "test9.org"); + + for (String teststr : testHost.keySet()) { + String host = Domains.stripToHostName(teststr); + String expectedHost = testHost.get(teststr); + assertEquals(teststr, expectedHost, host); + } + } }