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