Skip to content

Change headers to a dict that parses comma-separated values - #7679

Merged
Dreamsorcerer merged 95 commits into
masterfrom
list-headers
May 10, 2026
Merged

Dreamsorcerer merged 95 commits into
masterfrom
list-headers

Conversation

@Dreamsorcerer

@Dreamsorcerer Dreamsorcerer commented Oct 8, 2023 •

Copy link
Copy Markdown
Member

This is a proposal to change the headers from a CIMultiDict to a more regular dict (in v4). The problem with the multidict approach is that list headers (i.e. headers that can have multiple values) can have values combined in single headers and/or split over multiple headers.


Basically, these 2 payloads should be considered equivalent:

Foo: 1
Foo: 2
Foo: 1, 2

But, currently aiohttp will produce in the first case:

headers["Foo"]  # "1"
headers.getall("Foo")  # ["1", "2"]

And, in the second case:

headers["Foo"]  # "1, 2"
headers.getall("Foo")  # ["1, 2"]

The spec recommends concatenating duplicate headers together with ", ". This is also what the vast majority of existing software does (including requests).

The only problem with this, is that if the user wants the values as a list, they are left to parse the value themselves, which when accounting for quoted values becomes quite complex and easy to get wrong on edge cases. So, in this proposal I've concatenated the values as recommended, but added a .getall() method which parses the final value to get the list.

With the code in this PR, both of the previous payloads produce the same output:

headers["Foo"]  # "1, 2"
headers.getall("Foo")  # ["1", "2"]

"Field value" now refers to the value after multiple field lines are combined with commas -- by far the most common use.
From RFC 9110 appendix B2

From kenballus's testing:

Servers that join the duplicate headers by default: Apache httpd, Caddy, Gunicorn, H2O, IIS, Lighttpd, Nginx, Node.js, Puma.
Servers that accept the duplicate headers without joining them: aiohttp (currently), Boost::Beast, Mongoose, Tornado

I've also seen that requests combines into a regular dict. The only other library I've seen that uses a multidict for this is Starlette.

Also: https://www.rfc-editor.org/rfc/rfc9110.html#name-recipient-requirements

@Dreamsorcerer Dreamsorcerer added the backport:skip Skip backport bot label Oct 8, 2023
@Dreamsorcerer Dreamsorcerer added this to the 4.0 milestone Oct 8, 2023
@steverep

Copy link
Copy Markdown
Member

Some questions to consider here:

  1. Should the getAll list be converted to lowercase when the values are case-insensitive (which is true for most if not all I think)?
  2. If yes, should the list then also be de-duplicated?
  3. If yes, should content negotiation headers that can have quality values per RFC 9110 be parsed and assigned, e.g. by returning a dictionary instead of a list or tuple as {"<value>": <quality>, ...}?

I think all of these being done directly in the parsers is best for performance, and would make for easier and less error-prone usage.

Comment thread aiohttp/http_parser.py Resolved
@Dreamsorcerer

Copy link
Copy Markdown
Member Author

Some questions to consider here:

If you've got any information in the specs to answer those questions, that'd be great to have.

@steverep

steverep commented Feb 3, 2024

Copy link
Copy Markdown
Member

If you've got any information in the specs to answer those questions, that'd be great to have.

Just by their nature, I think it's certainly safe to deduplicate the content negotiation fields defined in Section 12.5 of RFC 9110. However, AFAICT, the RFC has no guidance on what quality value to assign if the duplicates happen to disagree. Seems like server's choice in that edge case would be conformant.

Other list headers should not be deduplicated because duplicates can actually mean something. For example, Content-Encoding: "gzip, gzip" means the content was double compressed with gzip.

@Dreamsorcerer

Copy link
Copy Markdown
Member Author

OK, now having time to think over this, I think it's all a level above what we should be doing here. I think we're just providing a list based on the definition of a general HTTP field. So, I feel the answer is no to all 3 questions. Maintaining logic for all the different kind of headers seems out of scope to me (and if we did, it'd be through dedicated attributes, like cookies).

@steverep

steverep commented Feb 4, 2024

Copy link
Copy Markdown
Member

OK, now having time to think over this, I think it's all a level above what we should be doing here. I think we're just providing a list based on the definition of a general HTTP field. So, I feel the answer is no to all 3 questions. Maintaining logic for all the different kind of headers seems out of scope to me (and if we did, it'd be through dedicated attributes, like cookies).

After reading the spec a bit more, I guess I agree with you on the first two, but parameters are actually generically defined in section 5.6.6. The syntax and case-insensitivity of parameter names is defined there (the content negotiation headers just happen to use "q" as the name). I think the parsers should provide a way to access them as a dictionary (maybe by returning a list subclass?).

Comment thread tests/test_http_parser.py Resolved
@codecov

codecov Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.94%. Comparing base (5a8ebdf) to head (396ec93).
⚠️ Report is 709 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7679   +/-   ##
=======================================
  Coverage   98.94%   98.94%           
=======================================
  Files         131      131           
  Lines       46505    46547   +42     
  Branches     2407     2408    +1     
=======================================
+ Hits        46015    46057   +42     
  Misses        367      367           
  Partials      123      123           
Flag Coverage Δ
Autobahn 22.43% <24.78%> (+0.02%) ⬆️
CI-GHA 98.92% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.67% <100.00%> (-0.01%) ⬇️
OS-Windows 97.03% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.93% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.42% <99.14%> (+<0.01%) ⬆️
Py-3.10.20 97.91% <99.14%> (+<0.01%) ⬆️
Py-3.11.15 98.16% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 97.68% <100.00%> (-0.01%) ⬇️
Py-3.12.10 97.77% <100.00%> (+<0.01%) ⬆️
Py-3.12.13 98.25% <100.00%> (+<0.01%) ⬆️
Py-3.13.13 98.48% <100.00%> (-0.01%) ⬇️
Py-3.14.4 98.45% <100.00%> (+<0.01%) ⬆️
Py-3.14.4t 97.56% <100.00%> (+<0.01%) ⬆️
Py-pypy3.11.15-7.3.21 97.42% <100.00%> (+<0.01%) ⬆️
VM-macos 97.93% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.67% <100.00%> (-0.01%) ⬇️
VM-windows 97.03% <100.00%> (+<0.01%) ⬆️
cython-coverage 37.99% <38.65%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Comment thread aiohttp/_http_parser.pyx Resolved
Comment thread aiohttp/http_parser.py Resolved
Comment thread aiohttp/http_parser.py Resolved
Comment thread tests/test_http_parser.py Resolved
@Dreamsorcerer

Copy link
Copy Markdown
Member Author

As nobody has given any reason not to do this, I think we can continue with finishing this PR now.

@Dreamsorcerer
Dreamsorcerer marked this pull request as ready for review March 28, 2026 19:09
@Dreamsorcerer
Dreamsorcerer requested a review from asvetlov as a code owner March 28, 2026 19:09
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Apr 10, 2026
Comment thread aiohttp/helpers.py Resolved
Comment thread aiohttp/helpers.py Resolved
@Dreamsorcerer

Copy link
Copy Markdown
Member Author

This should be ready to merge now, if anyone would like to do a final review.

Comment thread aiohttp/helpers.py Resolved
@Dreamsorcerer
Dreamsorcerer merged commit e8f4371 into master May 10, 2026
44 checks passed
@Dreamsorcerer
Dreamsorcerer deleted the list-headers branch May 10, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:skip Skip backport bot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants