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
pull/1/head
reger 11 years ago
parent 121d25be38
commit bb8181b2be

@ -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();

@ -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);
}
}
}

Loading…
Cancel
Save