Skip to content

aiohttp: Send the correct Host header when following a redirect. - #1171

Open
FuNK3Y wants to merge 1 commit into
micropython:masterfrom
FuNK3Y:aiohttp-redirect-host
Open

FuNK3Y wants to merge 1 commit into
micropython:masterfrom
FuNK3Y:aiohttp-redirect-host

Conversation

@FuNK3Y

@FuNK3Y FuNK3Y commented Oct 27, 2026 •

Copy link
Copy Markdown

Summary

When ClientSession follows a 301/302/303 redirect to a different host, the redirected request is sent with the Host header of the original URL.

request_raw() adds Host to the headers dict it receives, and _request() passes the same dict again for every redirect. Because of the if "Host" not in headers check, the stale value is kept. This change makes request_raw() work on a copy of the headers, so the caller's dict stays unchanged.

I faced the problem when trying to download a release from a GitHub repo.

Testing

I tested on CPython with asyncio.open_connection stubbed and a CPython-compatible copy of the module (bytes % formatting with str arguments only works on MicroPython). The stub returns 302 Location: http://b.example/x for a.example:

  • Before: GET /x was sent with Host: a.example
  • After: GET /x is sent with Host: b.example

I did not test on a MicroPython port or board.

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.

🤖 Generated with Claude Code

@FuNK3Y
FuNK3Y force-pushed the aiohttp-redirect-host branch from 3e90d21 to 67224fe Compare October 27, 2026 07:06
request_raw() added the Host header to the headers dict it was given,
which is the same dict _request() passes again for each redirect.  The
Host of the original URL was therefore still present when following a
redirect to another host, and was sent to that host instead of its own
name.

Work on a copy of the headers in request_raw() so the caller's dict is
left unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: FuNK3Y <fun__key@hotmail.com>
@FuNK3Y
FuNK3Y force-pushed the aiohttp-redirect-host branch from 67224fe to 09a943d Compare October 27, 2026 07:08
@FuNK3Y
FuNK3Y marked this pull request as ready for review October 27, 2026 07:09
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.

1 participant