mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Merge pull request #1102 from ephes/latin1_headers
ISO-8859-1 encoded http headers
This commit is contained in:
commit
9c1d4424e0
@ -317,7 +317,7 @@ class Response(object):
|
|||||||
tosend.extend(["%s: %s\r\n" % (k, v) for k, v in self.headers])
|
tosend.extend(["%s: %s\r\n" % (k, v) for k, v in self.headers])
|
||||||
|
|
||||||
header_str = "%s\r\n" % "".join(tosend)
|
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
|
self.headers_sent = True
|
||||||
|
|
||||||
def write(self, arg):
|
def write(self, arg):
|
||||||
|
|||||||
@ -508,6 +508,15 @@ def to_bytestring(value):
|
|||||||
return value.encode("utf-8")
|
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):
|
def is_fileobject(obj):
|
||||||
if not hasattr(obj, "tell") or not hasattr(obj, "fileno"):
|
if not hasattr(obj, "tell") or not hasattr(obj, "fileno"):
|
||||||
return False
|
return False
|
||||||
|
|||||||
@ -1,6 +1,14 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
import t
|
import t
|
||||||
|
from gunicorn import util
|
||||||
from gunicorn.http.body import Body
|
from gunicorn.http.body import Body
|
||||||
|
from gunicorn.http.wsgi import Response
|
||||||
from gunicorn.six import BytesIO
|
from gunicorn.six import BytesIO
|
||||||
|
try:
|
||||||
|
import unittest.mock as mock
|
||||||
|
except ImportError:
|
||||||
|
import mock
|
||||||
|
|
||||||
|
|
||||||
def assert_readline(payload, size, expected):
|
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"\n"
|
||||||
assert body.readline(2) == b"de"
|
assert body.readline(2) == b"de"
|
||||||
assert body.readline(2) == b"f"
|
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))
|
||||||
Loading…
x
Reference in New Issue
Block a user