Skip to content

Commit 15e6e21

Browse files
jschulenkloppermiss-islington
authored andcommitted
bpo-40321: Support HTTP response status code 308 in urllib.request (pythonGH-19588)
* 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). * Update documentation to include http_error_308() * Add blurb for bpo-40321 fix Co-authored-by: Roland Crosby <[email protected]> (cherry picked from commit c379bc5) Co-authored-by: Jochem Schulenklopper <[email protected]>
1 parent 1374459 commit 15e6e21

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Doc/library/urllib.request.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,12 @@ HTTPRedirectHandler Objects
874874
response.
875875

876876

877+
.. method:: HTTPRedirectHandler.http_error_308(req, fp, code, msg, hdrs)
878+
879+
The same as :meth:`http_error_301`, but called for the 'permanent redirect'
880+
response.
881+
882+
877883
.. _http-cookie-processor:
878884

879885
HTTPCookieProcessor Objects

Lib/urllib/request.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
Handlers needed to open the requested URL. For example, the
1212
HTTPHandler performs HTTP GET and POST requests and deals with
1313
non-error returns. The HTTPRedirectHandler automatically deals with
14-
HTTP 301, 302, 303 and 307 redirect errors, and the HTTPDigestAuthHandler
15-
deals with digest authentication.
14+
HTTP 301, 302, 303, 307 and 308 redirect errors, and the
15+
HTTPDigestAuthHandler deals with digest authentication.
1616
1717
urlopen(url, data=None) -- Basic usage is the same as original
1818
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):
659659
but another Handler might.
660660
"""
661661
m = req.get_method()
662-
if (not (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
662+
if (not (code in (301, 302, 303, 307, 308) and m in ("GET", "HEAD")
663663
or code in (301, 302, 303) and m == "POST")):
664664
raise HTTPError(req.full_url, code, msg, headers, fp)
665665

@@ -746,7 +746,7 @@ def http_error_302(self, req, fp, code, msg, headers):
746746

747747
return self.parent.open(new, timeout=req.timeout)
748748

749-
http_error_301 = http_error_303 = http_error_307 = http_error_302
749+
http_error_301 = http_error_303 = http_error_307 = http_error_308 = http_error_302
750750

751751
inf_msg = "The HTTP server returned a redirect error that would " \
752752
"lead to an infinite loop.\n" \
@@ -2209,6 +2209,13 @@ def http_error_307(self, url, fp, errcode, errmsg, headers, data=None):
22092209
else:
22102210
return self.http_error_default(url, fp, errcode, errmsg, headers)
22112211

2212+
def http_error_308(self, url, fp, errcode, errmsg, headers, data=None):
2213+
"""Error 308 -- relocated, but turn POST into error."""
2214+
if data is None:
2215+
return self.http_error_301(url, fp, errcode, errmsg, headers, data)
2216+
else:
2217+
return self.http_error_default(url, fp, errcode, errmsg, headers)
2218+
22122219
def http_error_401(self, url, fp, errcode, errmsg, headers, data=None,
22132220
retry=False):
22142221
"""Error 401 -- authentication required.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Adds support for HTTP 308 redirects to :mod:`urllib`. Patch by Jochem
2+
Schulenklopper.

0 commit comments

Comments
 (0)