Merge branch 'master' of git.dolda2000.com:/srv/git/r/ashd
[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-Address" in req: env["SERVER_ADDR"] = req["X-Ash-Server-Address"]
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"]
134     if "X-Ash-Port" in req: env["REMOTE_PORT"] = req["X-Ash-Port"]
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"]
142         del env["HTTP_CONTENT_LENGTH"]
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"]
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
154     def flushreq():
155         if not respsent:
156             if not resp:
157                 raise Exception, "Trying to write data before starting response."
158             status, headers = resp
159             respsent[:] = [True]
160             try:
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")
165             except IOError:
166                 raise closed()
167
168     def write(data):
169         if not data:
170             return
171         flushreq()
172         try:
173             req.sk.write(data)
174             req.sk.flush()
175         except IOError:
176             raise closed()
177
178     def startreq(status, headers, exc_info = None):
179         if resp:
180             if exc_info:                # Interesting, this...
181                 try:
182                     if respsent:
183                         raise exc_info[0], exc_info[1], exc_info[2]
184                 finally:
185                     exc_info = None     # CPython GC bug?
186             else:
187                 raise Exception, "Can only start responding once."
188         resp[:] = status, headers
189         return write
190
191     reqevent = ashd.perf.request(env)
192     exc = (None, None, None)
193     try:
194         try:
195             respiter = handler(env, startreq)
196             try:
197                 for data in respiter:
198                     write(data)
199                 if resp:
200                     flushreq()
201             finally:
202                 if hasattr(respiter, "close"):
203                     respiter.close()
204         except closed:
205             pass
206         if resp:
207             reqevent.response(resp)
208     except:
209         exc = sys.exc_info()
210         raise
211     finally:
212         reqevent.__exit__(*exc)
213
214 flightlock = threading.Condition()
215 inflight = 0
216
217 class reqthread(threading.Thread):
218     def __init__(self, req):
219         super(reqthread, self).__init__(name = "Request handler")
220         self.req = req.dup()
221     
222     def run(self):
223         global inflight
224         try:
225             flightlock.acquire()
226             try:
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
234             finally:
235                 flightlock.release()
236             try:
237                 dowsgi(self.req)
238             finally:
239                 flightlock.acquire()
240                 try:
241                     inflight -= 1
242                     flightlock.notify()
243                 finally:
244                     flightlock.release()
245         except:
246             log.error("exception occurred in handler thread", exc_info=True)
247         finally:
248             self.req.close()
249     
250 def handle(req):
251     reqthread(req).start()
252
253 ashd.util.serveloop(handle)