Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
4 changes: 2 additions & 2 deletions 4 Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,8 @@ def putrequest(self, method, url, skip_host=False,
f"(found at least {match.group()!r})")
request = '%s %s %s' % (method, url, self._http_vsn_str)

# Non-ASCII characters should have been eliminated earlier
self._output(request.encode('ascii'))
# Encode as latin-1, like we do headers and data
self._output(request.encode('latin-1'))

if self._http_vsn == 11:
# Issue some standard headers for better HTTP/1.1 compliance
Expand Down
34 changes: 32 additions & 2 deletions 34 Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,37 @@ def test_ipv6host_header(self):
conn.request('GET', '/foo')
self.assertTrue(sock.data.startswith(expected))

def test_request_path_handling(self):
happy_cases = (
('/', b'/'),
('', b'/'),
('/\xe4\xbd\xa0\xe5\xa5\xbd',
b'/\xe4\xbd\xa0\xe5\xa5\xbd'),
)
for caller_path, expected_path in happy_cases:
with self.subTest((caller_path, expected_path)):
conn = client.HTTPConnection('server.fqdn')
sock = FakeSocket('')
conn.sock = sock
conn.request('GET', caller_path)
expected = (b'GET ' + expected_path + b' HTTP/1.1\r\n'
b'Host: server.fqdn\r\n'
b'Accept-Encoding: identity\r\n\r\n')
self.assertEqual(sock.data, expected)

error_cases = (
'/\u4f60\u597d',
'/\udce4\udcbd\udca0\udce5\udca5\udcbd',
)
for caller_path in error_cases:
with self.subTest(caller_path):
conn = client.HTTPConnection('server.fqdn')
sock = FakeSocket('')
conn.sock = sock
with self.assertRaises(UnicodeEncodeError):
conn.request('GET', caller_path)
self.assertEqual(sock.data, b'')

def test_malformed_headers_coped_with(self):
# Issue 19996
body = "HTTP/1.1 200 OK\r\nFirst: val\r\n: nval\r\nSecond: val\r\n\r\n"
Expand Down Expand Up @@ -720,8 +751,7 @@ def test_send_file(self):
sock = FakeSocket(body)
conn.sock = sock
conn.request('GET', '/foo', body)
self.assertTrue(sock.data.startswith(expected), '%r != %r' %
(sock.data[:len(expected)], expected))
self.assertEqual(sock.data[:len(expected)], expected)

def test_send(self):
expected = b'this is a test this is only a test'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``http.client`` now accepts ISO-8859-1 request-targets. Callers are still
encouraged to URL-quote the request-target so as to comply with the RFC.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.