From 89027c4e2c592a6fe2c0bc500475a81c15ee0181 Mon Sep 17 00:00:00 2001 From: Jochem Schulenklopper Date: Sat, 18 Apr 2020 19:42:32 +0200 Subject: [PATCH 1/3] Support HTTP response status code 308 in urllib. HTTP response status code 308 is defined in https://tools.ietf.org/html/rfc7538 to be the permanent redirect variant of 307 (temporary redirect). --- Lib/urllib/request.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 2a3d71554f4bfe..34898966a8602f 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -11,8 +11,8 @@ Handlers needed to open the requested URL. For example, the HTTPHandler performs HTTP GET and POST requests and deals with non-error returns. The HTTPRedirectHandler automatically deals with -HTTP 301, 302, 303 and 307 redirect errors, and the HTTPDigestAuthHandler -deals with digest authentication. +HTTP 301, 302, 303, 307 and 308 redirect errors, and the +HTTPDigestAuthHandler deals with digest authentication. urlopen(url, data=None) -- Basic usage is the same as original urllib. pass the url and optionally data to post to an HTTP URL, and @@ -659,7 +659,7 @@ def redirect_request(self, req, fp, code, msg, headers, newurl): but another Handler might. """ m = req.get_method() - if (not (code in (301, 302, 303, 307) and m in ("GET", "HEAD") + if (not (code in (301, 302, 303, 307, 308) and m in ("GET", "HEAD") or code in (301, 302, 303) and m == "POST")): raise HTTPError(req.full_url, code, msg, headers, fp) @@ -746,7 +746,7 @@ def http_error_302(self, req, fp, code, msg, headers): return self.parent.open(new, timeout=req.timeout) - http_error_301 = http_error_303 = http_error_307 = http_error_302 + http_error_301 = http_error_303 = http_error_307 = http_error_308 = http_error_302 inf_msg = "The HTTP server returned a redirect error that would " \ "lead to an infinite loop.\n" \ @@ -2205,6 +2205,13 @@ def http_error_307(self, url, fp, errcode, errmsg, headers, data=None): else: return self.http_error_default(url, fp, errcode, errmsg, headers) + def http_error_308(self, url, fp, errcode, errmsg, headers, data=None): + """Error 308 -- relocated, but turn POST into error.""" + if data is None: + return self.http_error_301(url, fp, errcode, errmsg, headers, data) + else: + return self.http_error_default(url, fp, errcode, errmsg, headers) + def http_error_401(self, url, fp, errcode, errmsg, headers, data=None, retry=False): """Error 401 -- authentication required. From 4ccfa0229a31e5fe4dcc114d9305c44b2960d792 Mon Sep 17 00:00:00 2001 From: Jochem Schulenklopper Date: Sat, 18 Apr 2020 19:54:18 +0200 Subject: [PATCH 2/3] Update documentation to include http_error_308() --- Doc/library/urllib.request.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 03712c1f4a6eea..2988ba2ecec14d 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -874,6 +874,12 @@ HTTPRedirectHandler Objects response. +.. method:: HTTPRedirectHandler.http_error_308(req, fp, code, msg, hdrs) + + The same as :meth:`http_error_301`, but called for the 'permanent redirect' + response. + + .. _http-cookie-processor: HTTPCookieProcessor Objects From bcabc02760594cc58a941d7711f3915b6540702a Mon Sep 17 00:00:00 2001 From: Roland Crosby Date: Thu, 22 Jul 2021 21:28:04 -0400 Subject: [PATCH 3/3] Add blurb for bpo-40321 fix --- .../next/Library/2021-07-22-21-25-56.bpo-40321.gBlFmw.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-07-22-21-25-56.bpo-40321.gBlFmw.rst diff --git a/Misc/NEWS.d/next/Library/2021-07-22-21-25-56.bpo-40321.gBlFmw.rst b/Misc/NEWS.d/next/Library/2021-07-22-21-25-56.bpo-40321.gBlFmw.rst new file mode 100644 index 00000000000000..1a7dba249c7db0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-07-22-21-25-56.bpo-40321.gBlFmw.rst @@ -0,0 +1,2 @@ +Adds support for HTTP 308 redirects to :mod:`urllib`. Patch by Jochem +Schulenklopper.