doc: Fixed typo.
[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")
173e0e9e 40 self.sk = sk.dup().makefile("r+")
c06db49a
FT
41 self.handler = handler
42
43 def run(self):
44 try:
45 head = readhead(self.sk)
46 self.handler(head, self.sk)
47 finally:
48 self.sk.close()
49
50def handlescgi(sk, handler):
51 t = reqthread(sk, handler)
52 t.start()
53
54def servescgi(socket, handler):
55 while True:
56 nsk, addr = socket.accept()
57 try:
58 handlescgi(nsk, handler)
59 finally:
60 nsk.close()
61
62def wrapwsgi(handler):
63 def handle(head, sk):
173e0e9e 64 env = dict(head)
c06db49a
FT
65 env["wsgi.version"] = 1, 0
66 if "HTTP_X_ASH_PROTOCOL" in env:
67 env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
68 elif "HTTPS" in env:
69 env["wsgi.url_scheme"] = "https"
70 else:
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
77
78 resp = []
79 respsent = []
80
699754de 81 def flushreq():
c06db49a
FT
82 if not respsent:
83 if not resp:
173e0e9e 84 raise Exception, "Trying to write data before starting response."
c06db49a
FT
85 status, headers = resp
86 respsent[:] = [True]
8bb0e3c1 87 try:
173e0e9e
FT
88 sk.write("Status: %s\n" % status)
89 for nm, val in headers:
90 sk.write("%s: %s\n" % (nm, val))
91 sk.write("\n")
8bb0e3c1
FT
92 except IOError:
93 raise closed()
699754de
FT
94
95 def write(data):
96 if not data:
97 return
8bb0e3c1 98 flushreq()
81a0ca30 99 try:
81a0ca30
FT
100 sk.write(data)
101 sk.flush()
102 except IOError:
103 raise closed()
c06db49a
FT
104
105 def startreq(status, headers, exc_info = None):
106 if resp:
107 if exc_info: # Interesting, this...
108 try:
109 if respsent:
173e0e9e 110 raise exc_info[0], exc_info[1], exc_info[2]
c06db49a
FT
111 finally:
112 exc_info = None # CPython GC bug?
113 else:
173e0e9e 114 raise Exception, "Can only start responding once."
c06db49a
FT
115 resp[:] = status, headers
116 return write
117
118 respiter = handler(env, startreq)
119 try:
8bb0e3c1
FT
120 try:
121 for data in respiter:
122 write(data)
123 if resp:
124 flushreq()
125 except closed:
126 pass
c06db49a
FT
127 finally:
128 if hasattr(respiter, "close"):
129 respiter.close()
130 return handle