HTTPCLIENT-1166: more tolerant parsing logic for malformed uris with invalid authority part

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1244105 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2012-02-14 16:12:02 +00:00
parent d87d45a618
commit 647e4dc314
2 changed files with 16 additions and 5 deletions

View File

@ -306,9 +306,18 @@ public class URIUtils {
if (host != null) {
int colon = host.indexOf(':');
if (colon >= 0) {
if (colon + 1 < host.length()) {
int pos = colon + 1;
int len = 0;
for (int i = pos; i < host.length(); i++) {
if (Character.isDigit(host.charAt(i))) {
len++;
} else {
break;
}
}
if (len > 0) {
try {
port = Integer.parseInt(host.substring(colon + 1));
port = Integer.parseInt(host.substring(pos, pos + len));
} catch (NumberFormatException ex) {
}
}

View File

@ -341,9 +341,9 @@ public class TestURIUtils {
URIUtils.extractHost(new URI("http://local_host:8/abcd")));
// URI seems to OK with missing port number
Assert.assertEquals(new HttpHost("localhost"),URIUtils.extractHost(
Assert.assertEquals(new HttpHost("localhost",-1),URIUtils.extractHost(
new URI("http://localhost:/abcd")));
Assert.assertEquals(new HttpHost("local_host"),URIUtils.extractHost(
Assert.assertEquals(new HttpHost("local_host",-1),URIUtils.extractHost(
new URI("http://local_host:/abcd")));
Assert.assertEquals(new HttpHost("localhost",8080),
@ -361,8 +361,10 @@ public class TestURIUtils {
Assert.assertEquals(new HttpHost("localhost",8080),
URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
Assert.assertEquals(new HttpHost("localhost",-1),
Assert.assertEquals(new HttpHost("localhost",8080),
URIUtils.extractHost(new URI("http://localhost:8080;sessionid=stuff/abcd")));
Assert.assertEquals(new HttpHost("localhost",-1),
URIUtils.extractHost(new URI("http://localhost:;sessionid=stuff/abcd")));
}
}