Skip to content

Commit 162f081

Browse files
committed
Patch 549151, rev4: redirect posts for 301 also. Will backport to 2.2.
1 parent a685f52 commit 162f081

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

Doc/lib/liburllib.tex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ \section{\module{urllib} ---
257257

258258
\begin{classdesc}{FancyURLopener}{...}
259259
\class{FancyURLopener} subclasses \class{URLopener} providing default
260-
handling for the following HTTP response codes: 301, 302, 303 and 401.
261-
For 301, 302 and 303 response codes, the \mailheader{Location} header
262-
is used to fetch the actual URL. For 401 response codes
263-
(authentication required), basic HTTP authentication is performed.
264-
For 301, 302 and 303 response codes, recursion is bounded by the value
265-
of the \var{maxtries} attribute, which defaults 10.
260+
handling for the following HTTP response codes: 301, 302, 303, 307 and
261+
401. For the 30x response codes listed above, the
262+
\mailheader{Location} header is used to fetch the actual URL. For 401
263+
response codes (authentication required), basic HTTP authentication is
264+
performed. For the 30x response codes, recursion is bounded by the
265+
value of the \var{maxtries} attribute, which defaults to 10.
266266

267267
\note{According to the letter of \rfc{2616}, 301 and 302 responses to
268268
POST requests must not be automatically redirected without

Doc/lib/liburllib2.tex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,11 @@ \subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
424424
if you can't but another \class{Handler} might.
425425

426426
\note{The default implementation of this method does not strictly
427-
follow \rfc{2616}: it allows automatic 302 redirection of POST
428-
requests, because essentially all HTTP clients do this.}
427+
follow \rfc{2616}, which says that 301 and 302 responses to POST
428+
requests must not be automatically redirected without confirmation by
429+
the user. In reality, browsers do allow automatic redirection of
430+
these responses, changing the POST to a GET, and the default
431+
implementation reproduces this behaviour.}
429432

430433
\end{methoddesc}
431434

@@ -446,9 +449,14 @@ \subsection{HTTPRedirectHandler Objects \label{http-redirect-handler}}
446449
\begin{methoddesc}[HTTPRedirectHandler]{http_error_303}{req,
447450
fp, code, msg, hdrs}
448451
The same as \method{http_error_301()}, but called for the
449-
`see other' redirect response.
452+
`see other' response.
450453
\end{methoddesc}
451454

455+
\begin{methoddesc}[HTTPRedirectHandler]{http_error_307}{req,
456+
fp, code, msg, hdrs}
457+
The same as \method{http_error_301()}, but called for the
458+
`temporary redirect' response.
459+
452460
\subsection{ProxyHandler Objects \label{proxy-handler}}
453461

454462
\begin{methoddescni}[ProxyHandler]{\var{protocol}_open}{request}

Lib/urllib2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,9 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
431431
"""
432432
m = req.get_method()
433433
if (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
434-
or code in (302, 303) and m == "POST"):
435-
# Strictly (according to RFC 2616), 302 in response to a
436-
# POST MUST NOT cause a redirection without confirmation
434+
or code in (301, 302, 303) and m == "POST"):
435+
# Strictly (according to RFC 2616), 301 or 302 in response
436+
# to a POST MUST NOT cause a redirection without confirmation
437437
# from the user (of urllib2, in this case). In practice,
438438
# essentially all clients do redirect in this case, so we
439439
# do the same.
@@ -480,9 +480,9 @@ def http_error_302(self, req, fp, code, msg, headers):
480480

481481
http_error_301 = http_error_303 = http_error_307 = http_error_302
482482

483-
inf_msg = "The HTTP server returned a redirect error that would" \
483+
inf_msg = "The HTTP server returned a redirect error that would " \
484484
"lead to an infinite loop.\n" \
485-
"The last 302 error message was:\n"
485+
"The last 30x error message was:\n"
486486

487487
class ProxyHandler(BaseHandler):
488488
# Proxies must be in front

0 commit comments

Comments
 (0)