diff options
author | Harmen Stoppels <harmenstoppels@gmail.com> | 2024-05-01 16:01:47 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-01 16:01:47 (GMT) |
commit | 759e8e7ab83848c527a53d7b2051bc14ac7b7c76 (patch) | |
tree | abf1ce47e94ca537bdb5101ba41acae54721fa97 | |
parent | 49baa656cb994122869bc807a88ea2f3f0d7751b (diff) | |
download | cpython-759e8e7ab83848c527a53d7b2051bc14ac7b7c76.zip cpython-759e8e7ab83848c527a53d7b2051bc14ac7b7c76.tar.gz cpython-759e8e7ab83848c527a53d7b2051bc14ac7b7c76.tar.bz2 |
gh-99730: urllib.request: Keep HEAD method on redirect (GH-99731)
-rw-r--r-- | Lib/test/test_urllib2.py | 9 | ||||
-rw-r--r-- | Lib/urllib/request.py | 1 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-11-23-17-16-31.gh-issue-99730.bDQdaX.rst | 1 |
3 files changed, 11 insertions, 0 deletions
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 6febb49..eed0599 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -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): diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index d22af66..ac6719c 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -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 index 0000000..b595587 --- /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. |