Skip to content

Commit

Permalink
pythongh-99730: urllib.request: Keep HEAD method on redirect (pythonG…
Browse files Browse the repository at this point in the history
  • Loading branch information
haampie authored May 1, 2024
1 parent 49baa65 commit 759e8e7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
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.

0 comments on commit 759e8e7

Please sign in to comment.