lib: Reimplemented mtio-epoll timeout checking as a bin-heap.
[ashd.git] / python / ashd / scgi.py
CommitLineData
173e0e9e 1import sys
c06db49a
FT
2import threading
3
4class protoerr(Exception):
5 pass
6
81a0ca30
FT
7class closed(IOError):
8 def __init__(self):
9 super(closed, self).__init__("The client has closed the connection.")
10
c06db49a
FT
11def readns(sk):
12 hln = 0
13 while True:
14 c = sk.read(1)
173e0e9e 15 if c == ':':
c06db49a 16 break
173e0e9e
FT
17 elif c >= '0' or c <= '9':
18 hln = (hln * 10) + (ord(c) - ord('0'))
c06db49a 19 else:
173e0e9e 20 raise protoerr, "Invalid netstring length byte: " + c
c06db49a 21 ret = sk.read(hln)
173e0e9e
FT
22 if sk.read(1) != ',':
23 raise protoerr, "Non-terminated netstring"
c06db49a
FT
24 return ret
25
26def readhead(sk):
173e0e9e 27 parts = readns(sk).split('\0')[:-1]
c06db49a 28 if len(parts) % 2 != 0:
173e0e9e 29 raise protoerr, "Malformed headers"
c06db49a
FT
30 ret = {}
31 i = 0
32 while i < len(parts):
33 ret[parts[i]] = parts[i + 1]
34 i += 2
35 return ret
36
37class reqthread(threading.Thread):
38 def __init__(self, sk, handler):
39 super(reqthread, self).__init__(name = "SCGI request handler")
c329061e
FT
40 self.bsk = sk.dup()
41 self.sk = self.bsk.makefile("r+")
c06db49a
FT
42 self.handler = handler
43
44 def run(self):
45 try:
46 head = readhead(self.sk)
47 self.handler(head, self.sk)
48 finally:
49 self.sk.close()
c329061e 50 self.bsk.close()
c06db49a
FT
51
52def handlescgi(sk, handler):
53 t = reqthread(sk, handler)
54 t.start()
55
56def servescgi(socket, handler):
57 while True:
58 nsk, addr = socket.accept()
59 try:
60 handlescgi(nsk, handler)
61 finally:
62 nsk.close()
63
64def wrapwsgi(handler):
65 def handle(head, sk):
173e0e9e 66 env = dict(head)
c06db49a
FT
67 env["wsgi.version"] = 1, 0
68 if "HTTP_X_ASH_PROTOCOL" in env:
69 env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
70 elif "HTTPS" in env:
71 env["wsgi.url_scheme"] = "https"
72 else:
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
79
80 resp = []
81 respsent = []
82
699754de 83 def flushreq():
c06db49a
FT
84 if not respsent:
85 if not resp:
173e0e9e 86 raise Exception, "Trying to write data before starting response."
c06db49a
FT
87 status, headers = resp
88 respsent[:] = [True]
8bb0e3c1 89 try:
173e0e9e
FT
90 sk.write("Status: %s\n" % status)
91 for nm, val in headers:
92 sk.write("%s: %s\n" % (nm, val))
93 sk.write("\n")
8bb0e3c1
FT
94 except IOError:
95 raise closed()
699754de
FT
96
97 def write(data):
98 if not data:
99 return
8bb0e3c1 100 flushreq()
81a0ca30 101 try:
81a0ca30
FT
102 sk.write(data)
103 sk.flush()
104 except IOError:
105 raise closed()
c06db49a
FT
106
107 def startreq(status, headers, exc_info = None):
108 if resp:
109 if exc_info: # Interesting, this...
110 try:
111 if respsent:
173e0e9e 112 raise exc_info[0], exc_info[1], exc_info[2]
c06db49a
FT
113 finally:
114 exc_info = None # CPython GC bug?
115 else:
173e0e9e 116 raise Exception, "Can only start responding once."
c06db49a
FT
117 resp[:] = status, headers
118 return write
119
120 respiter = handler(env, startreq)
121 try:
8bb0e3c1
FT
122 try:
123 for data in respiter:
124 write(data)
125 if resp:
126 flushreq()
127 except closed:
128 pass
c06db49a
FT
129 finally:
130 if hasattr(respiter, "close"):
131 respiter.close()
132 return handle