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.


git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1080422 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Moore 2011-03-11 01:45:13 +00:00
parent 23bc2c7f78
commit 55742a46e1
2 changed files with 41 additions and 1 deletions

View File

@ -427,7 +427,7 @@ public class CachingHttpClient implements HttpClient {
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);

View File

@ -301,6 +301,46 @@ public class TestRFC5861Compliance extends AbstractProtocolTest {
assertTrue(warning110Found);
}
@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 {