Merge branch 'master' of git.dolda2000.com:/srv/git/r/ashd
[ashd.git] / python / ashd-wsgi
CommitLineData
173e0e9e 1#!/usr/bin/python
c270f222 2
14640dcc 3import sys, os, getopt, threading, logging, time
d5ee5cde
FT
4import ashd.proto, ashd.util, ashd.perf
5try:
6 import pdm.srv
7except:
8 pdm = None
c270f222
FT
9
10def usage(out):
14640dcc 11 out.write("usage: ashd-wsgi [-hAL] [-m PDM-SPEC] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n")
c270f222 12
3e11d7ed 13reqlimit = 0
c270f222 14modwsgi_compat = False
14640dcc 15setlog = True
d5ee5cde 16opts, args = getopt.getopt(sys.argv[1:], "+hAp:l:m:")
c270f222
FT
17for o, a in opts:
18 if o == "-h":
19 usage(sys.stdout)
20 sys.exit(0)
21 elif o == "-p":
22 sys.path.insert(0, a)
14640dcc
FT
23 elif o == "-L":
24 setlog = False
c270f222
FT
25 elif o == "-A":
26 modwsgi_compat = True
3e11d7ed
FT
27 elif o == "-l":
28 reqlimit = int(a)
d5ee5cde
FT
29 elif o == "-m":
30 if pdm is not None:
31 pdm.srv.listen(a)
c270f222
FT
32if len(args) < 1:
33 usage(sys.stderr)
34 sys.exit(1)
14640dcc
FT
35if setlog:
36 logging.basicConfig(format="ashd-wsgi(%(name)s): %(levelname)s: %(message)s")
64a8cd9f 37log = logging.getLogger("ashd-wsgi")
c270f222
FT
38
39try:
40 handlermod = __import__(args[0], fromlist = ["dummy"])
173e0e9e
FT
41except ImportError, exc:
42 sys.stderr.write("ashd-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
c270f222
FT
43 sys.exit(1)
44if not modwsgi_compat:
45 if not hasattr(handlermod, "wmain"):
46 sys.stderr.write("ashd-wsgi: handler %s has no `wmain' function\n" % args[0])
47 sys.exit(1)
adb11d5f 48 handler = handlermod.wmain(*args[1:])
c270f222
FT
49else:
50 if not hasattr(handlermod, "application"):
51 sys.stderr.write("ashd-wsgi: handler %s has no `application' object\n" % args[0])
52 sys.exit(1)
53 handler = handlermod.application
54
81a0ca30
FT
55class closed(IOError):
56 def __init__(self):
173e0e9e 57 super(closed, self).__init__("The client has closed the connection.")
81a0ca30 58
70d942a7
FT
59cwd = os.getcwd()
60def absolutify(path):
61 if path[0] != '/':
62 return os.path.join(cwd, path)
63 return path
64
09c82f9c 65def unquoteurl(url):
173e0e9e 66 buf = ""
09c82f9c
FT
67 i = 0
68 while i < len(url):
69 c = url[i]
70 i += 1
173e0e9e 71 if c == '%':
370d235f 72 if len(url) >= i + 2:
09c82f9c 73 c = 0
173e0e9e
FT
74 if '0' <= url[i] <= '9':
75 c |= (ord(url[i]) - ord('0')) << 4
76 elif 'a' <= url[i] <= 'f':
77 c |= (ord(url[i]) - ord('a') + 10) << 4
78 elif 'A' <= url[i] <= 'F':
79 c |= (ord(url[i]) - ord('A') + 10) << 4
09c82f9c
FT
80 else:
81 raise ValueError("Illegal URL escape character")
173e0e9e
FT
82 if '0' <= url[i + 1] <= '9':
83 c |= ord(url[i + 1]) - ord('0')
84 elif 'a' <= url[i + 1] <= 'f':
85 c |= ord(url[i + 1]) - ord('a') + 10
86 elif 'A' <= url[i + 1] <= 'F':
87 c |= ord(url[i + 1]) - ord('A') + 10
09c82f9c
FT
88 else:
89 raise ValueError("Illegal URL escape character")
173e0e9e 90 buf += chr(c)
09c82f9c
FT
91 i += 2
92 else:
93 raise ValueError("Incomplete URL escape character")
94 else:
173e0e9e 95 buf += c
09c82f9c 96 return buf
81a0ca30 97
c270f222
FT
98def dowsgi(req):
99 env = {}
100 env["wsgi.version"] = 1, 0
101 for key, val in req.headers:
173e0e9e 102 env["HTTP_" + key.upper().replace("-", "_")] = val
c270f222
FT
103 env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
104 env["GATEWAY_INTERFACE"] = "CGI/1.1"
173e0e9e
FT
105 env["SERVER_PROTOCOL"] = req.ver
106 env["REQUEST_METHOD"] = req.method
107 env["REQUEST_URI"] = req.url
108 name = req.url
c270f222
FT
109 p = name.find('?')
110 if p >= 0:
c270f222 111 env["QUERY_STRING"] = name[p + 1:]
8498ab28 112 name = name[:p]
c270f222
FT
113 else:
114 env["QUERY_STRING"] = ""
173e0e9e 115 if name[-len(req.rest):] == req.rest:
53d666ca 116 # This is the same hack used in call*cgi.
173e0e9e
FT
117 name = name[:-len(req.rest)]
118 try:
119 pi = unquoteurl(req.rest)
120 except:
121 pi = req.rest
122 if name == '/':
53d666ca
FT
123 # This seems to be normal CGI behavior, but see callcgi.c for
124 # details.
125 pi = "/" + pi
126 name = ""
c270f222 127 env["SCRIPT_NAME"] = name
53d666ca 128 env["PATH_INFO"] = pi
173e0e9e 129 if "Host" in req: env["SERVER_NAME"] = req["Host"]
1af656d2 130 if "X-Ash-Server-Address" in req: env["SERVER_ADDR"] = req["X-Ash-Server-Address"]
173e0e9e
FT
131 if "X-Ash-Server-Port" in req: env["SERVER_PORT"] = req["X-Ash-Server-Port"]
132 if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == "https": env["HTTPS"] = "on"
133 if "X-Ash-Address" in req: env["REMOTE_ADDR"] = req["X-Ash-Address"]
1af656d2 134 if "X-Ash-Port" in req: env["REMOTE_PORT"] = req["X-Ash-Port"]
0bf0720d
FT
135 if "Content-Type" in req:
136 env["CONTENT_TYPE"] = req["Content-Type"]
137 # The CGI specification does not strictly require this, but
138 # many actualy programs and libraries seem to.
139 del env["HTTP_CONTENT_TYPE"]
140 if "Content-Length" in req:
141 env["CONTENT_LENGTH"] = req["Content-Length"]
75c134b6 142 del env["HTTP_CONTENT_LENGTH"]
173e0e9e
FT
143 if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"])
144 if "X-Ash-Protocol" in req: env["wsgi.url_scheme"] = req["X-Ash-Protocol"]
c270f222
FT
145 env["wsgi.input"] = req.sk
146 env["wsgi.errors"] = sys.stderr
147 env["wsgi.multithread"] = True
148 env["wsgi.multiprocess"] = False
149 env["wsgi.run_once"] = False
150
151 resp = []
152 respsent = []
153
699754de 154 def flushreq():
c270f222
FT
155 if not respsent:
156 if not resp:
173e0e9e 157 raise Exception, "Trying to write data before starting response."
c270f222
FT
158 status, headers = resp
159 respsent[:] = [True]
8bb0e3c1 160 try:
173e0e9e
FT
161 req.sk.write("HTTP/1.1 %s\n" % status)
162 for nm, val in headers:
163 req.sk.write("%s: %s\n" % (nm, val))
164 req.sk.write("\n")
8bb0e3c1
FT
165 except IOError:
166 raise closed()
699754de
FT
167
168 def write(data):
169 if not data:
170 return
8bb0e3c1 171 flushreq()
81a0ca30
FT
172 try:
173 req.sk.write(data)
174 req.sk.flush()
175 except IOError:
176 raise closed()
c270f222
FT
177
178 def startreq(status, headers, exc_info = None):
179 if resp:
180 if exc_info: # Interesting, this...
181 try:
182 if respsent:
173e0e9e 183 raise exc_info[0], exc_info[1], exc_info[2]
c270f222
FT
184 finally:
185 exc_info = None # CPython GC bug?
186 else:
173e0e9e 187 raise Exception, "Can only start responding once."
c270f222
FT
188 resp[:] = status, headers
189 return write
190
d5ee5cde
FT
191 reqevent = ashd.perf.request(env)
192 exc = (None, None, None)
c270f222 193 try:
8bb0e3c1 194 try:
64a8cd9f 195 respiter = handler(env, startreq)
d5ee5cde
FT
196 try:
197 for data in respiter:
198 write(data)
199 if resp:
200 flushreq()
64a8cd9f
FT
201 finally:
202 if hasattr(respiter, "close"):
203 respiter.close()
204 except closed:
205 pass
d5ee5cde
FT
206 if resp:
207 reqevent.response(resp)
208 except:
209 exc = sys.exc_info()
f1263757 210 raise
c270f222 211 finally:
d5ee5cde 212 reqevent.__exit__(*exc)
c270f222 213
3e11d7ed
FT
214flightlock = threading.Condition()
215inflight = 0
216
c270f222
FT
217class reqthread(threading.Thread):
218 def __init__(self, req):
173e0e9e 219 super(reqthread, self).__init__(name = "Request handler")
c270f222
FT
220 self.req = req.dup()
221
222 def run(self):
3e11d7ed 223 global inflight
c270f222 224 try:
173e0e9e
FT
225 flightlock.acquire()
226 try:
3e11d7ed
FT
227 if reqlimit != 0:
228 start = time.time()
229 while inflight >= reqlimit:
230 flightlock.wait(10)
231 if time.time() - start > 10:
232 os.abort()
233 inflight += 1
173e0e9e
FT
234 finally:
235 flightlock.release()
3e11d7ed
FT
236 try:
237 dowsgi(self.req)
238 finally:
173e0e9e
FT
239 flightlock.acquire()
240 try:
3e11d7ed
FT
241 inflight -= 1
242 flightlock.notify()
173e0e9e
FT
243 finally:
244 flightlock.release()
64a8cd9f
FT
245 except:
246 log.error("exception occurred in handler thread", exc_info=True)
c270f222
FT
247 finally:
248 self.req.close()
249
250def handle(req):
251 reqthread(req).start()
252
4e7888f7 253ashd.util.serveloop(handle)