Skip to content
Myr

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

2 files+30 added−1 removed

modified:requests/adapters.py+4 added-1 removed
17 unmodified lines
18from urllib3.util.retry import Retry19from urllib3.exceptions import ClosedPoolError20from urllib3.exceptions import ConnectTimeoutError21from urllib3.exceptions import HTTPError as _HTTPError22from urllib3.exceptions import InvalidHeader as _InvalidHeader23from urllib3.exceptions import MaxRetryError24from urllib3.exceptions import NewConnectionError25from urllib3.exceptions import ProxyError as _ProxyError26from urllib3.exceptions import ProtocolError
10 unmodified lines
37from .structures import CaseInsensitiveDict38from .cookies import extract_cookies_to_jar39from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,40 ProxyError, RetryError, InvalidSchema, InvalidProxyURL,40 InvalidURL)41 InvalidURL, InvalidHeader)42from .auth import _basic_auth_str4344try:45 from urllib3.contrib.socks import SOCKSProxyManager
481 unmodified lines
527 # 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 raise535536 return self.build_response(request, resp)
modified:tests/test_lowlevel.py+26 added
44 unmodified lines
45 r = requests.get(url)46 close_server.set() # release server block474849def test_conflicting_content_lengths():50 """Ensure we correctly throw an InvalidHeader error if multiple51 conflicting Content-Length headers are returned.52 """5354 def multiple_content_length_response_handler(sock):55 request_content = consume_socket_content(sock, timeout=0.5)5657 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")6263 return request_content6465 close_server = threading.Event()66 server = Server(multiple_content_length_response_handler)6768 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()737475def 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