summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2012-06-28 23:40:17 +0200
committerHelmut Grohne <helmut@subdivi.de>2012-06-28 23:40:17 +0200
commitdd015aa622b25f8638ab0a13b5d75004d16004c8 (patch)
tree4d005a7aa3ca967b51de739d150badf47f7747a4
parent7e2e9173b2afcc2a8dca9e6047d0b82ad70c9dff (diff)
downloadwsgitools-dd015aa622b25f8638ab0a13b5d75004d16004c8.tar.gz
make scgi.asynchronous work with py3
-rw-r--r--wsgitools/scgi/asynchronous.py28
1 files changed, 17 insertions, 11 deletions
diff --git a/wsgitools/scgi/asynchronous.py b/wsgitools/scgi/asynchronous.py
index 7eb1a30..2c97f42 100644
--- a/wsgitools/scgi/asynchronous.py
+++ b/wsgitools/scgi/asynchronous.py
@@ -37,8 +37,8 @@ class SCGIConnection(asyncore.dispatcher):
self.state = SCGIConnection.NEW # internal state
self.environ = config.copy() # environment passed to wsgi app
self.reqlen = -1 # request length used in two different meanings
- self.inbuff = "" # input buffer
- self.outbuff = "" # output buffer
+ self.inbuff = b"" # input buffer
+ self.outbuff = b"" # output buffer
self.wsgihandler = None # wsgi application
self.wsgiiterator = None # wsgi application iterator
self.outheaders = () # headers to be sent
@@ -50,7 +50,10 @@ class SCGIConnection(asyncore.dispatcher):
assert not self.outbuff
status, headers = self.outheaders
headdata = "".join(map("%s: %s\r\n".__mod__, headers))
- self.outbuff = "Status: %s\r\n%s\r\n" % (status, headdata)
+ headdata = "Status: %s\r\n%s\r\n" % (status, headdata)
+ if not isinstance(headdata, bytes):
+ headdata = headdata.encode("iso-8859-1")
+ self.outbuff = headdata
self.outheaders = True
def _wsgi_write(self, data):
@@ -74,8 +77,8 @@ class SCGIConnection(asyncore.dispatcher):
data = self.recv(self.blocksize)
self.inbuff += data
if self.state == SCGIConnection.NEW:
- if ':' in self.inbuff:
- reqlen, self.inbuff = self.inbuff.split(':', 1)
+ if b':' in self.inbuff:
+ reqlen, self.inbuff = self.inbuff.split(b':', 1)
if not reqlen.isdigit():
self.close()
return # invalid request format
@@ -93,15 +96,18 @@ class SCGIConnection(asyncore.dispatcher):
buff = self.inbuff[:self.reqlen]
remainder = self.inbuff[self.reqlen:]
- while buff.count('\0') >= 2:
- key, value, buff = buff.split('\0', 2)
+ while buff.count(b'\0') >= 2:
+ key, value, buff = buff.split(b'\0', 2)
+ if not isinstance(key, str):
+ key = key.decode("iso-8859-1")
+ value = value.decode("iso-8859-1")
self.environ[key] = value
self.reqlen -= len(key) + len(value) + 2
self.inbuff = buff + remainder
if self.reqlen == 0:
- if self.inbuff.startswith(','):
+ if self.inbuff.startswith(b','):
self.inbuff = self.inbuff[1:]
if not self.environ.get("CONTENT_LENGTH", "bad").isdigit():
self.close()
@@ -119,7 +125,7 @@ class SCGIConnection(asyncore.dispatcher):
if len(self.inbuff) >= self.reqlen:
self.body.write(self.inbuff[:self.reqlen])
self.body.seek(0)
- self.inbuff = ""
+ self.inbuff = b""
self.reqlen = 0
_convert_environ(self.environ)
self.environ["wsgi.input"] = self.body
@@ -136,7 +142,7 @@ class SCGIConnection(asyncore.dispatcher):
else:
self.body.write(self.inbuff)
self.reqlen -= len(self.inbuff)
- self.inbuff = ""
+ self.inbuff = b""
def start_response(self, status, headers, exc_info=None):
assert isinstance(status, str)
@@ -165,7 +171,7 @@ class SCGIConnection(asyncore.dispatcher):
if len(self.outbuff) < self.blocksize:
self._try_send_headers()
for data in self.wsgiiterator:
- assert isinstance(data, str)
+ assert isinstance(data, bytes)
if data:
self.outbuff += data
break