Skip to content
Myr

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

3 files+52 added−1 removed

modified:requests/adapters.py+3 added-1 removed
456 unmodified lines
457458 low_conn = conn._get_conn(timeout=DEFAULT_POOL_TIMEOUT)459460 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)466467 for header, value in request.headers.items():468 low_conn.putheader(header, value)469
69 unmodified lines
modified:tests/test_lowlevel.py+48 added
7 unmodified lines
89from .utils import override_environ101112def 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)1516 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)222324def 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 lines
57 r = requests.get(url)58 close_server.set() # release server block596061def 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)6566 data = iter([b'a', b'b', b'c'])67 custom_host = 'sample-host'6869 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 block7374 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: ') == 1777879def 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)8384 data = iter([b'a', b'b', b'c'])8586 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 block9192 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: ') == 1959697def test_conflicting_content_lengths():98 """Ensure we correctly throw an InvalidHeader error if multiple99 conflicting Content-Length headers are returned.100 """
305 unmodified lines
modified:AUTHORS.rst+1 added
189 unmodified lines
190- 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>`_)