Appropriately handle urllib3's InvalidHeader exception (#5914)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by GitHub · parent b351e90 · Browse files · 2 files·+30 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+30 added−1 removed
- tests:+26 added−0 removed
- other:+4 added−1 removed
Provenance
- Commit
1096835- Tree
877e0a7- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
b351e90
2 files changed+30 added−1 removed
modified:requests/adapters.py+4 added-1 removed
17 unmodified lines18 from urllib3.util.retry import Retry19 from urllib3.exceptions import ClosedPoolError20 from urllib3.exceptions import ConnectTimeoutError21 from urllib3.exceptions import HTTPError as _HTTPError22+from urllib3.exceptions import InvalidHeader as _InvalidHeader23 from urllib3.exceptions import MaxRetryError24 from urllib3.exceptions import NewConnectionError25 from urllib3.exceptions import ProxyError as _ProxyError26 from urllib3.exceptions import ProtocolError10 unmodified lines37 from .structures import CaseInsensitiveDict38 from .cookies import extract_cookies_to_jar39 from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,40 ProxyError, RetryError, InvalidSchema, InvalidProxyURL,40- InvalidURL)41+ InvalidURL, InvalidHeader)42 from .auth import _basic_auth_str43 44 try:45 from urllib3.contrib.socks import SOCKSProxyManager481 unmodified lines527 # This branch is for urllib3 versions earlier than v1.22528 raise SSLError(e, request=request)529 elif isinstance(e, ReadTimeoutError):530 raise ReadTimeout(e, request=request)531+ elif isinstance(e, _InvalidHeader):532+ raise InvalidHeader(e, request=request)533 else:534 raise535 536 return self.build_response(request, resp)modified:tests/test_lowlevel.py+26 added
44 unmodified lines45 r = requests.get(url)46 close_server.set() # release server block47 48 49+def test_conflicting_content_lengths():50+ """Ensure we correctly throw an InvalidHeader error if multiple51+ conflicting Content-Length headers are returned.52+ """53+54+ def multiple_content_length_response_handler(sock):55+ request_content = consume_socket_content(sock, timeout=0.5)56+57+ sock.send(b"HTTP/1.1 200 OK\r\n" +58+ b"Content-Type: text/plain\r\n" +59+ b"Content-Length: 16\r\n" +60+ b"Content-Length: 32\r\n\r\n" +61+ b"-- Bad Actor -- Original Content\r\n")62+63+ return request_content64+65+ close_server = threading.Event()66+ server = Server(multiple_content_length_response_handler)67+68+ with server as (host, port):69+ url = 'http://{}:{}/'.format(host, port)70+ with pytest.raises(requests.exceptions.InvalidHeader):71+ r = requests.get(url)72+ close_server.set()73+74+75 def test_digestauth_401_count_reset_on_redirect():76 """Ensure we correctly reset num_401_calls after a successful digest auth,77 followed by a 302 redirect to another digest auth prompt.78 279 unmodified lines