Skip to content
Merged
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
3 changes: 2 additions & 1 deletion Doc/library/http.client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ HTTPConnection Objects
proxy server).

The *headers* argument should be a mapping of extra HTTP headers to send with
the CONNECT request.
the CONNECT request. Non-string header values are converted to strings via
:func:`str`.

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:``
Expand Down
2 changes: 1 addition & 1 deletion Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ def _tunnel(self):
headers = [connect]
for header, value in self._tunnel_headers.items():
header_bytes = header.encode("latin-1")
value_bytes = value.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
17 changes: 17 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,23 @@ def _create_connection(address, timeout=None, source_address=None):
self.assertIsNotNone(exc)
self.assertTrue(sock.file_closed)

def test_tunnel_nonstr_headers(self):
class CustomObject:
def __str__(self):
return 'custom-str'

self.conn.set_tunnel('destination.com', headers={
'None-Value': None,
'Integer-Value': 9,
'Custom-Value': CustomObject(),
})

self.conn.request('HEAD', '/', '')

self.assertIn(b'None-Value: None\r\n', self.conn.sock.data)
self.assertIn(b'Integer-Value: 9\r\n', self.conn.sock.data)
self.assertIn(b'Custom-Value: custom-str\r\n', self.conn.sock.data)


if __name__ == '__main__':
unittest.main(verbosity=2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Restore the previous behaviour of :meth:`http.client.HTTPConnection.set_tunnel`
where header values that are not strings (such as :const:`None`, :class:`int`
or other objects) are converted with :class:`str` instead of raising
:exc:`AttributeError` when the request is sent.
Loading