You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by jo...@apache.org on 2013/08/10 04:06:48 UTC
svn commit: r1512552 - 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/
Author: jonm
Date: Sat Aug 10 02:06:48 2013
New Revision: 1512552
URL: http://svn.apache.org/r1512552
Log:
HTTPCLIENT-1370: Response to non-GET requests should never be cached with the default
ResponseCachingPolicy
Contributed by James Leigh <james at 3roundstones dot com>
Modified:
httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseCachingPolicy.java
httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java
httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCachingPolicy.java
Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1512552&r1=1512551&r2=1512552&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Sat Aug 10 02:06:48 2013
@@ -1,6 +1,11 @@
Changes since release 4.3 BETA2
-------------------
+
+* [HTTPCLIENT-1370] Response to non-GET requests should never be cached with the default
+ ResponseCachingPolicy
+ Contributed by James Leigh <james at 3roundstones dot com>
+
* [HTTPCLIENT-1373] OPTIONS and TRACE should not invalidate cache
Contributed by James Leigh <james at 3roundstones dot com>
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseCachingPolicy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseCachingPolicy.java?rev=1512552&r1=1512551&r2=1512552&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseCachingPolicy.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseCachingPolicy.java Sat Aug 10 02:06:48 2013
@@ -53,6 +53,9 @@ import org.apache.http.protocol.HTTP;
@Immutable
class ResponseCachingPolicy {
+ private static final String[] AUTH_CACHEABLE_PARAMS = {
+ "s-maxage", HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE, HeaderConstants.PUBLIC
+ };
private final long maxObjectSizeBytes;
private final boolean sharedCache;
private final boolean neverCache1_0ResponsesWithQueryString;
@@ -258,11 +261,9 @@ class ResponseCachingPolicy {
if (sharedCache) {
final Header[] authNHeaders = request.getHeaders(HeaderConstants.AUTHORIZATION);
- if (authNHeaders != null && authNHeaders.length > 0) {
- final String[] authCacheableParams = {
- "s-maxage", HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE, HeaderConstants.PUBLIC
- };
- return hasCacheControlParameterFrom(response, authCacheableParams);
+ if (authNHeaders != null && authNHeaders.length > 0
+ && !hasCacheControlParameterFrom(response, AUTH_CACHEABLE_PARAMS)) {
+ return false;
}
}
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java?rev=1512552&r1=1512551&r2=1512552&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java Sat Aug 10 02:06:48 2013
@@ -66,6 +66,7 @@ import org.apache.http.client.cache.Http
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpExecutionAware;
import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
@@ -787,6 +788,41 @@ public abstract class TestCachingExecCha
}
@Test
+ public void testReturns200ForOptionsFollowedByGetIfAuthorizationHeaderAndSharedCache()
+ throws Exception {
+ impl = createCachingExecChain(mockBackend, new BasicHttpCache(), CacheConfig.custom().setSharedCache(true).build());
+ final Date now = new Date();
+ final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(new HttpOptions("http://foo.example.com/"));
+ req1.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(new HttpGet("http://foo.example.com/"));
+ req2.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ final HttpResponse resp1 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
+ HttpStatus.SC_NO_CONTENT, "No Content");
+ resp1.setHeader("Content-Length", "0");
+ resp1.setHeader("ETag", "\"options-etag\"");
+ resp1.setHeader("Date", DateUtils.formatDate(now));
+ resp1.setHeader("Cache-Control", "public, max-age=3600");
+ resp1.setHeader("Last-Modified", DateUtils.formatDate(now));
+ final HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
+ HttpStatus.SC_OK, "OK");
+ resp1.setEntity(HttpTestUtils.makeBody(128));
+ resp1.setHeader("Content-Length", "128");
+ resp1.setHeader("ETag", "\"get-etag\"");
+ resp1.setHeader("Date", DateUtils.formatDate(now));
+ resp1.setHeader("Cache-Control", "public, max-age=3600");
+ resp1.setHeader("Last-Modified", DateUtils.formatDate(now));
+
+ backendExpectsAnyRequestAndReturn(resp1);
+ backendExpectsAnyRequestAndReturn(resp2);
+
+ replayMocks();
+ impl.execute(route, req1, context, null);
+ final HttpResponse result = impl.execute(route, req2, context, null);
+ verifyMocks();
+ Assert.assertEquals(200, result.getStatusLine().getStatusCode());
+ }
+
+ @Test
public void testSetsValidatedContextIfRequestWasSuccessfullyValidated()
throws Exception {
final Date now = new Date();
Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCachingPolicy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCachingPolicy.java?rev=1512552&r1=1512551&r2=1512552&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCachingPolicy.java (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCachingPolicy.java Sat Aug 10 02:06:48 2013
@@ -34,6 +34,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
+import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.message.BasicHttpResponse;
@@ -134,6 +135,16 @@ public class TestResponseCachingPolicy {
}
@Test
+ public void test206ResponseCodeIsNotCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setStatusCode(HttpStatus.SC_PARTIAL_CONTENT);
+ response.setHeader("Cache-Control", "public");
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void test300ResponseCodeIsCacheable() {
response.setStatusCode(HttpStatus.SC_MULTIPLE_CHOICES);
Assert.assertTrue(policy.isResponseCacheable("GET", response));
@@ -328,6 +339,16 @@ public class TestResponseCachingPolicy {
}
@Test
+ public void testVaryStarIsNotCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setHeader("Cache-Control", "public");
+ response.setHeader("Vary", "*");
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void testIsGetWithVaryHeaderCacheable() {
response.addHeader("Vary", "Accept-Encoding");
Assert.assertTrue(policy.isResponseCacheable("GET", response));
@@ -342,6 +363,18 @@ public class TestResponseCachingPolicy {
}
@Test
+ public void testIsArbitraryMethodCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request = new HttpOptions("http://foo.example.com/");
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setStatusCode(HttpStatus.SC_NO_CONTENT);
+ response.setHeader("Cache-Control", "public");
+
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void testResponsesToRequestsWithNoStoreAreNotCacheable() {
request.setHeader("Cache-Control","no-store");
response.setHeader("Cache-Control","public");
@@ -356,6 +389,17 @@ public class TestResponseCachingPolicy {
}
@Test
+ public void testResponsesWithMultipleAgeHeadersAreNotCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setHeader("Cache-Control", "public");
+ response.addHeader("Age", "3");
+ response.addHeader("Age", "5");
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void testResponsesWithMultipleDateHeadersAreNotCacheable() {
response.addHeader("Date", DateUtils.formatDate(now));
response.addHeader("Date", DateUtils.formatDate(sixSecondsAgo));
@@ -363,12 +407,33 @@ public class TestResponseCachingPolicy {
}
@Test
+ public void testResponsesWithMultipleDateHeadersAreNotCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setHeader("Cache-Control", "public");
+ response.addHeader("Date", DateUtils.formatDate(now));
+ response.addHeader("Date", DateUtils.formatDate(sixSecondsAgo));
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void testResponsesWithMalformedDateHeadersAreNotCacheable() {
response.addHeader("Date", "garbage");
Assert.assertFalse(policy.isResponseCacheable("GET", response));
}
@Test
+ public void testResponsesWithMalformedDateHeadersAreNotCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setHeader("Cache-Control", "public");
+ response.addHeader("Date", "garbage");
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void testResponsesWithMultipleExpiresHeadersAreNotCacheable() {
final Date now = new Date();
final Date sixSecondsAgo = new Date(now.getTime() - 6 * 1000L);
@@ -378,6 +443,19 @@ public class TestResponseCachingPolicy {
}
@Test
+ public void testResponsesWithMultipleExpiresHeadersAreNotCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setHeader("Cache-Control", "public");
+ final Date now = new Date();
+ final Date sixSecondsAgo = new Date(now.getTime() - 6 * 1000L);
+ response.addHeader("Expires", DateUtils.formatDate(now));
+ response.addHeader("Expires", DateUtils.formatDate(sixSecondsAgo));
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void testResponsesWithoutDateHeadersAreNotCacheable() {
response.removeHeaders("Date");
Assert.assertFalse(policy.isResponseCacheable("GET", response));
@@ -390,6 +468,16 @@ public class TestResponseCachingPolicy {
}
@Test
+ public void testResponseThatHasTooMuchContentIsNotCacheableUsingSharedPublicCache() {
+ policy = new ResponseCachingPolicy(0, true, false, false);
+
+ request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
+ response.setHeader("Cache-Control", "public");
+ response.setHeader("Content-Length", "9000");
+ Assert.assertFalse(policy.isResponseCacheable(request, response));
+ }
+
+ @Test
public void testResponsesThatAreSmallEnoughAreCacheable() {
response.setHeader("Content-Length", "0");
Assert.assertTrue(policy.isResponseCacheable("GET", response));