Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/library/http.client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ HTTPConnection Objects
The *headers* argument should be a mapping of extra HTTP headers to send with
the CONNECT request.

Header values in the mapping are converted to strings before they are sent.

As HTTP/1.1 is used for HTTP CONNECT tunnelling request, `as per the RFC
<https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.6>`_, a HTTP ``Host:``
header must be provided, matching the authority-form of the request target
Expand Down
4 changes: 2 additions & 2 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,8 @@ def _tunnel(self):
self._http_vsn_str.encode("ascii"))
headers = [connect]
for header, value in self._tunnel_headers.items():
header_bytes = header.encode("latin-1")
value_bytes = value.encode("latin-1")
header_bytes = str(header).encode("latin-1")
value_bytes = str(value).encode("latin-1")
if not _is_legal_header_name(header_bytes):
raise ValueError('Invalid header name %r' % (header_bytes,))
if _is_illegal_header_value(value_bytes):
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,19 @@ def test_invalid_tunnel_headers(self):
with self.assertRaisesRegex(ValueError, 'Invalid header'):
conn._tunnel() # Called in .connect()

def test_tunnel_headers_are_converted_to_strings(self):
conn = client.HTTPConnection('example.com')
conn.set_tunnel('tunnel', headers={
'None-Value': None,
'Integer-Value': 42,
})
conn.sock = FakeSocket('HTTP/1.1 200 Connection established\r\n\r\n')

conn._tunnel() # Called in .connect()

self.assertIn(b'None-Value: None\r\n', conn.sock.data)
self.assertIn(b'Integer-Value: 42\r\n', conn.sock.data)

def test_invalid_tunnel_host(self):
cases = (
'invalid\r.host',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Restore string conversion of HTTP CONNECT tunnel header names and values.
Loading