-
Notifications
You must be signed in to change notification settings - Fork 146
cache temporary redirects with explicit caching directives #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cburroughs
wants to merge
1
commit into
psf:master
Choose a base branch
from
cburroughs:csb/explicit-headers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−24
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
|
|
||
| from requests.structures import CaseInsensitiveDict | ||
|
|
||
| from cachecontrol.heuristics import HEURISTICALLY_CACHEABLE_STATUSES | ||
| from cachecontrol.cache import DictCache, SeparateBodyBaseCache | ||
| from cachecontrol.serialize import Serializer | ||
|
|
||
|
|
@@ -33,6 +34,17 @@ | |
|
|
||
| URI = re.compile(r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?") | ||
|
|
||
| NEVER_CACHE_STATUSES = ( | ||
| # Per https://www.rfc-editor.org/rfc/rfc9111.html#section-3 the status | ||
| # code must be final | ||
| 100, | ||
| 101, | ||
| # From httplib2: Don't cache 206's since we aren't going to | ||
| # handle byte range requests | ||
| 206, | ||
| ) | ||
|
|
||
|
|
||
| PERMANENT_REDIRECT_STATUSES = (301, 308) | ||
|
|
||
|
|
||
|
|
@@ -60,7 +72,20 @@ def __init__( | |
| self.cache = DictCache() if cache is None else cache | ||
| self.cache_etags = cache_etags | ||
| self.serializer = serializer or Serializer() | ||
| self.cacheable_status_codes = status_codes or (200, 203, 300, 301, 308) | ||
| # Per https://www.rfc-editor.org/rfc/rfc9111.html#section-3-2.7.1 all | ||
| # all final response codes are potentially cacheable, subject to the | ||
| # other conditions. CacheController conservatively only caches common | ||
| # ones codes. For example, even with a max-age set, a 500 Internal | ||
|
Comment on lines
+77
to
+78
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: what does "common ones" mean here? |
||
| # Server Error will not be cached | ||
| self.cacheable_status_codes = status_codes or ( | ||
| 200, | ||
| 203, | ||
| 300, | ||
| 301, | ||
| 302, | ||
| 307, | ||
| 308, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _urlnorm(cls, uri: str) -> str: | ||
|
|
@@ -128,12 +153,12 @@ def parse_cache_control(self, headers: Mapping[str, str]) -> dict[str, int | Non | |
| except IndexError: | ||
| if required: | ||
| logger.debug( | ||
| "Missing value for cache-control " "directive: %s", | ||
| "Missing value for cache-control directive: %s", | ||
| directive, | ||
| ) | ||
| except ValueError: | ||
| logger.debug( | ||
| "Invalid value for cache-control directive " "%s, must be %s", | ||
| "Invalid value for cache-control directive %s, must be %s", | ||
| directive, | ||
| typ.__name__, | ||
| ) | ||
|
|
@@ -343,12 +368,11 @@ def cache_response( | |
| else: | ||
| response = response_or_ref | ||
|
|
||
| # From httplib2: Don't cache 206's since we aren't going to | ||
| # handle byte range requests | ||
| cacheable_status_codes = status_codes or self.cacheable_status_codes | ||
| if response.status not in cacheable_status_codes: | ||
| if response.status in NEVER_CACHE_STATUSES: | ||
| logger.debug( | ||
| "Status code %s not in %s", response.status, cacheable_status_codes | ||
| "Status code %s in never cache set %s", | ||
| response.status, | ||
| NEVER_CACHE_STATUSES, | ||
| ) | ||
| return | ||
|
|
||
|
|
@@ -378,6 +402,30 @@ def cache_response( | |
| cc_req = self.parse_cache_control(request.headers) | ||
| cc = self.parse_cache_control(response_headers) | ||
|
|
||
| # "at least one of the following" from | ||
| # https://www.rfc-editor.org/rfc/rfc9111.html#section-3-2.7.1 | ||
| has_explicit_freshness = ( | ||
| "public" in cc or "expires" in response_headers or "max-age" in cc | ||
| # NOTE: s-maxage is also listed in the RFC section, but | ||
| # cache_response() doesn't currently express the concept of shared | ||
| # vs private caching | ||
| ) | ||
| cacheable_status_codes = status_codes or self.cacheable_status_codes | ||
| if response.status not in cacheable_status_codes: | ||
| logger.debug( | ||
| "Status code %s not in %s", response.status, cacheable_status_codes | ||
| ) | ||
| return | ||
| if ( | ||
| response.status not in HEURISTICALLY_CACHEABLE_STATUSES | ||
| and not has_explicit_freshness | ||
| ): | ||
| logger.debug( | ||
| "Status code %s is not heuristically cacheable and no explicit caching headers are set", | ||
| response.status, | ||
| ) | ||
| return | ||
|
|
||
| assert request.url is not None | ||
| cache_url = self.cache_url(request.url) | ||
| logger.debug('Updating cache with response from "%s"', cache_url) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: let's make this and the other constants introduced by this PR private, since we're not committing to them as new public APIs 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(We do already have some public constants, which is unfortunate. So I'm not 100% opposed to making these public if you have an argument for it, but by default I'd prefer not to make the public API surface any bigger.)