Skip to content

Commit a6cd380

Browse files
authored
Merge pull request #4765 from requests/encapsulate_urllib3_exc
wrap url parsing exceptions from urllib3's PoolManager
2 parents ff0c325 + bbdbcc8 commit a6cd380

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

HISTORY.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ Release History
44
dev
55
---
66

7-
**Bugfixes** - Content-Type header parsing is now case-insensitive (e.g.
8-
charset=utf8 v Charset=utf8).
7+
**Bugfixes**
8+
9+
- Content-Type header parsing is now case-insensitive (e.g.
10+
charset=utf8 v Charset=utf8).
11+
- Fixed exception leak where certain redirect urls would raise
12+
uncaught urllib3 exceptions.
913

1014
- \[Short description of non-trivial change.\]
1115

requests/adapters.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from urllib3.exceptions import ReadTimeoutError
2727
from urllib3.exceptions import SSLError as _SSLError
2828
from urllib3.exceptions import ResponseError
29+
from urllib3.exceptions import LocationValueError
2930

3031
from .models import Response
3132
from .compat import urlparse, basestring
@@ -35,7 +36,8 @@
3536
from .structures import CaseInsensitiveDict
3637
from .cookies import extract_cookies_to_jar
3738
from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,
38-
ProxyError, RetryError, InvalidSchema, InvalidProxyURL)
39+
ProxyError, RetryError, InvalidSchema, InvalidProxyURL,
40+
InvalidURL)
3941
from .auth import _basic_auth_str
4042

4143
try:
@@ -407,7 +409,10 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
407409
:rtype: requests.Response
408410
"""
409411

410-
conn = self.get_connection(request.url, proxies)
412+
try:
413+
conn = self.get_connection(request.url, proxies)
414+
except LocationValueError as e:
415+
raise InvalidURL(e, request=request)
411416

412417
self.cert_verify(conn, request.url, verify, cert)
413418
url = self.request_url(request, proxies)

tests/test_requests.py

+10
Original file line numberDiff line numberDiff line change
@@ -2426,6 +2426,16 @@ def test_preparing_bad_url(self, url):
24262426
with pytest.raises(requests.exceptions.InvalidURL):
24272427
r.prepare()
24282428

2429+
@pytest.mark.parametrize(
2430+
'url, exception',
2431+
(
2432+
('http://localhost:-1', InvalidURL),
2433+
)
2434+
)
2435+
def test_redirecting_to_bad_url(self, httpbin, url, exception):
2436+
with pytest.raises(exception):
2437+
r = requests.get(httpbin('redirect-to'), params={'url': url})
2438+
24292439
@pytest.mark.parametrize(
24302440
'input, expected',
24312441
(

0 commit comments

Comments
 (0)