4 class protoerr(Exception):
9 super(closed, self).__init__("The client has closed the connection.")
17 elif c >= '0' or c <= '9':
18 hln = (hln * 10) + (ord(c) - ord('0'))
20 raise protoerr, "Invalid netstring length byte: " + c
23 raise protoerr, "Non-terminated netstring"
27 parts = readns(sk).split('\0')[:-1]
28 if len(parts) % 2 != 0:
29 raise protoerr, "Malformed headers"
33 ret[parts[i]] = parts[i + 1]
37 class reqthread(threading.Thread):
38 def __init__(self, sk, handler):
39 super(reqthread, self).__init__(name = "SCGI request handler")
41 self.sk = self.bsk.makefile("r+")
42 self.handler = handler
46 head = readhead(self.sk)
47 self.handler(head, self.sk)
52 def handlescgi(sk, handler):
53 t = reqthread(sk, handler)
56 def servescgi(socket, handler):
58 nsk, addr = socket.accept()
60 handlescgi(nsk, handler)
64 def wrapwsgi(handler):
67 env["wsgi.version"] = 1, 0
68 if "HTTP_X_ASH_PROTOCOL" in env:
69 env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
71 env["wsgi.url_scheme"] = "https"
73 env["wsgi.url_scheme"] = "http"
74 env["wsgi.input"] = sk
75 env["wsgi.errors"] = sys.stderr
76 env["wsgi.multithread"] = True
77 env["wsgi.multiprocess"] = False
78 env["wsgi.run_once"] = False
86 raise Exception, "Trying to write data before starting response."
87 status, headers = resp
90 sk.write("Status: %s\n" % status)
91 for nm, val in headers:
92 sk.write("%s: %s\n" % (nm, val))
107 def startreq(status, headers, exc_info = None):
109 if exc_info: # Interesting, this...
112 raise exc_info[0], exc_info[1], exc_info[2]
114 exc_info = None # CPython GC bug?
116 raise Exception, "Can only start responding once."
117 resp[:] = status, headers
120 respiter = handler(env, startreq)
123 for data in respiter:
130 if hasattr(respiter, "close"):