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 committed Nov 14, 2020
1 parent 2d39c0d commit 925dd80
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -190,3 +190,4 @@ Patches and Suggestions
- Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)
- "Dull Bananas" <dull.bananas0@gmail.com> (`@dullbananas <https://github.com/dullbananas>`_)
- Alessio Izzo (`@aless10 <https://github.com/aless10>`_)
- Hod Bin Noon (`@hodbn <https://github.com/hodbn>`_)
3 changes: 2 additions & 1 deletion requests/adapters.py
Expand Up @@ -459,7 +459,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
26 changes: 26 additions & 0 deletions tests/test_lowlevel.py
Expand Up @@ -24,6 +24,32 @@ def test_chunked_upload():
assert r.request.headers['Transfer-Encoding'] == 'chunked'


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_digestauth_401_count_reset_on_redirect():
"""Ensure we correctly reset num_401_calls after a successful digest auth,
followed by a 302 redirect to another digest auth prompt.
Expand Down

0 comments on commit 925dd80

Please sign in to comment.