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