Skip to content

Commit

Permalink
To match the behaviour of HTTP server, the HTTP client library now al…
Browse files Browse the repository at this point in the history
…so encodes

headers with iso-8859-1 (latin1) encoding.  It was already doing that for
incoming headers which makes this behaviour now consistent in both incoming and
outgoing direction.
  • Loading branch information
mitsuhiko committed Jan 22, 2011
1 parent 8d96d77 commit 5953128
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def _tunnel(self):
self.send(connect_bytes)
for header, value in self._tunnel_headers.iteritems():
header_str = "%s: %s\r\n" % (header, value)
header_bytes = header_str.encode("ascii")
header_bytes = header_str.encode("latin1")
self.send(header_bytes)

response = self.response_class(self.sock, method = self._method)
Expand Down Expand Up @@ -935,7 +935,7 @@ def putheader(self, header, *values):
values = list(values)
for i, one_value in enumerate(values):
if hasattr(one_value, 'encode'):
values[i] = one_value.encode('ascii')
values[i] = one_value.encode('latin1')
elif isinstance(one_value, int):
values[i] = str(one_value).encode('ascii')
value = b'\r\n\t'.join(values)
Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def do_CUSTOM(self):
def do_LATINONEHEADER(self):
self.send_response(999)
self.send_header('X-Special', 'Dängerous Mind')
self.send_header('Connection', 'close')
self.end_headers()
body = self.headers['x-special-incoming'].encode('utf-8')
self.wfile.write(body)

def setUp(self):
BaseTestCase.setUp(self)
Expand Down Expand Up @@ -200,9 +203,12 @@ def test_return_custom_status(self):
self.assertEqual(res.status, 999)

def test_latin1_header(self):
self.con.request('LATINONEHEADER', '/')
self.con.request('LATINONEHEADER', '/', headers={
'X-Special-Incoming': 'Ärger mit Unicode'
})
res = self.con.getresponse()
self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind')
self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8'))


class SimpleHTTPServerTestCase(BaseTestCase):
Expand Down
5 changes: 5 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Library
encoding. This is the preferred encoding of PEP 3333 and the base encoding
of HTTP 1.1.

- To match the behaviour of HTTP server, the HTTP client library now also
encodes headers with iso-8859-1 (latin1) encoding. It was already doing
that for incoming headers which makes this behaviour now consistent in
both incoming and outgoing direction.


What's New in Python 3.2 Release Candidate 1
============================================
Expand Down

0 comments on commit 5953128

Please sign in to comment.