Skip to content

gh-99730: HEAD should remain HEAD request on redirect #99731

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

Merged
merged 6 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,15 @@ def http_open(self, req):
request = handler.last_buf
self.assertTrue(request.startswith(expected), repr(request))

def test_redirect_head_request(self):
from_url = "http://example.com/a.html"
to_url = "http://example.com/b.html"
h = urllib.request.HTTPRedirectHandler()
req = Request(from_url, method="HEAD")
fp = MockFile()
new_req = h.redirect_request(req, fp, 302, "Found", {}, to_url)
self.assertEqual(new_req.get_method(), "HEAD")

def test_proxy(self):
u = "proxy.example.com:3128"
for d in dict(http=u), dict(HTTP=u):
Expand Down
1 change: 1 addition & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
newheaders = {k: v for k, v in req.headers.items()
if k.lower() not in CONTENT_HEADERS}
return Request(newurl,
method="HEAD" if m == "HEAD" else "GET",
headers=newheaders,
origin_req_host=req.origin_req_host,
unverifiable=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HEAD requests are no longer upgraded to GET request during redirects in urllib.