Skip to content

Commit

Permalink
Send only one Host header in chunked request
Browse files Browse the repository at this point in the history
Closes #5274
  • Loading branch information
hodbn authored and nateprewitt committed Sep 2, 2021
1 parent aae7801 commit 6fbfca9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -191,3 +191,4 @@ Patches and Suggestions
- "Dull Bananas" <dull.bananas0@gmail.com> (`@dullbananas <https://github.com/dullbananas>`_)
- Alessio Izzo (`@aless10 <https://github.com/aless10>`_)
- Sylvain Marié (`@smarie <https://github.com/smarie>`_)
- Hod Bin Noon (`@hodbn <https://github.com/hodbn>`_)
3 changes: 2 additions & 1 deletion requests/adapters.py
Expand Up @@ -460,7 +460,8 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
try:
low_conn.putrequest(request.method,
url,
skip_accept_encoding=True)
skip_accept_encoding=True,
skip_host='Host' in request.headers)

for header, value in request.headers.items():
low_conn.putheader(header, value)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_lowlevel.py
Expand Up @@ -46,6 +46,33 @@ def incomplete_chunked_response_handler(sock):
close_server.set() # release server block


def test_chunked_upload_uses_only_specified_host_header():
"""Ensure we use only the specified Host header for chunked requests."""
text_200 = (b'HTTP/1.1 200 OK\r\n'
b'Content-Length: 0\r\n\r\n')
wanted_host = 'sample-host'
expected_header = 'Host: {}'.format(wanted_host).encode('utf-8')
def single_host_resp_handler(sock):
request_content = consume_socket_content(sock, timeout=0.5)
assert expected_header in request_content
assert request_content.count(b'Host: ') == 1
sock.send(text_200)

return request_content

close_server = threading.Event()

server = Server(single_host_resp_handler, wait_to_close_event=close_server)
data = iter([b'a', b'b', b'c'])

with server as (host, port):
url = 'http://{}:{}/'.format(host, port)
r = requests.post(url, data=data, headers={'Host': wanted_host}, stream=True)
close_server.set() # release server block

assert r.status_code == 200


def test_conflicting_content_lengths():
"""Ensure we correctly throw an InvalidHeader error if multiple
conflicting Content-Length headers are returned.
Expand Down

0 comments on commit 6fbfca9

Please sign in to comment.