Skip to content

Commit

Permalink
Header values are encoded using latin-1, not ascii. (#1914)
Browse files Browse the repository at this point in the history
This commit reverts one aspect changed by 5f4ebd2 (#1151);
header-values are again encoded as latin-1 and not ascii. Test is restored but uses
a latin-1-mappable test-character, not a general utf8 character.

Fixed #1778.

Signed-off-by: Brett Randall <javabrett@gmail.com>
(cherry picked from commit 879651b)
  • Loading branch information
javabrett authored and tilgovi committed Oct 13, 2019
1 parent 5d72755 commit b8b32c6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion examples/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
2 changes: 1 addition & 1 deletion gunicorn/http/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,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):
Expand Down
7 changes: 6 additions & 1 deletion tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', u'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', u'măr'))
with pytest.raises(UnicodeEncodeError):
response.send_headers()

Expand Down

0 comments on commit b8b32c6

Please sign in to comment.