summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorreger <reger18@arcor.de>2014-04-25 20:15:55 +0200
committerreger <reger18@arcor.de>2014-04-25 20:15:55 +0200
commitbb8181b2be97b0199e5b436eb210cb227120e9b0 (patch)
treeba2be7654f5b7878730a19268901a2232f60c3e1
parent121d25be38ec8e544e4521248e98a9e4f24538a1 (diff)
fix: resolve url without path but searchpart
e.g. http://yacy.net?q=test was resolved as host "yacy.net?q=test" now host="yacy.net" path="/" fixes http://mantis.tokeek.de/view.php?id=47 added test case for getHost
-rw-r--r--source/net/yacy/cora/document/id/MultiProtocolURL.java7
-rw-r--r--test/net/yacy/cora/document/id/MultiProtocolURLTest.java28
2 files changed, 32 insertions, 3 deletions
diff --git a/source/net/yacy/cora/document/id/MultiProtocolURL.java b/source/net/yacy/cora/document/id/MultiProtocolURL.java
index c9af0763e..1fe482bc2 100644
--- a/source/net/yacy/cora/document/id/MultiProtocolURL.java
+++ b/source/net/yacy/cora/document/id/MultiProtocolURL.java
@@ -164,7 +164,8 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
if (url.length() < p + 4) throw new MalformedURLException("URL not parseable: '" + url + "'");
if (!this.protocol.equals("file") && url.substring(p + 1, p + 3).equals("//")) {
// identify host, userInfo and file for http and ftp protocol
- final int q = url.indexOf('/', p + 3);
+ int q = url.indexOf('/', p + 3);
+ if (q < 0) q = url.indexOf("?", p + 3); // check for www.test.com?searchpart
int r;
if (q < 0) {
if ((r = url.indexOf('@', p + 3)) < 0) {
@@ -183,11 +184,11 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
this.userInfo = this.host.substring(0, r);
this.host = this.host.substring(r + 1).intern();
}
- this.path = url.substring(q);
+ this.path = url.substring(q); // may result in "?searchpart" (resolveBackpath prepends a "/" )
}
if (this.host.length() < 4 && !this.protocol.equals("file")) throw new MalformedURLException("host too short: '" + this.host + "', url = " + url);
if (this.host.indexOf('&') >= 0) throw new MalformedURLException("invalid '&' in host");
- this.path = resolveBackpath(this.path);
+ this.path = resolveBackpath(this.path); // adds "/" if missing
identPort(url, (isHTTP() ? 80 : (isHTTPS() ? 443 : (isFTP() ? 21 : (isSMB() ? 445 : -1)))));
identAnchor();
identSearchpart();
diff --git a/test/net/yacy/cora/document/id/MultiProtocolURLTest.java b/test/net/yacy/cora/document/id/MultiProtocolURLTest.java
index 046083b8d..aae13fa1a 100644
--- a/test/net/yacy/cora/document/id/MultiProtocolURLTest.java
+++ b/test/net/yacy/cora/document/id/MultiProtocolURLTest.java
@@ -113,6 +113,34 @@ public class MultiProtocolURLTest {
}
}
+
+ @Test
+ public void testGetHost() throws MalformedURLException {
+ String[][] testStrings = new String[][]{
+ // teststring , expectedresult
+ new String[]{"http://www.yacy.net", "www.yacy.net"},
+ new String[]{"http://www.yacy.net:8090", "www.yacy.net"},
+ new String[]{"http://www.yacy.net/test?query=test", "www.yacy.net"},
+ new String[]{"http://www.yacy.net/?query=test", "www.yacy.net"},
+ new String[]{"http://www.yacy.net?query=test", "www.yacy.net"},
+ new String[]{"http://www.yacy.net:?query=test", "www.yacy.net"},
+ new String[]{"//www.yacy.net:?query=test", "www.yacy.net"},
+ };
+
+ for (int i = 0; i < testStrings.length; i++) {
+ // desired conversion result
+ System.out.print("testGetHost: " + testStrings[i][0]);
+ String shouldBe = testStrings[i][1];
+
+ // conversion result
+ String resolvedHost = new MultiProtocolURL(testStrings[i][0]).getHost();
+
+ // test if equal
+ assertEquals(shouldBe, resolvedHost);
+ System.out.println(" -> " + resolvedHost);
+
+ }
+ }
}