4 class protoerr(Exception):
13 elif c >= '0' or c <= '9':
14 hln = (hln * 10) + (ord(c) - ord('0'))
16 raise protoerr, "Invalid netstring length byte: " + c
19 raise protoerr, "Non-terminated netstring"
23 parts = readns(sk).split('\0')[:-1]
24 if len(parts) % 2 != 0:
25 raise protoerr, "Malformed headers"
29 ret[parts[i]] = parts[i + 1]
33 class reqthread(threading.Thread):
34 def __init__(self, sk, handler):
35 super(reqthread, self).__init__(name = "SCGI request handler")
36 self.sk = sk.dup().makefile("r+")
37 self.handler = handler
41 head = readhead(self.sk)
42 self.handler(head, self.sk)
46 def handlescgi(sk, handler):
47 t = reqthread(sk, handler)
50 def servescgi(socket, handler):
52 nsk, addr = socket.accept()
54 handlescgi(nsk, handler)
58 def wrapwsgi(handler):
61 env["wsgi.version"] = 1, 0
62 if "HTTP_X_ASH_PROTOCOL" in env:
63 env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
65 env["wsgi.url_scheme"] = "https"
67 env["wsgi.url_scheme"] = "http"
68 env["wsgi.input"] = sk
69 env["wsgi.errors"] = sys.stderr
70 env["wsgi.multithread"] = True
71 env["wsgi.multiprocess"] = False
72 env["wsgi.run_once"] = False
82 raise Exception, "Trying to write data before starting response."
83 status, headers = resp
85 sk.write("Status: %s\n" % status)
86 for nm, val in headers:
87 sk.write("%s: %s\n" % (nm, val))
92 def startreq(status, headers, exc_info = None):
94 if exc_info: # Interesting, this...
97 raise exc_info[0], exc_info[1], exc_info[2]
99 exc_info = None # CPython GC bug?
101 raise Exception, "Can only start responding once."
102 resp[:] = status, headers
105 respiter = handler(env, startreq)
107 for data in respiter:
111 if hasattr(respiter, "close"):