Need to actually encode headers being sent...

This commit is contained in:
Phillip J. Eby 2011-01-07 15:53:02 +00:00
parent f9ef4167cb
commit 90ea9f78d1
1 changed files with 12 additions and 7 deletions

View File

@ -283,12 +283,15 @@ server.
enc, esc = sys.getfilesystemencoding(), 'surrogateescape'
def wsgi_string(u):
def unicode_to_wsgi(u):
# Convert an environment variable to a WSGI "bytes-as-unicode" string
return u.encode(enc, esc).decode('iso-8859-1')
def wsgi_to_bytes(s):
return s.encode('iso-8859-1')
def run_with_cgi(application):
environ = {k: wsgi_string(v) for k,v in os.environ.items()}
environ = {k: unicode_to_wsgi(v) for k,v in os.environ.items()}
environ['wsgi.input'] = sys.stdin
environ['wsgi.errors'] = sys.stderr
environ['wsgi.version'] = (1, 0)
@ -305,19 +308,21 @@ server.
headers_sent = []
def write(data):
out = sys.stdout.buffer
if not headers_set:
raise AssertionError("write() before start_response()")
elif not headers_sent:
# Before the first output, send the stored headers
status, response_headers = headers_sent[:] = headers_set
sys.stdout.buffer.write('Status: %s\r\n' % status)
out.write(wsgi_to_bytes('Status: %s\r\n' % status))
for header in response_headers:
sys.stdout.buffer.write('%s: %s\r\n' % header)
sys.stdout.write('\r\n')
out.write(wsgi_to_bytes('%s: %s\r\n' % header))
out.write(wsgi_to_bytes('\r\n'))
sys.stdout.buffer.write(data)
sys.stdout.buffer.flush()
out.write(data)
out.flush()
def start_response(status, response_headers, exc_info=None):
if exc_info: