hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1053679 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/client/utils/ main/java/org/apache/http/impl/client/ test/java/org/apache/http/client/utils/ test/java/org/apache/http/impl/client/
Date Wed, 29 Dec 2010 17:35:37 GMT
Author: olegk
Date: Wed Dec 29 17:35:36 2010
New Revision: 1053679

URL: http://svn.apache.org/viewvc?rev=1053679&view=rev
Log:
Moved target host extraction code to a re-usable static method in the URIUtils class

Removed:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractHttpClient.java
Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java?rev=1053679&r1=1053678&r2=1053679&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
Wed Dec 29 17:35:36 2010
@@ -278,6 +278,56 @@ public class URIUtils {
     }
 
     /**
+     * Extracts target host from the given {@link URI}.
+     * 
+     * @param uri 
+     * @return the target host if the URI is absolute or <code>null</null> if
the URI is 
+     * relative or does not contain a valid host name.
+     * 
+     * @since 4.1
+     */
+    public static HttpHost exctractHost(final URI uri) {
+        if (uri == null) {
+            return null;
+        }
+        HttpHost target = null;
+        if (uri.isAbsolute()) {
+            int port = uri.getPort(); // may be overridden later
+            String host = uri.getHost();
+            if (host == null) { // normal parse failed; let's do it ourselves
+                // authority does not seem to care about the valid character-set for host
names
+                host = uri.getAuthority();
+                if (host != null) {
+                    // Strip off any leading user credentials
+                    int at = host.indexOf('@');
+                    if (at >= 0) {
+                        if (host.length() > at+1 ) {
+                            host = host.substring(at+1);
+                        } else {
+                            host = null; // @ on its own
+                        }
+                    }
+                    // Extract the port suffix, if present
+                    if (host != null) { 
+                        int colon = host.indexOf(':');
+                        if (colon >= 0) {
+                            if (colon+1 < host.length()) {
+                                port = Integer.parseInt(host.substring(colon+1));
+                            }
+                            host = host.substring(0,colon);
+                        }                
+                    }                    
+                }
+            }
+            String scheme = uri.getScheme();
+            if (host != null) {
+                target = new HttpHost(host, port, scheme);
+            }
+        }
+        return target;
+    }
+    
+    /**
      * This class should not be instantiated.
      */
     private URIUtils() {

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java?rev=1053679&r1=1053678&r2=1053679&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
Wed Dec 29 17:35:36 2010
@@ -57,6 +57,7 @@ import org.apache.http.client.HttpClient
 import org.apache.http.client.HttpRequestRetryHandler;
 import org.apache.http.client.UserTokenHandler;
 import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.utils.URIUtils;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.ConnectionKeepAliveStrategy;
 import org.apache.http.conn.routing.HttpRoutePlanner;
@@ -626,47 +627,18 @@ public abstract class AbstractHttpClient
         return execute(determineTarget(request), request, context);
     }
 
-    // Package-protected so can be accessed from unit tests
-    static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException
{
+    private static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException
{
         // A null target may be acceptable if there is a default target.
         // Otherwise, the null target is detected in the director.
         HttpHost target = null;
 
         URI requestURI = request.getURI();
         if (requestURI.isAbsolute()) {
-            int port = requestURI.getPort(); // may be overridden later
-            String host = requestURI.getHost();
-            if (host == null) { // normal parse failed; let's do it ourselves
-                // authority does not seem to care about the valid character-set for host
names
-                host = requestURI.getAuthority();
-                if (host != null) {
-                    // Strip off any leading user credentials
-                    int at = host.indexOf('@');
-                    if (at >= 0) {
-                        if (host.length() > at+1 ) {
-                            host = host.substring(at+1);
-                        } else {
-                            host = null; // @ on its own
-                        }
-                    }
-                    // Extract the port suffix, if present
-                    if (host != null) { 
-                        int colon = host.indexOf(':');
-                        if (colon >= 0) {
-                            if (colon+1 < host.length()) {
-                                port = Integer.parseInt(host.substring(colon+1));
-                            }
-                            host = host.substring(0,colon);
-                        }                
-                    }                    
-                }
-            }
-            String scheme = requestURI.getScheme();
-            if (host == null) {
+            target = URIUtils.exctractHost(requestURI);
+            if (target == null) {
                 throw new ClientProtocolException(
                         "URI does not specify a valid host name: " + requestURI);
             }
-            target = new HttpHost(host, port, scheme);
         }
         return target;
     }

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java?rev=1053679&r1=1053678&r2=1053679&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
Wed Dec 29 17:35:36 2010
@@ -317,4 +317,27 @@ public class TestURIUtils {
                 "http://s/mid/content=5/../6").toString());
     }
 
+    @Test
+    public void testHTTPCLIENT_911() throws Exception{
+        Assert.assertEquals(new HttpHost("localhost"),URIUtils.exctractHost(new URI("http://localhost/abcd")));
+        Assert.assertEquals(new HttpHost("localhost"),URIUtils.exctractHost(new URI("http://localhost/abcd%3A")));
+        
+        Assert.assertEquals(new HttpHost("local_host"),URIUtils.exctractHost(new URI("http://local_host/abcd")));
+        Assert.assertEquals(new HttpHost("local_host"),URIUtils.exctractHost(new URI("http://local_host/abcd%3A")));
+        
+        Assert.assertEquals(new HttpHost("localhost",8),URIUtils.exctractHost(new URI("http://localhost:8/abcd")));
+        Assert.assertEquals(new HttpHost("local_host",8),URIUtils.exctractHost(new URI("http://local_host:8/abcd")));
+
+        // URI seems to OK with missing port number
+        Assert.assertEquals(new HttpHost("localhost"),URIUtils.exctractHost(new URI("http://localhost:/abcd")));
+        Assert.assertEquals(new HttpHost("local_host"),URIUtils.exctractHost(new URI("http://local_host:/abcd")));
+
+        Assert.assertEquals(new HttpHost("localhost",8080),URIUtils.exctractHost(new URI("http://user:pass@localhost:8080/abcd")));
+        Assert.assertEquals(new HttpHost("local_host",8080),URIUtils.exctractHost(new URI("http://user:pass@local_host:8080/abcd")));
+
+        Assert.assertEquals(new HttpHost("localhost",8080),URIUtils.exctractHost(new URI("http://@localhost:8080/abcd")));
+        Assert.assertEquals(new HttpHost("local_host",8080),URIUtils.exctractHost(new URI("http://@local_host:8080/abcd")));
+
+    }
+    
 }



Mime
View raw message