Skip to content

Commit

Permalink
Merge pull request benoitc#1102 from ephes/latin1_headers
Browse files Browse the repository at this point in the history
ISO-8859-1 encoded http headers
  • Loading branch information
berkerpeksag committed Aug 31, 2015
2 parents 209a605 + 338721a commit 9c1d442
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gunicorn/http/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def send_headers(self):
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):
Expand Down
9 changes: 9 additions & 0 deletions gunicorn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/test_http_body.py → tests/test_http.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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))

0 comments on commit 9c1d442

Please sign in to comment.