Skip to content

Commit 4de7ad2

Browse files
committed
fix issue #5274
1 parent 2d39c0d commit 4de7ad2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

AUTHORS.rst

+1
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,4 @@ Patches and Suggestions
190190
- Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)
191191
- "Dull Bananas" <[email protected]> (`@dullbananas <https://github.com/dullbananas>`_)
192192
- Alessio Izzo (`@aless10 <https://github.com/aless10>`_)
193+
- Hod Bin Noon (`@hodbn <https://github.com/hodbn>`_)

requests/adapters.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
459459
try:
460460
low_conn.putrequest(request.method,
461461
url,
462-
skip_accept_encoding=True)
462+
skip_accept_encoding=True,
463+
skip_host='Host' in request.headers)
463464

464465
for header, value in request.headers.items():
465466
low_conn.putheader(header, value)

tests/test_lowlevel.py

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ def test_chunked_upload():
2424
assert r.request.headers['Transfer-Encoding'] == 'chunked'
2525

2626

27+
def test_chunked_upload_uses_only_specified_host_header():
28+
"""Ensure we use only the specified Host header for chunked requests."""
29+
text_200 = (b'HTTP/1.1 200 OK\r\n'
30+
b'Content-Length: 0\r\n\r\n')
31+
wanted_host = 'sample-host'
32+
expected_header = b'Host: {}'.format(wanted_host)
33+
def single_host_resp_handler(sock):
34+
request_content = consume_socket_content(sock, timeout=0.5)
35+
assert expected_header in request_content
36+
assert request_content.count(b'Host: ') == 1
37+
sock.send(text_200)
38+
39+
return request_content
40+
41+
close_server = threading.Event()
42+
server = Server(single_host_resp_handler, wait_to_close_event=close_server)
43+
data = iter([b'a', b'b', b'c'])
44+
45+
with server as (host, port):
46+
url = 'http://{}:{}/'.format(host, port)
47+
r = requests.post(url, data=data, headers={'Host': wanted_host}, stream=True)
48+
close_server.set() # release server block
49+
50+
assert r.status_code == 200
51+
52+
2753
def test_digestauth_401_count_reset_on_redirect():
2854
"""Ensure we correctly reset num_401_calls after a successful digest auth,
2955
followed by a 302 redirect to another digest auth prompt.

0 commit comments

Comments
 (0)