Fixed parsing of username and password encoded in the URI

This commit is contained in:
Roberto Migli
2014-01-13 13:32:00 +01:00
parent ac4e05874a
commit a6a8342066
2 changed files with 18 additions and 3 deletions
+8 -2
View 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 ('', '')
+10 -1
View File
@@ -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):