Skip to content

Commit

Permalink
don't return utf8 header in example
Browse files Browse the repository at this point in the history
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
  • Loading branch information
benoitc committed Nov 25, 2015
1 parent 6b92575 commit 5f4ebd2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
2 changes: 1 addition & 1 deletion gunicorn/http/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,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_latin1(header_str))
util.write(self.sock, util.to_bytestring(header_str, "ascii"))
self.headers_sent = True

def write(self, arg):
Expand Down
14 changes: 2 additions & 12 deletions gunicorn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from gunicorn.six import text_type
from gunicorn.workers import SUPPORTED_WORKERS


MAXFD = 1024
REDIRECT_TO = getattr(os, 'devnull', '/dev/null')

Expand Down Expand Up @@ -498,23 +497,14 @@ def check_is_writeable(path):
f.close()


def to_bytestring(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("utf-8")


def to_latin1(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("latin-1")

return value.encode(encoding)

def warn(msg):
print("!!!", file=sys.stderr)
Expand Down

0 comments on commit 5f4ebd2

Please sign in to comment.