Skip to content

aiohttp: Match response header names case-insensitively. - #1146

Merged
dpgeorge merged 1 commit into
micropython:masterfrom
pablogventura:aiohttp-case-insensitive-headers
Aug 10, 2026
Merged

aiohttp: Match response header names case-insensitively.#1146
dpgeorge merged 1 commit into
micropython:masterfrom
pablogventura:aiohttp-case-insensitive-headers

Conversation

@pablogventura

Copy link
Copy Markdown
Contributor

Summary

Fixes #1126. Response header field names are case-insensitive (RFC 9110
Sec. 5.1), but ClientSession._request matched Transfer-Encoding and
Location with fixed capitalization via startswith. Servers that send
transfer-encoding: or location: would skip chunked detection or
redirects.

Match those names with line.lower().startswith(...), same idea as the
sketch on the issue and prior art in #523 (for old urequests, superseded
after the rename to requests). The Location value is still taken from
the original line so URL case is preserved.

Testing

  • mpy-cross on aiohttp/__init__.py (compiled as __init__.py):
    • master: 3787 bytes
    • this PR: 3796 bytes (+9)
  • No automated aiohttp package test in CI yet; change is two comparisons in
    the header parse loop.

Trade-offs and Alternatives

  • aiohttp-only. The same wire-parse pattern exists in requests and can be
    a small follow-up; left out here to keep the diff minimal and avoid
    overlapping the open chunked-response work.
  • Outgoing request header dict case-insensitivity (e.g. a _Headers
    helper) is a larger change and out of scope for this issue.

Generative AI

I used generative AI tools when creating this PR, but a human has checked the
code and is responsible for the code and the description above.

@dpgeorge

dpgeorge commented Aug 6, 2026

Copy link
Copy Markdown
Member

see related #523

@pablogventura

Copy link
Copy Markdown
Contributor Author

Thanks. #523 looks like the same wire-parse issue in requests
(Transfer-Encoding / Location matched case-sensitively).

Would you rather I fold that fix into this PR, or keep a separate
small PR for requests? (If separate, I'd wait until #1135 merges so
it does not collide with the chunked work on the same header loop.)

@dpgeorge

dpgeorge commented Aug 7, 2026

Copy link
Copy Markdown
Member

I think we can just merge #523 (after resolving merge conflicts). And then this PR is independent.

But, would be good to optimize this one to only call .lower() once, per #523.

@pablogventura
pablogventura force-pushed the aiohttp-case-insensitive-headers branch from cdb5cf2 to 8678eea Compare August 10, 2026 02:16
@pablogventura

Copy link
Copy Markdown
Contributor Author

Thanks. Applied in 8678eea: one lowerl = line.lower() per header line, same pattern as #523.

__init__.mpy (mpy-cross, compiled as __init__.py):

Version bytes vs master
master 3787 -
two .lower() calls 3796 +9
8678eea (lowerl once) 3803 +16

I'll put the requests side in a separate PR (current requests, post-#1135) rather than rebasing #523, since that PR still targets the old urequests path.

@dpgeorge dpgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good.

Response header field names are case-insensitive (RFC 9110 Sec. 5.1), but
ClientSession._request matched Transfer-Encoding and Location with fixed
capitalization via startswith.  Servers that send transfer-encoding: or
location: would skip chunked detection or redirects.

Fixes issue micropython#1126.

Signed-off-by: Pablo Ventura <pablogventura@gmail.com>
@dpgeorge
dpgeorge force-pushed the aiohttp-case-insensitive-headers branch from 8678eea to 220ca9d Compare August 10, 2026 05:11
@dpgeorge
dpgeorge merged commit 220ca9d into micropython:master Aug 10, 2026
5 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.

aiohttp module case-insensitive headers

2 participants