Skip to content

Commit 759e8e7

Browse files
authored
gh-99730: urllib.request: Keep HEAD method on redirect (GH-99731)
1 parent 49baa65 commit 759e8e7

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_urllib2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,15 @@ def http_open(self, req):
14021402
request = handler.last_buf
14031403
self.assertTrue(request.startswith(expected), repr(request))
14041404

1405+
def test_redirect_head_request(self):
1406+
from_url = "http://example.com/a.html"
1407+
to_url = "http://example.com/b.html"
1408+
h = urllib.request.HTTPRedirectHandler()
1409+
req = Request(from_url, method="HEAD")
1410+
fp = MockFile()
1411+
new_req = h.redirect_request(req, fp, 302, "Found", {}, to_url)
1412+
self.assertEqual(new_req.get_method(), "HEAD")
1413+
14051414
def test_proxy(self):
14061415
u = "proxy.example.com:3128"
14071416
for d in dict(http=u), dict(HTTP=u):

Lib/urllib/request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
650650
newheaders = {k: v for k, v in req.headers.items()
651651
if k.lower() not in CONTENT_HEADERS}
652652
return Request(newurl,
653+
method="HEAD" if m == "HEAD" else "GET",
653654
headers=newheaders,
654655
origin_req_host=req.origin_req_host,
655656
unverifiable=True)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HEAD requests are no longer upgraded to GET request during redirects in urllib.

0 commit comments

Comments
 (0)