Merge pull request #1102 from ephes/latin1_headers

ISO-8859-1 encoded http headers
This commit is contained in:
Berker Peksag 2015-08-31 06:55:04 +03:00
commit 9c1d4424e0
3 changed files with 38 additions and 1 deletions

View File

@ -317,7 +317,7 @@ class Response(object):
tosend.extend(["%s: %s\r\n" % (k, v) for k, v in self.headers])
header_str = "%s\r\n" % "".join(tosend)
util.write(self.sock, util.to_bytestring(header_str))
util.write(self.sock, util.to_latin1(header_str))
self.headers_sent = True
def write(self, arg):

View File

@ -508,6 +508,15 @@ def to_bytestring(value):
return value.encode("utf-8")
def to_latin1(value):
"""Converts a string argument to a byte string"""
if isinstance(value, bytes):
return value
if not isinstance(value, text_type):
raise TypeError('%r is not a string' % value)
return value.encode("latin-1")
def is_fileobject(obj):
if not hasattr(obj, "tell") or not hasattr(obj, "fileno"):
return False

View File

@ -1,6 +1,14 @@
# -*- encoding: utf-8 -*-
import t
from gunicorn import util
from gunicorn.http.body import Body
from gunicorn.http.wsgi import Response
from gunicorn.six import BytesIO
try:
import unittest.mock as mock
except ImportError:
import mock
def assert_readline(payload, size, expected):
@ -57,3 +65,23 @@ def test_readline_buffer_loaded_with_size():
assert body.readline(2) == b"\n"
assert body.readline(2) == b"de"
assert body.readline(2) == b"f"
def test_http_header_encoding():
""" tests whether http response headers are ISO-8859-1 encoded """
mocked_socket = mock.MagicMock()
mocked_socket.sendall = mock.MagicMock()
mocked_request = mock.MagicMock()
response = Response(mocked_request, mocked_socket, None)
# set umlaut header
response.headers.append(('foo', 'häder'))
response.send_headers()
# build our own header_str to compare against
tosend = response.default_headers()
tosend.extend(["%s: %s\r\n" % (k, v) for k, v in response.headers])
header_str = "%s\r\n" % "".join(tosend)
mocked_socket.sendall.assert_called_with(util.to_latin1(header_str))