]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99730: urllib.request: Keep HEAD method on redirect (GH-99731)
authorHarmen Stoppels <harmenstoppels@gmail.com>
Wed, 1 May 2024 16:01:47 +0000 (18:01 +0200)
committerGitHub <noreply@github.com>
Wed, 1 May 2024 16:01:47 +0000 (18:01 +0200)
Lib/test/test_urllib2.py
Lib/urllib/request.py
Misc/NEWS.d/next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst [new file with mode: 0644]

index 6febb491788b42894d5714cee1800918ed5caaba..eed0599642edfb2fe2572fad8f49bf3a60fe19dd 100644 (file)
@@ -1402,6 +1402,15 @@ class HandlerTests(unittest.TestCase):
                 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):
index d22af6618d80f168224a6bc5a31bdb8fe8a9573e..ac6719ce854182476a0cfa164a8f6aad2901e516 100644 (file)
@@ -650,6 +650,7 @@ class HTTPRedirectHandler(BaseHandler):
         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 (file)
index 0000000..b595587
--- /dev/null
@@ -0,0 +1 @@
+HEAD requests are no longer upgraded to GET request during redirects in urllib.