Fixed parsing of username and password encoded in the URI
authorRoberto Migli <robertomigli@gmail.com>
Mon, 13 Jan 2014 12:32:00 +0000 (13:32 +0100)
committerRoberto Migli <robertomigli@gmail.com>
Mon, 13 Jan 2014 12:32:00 +0000 (13:32 +0100)
requests/utils.py
test_requests.py

index 7a4d44d56a65eb82e038ce888a99deae4ed4066b..73934110855a891055da8971c5ba297ac7755cc4 100644 (file)
@@ -635,9 +635,15 @@ def get_auth_from_url(url):
     """Given a url with authentication components, extract them into a tuple of
     username,password."""
     if url:
-        url = unquote(url)
         parsed = urlparse(url)
-        return (parsed.username, parsed.password)
+        username = ""
+        password = ""
+
+        if parsed.username is not None:
+            username = unquote(parsed.username)
+        if parsed.password is not None:
+            password = unquote(parsed.password)
+        return (username, password)
     else:
         return ('', '')
 
index 1e8e723f4638fca152f8cbf0dce9a57294b51b3d..2f4c40a3bffc6a8764ba3fa2bffd335b40cac016 100755 (executable)
@@ -702,7 +702,7 @@ class RequestsTestCase(unittest.TestCase):
         assert ('user', 'pass pass') == requests.utils.get_auth_from_url(url)
 
     def test_get_auth_from_url_percent_chars(self):
-        url = 'http://user%user:pass@complex.url.com/path?query=yes'
+        url = 'http://user%25user:pass@complex.url.com/path?query=yes'
         assert ('user%user', 'pass') == requests.utils.get_auth_from_url(url)
 
     def test_cannot_send_unprepared_requests(self):
@@ -1092,6 +1092,15 @@ class UtilsTestCase(unittest.TestCase):
         assert address_in_network('192.168.1.1', '192.168.1.0/24')
         assert not address_in_network('172.16.0.1', '192.168.1.0/24')
 
+    def test_get_auth_from_url(self):
+        from requests.utils import get_auth_from_url
+        from requests.compat import quote
+        percent_encoding_test_chars = "%!*'();:@&=+$,/?#[] "
+        url_address = "request.com/url.html#test"
+        url = "http://" + quote(percent_encoding_test_chars, '') + ':' + quote(percent_encoding_test_chars, '') + '@' + url_address
+        (username, password) = get_auth_from_url(url)
+        assert username == percent_encoding_test_chars
+        assert password == percent_encoding_test_chars
 
 
 class TestMorselToCookieExpires(unittest.TestCase):