Skip to content

Commit 859a038

Browse files
garenchanbdarnell
authored andcommitted
_HTTPConnection: check location on _should_follow_redirect() and retain safe request when following redirects (#2409)
1 parent 0b2b055 commit 859a038

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

tornado/simple_httpclient.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def _on_timeout(self, key, info=None):
210210

211211
class _HTTPConnection(httputil.HTTPMessageDelegate):
212212
_SUPPORTED_METHODS = set(["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
213+
_SAFE_METHODS = set(["GET", "HEAD", "OPTIONS"])
213214

214215
def __init__(self, client, request, release_callback,
215216
final_callback, max_buffer_size, tcp_client,
@@ -496,7 +497,8 @@ def headers_received(self, first_line, headers):
496497
def _should_follow_redirect(self):
497498
return (self.request.follow_redirects and
498499
self.request.max_redirects > 0 and
499-
self.code in (301, 302, 303, 307, 308))
500+
self.code in (301, 302, 303, 307, 308) and
501+
self.headers.get("Location") is not None)
500502

501503
def finish(self):
502504
data = b''.join(self.chunks)
@@ -517,8 +519,9 @@ def finish(self):
517519
# treat 302 the same as 303, and many servers use 302 for
518520
# compatibility with pre-HTTP/1.1 user agents which don't
519521
# understand the 303 status.
520-
if self.code in (302, 303):
521-
new_request.method = "GET"
522+
if self.code in (301, 302, 303):
523+
if self.request.method not in self._SAFE_METHODS:
524+
new_request.method = "GET"
522525
new_request.body = None
523526
for h in ["Content-Length", "Content-Type",
524527
"Content-Encoding", "Transfer-Encoding"]:

0 commit comments

Comments
 (0)