Skip to content

Commit

Permalink
encode http headers as latin1 RFC 2616
Browse files Browse the repository at this point in the history
  • Loading branch information
ephes committed Aug 29, 2015
1 parent 209a605 commit 71eeb75
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/source/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Commonly Used Arguments
Check the :ref:`faq` for ideas on tuning this parameter.
* ``-k WORKERCLASS, --worker-class=WORKERCLASS`` - The type of worker process
to run. You'll definitely want to read the production page for the
implications of this parameter. You can set this to ``$(NAME)``
implications of this parameter. You can set this to ``egg:gunicorn#$(NAME)``
where ``$(NAME)`` is one of ``sync``, ``eventlet``, ``gevent``, or
``tornado``, ``gthread``, ``gaiohttp``. ``sync`` is the default.
* ``-n APP_NAME, --name=APP_NAME`` - If setproctitle_ is installed you can
Expand Down
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 71eeb75

Please sign in to comment.