Skip to content
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

bpo-42527: Use ignore mode instead strict for headers enconding in order to encode headers with special chars like emojis #27948

Closed
wants to merge 6 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,15 @@ def send_response_only(self, code, message=None):
self._headers_buffer = []
self._headers_buffer.append(("%s %d %s\r\n" %
(self.protocol_version, code, message)).encode(
'latin-1', 'strict'))
'latin-1', 'ignore'))

def send_header(self, keyword, value):
"""Send a MIME header to the headers buffer."""
if self.request_version != 'HTTP/0.9':
if not hasattr(self, '_headers_buffer'):
self._headers_buffer = []
self._headers_buffer.append(
("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict'))
("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'ignore'))

if keyword.lower() == 'connection':
if value.lower() == 'close':
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ def do_LATINONEHEADER(self):
body = self.headers['x-special-incoming'].encode('utf-8')
self.wfile.write(body)

def do_LATINONEEMOJIHEADER(self):
self.send_response(999)
self.send_header('X-Special', 'Emoji 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 do_SEND_ERROR(self):
self.send_error(int(self.path[1:]))

Expand Down Expand Up @@ -244,6 +252,14 @@ def test_latin1_header(self):
self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind')
self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8'))

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

def test_error_content_length(self):
# Issue #16088: standard error responses should have a content-length
self.con.request('NOTFOUND', '/')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use utf-8 instead of latin-1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use utf-8 instead of latin-1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use ignore mode instead strict for headers enconding in order to encode headers with special chars like emojis