python: Properly abort when over request limit.
[ashd.git] / python / ashd-wsgi
CommitLineData
173e0e9e 1#!/usr/bin/python
c270f222 2
14640dcc 3import sys, os, getopt, threading, logging, time
761e7292 4import ashd.proto, ashd.util, ashd.perf, ashd.serve
d5ee5cde
FT
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
70d942a7
FT
55cwd = os.getcwd()
56def absolutify(path):
57 if path[0] != '/':
58 return os.path.join(cwd, path)
59 return path
60
09c82f9c 61def unquoteurl(url):
173e0e9e 62 buf = ""
09c82f9c
FT
63 i = 0
64 while i < len(url):
65 c = url[i]
66 i += 1
173e0e9e 67 if c == '%':
370d235f 68 if len(url) >= i + 2:
09c82f9c 69 c = 0
173e0e9e
FT
70 if '0' <= url[i] <= '9':
71 c |= (ord(url[i]) - ord('0')) << 4
72 elif 'a' <= url[i] <= 'f':
73 c |= (ord(url[i]) - ord('a') + 10) << 4
74 elif 'A' <= url[i] <= 'F':
75 c |= (ord(url[i]) - ord('A') + 10) << 4
09c82f9c
FT
76 else:
77 raise ValueError("Illegal URL escape character")
173e0e9e
FT
78 if '0' <= url[i + 1] <= '9':
79 c |= ord(url[i + 1]) - ord('0')
80 elif 'a' <= url[i + 1] <= 'f':
81 c |= ord(url[i + 1]) - ord('a') + 10
82 elif 'A' <= url[i + 1] <= 'F':
83 c |= ord(url[i + 1]) - ord('A') + 10
09c82f9c
FT
84 else:
85 raise ValueError("Illegal URL escape character")
173e0e9e 86 buf += chr(c)
09c82f9c
FT
87 i += 2
88 else:
89 raise ValueError("Incomplete URL escape character")
90 else:
173e0e9e 91 buf += c
09c82f9c 92 return buf
81a0ca30 93
761e7292 94def mkenv(req):
c270f222
FT
95 env = {}
96 env["wsgi.version"] = 1, 0
97 for key, val in req.headers:
173e0e9e 98 env["HTTP_" + key.upper().replace("-", "_")] = val
c270f222
FT
99 env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
100 env["GATEWAY_INTERFACE"] = "CGI/1.1"
173e0e9e
FT
101 env["SERVER_PROTOCOL"] = req.ver
102 env["REQUEST_METHOD"] = req.method
103 env["REQUEST_URI"] = req.url
104 name = req.url
c270f222
FT
105 p = name.find('?')
106 if p >= 0:
c270f222 107 env["QUERY_STRING"] = name[p + 1:]
8498ab28 108 name = name[:p]
c270f222
FT
109 else:
110 env["QUERY_STRING"] = ""
173e0e9e 111 if name[-len(req.rest):] == req.rest:
53d666ca 112 # This is the same hack used in call*cgi.
173e0e9e
FT
113 name = name[:-len(req.rest)]
114 try:
115 pi = unquoteurl(req.rest)
116 except:
117 pi = req.rest
118 if name == '/':
53d666ca
FT
119 # This seems to be normal CGI behavior, but see callcgi.c for
120 # details.
121 pi = "/" + pi
122 name = ""
c270f222 123 env["SCRIPT_NAME"] = name
53d666ca 124 env["PATH_INFO"] = pi
173e0e9e 125 if "Host" in req: env["SERVER_NAME"] = req["Host"]
1af656d2 126 if "X-Ash-Server-Address" in req: env["SERVER_ADDR"] = req["X-Ash-Server-Address"]
173e0e9e
FT
127 if "X-Ash-Server-Port" in req: env["SERVER_PORT"] = req["X-Ash-Server-Port"]
128 if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == "https": env["HTTPS"] = "on"
129 if "X-Ash-Address" in req: env["REMOTE_ADDR"] = req["X-Ash-Address"]
1af656d2 130 if "X-Ash-Port" in req: env["REMOTE_PORT"] = req["X-Ash-Port"]
0bf0720d
FT
131 if "Content-Type" in req:
132 env["CONTENT_TYPE"] = req["Content-Type"]
133 # The CGI specification does not strictly require this, but
134 # many actualy programs and libraries seem to.
135 del env["HTTP_CONTENT_TYPE"]
136 if "Content-Length" in req:
137 env["CONTENT_LENGTH"] = req["Content-Length"]
75c134b6 138 del env["HTTP_CONTENT_LENGTH"]
173e0e9e
FT
139 if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"])
140 if "X-Ash-Protocol" in req: env["wsgi.url_scheme"] = req["X-Ash-Protocol"]
c270f222
FT
141 env["wsgi.input"] = req.sk
142 env["wsgi.errors"] = sys.stderr
143 env["wsgi.multithread"] = True
144 env["wsgi.multiprocess"] = False
145 env["wsgi.run_once"] = False
761e7292 146 return env
c270f222 147
761e7292 148if reqlimit != 0:
e0c88e73 149 guard = ashd.serve.abortlimiter(reqlimit).call
761e7292
FT
150else:
151 guard = lambda fun: fun()
c270f222 152
761e7292
FT
153class reqthread(ashd.serve.wsgithread):
154 def __init__(self, req):
155 super(reqthread, self).__init__()
156 self.req = req.dup()
157
158 def handlewsgi(self):
159 return handler(self.env, self.startreq)
699754de 160
761e7292 161 def writehead(self, status, headers):
81a0ca30 162 try:
761e7292
FT
163 self.req.sk.write("HTTP/1.1 %s\n" % status)
164 for nm, val in headers:
165 self.req.sk.write("%s: %s\n" % (nm, val))
166 self.req.sk.write("\n")
81a0ca30 167 except IOError:
761e7292 168 raise ashd.serve.closed()
c270f222 169
761e7292 170 def writedata(self, data):
8bb0e3c1 171 try:
761e7292
FT
172 self.req.sk.write(data)
173 self.req.sk.flush()
174 except IOError:
175 raise ashd.serve.closed()
c270f222 176
761e7292
FT
177 def handle(self):
178 self.env = mkenv(self.req)
179 reqevent = ashd.perf.request(self.env)
180 exc = (None, None, None)
181 try:
182 super(reqthread, self).handle()
183 if self.status:
184 reqevent.response([self.status, self.headers])
185 except:
186 exc = sys.exc_info()
187 raise
188 finally:
189 reqevent.__exit__(*exc)
3e11d7ed 190
c270f222
FT
191 def run(self):
192 try:
761e7292 193 guard(super(reqthread, self).run)
c270f222
FT
194 finally:
195 self.req.close()
196
197def handle(req):
198 reqthread(req).start()
199
4e7888f7 200ashd.util.serveloop(handle)