X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=blobdiff_plain;f=python%2Fashd%2Fscgi.py;h=95325f215417e83c064cf94fa6be15589ceebb76;hp=a06267f530a37b295489d91269f05262c6cfa70b;hb=173e0e9efec5ae690cc157fe238113fcd814895e;hpb=188cd02daf85ef68a832deab4fcbf0daaf2d4573 diff --git a/python/ashd/scgi.py b/python/ashd/scgi.py index a06267f..95325f2 100644 --- a/python/ashd/scgi.py +++ b/python/ashd/scgi.py @@ -1,4 +1,4 @@ -import sys, collections +import sys import threading class protoerr(Exception): @@ -12,21 +12,21 @@ def readns(sk): 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): @@ -37,7 +37,7 @@ def readhead(sk): 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): @@ -59,17 +59,9 @@ def servescgi(socket, handler): 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"] @@ -86,25 +78,17 @@ def wrapwsgi(handler): 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() @@ -123,11 +107,11 @@ def wrapwsgi(handler): 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