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

Skip to content

Commit 8d96d77

Browse files
committed
Issue #10980: encode headers with latin1 instead of ASCII in the HTTP server.
This makes the implementation of PEP 3333 compliant servers on top of BaseHTTPServer possible.
1 parent 137e0f0 commit 8d96d77

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

Lib/http/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,15 @@ def send_response_only(self, code, message=None):
448448
message = ''
449449
if self.request_version != 'HTTP/0.9':
450450
self.wfile.write(("%s %d %s\r\n" %
451-
(self.protocol_version, code, message)).encode('ASCII', 'strict'))
451+
(self.protocol_version, code, message)).encode('latin1', 'strict'))
452452

453453
def send_header(self, keyword, value):
454454
"""Send a MIME header."""
455455
if self.request_version != 'HTTP/0.9':
456456
if not hasattr(self, '_headers_buffer'):
457457
self._headers_buffer = []
458458
self._headers_buffer.append(
459-
("%s: %s\r\n" % (keyword, value)).encode('ASCII', 'strict'))
459+
("%s: %s\r\n" % (keyword, value)).encode('latin1', 'strict'))
460460

461461
if keyword.lower() == 'connection':
462462
if value.lower() == 'close':

Lib/test/test_httpservers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ def do_CUSTOM(self):
9797
self.send_header('Connection', 'close')
9898
self.end_headers()
9999

100+
def do_LATINONEHEADER(self):
101+
self.send_response(999)
102+
self.send_header('X-Special', 'Dängerous Mind')
103+
self.end_headers()
104+
100105
def setUp(self):
101106
BaseTestCase.setUp(self)
102107
self.con = http.client.HTTPConnection('localhost', self.PORT)
@@ -194,6 +199,11 @@ def test_return_custom_status(self):
194199
res = self.con.getresponse()
195200
self.assertEqual(res.status, 999)
196201

202+
def test_latin1_header(self):
203+
self.con.request('LATINONEHEADER', '/')
204+
res = self.con.getresponse()
205+
self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind')
206+
197207

198208
class SimpleHTTPServerTestCase(BaseTestCase):
199209
class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Library
2929
- Issue #10898: Allow compiling the posix module when the C library defines
3030
a symbol named FSTAT.
3131

32+
- Issue #10980: the HTTP server now encodes headers with iso-8859-1 (latin1)
33+
encoding. This is the preferred encoding of PEP 3333 and the base encoding
34+
of HTTP 1.1.
35+
3236

3337
What's New in Python 3.2 Release Candidate 1
3438
============================================

0 commit comments

Comments
 (0)