]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Revert "Revert "_HTTPConnection: check location on _should_follow_redirect() and...
authorBen Darnell <ben@bendarnell.com>
Wed, 11 Jul 2018 02:32:57 +0000 (22:32 -0400)
committerBen Darnell <ben@bendarnell.com>
Tue, 1 Jan 2019 16:44:04 +0000 (11:44 -0500)
This reverts commit dda77f2ca3365bd031a0c56dd19c8ed781a0695a.

tornado/simple_httpclient.py
tornado/test/httpclient_test.py

index 81a0fd24760e4446bce78135531b95848ffb6f21..6af9dd15c3e9060a3fd22b765790e1d1663583a3 100644 (file)
@@ -608,6 +608,8 @@ class _HTTPConnection(httputil.HTTPMessageDelegate):
             return (
                 self.code in (301, 302, 303, 307, 308)
                 and self.request.max_redirects > 0
+                and self.headers is not None
+                and self.headers.get("Location") is not None
             )
         return False
 
index ca7fb5a9d9afc5c2cdb7be4e67a581a0c5dc67f3..29319c743a7b32ff0520d940fadf221c2e9a67c7 100644 (file)
@@ -62,6 +62,13 @@ class RedirectHandler(RequestHandler):
         )
 
 
+class RedirectWithoutLocationHandler(RequestHandler):
+    def prepare(self):
+        # For testing error handling of a redirect with no location header.
+        self.set_status(301)
+        self.finish()
+
+
 class ChunkHandler(RequestHandler):
     @gen.coroutine
     def get(self):
@@ -143,6 +150,7 @@ class HTTPClientCommonTestCase(AsyncHTTPTestCase):
                 url("/post", PostHandler),
                 url("/put", PutHandler),
                 url("/redirect", RedirectHandler),
+                url("/redirect_without_location", RedirectWithoutLocationHandler),
                 url("/chunk", ChunkHandler),
                 url("/auth", AuthHandler),
                 url("/countdown/([0-9]+)", CountdownHandler, name="countdown"),
@@ -291,6 +299,14 @@ Transfer-Encoding: chunked
         self.assertTrue(response.effective_url.endswith("/countdown/0"))
         self.assertEqual(b"Zero", response.body)
 
+    def test_redirect_without_location(self):
+        response = self.fetch("/redirect_without_location", follow_redirects=True)
+        # If there is no location header, the redirect response should
+        # just be returned as-is. (This should arguably raise an
+        # error, but libcurl doesn't treat this as an error, so we
+        # don't either).
+        self.assertEqual(301, response.code)
+
     def test_credentials_in_url(self):
         url = self.get_url("/auth").replace("http://", "http://me:secret@")
         response = self.fetch(url)