hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1080422 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/CachingHttpClient.java test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java
Date Fri, 11 Mar 2011 01:45:14 GMT
Author: jonm
Date: Fri Mar 11 01:45:13 2011
New Revision: 1080422

URL: http://svn.apache.org/viewvc?rev=1080422&view=rev
Log:
Minor enhancement to stale-while-revalidate (RFC5861) handling; now we can also
serve stale 304s (Not Modified) while asynchronously revalidating. Prior to this,
we were always returning a stale 200 response regardless of whether the incoming
request was conditional or not. The old behavior was not incorrect, but this is
better.

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java?rev=1080422&r1=1080421&r2=1080422&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
Fri Mar 11 01:45:13 2011
@@ -427,7 +427,7 @@ public class CachingHttpClient implement
             if (asynchRevalidator != null
                 && !staleResponseNotAllowed(request, entry, now)
                 && validityPolicy.mayReturnStaleWhileRevalidating(entry, now)) {
-                final HttpResponse resp = responseGenerator.generateResponse(entry);
+                final HttpResponse resp = generateCachedResponse(request, context, entry,
now);
                 resp.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\"");
                 
                 asynchRevalidator.revalidateCacheEntry(target, request, context, entry);

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java?rev=1080422&r1=1080421&r2=1080422&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java
Fri Mar 11 01:45:13 2011
@@ -302,6 +302,46 @@ public class TestRFC5861Compliance exten
     }
     
     @Test
+    public void testCanAlsoServeStale304sWhileRevalidating()
+        throws Exception {
+        
+        params.setAsynchronousWorkersMax(1);
+        params.setSharedCache(false);
+        impl = new CachingHttpClient(mockBackend, cache, params);
+        
+        HttpRequest req1 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        HttpResponse resp1 = HttpTestUtils.make200Response();
+        Date now = new Date();
+        Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
+        resp1.setHeader("Cache-Control", "private, stale-while-revalidate=15");
+        resp1.setHeader("ETag","\"etag\"");
+        resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
+
+        backendExpectsAnyRequest().andReturn(resp1).times(1,2);
+
+        HttpRequest req2 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        req2.setHeader("If-None-Match","\"etag\"");
+
+        replayMocks();
+        impl.execute(host, req1);
+        HttpResponse result = impl.execute(host, req2);
+        verifyMocks();
+
+        assertEquals(HttpStatus.SC_NOT_MODIFIED, result.getStatusLine().getStatusCode());
+        boolean warning110Found = false;
+        for(Header h : result.getHeaders("Warning")) {
+            for(WarningValue wv : WarningValue.getWarningValues(h)) {
+                if (wv.getWarnCode() == 110) {
+                    warning110Found = true;
+                    break;
+                }
+            }
+        }
+        assertTrue(warning110Found);
+    }
+
+    
+    @Test
     public void testStaleWhileRevalidateYieldsToMustRevalidate()
         throws Exception {
         



Mime
View raw message