diff --git a/requests/adapters.py b/requests/adapters.py index 43addb1b..dd10e959 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -286,10 +286,6 @@ class HTTPAdapter(BaseAdapter): username, password = get_auth_from_url(proxy) if username and password: - # Proxy auth usernames and passwords will be urlencoded, we need - # to decode them. - username = unquote(username) - password = unquote(password) headers['Proxy-Authorization'] = _basic_auth_str(username, password) diff --git a/requests/utils.py b/requests/utils.py index 73934110..7b7ff0a7 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -634,18 +634,14 @@ def except_on_missing_scheme(url): def get_auth_from_url(url): """Given a url with authentication components, extract them into a tuple of username,password.""" - if url: - parsed = urlparse(url) - username = "" - password = "" + parsed = urlparse(url) - 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 ('', '') + try: + auth = (unquote(parsed.username), unquote(parsed.password)) + except (AttributeError, TypeError): + auth = ('', '') + + return auth def to_native_string(string, encoding='ascii'): diff --git a/test_requests.py b/test_requests.py index 2f4c40a3..0a733104 100755 --- a/test_requests.py +++ b/test_requests.py @@ -194,7 +194,7 @@ class RequestsTestCase(unittest.TestCase): assert r.json()['cookies']['foo'] == 'bar' # Make sure the session cj is still the custom one assert s.cookies is cj - + def test_param_cookiejar_works(self): cj = cookielib.CookieJar() cookiejar_from_dict({'foo' : 'bar'}, cj) @@ -705,6 +705,10 @@ class RequestsTestCase(unittest.TestCase): url = 'http://user%25user:pass@complex.url.com/path?query=yes' assert ('user%user', 'pass') == requests.utils.get_auth_from_url(url) + def test_get_auth_from_url_encoded_hashes(self): + url = 'http://user:pass%23pass@complex.url.com/path?query=yes' + assert ('user', 'pass#pass') == requests.utils.get_auth_from_url(url) + def test_cannot_send_unprepared_requests(self): r = requests.Request(url=HTTPBIN) with pytest.raises(ValueError):