-import sys, collections
+import sys
import threading
class protoerr(Exception):
hln = 0
while True:
c = sk.read(1)
- if c == b':':
+ if c == ':':
break
- elif c >= b'0' or c <= b'9':
- hln = (hln * 10) + (ord(c) - ord(b'0'))
+ elif c >= '0' or c <= '9':
+ hln = (hln * 10) + (ord(c) - ord('0'))
else:
- raise protoerr("Invalid netstring length byte: " + c)
+ raise protoerr, "Invalid netstring length byte: " + c
ret = sk.read(hln)
- if sk.read(1) != b',':
- raise protoerr("Non-terminated netstring")
+ if sk.read(1) != ',':
+ raise protoerr, "Non-terminated netstring"
return ret
def readhead(sk):
- parts = readns(sk).split(b'\0')[:-1]
+ parts = readns(sk).split('\0')[:-1]
if len(parts) % 2 != 0:
- raise protoerr("Malformed headers")
+ raise protoerr, "Malformed headers"
ret = {}
i = 0
while i < len(parts):
class reqthread(threading.Thread):
def __init__(self, sk, handler):
super(reqthread, self).__init__(name = "SCGI request handler")
- self.sk = sk.dup().makefile("rwb")
+ self.sk = sk.dup().makefile("r+")
self.handler = handler
def run(self):
finally:
nsk.close()
-def decodehead(head, coding):
- return {k.decode(coding): v.decode(coding) for k, v in head.items()}
-
def wrapwsgi(handler):
def handle(head, sk):
- try:
- env = decodehead(head, "utf-8")
- env["wsgi.uri_encoding"] = "utf-8"
- except UnicodeError:
- env = decodehead(head, "latin-1")
- env["wsgi.uri_encoding"] = "latin-1"
+ env = dict(head)
env["wsgi.version"] = 1, 0
if "HTTP_X_ASH_PROTOCOL" in env:
env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
resp = []
respsent = []
- def recode(thing):
- if isinstance(thing, collections.ByteString):
- return thing
- else:
- return str(thing).encode("latin-1")
-
def flushreq():
if not respsent:
if not resp:
- raise Exception("Trying to write data before starting response.")
+ raise Exception, "Trying to write data before starting response."
status, headers = resp
respsent[:] = [True]
- buf = bytearray()
- buf += b"Status: " + recode(status) + b"\n"
- for nm, val in headers:
- buf += recode(nm) + b": " + recode(val) + b"\n"
- buf += b"\n"
try:
- sk.write(buf)
+ sk.write("Status: %s\n" % status)
+ for nm, val in headers:
+ sk.write("%s: %s\n" % (nm, val))
+ sk.write("\n")
except IOError:
raise closed()
if exc_info: # Interesting, this...
try:
if respsent:
- raise exc_info[1]
+ raise exc_info[0], exc_info[1], exc_info[2]
finally:
exc_info = None # CPython GC bug?
else:
- raise Exception("Can only start responding once.")
+ raise Exception, "Can only start responding once."
resp[:] = status, headers
return write