Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 96
Preserve header casing. Take two. 🎬#104
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
Merged
pgjones
merged 10 commits into
python-hyper:master
from
lovelydinosaur:preserve-header-casing-2Oct 4, 2020
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bd3cb57
Minimal Headers data structure
lovelydinosaur 84a4c0b
Preserve header casing
lovelydinosaur f1c661c
Lower change footprint
lovelydinosaur ce5a6ce
Clean up public API for Headers data structure
lovelydinosaur e00a701
Black formatting
lovelydinosaur 0136171
Update docs, docstrings
lovelydinosaur f1ac89c
Use __slots__ on Headers. Because why not?
lovelydinosaur 25c2ec0
Update docs/source/api.rst
lovelydinosaur c005892
Note on title casing in set_headers
lovelydinosaur 5f77670
Merge branch 'preserve-header-casing-2' of https://github.com/tomchri…
lovelydinosaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -62,6 +62,59 @@ | ||
| _field_value_re = re.compile(field_value.encode("ascii")) | ||
| class Headers: | ||
| """ | ||
| A list-like interface that allows iterating over headers as byte-pairs | ||
| of (lowercased-name, value). | ||
| Internally we actually store the representation as three-tuples, | ||
| including both the raw original casing, in order to preserve casing | ||
| over-the-wire, and the lowercased name, for case-insensitive comparisions. | ||
| r = Request( | ||
| method="GET", | ||
| target="/", | ||
| headers=[("Host", "example.org"), ("Connection", "keep-alive")], | ||
| http_version="1.1", | ||
| ) | ||
| assert r.headers == [ | ||
| (b"host", b"example.org"), | ||
| (b"connection", b"keep-alive") | ||
| ] | ||
| assert r.headers.raw_items() == [ | ||
| (b"Host", b"example.org"), | ||
| (b"Connection", b"keep-alive") | ||
| ] | ||
| """ | ||
| __slots__ = '_full_items' | ||
| def __init__(self, full_items): | ||
| self._full_items = full_items | ||
| def __iter__(self): | ||
| for _, name, value in self._full_items: | ||
| yield name, value | ||
| def __bool__(self): | ||
| return bool(self._full_items) | ||
| def __eq__(self, other): | ||
| return list(self) == list(other) | ||
| def __len__(self): | ||
| return len(self._full_items) | ||
| def __repr__(self): | ||
| return "<Headers(%s)>" % repr(list(self)) | ||
| def __getitem__(self, idx): | ||
| _, name, value = self._full_items[idx] | ||
| return (name, value) | ||
| def raw_items(self): | ||
| return [(raw_name, value) for raw_name, _, value in self._full_items] | ||
| def normalize_and_validate(headers, _parsed=False): | ||
| new_headers = [] | ||
| saw_content_length = False | ||
| @@ -75,6 +128,7 @@ def normalize_and_validate(headers, _parsed=False): | ||
| value = bytesify(value) | ||
| validate(_field_name_re, name, "Illegal header name {!r}", name) | ||
| validate(_field_value_re, value, "Illegal header value {!r}", value) | ||
| raw_name = name | ||
| name = name.lower() | ||
| if name == b"content-length": | ||
| if saw_content_length: | ||
| @@ -99,8 +153,8 @@ def normalize_and_validate(headers, _parsed=False): | ||
| error_status_hint=501, | ||
| ) | ||
| saw_transfer_encoding = True | ||
| new_headers.append((name, value)) | ||
| return new_headers | ||
| new_headers.append((raw_name, name, value)) | ||
| return Headers(new_headers) | ||
| def get_comma_header(headers, name): | ||
| @@ -140,7 +194,7 @@ def get_comma_header(headers, name): | ||
| # "100-continue". Splitting on commas is harmless. Case insensitive. | ||
| # | ||
| out = [] | ||
| for found_name, found_raw_value in headers: | ||
| for _, found_name, found_raw_value in headers._full_items: | ||
| if found_name == name: | ||
| found_raw_value = found_raw_value.lower() | ||
| for found_split_value in found_raw_value.split(b","): | ||
| @@ -152,13 +206,21 @@ def get_comma_header(headers, name): | ||
| def set_comma_header(headers, name, new_values): | ||
| # The header name `name` is expected to be lower-case bytes. | ||
| # | ||
| # Note that when we store the header we use title casing for the header | ||
| # names, in order to match the conventional HTTP header style. | ||
| # | ||
| # Simply calling `.title()` is a blunt approach, but it's correct | ||
| # here given the cases where we're using `set_comma_header`... | ||
| # | ||
| # Connection, Content-Length, Transfer-Encoding. | ||
| new_headers = [] | ||
| for found_name, found_raw_value in headers: | ||
| for found_raw_name, found_name, found_raw_value in headers._full_items: | ||
| if found_name != name: | ||
| new_headers.append((found_name, found_raw_value)) | ||
| new_headers.append((found_raw_name, found_raw_value)) | ||
| for new_value in new_values: | ||
| new_headers.append((name, new_value)) | ||
| headers[:] = normalize_and_validate(new_headers) | ||
| new_headers.append((name.title(), new_value)) | ||
pgjones marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return normalize_and_validate(new_headers) | ||
| def has_expect_100_continue(request): | ||
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
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
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.
Will there be output demonstrating the result?
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.
Yup, see https://h11.readthedocs.io/en/latest/api.html - but I wasn't able to figure out the docs build locally just yet, so I've not seen the rendered docs myself.
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.
You can see the docs rendered for this pull request at https://h11--104.org.readthedocs.build/en/104/
Uh oh!
There was an error while loading. Please reload this page.
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.
Ah neat, thanks @pquentin.
Not 100% obvious what repr we'd want for the
Headersthere:<Headers([(b'host', b'example.org')])>to make it clear it's not (quite) a plain list.[(b'host', b'example.org')]to keep it looking simple.((b'host', b'example.org'),)since it's a non-mutable sequence.Some examples as currently rendered...