Skip to content

Commit c379bc5

Browse files
bpo-40321: Support HTTP response status code 308 in urllib.request (#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]>
1 parent 241bda7 commit c379bc5

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
@@ -879,6 +879,12 @@ HTTPRedirectHandler Objects
879879
response.
880880

881881

882+
.. method:: HTTPRedirectHandler.http_error_308(req, fp, code, msg, hdrs)
883+
884+
The same as :meth:`http_error_301`, but called for the 'permanent redirect'
885+
response.
886+
887+
882888
.. _http-cookie-processor:
883889

884890
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
@@ -661,7 +661,7 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
661661
but another Handler might.
662662
"""
663663
m = req.get_method()
664-
if (not (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
664+
if (not (code in (301, 302, 303, 307, 308) and m in ("GET", "HEAD")
665665
or code in (301, 302, 303) and m == "POST")):
666666
raise HTTPError(req.full_url, code, msg, headers, fp)
667667

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

749749
return self.parent.open(new, timeout=req.timeout)
750750

751-
http_error_301 = http_error_303 = http_error_307 = http_error_302
751+
http_error_301 = http_error_303 = http_error_307 = http_error_308 = http_error_302
752752

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

2214+
def http_error_308(self, url, fp, errcode, errmsg, headers, data=None):
2215+
"""Error 308 -- relocated, but turn POST into error."""
2216+
if data is None:
2217+
return self.http_error_301(url, fp, errcode, errmsg, headers, data)
2218+
else:
2219+
return self.http_error_default(url, fp, errcode, errmsg, headers)
2220+
22142221
def http_error_401(self, url, fp, errcode, errmsg, headers, data=None,
22152222
retry=False):
22162223
"""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)