hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1618324 - in /httpcomponents/httpclient/trunk: httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ httpclient/src/main/java/org/apache/http/client/utils/ httpc...
Date Sat, 16 Aug 2014 10:51:35 GMT
Author: olegk
Date: Sat Aug 16 10:51:35 2014
New Revision: 1618324

URL: http://svn.apache.org/r1618324
Log:
HTTPCLIENT-1542: Caching HttpClient uses absolute URIs for validation
Based on contribution by Joseph Walton <joe at kafsemo.org>

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheJiraNumber1147.java
    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/execchain/ProtocolExec.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestProtocolExec.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java?rev=1618324&r1=1618323&r2=1618324&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java
Sat Aug 16 10:51:35 2014
@@ -27,6 +27,8 @@
 package org.apache.http.impl.client.cache;
 
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -44,6 +46,7 @@ import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
+import org.apache.http.ProtocolException;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.RequestLine;
 import org.apache.http.annotation.ThreadSafe;
@@ -58,6 +61,7 @@ import org.apache.http.client.methods.Ht
 import org.apache.http.client.methods.HttpRequestWrapper;
 import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.client.utils.DateUtils;
+import org.apache.http.client.utils.URIUtils;
 import org.apache.http.conn.routing.HttpRoute;
 import org.apache.http.impl.execchain.ClientExecChain;
 import org.apache.http.message.BasicHttpResponse;
@@ -738,6 +742,14 @@ public class CachingExec implements Clie
             final HttpCacheEntry cacheEntry) throws IOException, HttpException {
 
         final HttpRequestWrapper conditionalRequest = conditionalRequestBuilder.buildConditionalRequest(request,
cacheEntry);
+        final URI uri = conditionalRequest.getURI();
+        if (uri != null) {
+            try {
+                request.setURI(URIUtils.rewriteURIForRoute(uri, route));
+            } catch (final URISyntaxException ex) {
+                throw new ProtocolException("Invalid URI: " + uri, ex);
+            }
+        }
 
         Date requestDate = getCurrentDate();
         CloseableHttpResponse backendResponse = backend.execute(

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheJiraNumber1147.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheJiraNumber1147.java?rev=1618324&r1=1618323&r2=1618324&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheJiraNumber1147.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheJiraNumber1147.java
Sat Aug 16 10:51:35 2014
@@ -93,6 +93,8 @@ public class TestHttpCacheJiraNumber1147
         final HttpHost target = new HttpHost("somehost");
         final HttpRoute route = new HttpRoute(target);
 
+        context.setTargetHost(target);
+
         final Date now = new Date();
         final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
 

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=1618324&r1=1618323&r2=1618324&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
Sat Aug 16 10:51:35 2014
@@ -34,6 +34,7 @@ import java.util.Stack;
 
 import org.apache.http.HttpHost;
 import org.apache.http.annotation.Immutable;
+import org.apache.http.conn.routing.RouteInfo;
 import org.apache.http.util.Args;
 import org.apache.http.util.TextUtils;
 
@@ -196,6 +197,39 @@ public class URIUtils {
     }
 
     /**
+     * A convenience method that optionally converts the original {@link java.net.URI} either
+     * to a relative or an absolute form as required by the specified route.
+     *
+     * @param uri
+     *            original URI.
+     * @throws URISyntaxException
+     *             If the resulting URI is invalid.
+     *
+     * @since 4.4
+     */
+    public static URI rewriteURIForRoute(final URI uri, final RouteInfo route) throws URISyntaxException
{
+        if (uri == null) {
+            return null;
+        }
+        if (route.getProxyHost() != null && !route.isTunnelled()) {
+            // Make sure the request URI is absolute
+            if (!uri.isAbsolute()) {
+                final HttpHost target = route.getTargetHost();
+                return rewriteURI(uri, target, true);
+            } else {
+                return rewriteURI(uri);
+            }
+        } else {
+            // Make sure the request URI is relative
+            if (uri.isAbsolute()) {
+                return rewriteURI(uri, null, true);
+            } else {
+                return rewriteURI(uri);
+            }
+        }
+    }
+
+    /**
      * Resolves a URI reference against a base URI. Work-around for bug in
      * java.net.URI (<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535>)
      *

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java?rev=1618324&r1=1618323&r2=1618324&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/ProtocolExec.java
Sat Aug 16 10:51:35 2014
@@ -87,29 +87,13 @@ public class ProtocolExec implements Cli
     void rewriteRequestURI(
             final HttpRequestWrapper request,
             final HttpRoute route) throws ProtocolException {
-        try {
-            URI uri = request.getURI();
-            if (uri != null) {
-                if (route.getProxyHost() != null && !route.isTunnelled()) {
-                    // Make sure the request URI is absolute
-                    if (!uri.isAbsolute()) {
-                        final HttpHost target = route.getTargetHost();
-                        uri = URIUtils.rewriteURI(uri, target, true);
-                    } else {
-                        uri = URIUtils.rewriteURI(uri);
-                    }
-                } else {
-                    // Make sure the request URI is relative
-                    if (uri.isAbsolute()) {
-                        uri = URIUtils.rewriteURI(uri, null, true);
-                    } else {
-                        uri = URIUtils.rewriteURI(uri);
-                    }
-                }
-                request.setURI(uri);
+        final URI uri = request.getURI();
+        if (uri != null) {
+            try {
+                request.setURI(URIUtils.rewriteURIForRoute(uri, route));
+            } catch (final URISyntaxException ex) {
+                throw new ProtocolException("Invalid URI: " + uri, ex);
             }
-        } catch (final URISyntaxException ex) {
-            throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(),
ex);
         }
     }
 

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=1618324&r1=1618323&r2=1618324&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
Sat Aug 16 10:51:35 2014
@@ -30,6 +30,7 @@ import java.net.URI;
 import java.util.Arrays;
 
 import org.apache.http.HttpHost;
+import org.apache.http.conn.routing.HttpRoute;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -101,6 +102,34 @@ public class TestURIUtils {
     }
 
     @Test
+    public void testRewriteForRoute() throws Exception {
+
+        final HttpHost target1 = new HttpHost("foo", 80);
+        final HttpHost target2 = new HttpHost("foo", 443, "https");
+        final HttpHost proxy = new HttpHost("bar", 8888);
+
+        // Direct route
+        Assert.assertEquals(new URI("/test"), URIUtils
+                .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(target1)));
+
+        // Direct route
+        Assert.assertEquals(new URI("/"), URIUtils
+                .rewriteURIForRoute(new URI(""), new HttpRoute(target1)));
+
+        // Via proxy
+        Assert.assertEquals(new URI("http://foo/test"), URIUtils
+                .rewriteURIForRoute(new URI("http://foo/test"), new HttpRoute(target1, proxy)));
+
+        // Via proxy
+        Assert.assertEquals(new URI("http://foo:80/test"), URIUtils
+                .rewriteURIForRoute(new URI("/test"), new HttpRoute(target1, proxy)));
+
+        // Via proxy tunnel
+        Assert.assertEquals(new URI("/test"), URIUtils
+                .rewriteURIForRoute(new URI("https://foo:443/test"), new HttpRoute(target2,
null, proxy, true)));
+    }
+
+    @Test
     public void testNormalization() {
         Assert.assertEquals("example://a/b/c/%7Bfoo%7D", URIUtils.resolve(this.baseURI, "eXAMPLE://a/./b/../b/%63/%7bfoo%7d").toString());
         Assert.assertEquals("http://www.example.com/%3C", URIUtils.resolve(this.baseURI,
"http://www.example.com/%3c").toString());

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestProtocolExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestProtocolExec.java?rev=1618324&r1=1618323&r2=1618324&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestProtocolExec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/execchain/TestProtocolExec.java
Sat Aug 16 10:51:35 2014
@@ -26,6 +26,9 @@
  */
 package org.apache.http.impl.execchain;
 
+import java.io.IOException;
+import java.net.URI;
+
 import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.auth.AuthScope;
@@ -47,9 +50,6 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 
-import java.io.IOException;
-import java.net.URI;
-
 @SuppressWarnings({"static-access"}) // test code
 public class TestProtocolExec {
 



Mime
View raw message