don't return utf8 header in example

Since the updated RFC 7230 implys that new Headers Key and Value should be
sent as USASCII only don't try to test utf8 headers in examples.

We now only encode them to ascii. Gunicorn will fail if it's unable to encode
them letting the responsability to the application to correctly encode the
response. (we are just a gateway).

While i'm here simplify the code to not create an extra function only used at
one place.

NOTE: if anyone come to a better solution, i am happy to revisit it on the
next release.

fix #1151
This commit is contained in:
benoitc 2015-11-25 13:25:27 +01:00
parent 6b92575e00
commit 5f4ebd2eb2
3 changed files with 4 additions and 14 deletions

View File

@ -21,7 +21,7 @@ def app(environ, start_response):
('Content-type', 'text/plain'),
('Content-Length', str(len(data))),
('X-Gunicorn-Version', __version__),
("Test", "test тест"),
#("Test", "test тест"),
]
start_response(status, response_headers)
return iter([data])

View File

@ -320,7 +320,7 @@ class Response(object):
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_latin1(header_str))
util.write(self.sock, util.to_bytestring(header_str, "ascii"))
self.headers_sent = True
def write(self, arg):

View File

@ -25,7 +25,6 @@ from gunicorn.errors import AppImportError
from gunicorn.six import text_type
from gunicorn.workers import SUPPORTED_WORKERS
MAXFD = 1024
REDIRECT_TO = getattr(os, 'devnull', '/dev/null')
@ -498,23 +497,14 @@ def check_is_writeable(path):
f.close()
def to_bytestring(value):
def to_bytestring(value, encoding="utf8"):
"""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("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")
return value.encode(encoding)
def warn(msg):
print("!!!", file=sys.stderr)