Skip to content
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

Better headers #203

Merged
merged 1 commit into from
Apr 11, 2024
Merged

Better headers #203

merged 1 commit into from
Apr 11, 2024

Conversation

ml31415
Copy link
Collaborator

@ml31415 ml31415 commented Apr 11, 2024

Currently, the original case sensitive header fields are only preserved for the first lines with matching header fields. This is nothing too bad, as the RFC only demands a correct case-insensitive treatment, but it's not nice as well.

This PR now stores all case-sensitive header fields along with the header values. While at the same time it sticks with the principle of handling a single header field without any duplication, as default case with nearly the handling speed of an ordinary python dictionary.

Iterating over all header lines with their original cases is now the default behaviour of Header.items() and Headers.iteroriginal() is deprecated. So as a result, Headers can now be formatted nearly as the plain HTTP hext received over the wire.

>>> h = Headers(asdf="ddd", ASDF="fff", AsDf="asdfasdf")
>>> print(h)
asdf: ddd
ASDF: fff
AsDf: asdfasdf

@cyberw
Copy link
Collaborator

cyberw commented Apr 11, 2024

Cool stuff. Just a question: Does this make us more similar to requests or less?

@ml31415
Copy link
Collaborator Author

ml31415 commented Apr 11, 2024

More. But we're quite similar anyways, as I had contributed a similar headers implementation as our current one to urllib3 ~10 years ago.
urllib3/urllib3@5e73e72

For requests compatibility, I suggest to add some thin front-end to UserAgent, so that it can serve as a drop-in replacement for requests for simple use cases. I had an attempt to use geventhttpclient as backend for requests instead of urllib3, but that didn't achieve much, as requests wasted so much time on top, that it completely defeated the original goal.

…names

Deprecate Headers.iteroriginal() and make case-sensitive iteration the default
behaviour for Header.items() instead
@ml31415 ml31415 merged commit 0a61d0e into master Apr 11, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants