Merge pull request #5391 from hodbn/fix-5274
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Send only one Host header in chunked request
authored by Nate Prewitt · committed by GitHub · parents 2d2447e, 4ab2550 · Browse files · 3 files·+52 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
3 files+52 added−1 removed
- tests:+48 added−0 removed
- other:+4 added−1 removed
Provenance
- Commit
7556ea4- Tree
4d1eee0- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2d2447e- Parent 2
4ab2550
3 files changed+52 added−1 removed
modified:requests/adapters.py+3 added-1 removed
456 unmodified lines457 458 low_conn = conn._get_conn(timeout=DEFAULT_POOL_TIMEOUT)459 460 try:461+ skip_host = 'Host' in request.headers462 low_conn.putrequest(request.method,463 url,463- skip_accept_encoding=True)464+ skip_accept_encoding=True,465+ skip_host=skip_host)466 467 for header, value in request.headers.items():468 low_conn.putheader(header, value)469 69 unmodified linesmodified:tests/test_lowlevel.py+48 added
7 unmodified lines8 9 from .utils import override_environ10 11 12+def echo_response_handler(sock):13+ """Simple handler that will take request and echo it back to requester."""14+ request_content = consume_socket_content(sock, timeout=0.5)15+16+ text_200 = (17+ b'HTTP/1.1 200 OK\r\n'18+ b'Content-Length: %d\r\n\r\n'19+ b'%s'20+ ) % (len(request_content), request_content)21+ sock.send(text_200)22+23+24 def test_chunked_upload():25 """can safely send generators"""26 close_server = threading.Event()27 server = Server.basic_response_server(wait_to_close_event=close_server)29 unmodified lines57 r = requests.get(url)58 close_server.set() # release server block59 60 61+def test_chunked_upload_uses_only_specified_host_header():62+ """Ensure we use only the specified Host header for chunked requests."""63+ close_server = threading.Event()64+ server = Server(echo_response_handler, wait_to_close_event=close_server)65+66+ data = iter([b'a', b'b', b'c'])67+ custom_host = 'sample-host'68+69+ with server as (host, port):70+ url = 'http://{}:{}/'.format(host, port)71+ r = requests.post(url, data=data, headers={'Host': custom_host}, stream=True)72+ close_server.set() # release server block73+74+ expected_header = b'Host: %s\r\n' % custom_host.encode('utf-8')75+ assert expected_header in r.content76+ assert r.content.count(b'Host: ') == 177+78+79+def test_chunked_upload_doesnt_skip_host_header():80+ """Ensure we don't omit all Host headers with chunked requests."""81+ close_server = threading.Event()82+ server = Server(echo_response_handler, wait_to_close_event=close_server)83+84+ data = iter([b'a', b'b', b'c'])85+86+ with server as (host, port):87+ expected_host = '{}:{}'.format(host, port)88+ url = 'http://{}:{}/'.format(host, port)89+ r = requests.post(url, data=data, stream=True)90+ close_server.set() # release server block91+92+ expected_header = b'Host: %s\r\n' % expected_host.encode('utf-8')93+ assert expected_header in r.content94+ assert r.content.count(b'Host: ') == 195+96+97 def test_conflicting_content_lengths():98 """Ensure we correctly throw an InvalidHeader error if multiple99 conflicting Content-Length headers are returned.100 """305 unmodified linesmodified:AUTHORS.rst+1 added
189 unmodified lines190 - Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)191 - "Dull Bananas" <dull.bananas0@gmail.com> (`@dullbananas <https://github.com/dullbananas>`_)192 - Alessio Izzo (`@aless10 <https://github.com/aless10>`_)193 - Sylvain Marié (`@smarie <https://github.com/smarie>`_)194+- Hod Bin Noon (`@hodbn <https://github.com/hodbn>`_)