Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit a46f8ac

Browse files
committed
Automatically add Host: to HTTP/1.1 requests
1 parent 2bbd105 commit a46f8ac

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

hyper/http11/connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ def request(self, method, url, body=None, headers={}):
101101
if self._sock is None:
102102
self.connect()
103103

104-
# We may need extra headers. For now, we only add headers based on
105-
# body content.
104+
# We may need extra headers.
106105
if body:
107106
body_type = self._add_body_headers(headers, body)
108107

108+
if b'host' not in headers:
109+
headers[b'host'] = self.host
110+
109111
# Begin by emitting the header block.
110112
self._send_headers(method, url, headers)
111113

test/test_http11.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def test_basic_request(self):
4848
expected = (
4949
b"GET /get HTTP/1.1\r\n"
5050
b"User-Agent: hyper\r\n"
51+
b"host: http2bin.org\r\n"
5152
b"\r\n"
5253
)
5354
received = b''.join(sock.queue)
@@ -69,6 +70,7 @@ def test_request_with_bytestring_body(self):
6970
b"POST /post HTTP/1.1\r\n"
7071
b"User-Agent: hyper\r\n"
7172
b"content-length: 2\r\n"
73+
b"host: http2bin.org\r\n"
7274
b"\r\n"
7375
b"hi"
7476
)
@@ -97,6 +99,7 @@ def fake_fstat(*args):
9799
expected = (
98100
b"POST /post HTTP/1.1\r\n"
99101
b"content-length: 16\r\n"
102+
b"host: http2bin.org\r\n"
100103
b"\r\n"
101104
b"some binary data"
102105
)
@@ -121,6 +124,7 @@ def body():
121124
expected = (
122125
b"POST /post HTTP/1.1\r\n"
123126
b"transfer-encoding: chunked\r\n"
127+
b"host: http2bin.org\r\n"
124128
b"\r\n"
125129
b"2\r\nhi\r\n"
126130
b"5\r\nthere\r\n"
@@ -146,6 +150,7 @@ def body():
146150
expected = (
147151
b"POST /post HTTP/1.1\r\n"
148152
b"content-length: 10\r\n"
153+
b"host: http2bin.org\r\n"
149154
b"\r\n"
150155
b"hitheresir"
151156
)
@@ -169,6 +174,7 @@ def test_chunked_overrides_body(self):
169174
expected = (
170175
b"POST /post HTTP/1.1\r\n"
171176
b"transfer-encoding: chunked\r\n"
177+
b"host: http2bin.org\r\n"
172178
b"\r\n"
173179
b"8\r\noneline\n\r\n"
174180
b"b\r\nanotherline\r\n"

0 commit comments

Comments
 (0)