]> granicus.if.org Git - python/commitdiff
Issue13696 - Fix 302 Redirection for Relative urls.
authorSenthil Kumaran <senthil@uthcode.com>
Wed, 4 Jan 2012 05:46:59 +0000 (13:46 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Wed, 4 Jan 2012 05:46:59 +0000 (13:46 +0800)
Lib/test/test_urllib2.py
Lib/urllib/request.py
Misc/NEWS

index c94ebf8165ea0c9a3980ff2ba66195b40a2a5dac..3d80e0140b9be1b19229773e9b49686f74a5cf0c 100644 (file)
@@ -1059,6 +1059,19 @@ class HandlerTests(unittest.TestCase):
                 MockHeaders({"location": valid_url}))
             self.assertEqual(o.req.get_full_url(), valid_url)
 
+    def test_relative_redirect(self):
+        from_url = "http://example.com/a.html"
+        relative_url = "/b.html"
+        h = urllib.request.HTTPRedirectHandler()
+        o = h.parent = MockOpener()
+        req = Request(from_url)
+        req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
+
+        valid_url = urllib.parse.urljoin(from_url,relative_url)
+        h.http_error_302(req, MockFile(), 302, "That's fine",
+            MockHeaders({"location": valid_url}))
+        self.assertEqual(o.req.get_full_url(), valid_url)
+
     def test_cookie_redirect(self):
         # cookies shouldn't leak into redirected requests
         from http.cookiejar import CookieJar
index b41dd7eaa82fe56b2cbab6b70717029fbca630f0..77b7c7ffb2fd60fdc116f7144d3ddaebdba189fb 100644 (file)
@@ -552,7 +552,7 @@ class HTTPRedirectHandler(BaseHandler):
         # For security reasons we don't allow redirection to anything other
         # than http, https or ftp.
 
-        if urlparts.scheme not in ('http', 'https', 'ftp'):
+        if urlparts.scheme not in ('http', 'https', 'ftp', ''):
             raise HTTPError(
                 newurl, code,
                 "%s - Redirection to url '%s' is not allowed" % (msg, newurl),
@@ -1935,7 +1935,7 @@ class FancyURLopener(URLopener):
         # We are using newer HTTPError with older redirect_internal method
         # This older method will get deprecated in 3.3
 
-        if urlparts.scheme not in ('http', 'https', 'ftp'):
+        if urlparts.scheme not in ('http', 'https', 'ftp', ''):
             raise HTTPError(newurl, errcode,
                             errmsg +
                             " Redirection to url '%s' is not allowed." % newurl,
index 6e5b7f303aa25b4da468b9bc959c5a1d9c89e761..08f897f583b3f56bcc989d427c0604c47d8ed9f2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -97,6 +97,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #13696: Fix the 302 Relative URL Redirection problem.
+
 - Issue #13636: Weak ciphers are now disabled by default in the ssl module
   (except when SSLv2 is explicitly asked for).