On a 304 we will get a new set of headers that we want to update our cached value with, assuming we have one. This should only ever be called when we've sent an ETag and gotten a 304 as the response.
(
self, request: PreparedRequest, response: HTTPResponse
)
| 469 | ) |
| 470 | |
| 471 | def update_cached_response( |
| 472 | self, request: PreparedRequest, response: HTTPResponse |
| 473 | ) -> HTTPResponse: |
| 474 | """On a 304 we will get a new set of headers that we want to |
| 475 | update our cached value with, assuming we have one. |
| 476 | |
| 477 | This should only ever be called when we've sent an ETag and |
| 478 | gotten a 304 as the response. |
| 479 | """ |
| 480 | assert request.url is not None |
| 481 | cache_url = self.cache_url(request.url) |
| 482 | cached_response = self._load_from_cache(request) |
| 483 | |
| 484 | if not cached_response: |
| 485 | # we didn't have a cached response |
| 486 | return response |
| 487 | |
| 488 | # Lets update our headers with the headers from the new request: |
| 489 | # http://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-26#section-4.1 |
| 490 | # |
| 491 | # The server isn't supposed to send headers that would make |
| 492 | # the cached body invalid. But... just in case, we'll be sure |
| 493 | # to strip out ones we know that might be problematic due to |
| 494 | # typical assumptions. |
| 495 | excluded_headers = ["content-length"] |
| 496 | |
| 497 | cached_response.headers.update( |
| 498 | { |
| 499 | k: v |
| 500 | for k, v in response.headers.items() |
| 501 | if k.lower() not in excluded_headers |
| 502 | } |
| 503 | ) |
| 504 | |
| 505 | # we want a 200 b/c we have content via the cache |
| 506 | cached_response.status = 200 |
| 507 | |
| 508 | # update our cache |
| 509 | self._cache_set(cache_url, request, cached_response) |
| 510 | |
| 511 | return cached_response |