Ticket #65 (new defect)

Opened 4 months ago

flud.cgi does not handle bytes/str properly

Reported by: trac Assigned to: asaddi
Priority: major Milestone: flup.server-1.0.3
Component: flup.server Version: 1.0.2
Keywords: cgi Cc: marc.zonzon@gmail.com

Description

I could not get flup-py3.0 server.cgi.py working with python 3.2 because of wrong handling of bytes/str. This patch seems to work ok

--- /home/share/marc/python/virtualenv/python3/lib/python3.2/site-packages/flup/ server/cgi.py 2012-01-20 10:29:17.000000000 +0100 +++ cgi.py 2012-01-20 16:34:00.000000000 +0100 @@ -1,8 +1,11 @@ -# Taken from <http://www.python.org/dev/peps/pep-0333/> -# which was placed in the public domain. +# Taken from flup flup-py3.0 138:382e66286d1f and modified for python 3.x +# Under Python 3.x sys.stdin, sys.stdout, sys.stderr are str, not bytes +# So I use the bytes buffer +# To write the headers write them as ascii

import os, sys

- +import codecs +header_writer = codecs.getwriter('ascii')(sys.stdout.buffer)

all = WSGIServer?

@@ -15,8 +18,8 @@

def run(self):

environ = dict(list(os.environ.items()))

- environwsgi.input? = sys.stdin - environwsgi.errors? = sys.stderr + environwsgi.input? = sys.stdin.buffer + environwsgi.errors? = sys.stderr.buffer

environwsgi.version? = (1,0) environwsgi.multithread? = False environwsgi.multiprocess? = True

@@ -37,13 +40,13 @@

elif not headers_sent:

# Before the first output, send the stored headers status, response_headers = headers_sent[:] = headers_set

- sys.stdout.write('Status: %s\r\n' % status) + header_writer.write('Status: %s\r\n' % status)

for header in response_headers:

- sys.stdout.write('%s: %s\r\n' % header) - sys.stdout.write('\r\n') + header_writer.write('%s: %s\r\n' % header) + header_writer.write('\r\n') - sys.stdout.write(data) - sys.stdout.flush() + sys.stdout.buffer.write(data) + sys.stdout.buffer.flush()

def start_response(status,response_headers,exc_info=None):

if exc_info: