hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1244095 - in /httpcomponents/httpclient/branches/4.1.x/httpclient/src: main/java/org/apache/http/client/utils/URIUtils.java test/java/org/apache/http/client/utils/TestURIUtils.java
Date Tue, 14 Feb 2012 16:10:35 GMT
Author: olegk
Date: Tue Feb 14 16:10:34 2012
New Revision: 1244095

URL: http://svn.apache.org/viewvc?rev=1244095&view=rev
Log:
HTTPCLIENT-1166: more tolerant parsing logic for malformed uris with invalid authority part

Modified:
    httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
    httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java?rev=1244095&r1=1244094&r2=1244095&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
(original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
Tue Feb 14 16:10:34 2012
@@ -306,8 +306,20 @@ public class URIUtils {
                     if (host != null) { 
                         int colon = host.indexOf(':');
                         if (colon >= 0) {
-                            if (colon+1 < host.length()) {
-                                port = Integer.parseInt(host.substring(colon+1));
+                            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(pos, pos + len));
+                                } catch (NumberFormatException ex) {
+                                }
                             }
                             host = host.substring(0,colon);
                         }                

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java?rev=1244095&r1=1244094&r2=1244095&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
(original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
Tue Feb 14 16:10:34 2012
@@ -325,25 +325,46 @@ public class TestURIUtils {
 
     @Test
     public void testHTTPCLIENT_911() throws Exception{
-        Assert.assertEquals(new HttpHost("localhost"),URIUtils.extractHost(new URI("http://localhost/abcd")));
-        Assert.assertEquals(new HttpHost("localhost"),URIUtils.extractHost(new URI("http://localhost/abcd%3A")));
+        Assert.assertEquals(new HttpHost("localhost"),
+                URIUtils.extractHost(new URI("http://localhost/abcd")));
+        Assert.assertEquals(new HttpHost("localhost"),
+                URIUtils.extractHost(new URI("http://localhost/abcd%3A")));
         
-        Assert.assertEquals(new HttpHost("local_host"),URIUtils.extractHost(new URI("http://local_host/abcd")));
-        Assert.assertEquals(new HttpHost("local_host"),URIUtils.extractHost(new URI("http://local_host/abcd%3A")));
+        Assert.assertEquals(new HttpHost("local_host"),
+                URIUtils.extractHost(new URI("http://local_host/abcd")));
+        Assert.assertEquals(new HttpHost("local_host"),
+                URIUtils.extractHost(new URI("http://local_host/abcd%3A")));
         
-        Assert.assertEquals(new HttpHost("localhost",8),URIUtils.extractHost(new URI("http://localhost:8/abcd")));
-        Assert.assertEquals(new HttpHost("local_host",8),URIUtils.extractHost(new URI("http://local_host:8/abcd")));
+        Assert.assertEquals(new HttpHost("localhost",8),
+                URIUtils.extractHost(new URI("http://localhost:8/abcd")));
+        Assert.assertEquals(new HttpHost("local_host",8),
+                URIUtils.extractHost(new URI("http://local_host:8/abcd")));
 
         // URI seems to OK with missing port number
-        Assert.assertEquals(new HttpHost("localhost"),URIUtils.extractHost(new URI("http://localhost:/abcd")));
-        Assert.assertEquals(new HttpHost("local_host"),URIUtils.extractHost(new URI("http://local_host:/abcd")));
+        Assert.assertEquals(new HttpHost("localhost"),
+                URIUtils.extractHost(new URI("http://localhost:/abcd")));
+        Assert.assertEquals(new HttpHost("local_host"),
+                URIUtils.extractHost(new URI("http://local_host:/abcd")));
 
-        Assert.assertEquals(new HttpHost("localhost",8080),URIUtils.extractHost(new URI("http://user:pass@localhost:8080/abcd")));
-        Assert.assertEquals(new HttpHost("local_host",8080),URIUtils.extractHost(new URI("http://user:pass@local_host:8080/abcd")));
+        Assert.assertEquals(new HttpHost("localhost",8080),
+                URIUtils.extractHost(new URI("http://user:pass@localhost:8080/abcd")));
+        Assert.assertEquals(new HttpHost("local_host",8080),
+                URIUtils.extractHost(new URI("http://user:pass@local_host:8080/abcd")));
 
-        Assert.assertEquals(new HttpHost("localhost",8080),URIUtils.extractHost(new URI("http://@localhost:8080/abcd")));
-        Assert.assertEquals(new HttpHost("local_host",8080),URIUtils.extractHost(new URI("http://@local_host:8080/abcd")));
+        Assert.assertEquals(new HttpHost("localhost",8080),
+                URIUtils.extractHost(new URI("http://@localhost:8080/abcd")));
+        Assert.assertEquals(new HttpHost("local_host",8080),
+                URIUtils.extractHost(new URI("http://@local_host:8080/abcd")));
 
+        Assert.assertEquals(new HttpHost("[2a00:1450:400c:c01::69]",8080),
+                URIUtils.extractHost(new URI("http://[2a00:1450:400c:c01::69]:8080/")));
+
+        Assert.assertEquals(new HttpHost("localhost",8080),
+                URIUtils.extractHost(new URI("http://localhost:8080/;sessionid=stuff/abcd")));
+        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")));
     }
     
 }



Mime
View raw message