mirror of
https://github.com/square/okhttp.git
synced 2026-01-24 04:02:07 +03:00
Merge pull request #315 from aahlenst/fix-issue-298
Bug fixed that caused gzipped responses to be returned from cache after ...
This commit is contained in:
@@ -649,10 +649,17 @@ public class HttpEngine {
|
||||
if (cachedResponseHeaders.validate(responseHeaders)) {
|
||||
release(false);
|
||||
ResponseHeaders combinedHeaders = cachedResponseHeaders.combine(responseHeaders);
|
||||
setResponse(combinedHeaders, cachedResponseBody);
|
||||
this.responseHeaders = combinedHeaders;
|
||||
|
||||
// Update the cache after applying the combined headers but before initializing the content
|
||||
// stream, otherwise the Content-Encoding header (if present) will be stripped from the
|
||||
// combined headers and not end up in the cache file if transparent gzip compression is
|
||||
// turned on.
|
||||
OkResponseCache responseCache = client.getOkResponseCache();
|
||||
responseCache.trackConditionalCacheHit();
|
||||
responseCache.update(cacheResponse, policy.getHttpConnectionToCache());
|
||||
|
||||
initContentStream(cachedResponseBody);
|
||||
return;
|
||||
} else {
|
||||
Util.closeQuietly(cachedResponseBody);
|
||||
|
||||
@@ -946,8 +946,14 @@ public final class HttpResponseCacheTest {
|
||||
server.enqueue(
|
||||
response.setBody(gzip("ABCABCABC".getBytes("UTF-8"))).addHeader("Content-Encoding: gzip"));
|
||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||
|
||||
server.play();
|
||||
|
||||
// At least three request/response pairs are required because after the first request is cached
|
||||
// a different execution path might be taken. Thus modifications to the cache applied during
|
||||
// the second request might not be visible until another request is performed.
|
||||
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
|
||||
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
|
||||
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user