From 5f4ebd2eb2b08783a5fbefe79d09fcb3fc1fbc73 Mon Sep 17 00:00:00 2001 From: benoitc Date: Wed, 25 Nov 2015 13:25:27 +0100 Subject: [PATCH] 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 --- examples/test.py | 2 +- gunicorn/http/wsgi.py | 2 +- gunicorn/util.py | 14 ++------------ 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/examples/test.py b/examples/test.py index 84f9102c..476fd62f 100644 --- a/examples/test.py +++ b/examples/test.py @@ -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]) diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index 6804eb80..64e547e1 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -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): diff --git a/gunicorn/util.py b/gunicorn/util.py index ad99fb9d..49d08690 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -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)