diff --git a/examples/test.py b/examples/test.py index ffface083..09ea06f08 100644 --- a/examples/test.py +++ b/examples/test.py @@ -20,7 +20,8 @@ def app(environ, start_response): response_headers = [ ('Content-type', 'text/plain'), ('Content-Length', str(len(data))), - ('X-Gunicorn-Version', __version__) + ('X-Gunicorn-Version', __version__), + ('Foo', 'B\u00e5r'), # Foo: Bår ] start_response(status, response_headers) return iter([data]) diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index 593c8f24d..1b8b9cc0c 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -315,7 +315,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, "ascii")) + util.write(self.sock, util.to_bytestring(header_str, "latin-1")) self.headers_sent = True def write(self, arg): diff --git a/tests/test_http.py b/tests/test_http.py index a91f4794c..6bcb5f099 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -81,8 +81,13 @@ def test_http_header_encoding(): mocked_request = mock.MagicMock() response = Response(mocked_request, mocked_socket, None) - # set umlaut header + # set umlaut header value - latin-1 is OK response.headers.append(('foo', 'häder')) + response.send_headers() + + # set a-breve header value - unicode, non-latin-1 fails + response = Response(mocked_request, mocked_socket, None) + response.headers.append(('apple', 'măr')) with pytest.raises(UnicodeEncodeError): response.send_headers()