Scrapy, a fast high-level web crawling & scraping framework for Python.
Freshen cached responses on successful revalidation (#8068)
Adrian23e3c18c164d
diff --git a/scrapy/downloadermiddlewares/httpcache.py b/scrapy/downloadermiddlewares/httpcache.pyindex 3ebd98027..331543d83 100644--- a/scrapy/downloadermiddlewares/httpcache.py+++ b/scrapy/downloadermiddlewares/httpcache.py@@ -139,6 +139,9 @@ class HttpCacheMiddleware:if self.policy.is_cached_response_valid(cachedresponse, response, request):self.stats.inc_value("httpcache/revalidate")+ if response.status == 304:+ self._freshen_cached_response(cachedresponse, response)+ self._cache_response(cachedresponse, request)return cachedresponseself.stats.inc_value("httpcache/invalidate")@@ -157,6 +160,22 @@ class HttpCacheMiddleware:return cachedresponsereturn None+ def _freshen_cached_response(+ self, cachedresponse: Response, response: Response+ ) -> None:+ # RFC 7234, section 4.3.4: update the stored response with the+ # header fields from a successful revalidation (304) response.+ warnings = [+ warning+ for warning in cachedresponse.headers.getlist(b"Warning")+ if warning.split(None, 1)[0].startswith(b"2")+ ]+ cachedresponse.headers.update(response.headers)+ if warnings:+ cachedresponse.headers[b"Warning"] = warnings+ else:+ cachedresponse.headers.pop(b"Warning", None)+def _cache_response(self, response: Response, request: Request) -> None:if self.policy.should_cache_response(response, request):self.stats.inc_value("httpcache/store")diff --git a/tests/test_downloadermiddleware_httpcache.py b/tests/test_downloadermiddleware_httpcache.pyindex 75affe343..c48f77ea5 100644--- a/tests/test_downloadermiddleware_httpcache.py+++ b/tests/test_downloadermiddleware_httpcache.py@@ -614,6 +614,35 @@ class RFC2616PolicyTestMixin(PolicyTestMixin):req3 = req0.replace(headers={"Cache-Control": "max-stale=soon"})assert mw.process_request(req3) is None+ def test_freshen_response_on_revalidation(self):+ # A response successfully revalidated (304) must have its Date+ # header, and thus its age, refreshed in the cache.+ headers = {+ "Date": self.yesterday,+ "Cache-Control": "max-age=60",+ "Last-Modified": self.yesterday,+ "Warning": ["110 - old", "214 - keep"],+ }+ with self._middleware() as mw:+ req0 = Request("http://example.com")+ res0 = Response(req0.url, headers=headers)+ self._process_requestresponse(mw, req0, res0)++ # The cached response is stale, so the request must be+ # revalidated with the server.+ assert mw.process_request(req0) is None+ res304 = Response(req0.url, status=304, headers={"Date": self.today})+ res1 = mw.process_response(req0, res304)+ assert res1.headers["Date"] == self.today.encode()+ assert res1.headers["Warning"] == b"214 - keep"++ # The freshened response must now be considered fresh again,+ # without a further round trip to the server.+ res2 = mw.process_request(req0)+ assert isinstance(res2, Response)+ assert "cached" in res2.flags+ assert res2.headers["Date"] == self.today.encode()+def test_response_dated_in_the_future(self):# A Date header ahead of the local clock must not make the cached# response look aged.