diff options
author | Nate Prewitt <Nate.Prewitt@gmail.com> | 2018-08-11 12:58:15 -0700 |
---|---|---|
committer | Nate Prewitt <Nate.Prewitt@gmail.com> | 2018-09-30 14:44:32 -0600 |
commit | f3ec42fb480d1752de80330bcb0e9d5ed686dd1b (patch) | |
tree | 91706287bb1e881b1cd818bc809345a35fc4ae2f | |
parent | 8f20b7dc8f79046d0a1015b9605df5c30fa44197 (diff) | |
download | python-requests-encapsulate_urllib3_exc.tar.gz |
wrap url parsing exceptions from urllib3's PoolManagerencapsulate_urllib3_exc
-rw-r--r-- | HISTORY.md | 8 | ||||
-rw-r--r-- | requests/adapters.py | 9 | ||||
-rw-r--r-- | tests/test_requests.py | 10 |
3 files changed, 23 insertions, 4 deletions
@@ -4,8 +4,12 @@ Release History dev --- -**Bugfixes** - Content-Type header parsing is now case-insensitive (e.g. -charset=utf8 v Charset=utf8). +**Bugfixes** + +- Content-Type header parsing is now case-insensitive (e.g. + charset=utf8 v Charset=utf8). +- Fixed exception leak where certain redirect urls would raise + uncaught urllib3 exceptions. - \[Short description of non-trivial change.\] diff --git a/requests/adapters.py b/requests/adapters.py index 3b923a5a..f2639150 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -26,6 +26,7 @@ from urllib3.exceptions import ProtocolError from urllib3.exceptions import ReadTimeoutError from urllib3.exceptions import SSLError as _SSLError from urllib3.exceptions import ResponseError +from urllib3.exceptions import LocationValueError from .models import Response from .compat import urlparse, basestring @@ -35,7 +36,8 @@ from .utils import (DEFAULT_CA_BUNDLE_PATH, extract_zipped_paths, from .structures import CaseInsensitiveDict from .cookies import extract_cookies_to_jar from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError, - ProxyError, RetryError, InvalidSchema, InvalidProxyURL) + ProxyError, RetryError, InvalidSchema, InvalidProxyURL, + InvalidURL) from .auth import _basic_auth_str try: @@ -407,7 +409,10 @@ class HTTPAdapter(BaseAdapter): :rtype: requests.Response """ - conn = self.get_connection(request.url, proxies) + try: + conn = self.get_connection(request.url, proxies) + except LocationValueError as e: + raise InvalidURL(e, request=request) self.cert_verify(conn, request.url, verify, cert) url = self.request_url(request, proxies) diff --git a/tests/test_requests.py b/tests/test_requests.py index 66043798..b4ee9f94 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -2427,6 +2427,16 @@ class TestPreparingURLs(object): r.prepare() @pytest.mark.parametrize( + 'url, exception', + ( + ('http://localhost:-1', InvalidURL), + ) + ) + def test_redirecting_to_bad_url(self, httpbin, url, exception): + with pytest.raises(exception): + r = requests.get(httpbin('redirect-to'), params={'url': url}) + + @pytest.mark.parametrize( 'input, expected', ( ( |