https://github.com/python/cpython/commit/759e8e7ab83848c527a53d7b2051bc14ac7b7c76
commit: 759e8e7ab83848c527a53d7b2051bc14ac7b7c76
branch: main
author: Harmen Stoppels <[email protected]>
committer: encukou <[email protected]>
date: 2024-05-01T18:01:47+02:00
summary:

gh-99730: urllib.request: Keep HEAD method on redirect (GH-99731)

files:
A Misc/NEWS.d/next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst
M Lib/test/test_urllib2.py
M Lib/urllib/request.py

diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index 6febb491788b42..eed0599642edfb 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -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):
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index d22af6618d80f1..ac6719ce854182 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -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)
diff --git 
a/Misc/NEWS.d/next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst 
b/Misc/NEWS.d/next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst
new file mode 100644
index 00000000000000..b5955879c21ce2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst
@@ -0,0 +1 @@
+HEAD requests are no longer upgraded to GET request during redirects in urllib.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to