Skip to content

gh-47005: fix do_open() to let regular headers override unredirected …#146506

Open
Das-Chinmay wants to merge 4 commits intopython:mainfrom
Das-Chinmay:fix-urllib-header-priority
Open

gh-47005: fix do_open() to let regular headers override unredirected …#146506
Das-Chinmay wants to merge 4 commits intopython:mainfrom
Das-Chinmay:fix-urllib-header-priority

Conversation

@Das-Chinmay
Copy link

AbstractHTTPHandler.do_open() builds the outgoing header dict by starting
with unredirected_hdrs and only merging regular headers that are not
already present — giving unredirected headers the higher priority.

This contradicts both get_header() (checks self.headers first) and
header_items() ({**unredirected_hdrs, **headers}), which both give
regular headers priority. The result: a header set via add_header() is
silently ignored if the same name was already set via add_unredirected_header().

Fix: replace the conditional merge with an unconditional headers.update(req.headers).

Fixes gh-47005

…ected headers

AbstractHTTPHandler.do_open() was building the request header dict by
starting with unredirected_hdrs and only inserting regular headers that
were not already present, giving unredirected headers priority.  This
contradicts get_header() and header_items(), both of which give regular
headers the higher priority.

Fix by unconditionally updating with req.headers so that a header set
via add_header() always overrides one set via add_unredirected_header().
@bedevere-app
Copy link

bedevere-app bot commented Mar 27, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

urllib.request.add_header fails with existing unredirected_header

1 participant