From 13ac4a9919e8305924a4ef7046e31f0b18144c8e Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Wed, 23 Nov 2022 17:53:58 +0100 Subject: [PATCH 1/4] HEAD requests be HEAD requests upon redirect --- Lib/urllib/request.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 278aa3a14bfeea..5f3009186f3893 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -679,6 +679,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) From 8cd2bd70bf679cb533429a9d59909692401ee2fe Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Wed, 23 Nov 2022 18:12:08 +0100 Subject: [PATCH 2/4] test --- Lib/test/test_urllib2.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 28f88412fdcaac..18777c4f1b6141 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1361,6 +1361,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, "Permanently Moved", {}, 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): From 122c8db90d158279e41a164fcdd8dc9a33684188 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 23 Nov 2022 17:16:32 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst 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. From a74f76994b948d72bb92f6e11ef81603e884d75c Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 29 Apr 2024 16:28:42 +0200 Subject: [PATCH 4/4] Fix HTTP status description --- Lib/test/test_urllib2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 7485a4f9359539..4a3de1818d7a15 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1408,7 +1408,7 @@ def test_redirect_head_request(self): h = urllib.request.HTTPRedirectHandler() req = Request(from_url, method="HEAD") fp = MockFile() - new_req = h.redirect_request(req, fp, 302, "Permanently Moved", {}, to_url) + new_req = h.redirect_request(req, fp, 302, "Found", {}, to_url) self.assertEqual(new_req.get_method(), "HEAD") def test_proxy(self):