Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5953128

Browse files
committed
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.
1 parent 8d96d77 commit 5953128

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

Lib/http/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ def _tunnel(self):
696696
self.send(connect_bytes)
697697
for header, value in self._tunnel_headers.iteritems():
698698
header_str = "%s: %s\r\n" % (header, value)
699-
header_bytes = header_str.encode("ascii")
699+
header_bytes = header_str.encode("latin1")
700700
self.send(header_bytes)
701701

702702
response = self.response_class(self.sock, method = self._method)
@@ -935,7 +935,7 @@ def putheader(self, header, *values):
935935
values = list(values)
936936
for i, one_value in enumerate(values):
937937
if hasattr(one_value, 'encode'):
938-
values[i] = one_value.encode('ascii')
938+
values[i] = one_value.encode('latin1')
939939
elif isinstance(one_value, int):
940940
values[i] = str(one_value).encode('ascii')
941941
value = b'\r\n\t'.join(values)

Lib/test/test_httpservers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ def do_CUSTOM(self):
100100
def do_LATINONEHEADER(self):
101101
self.send_response(999)
102102
self.send_header('X-Special', 'Dängerous Mind')
103+
self.send_header('Connection', 'close')
103104
self.end_headers()
105+
body = self.headers['x-special-incoming'].encode('utf-8')
106+
self.wfile.write(body)
104107

105108
def setUp(self):
106109
BaseTestCase.setUp(self)
@@ -200,9 +203,12 @@ def test_return_custom_status(self):
200203
self.assertEqual(res.status, 999)
201204

202205
def test_latin1_header(self):
203-
self.con.request('LATINONEHEADER', '/')
206+
self.con.request('LATINONEHEADER', '/', headers={
207+
'X-Special-Incoming': 'Ärger mit Unicode'
208+
})
204209
res = self.con.getresponse()
205210
self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind')
211+
self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8'))
206212

207213

208214
class SimpleHTTPServerTestCase(BaseTestCase):

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Library
3333
encoding. This is the preferred encoding of PEP 3333 and the base encoding
3434
of HTTP 1.1.
3535

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

3742
What's New in Python 3.2 Release Candidate 1
3843
============================================

0 commit comments

Comments
 (0)