doc: Fixed typo.
[ashd.git] / python / ashd / scgi.py
1 import sys
2 import threading
3
4 class protoerr(Exception):
5     pass
6
7 class closed(IOError):
8     def __init__(self):
9         super(closed, self).__init__("The client has closed the connection.")
10
11 def readns(sk):
12     hln = 0
13     while True:
14         c = sk.read(1)
15         if c == ':':
16             break
17         elif c >= '0' or c <= '9':
18             hln = (hln * 10) + (ord(c) - ord('0'))
19         else:
20             raise protoerr, "Invalid netstring length byte: " + c
21     ret = sk.read(hln)
22     if sk.read(1) != ',':
23         raise protoerr, "Non-terminated netstring"
24     return ret
25
26 def readhead(sk):
27     parts = readns(sk).split('\0')[:-1]
28     if len(parts) % 2 != 0:
29         raise protoerr, "Malformed headers"
30     ret = {}
31     i = 0
32     while i < len(parts):
33         ret[parts[i]] = parts[i + 1]
34         i += 2
35     return ret
36
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
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
50 def handlescgi(sk, handler):
51     t = reqthread(sk, handler)
52     t.start()
53
54 def servescgi(socket, handler):
55     while True:
56         nsk, addr = socket.accept()
57         try:
58             handlescgi(nsk, handler)
59         finally:
60             nsk.close()
61
62 def wrapwsgi(handler):
63     def handle(head, sk):
64         env = dict(head)
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
81         def flushreq():
82             if not respsent:
83                 if not resp:
84                     raise Exception, "Trying to write data before starting response."
85                 status, headers = resp
86                 respsent[:] = [True]
87                 try:
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")
92                 except IOError:
93                     raise closed()
94
95         def write(data):
96             if not data:
97                 return
98             flushreq()
99             try:
100                 sk.write(data)
101                 sk.flush()
102             except IOError:
103                 raise closed()
104
105         def startreq(status, headers, exc_info = None):
106             if resp:
107                 if exc_info:                # Interesting, this...
108                     try:
109                         if respsent:
110                             raise exc_info[0], exc_info[1], exc_info[2]
111                     finally:
112                         exc_info = None     # CPython GC bug?
113                 else:
114                     raise Exception, "Can only start responding once."
115             resp[:] = status, headers
116             return write
117
118         respiter = handler(env, startreq)
119         try:
120             try:
121                 for data in respiter:
122                     write(data)
123                 if resp:
124                     flushreq()
125             except closed:
126                 pass
127         finally:
128             if hasattr(respiter, "close"):
129                 respiter.close()
130     return handle