From a6a83420662b4ae2edda6949cf584415d3495c8e Mon Sep 17 00:00:00 2001 From: Roberto Migli Date: Mon, 13 Jan 2014 13:32:00 +0100 Subject: [PATCH] Fixed parsing of username and password encoded in the URI --- requests/utils.py | 10 ++++++++-- test_requests.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/requests/utils.py b/requests/utils.py index 7a4d44d..7393411 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -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 ('', '') diff --git a/test_requests.py b/test_requests.py index 1e8e723..2f4c40a 100755 --- a/test_requests.py +++ b/test_requests.py @@ -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): -- 2.34.1