FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.14] gh-159044: Restore string conversion for HTTP tunnel header values (GH-159216) by miss-islington · Pull Request #159383 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 2 additions & 1 deletion Doc/library/http.client.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL