feat(cache)!: normalise the host in HTTP cache keys - #407
Merged
Merged
Conversation
Hostnames are case-insensitive, and an empty port or the scheme's default one names the same origin as no port. The host component is now lower-cased and such a port dropped (`:80` on http/ws, `:443` on https/wss, read from the ASGI scope's scheme), so `Example.com`, `example.com:80` and `example.com` share one entry. IPv6 literals keep their brackets; malformed hosts are kept as sent. BREAKING CHANGE: keys for hosts written with upper case or a default port change, so those entries are cached afresh once, and `clear_pattern()` patterns naming such a host no longer match. Closes #265
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Third of the 0.4.0 cache-key changes, after #405 and #406.
What changes
The host component of an HTTP cache key is normalised, so every spelling of one origin shares an entry:
:80on http/ws,:443on https/wss; RFC 3986 §6.2.3). The scheme is read from the ASGI scope, which is also whatrequest.urluses;request.url.schemeitself comes back empty for some malformed hosts;[fe80::1]:8000);Hoststill givesunknown, and a malformed host (unbracketed IPv6,:80,host:80:80) is kept as sent.@cache,build_cache_key(),invalidate()and the monitoring routes all go throughCacheKey.from_request, so they agree.clear_path()wildcards the host.Docs
HTTP_CACHING.mdandCACHE_FLOW.md(English and zh-TW) describe the rules, including that behind a TLS-terminating proxy the scheme ishttpunless the proxy's headers are applied.MIGRATING_0_4.mdstates the rule precisely and notes thatclear_pattern()patterns naming an upper-case host or a default port need rewriting.changelog.d/265.changed.md.Tests
tests/test_host_normalization.py:EXAMPLE.com,example.com:80andexample.comhit one entry;invalidate()finding the entry under another spelling.Closes #265