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")
40 self.sk = sk.dup().makefile("r+")
41 self.handler = handler
45 head = readhead(self.sk)
46 self.handler(head, self.sk)
50 def handlescgi(sk, handler):
51 t = reqthread(sk, handler)
54 def servescgi(socket, handler):
56 nsk, addr = socket.accept()
58 handlescgi(nsk, handler)
62 def wrapwsgi(handler):
65 env["wsgi.version"] = 1, 0
66 if "HTTP_X_ASH_PROTOCOL" in env:
67 env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
69 env["wsgi.url_scheme"] = "https"
71 env["wsgi.url_scheme"] = "http"
72 env["wsgi.input"] = sk
73 env["wsgi.errors"] = sys.stderr
74 env["wsgi.multithread"] = True
75 env["wsgi.multiprocess"] = False
76 env["wsgi.run_once"] = False
84 raise Exception, "Trying to write data before starting response."
85 status, headers = resp
88 sk.write("Status: %s\n" % status)
89 for nm, val in headers:
90 sk.write("%s: %s\n" % (nm, val))
105 def startreq(status, headers, exc_info = None):
107 if exc_info: # Interesting, this...
110 raise exc_info[0], exc_info[1], exc_info[2]
112 exc_info = None # CPython GC bug?
114 raise Exception, "Can only start responding once."
115 resp[:] = status, headers
118 respiter = handler(env, startreq)
121 for data in respiter:
128 if hasattr(respiter, "close"):