Repository navigation
gh-159044: Restore string conversion for HTTP tunnel header values #159216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2793,6 +2793,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(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are converting header values and keys to strings. Maybe the tests can also include this to avoid future regressions?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, I think it makes sense. However, I tried adding a non-string key to the test, but it fails earlier: That check predates this issue, so supporting non-string keys would probably be new behavior. I think it'd make sense to update the docs to only mention values. Do you think that's the right approach?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ERROR: test_tunnel_nonstr_headers (test.test_httplib.TunnelTests.test_tunnel_nonstr_headers)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".../cpython/Lib/test/test_httplib.py", line 2801, in test_tunnel_nonstr_headers
self.conn.set_tunnel('destination.com', headers={
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'None-Value': None,
^^^^^^^^^^^^^^^^^^^
...<4 lines>...
None: 'none-key',
^^^^^^^^^^^^^^^^^
})
^^
File ".../cpython/Lib/http/client.py", line 974, in set_tunnel
if not any(header.lower() == "host" for header in self._tunnel_headers):
^^^
File ".../cpython/Lib/http/client.py", line 974, in <genexpr>
if not any(header.lower() == "host" for header in self._tunnel_headers):
^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'lower'
----------------------------------------------------------------------
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, let's just limit the PR to values only if keys didn't work anyway.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review, @ZeroIntensity! I reverted - header_bytes = str(header).encode("latin-1")
+ header_bytes = header.encode("latin-1")
value_bytes = str(value).encode("latin-1")And thanks a lot, @edvilme, for the discussion here and in the issue. Your review comments helped us find this gap in the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank YOU, glad you found it :) |
||
| }) | ||
|
|
||
| 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. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are using an example domain maybe use that one instead (destination.com is a real non-example domain)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion, @edvilme! I used
destination.comto stay consistent with the other tests inTunnelTests. The file also usesexample.com, but in this classdestination.comis the most common. So I'd lean towards keeping it, but I'm happy to switch toexample.comif you think it reads better.