Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap url parsing exceptions from urllib3's PoolManager #4765

Merged
merged 1 commit into from Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions HISTORY.md
Expand Up @@ -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.\]

Expand Down
9 changes: 7 additions & 2 deletions requests/adapters.py
Expand Up @@ -26,6 +26,7 @@
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
Expand All @@ -35,7 +36,8 @@
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:
Expand Down Expand Up @@ -407,7 +409,10 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
: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)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_requests.py
Expand Up @@ -2426,6 +2426,16 @@ def test_preparing_bad_url(self, url):
with pytest.raises(requests.exceptions.InvalidURL):
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',
(
Expand Down