python: Improved error handling and logging in ashd-wsgi.
[ashd.git] / python3 / ashd-wsgi3
1 #!/usr/bin/python3
2
3 import sys, os, getopt, threading, logging, time, locale, collections
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-wsgi3 [-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:], "+hALp: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-wsgi3(%(name)s): %(levelname)s: %(message)s")
37 log = logging.getLogger("ashd-wsgi3")
38
39 try:
40     handlermod = __import__(args[0], fromlist = ["dummy"])
41 except ImportError as exc:
42     sys.stderr.write("ashd-wsgi3: handler %s not found: %s\n" % (args[0], exc.args[0]))
43     sys.exit(1)
44 if not modwsgi_compat:
45     if not hasattr(handlermod, "wmain"):
46         sys.stderr.write("ashd-wsgi3: 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-wsgi3: 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().__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 = bytearray()
67     i = 0
68     while i < len(url):
69         c = url[i]
70         i += 1
71         if c == ord(b'%'):
72             if len(url) >= i + 2:
73                 c = 0
74                 if ord(b'0') <= url[i] <= ord(b'9'):
75                     c |= (url[i] - ord(b'0')) << 4
76                 elif ord(b'a') <= url[i] <= ord(b'f'):
77                     c |= (url[i] - ord(b'a') + 10) << 4
78                 elif ord(b'A') <= url[i] <= ord(b'F'):
79                     c |= (url[i] - ord(b'A') + 10) << 4
80                 else:
81                     raise ValueError("Illegal URL escape character")
82                 if ord(b'0') <= url[i + 1] <= ord(b'9'):
83                     c |= url[i + 1] - ord('0')
84                 elif ord(b'a') <= url[i + 1] <= ord(b'f'):
85                     c |= url[i + 1] - ord(b'a') + 10
86                 elif ord(b'A') <= url[i + 1] <= ord(b'F'):
87                     c |= url[i + 1] - ord(b'A') + 10
88                 else:
89                     raise ValueError("Illegal URL escape character")
90                 buf.append(c)
91                 i += 2
92             else:
93                 raise ValueError("Incomplete URL escape character")
94         else:
95             buf.append(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(b"-", b"_").decode("latin-1")] = val.decode("latin-1")
103     env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
104     env["GATEWAY_INTERFACE"] = "CGI/1.1"
105     env["SERVER_PROTOCOL"] = req.ver.decode("latin-1")
106     env["REQUEST_METHOD"] = req.method.decode("latin-1")
107     try:
108         rawpi = unquoteurl(req.rest)
109     except:
110         rawpi = req.rest
111     try:
112         name, rest, pi = (v.decode("utf-8") for v in (req.url, req.rest, rawpi))
113         env["wsgi.uri_encoding"] = "utf-8"
114     except UnicodeError as exc:
115         name, rest, pi = (v.decode("latin-1") for v in (req.url, req.rest, rawpi))
116         env["wsgi.uri_encoding"] = "latin-1"
117     env["REQUEST_URI"] = name
118     p = name.find('?')
119     if p >= 0:
120         env["QUERY_STRING"] = name[p + 1:]
121         name = name[:p]
122     else:
123         env["QUERY_STRING"] = ""
124     if name[-len(rest):] == rest:
125         # This is the same hack used in call*cgi.
126         name = name[:-len(rest)]
127     if name == "/":
128         # This seems to be normal CGI behavior, but see callcgi.c for
129         # details.
130         pi = "/" + pi
131         name = ""
132     env["SCRIPT_NAME"] = name
133     env["PATH_INFO"] = pi
134     for src, tgt in [("HTTP_HOST", "SERVER_NAME"), ("HTTP_X_ASH_SERVER_PORT", "SERVER_PORT"),
135                      ("HTTP_X_ASH_ADDRESS", "REMOTE_ADDR"), ("HTTP_CONTENT_TYPE", "CONTENT_TYPE"),
136                      ("HTTP_CONTENT_LENGTH", "CONTENT_LENGTH"), ("HTTP_X_ASH_PROTOCOL", "wsgi.url_scheme")]:
137         if src in env: env[tgt] = env[src]
138     if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == b"https": env["HTTPS"] = "on"
139     if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"].decode(locale.getpreferredencoding()))
140     env["wsgi.input"] = req.sk
141     env["wsgi.errors"] = sys.stderr
142     env["wsgi.multithread"] = True
143     env["wsgi.multiprocess"] = False
144     env["wsgi.run_once"] = False
145
146     resp = []
147     respsent = []
148
149     def recode(thing):
150         if isinstance(thing, collections.ByteString):
151             return thing
152         else:
153             return str(thing).encode("latin-1")
154
155     def flushreq():
156         if not respsent:
157             if not resp:
158                 raise Exception("Trying to write data before starting response.")
159             status, headers = resp
160             respsent[:] = [True]
161             buf = bytearray()
162             buf += b"HTTP/1.1 " + recode(status) + b"\n"
163             for nm, val in headers:
164                 buf += recode(nm) + b": " + recode(val) + b"\n"
165             buf += b"\n"
166             try:
167                 req.sk.write(buf)
168             except IOError:
169                 raise closed()
170
171     def write(data):
172         if not data:
173             return
174         flushreq()
175         try:
176             req.sk.write(data)
177             req.sk.flush()
178         except IOError:
179             raise closed()
180
181     def startreq(status, headers, exc_info = None):
182         if resp:
183             if exc_info:                # Interesting, this...
184                 try:
185                     if respsent:
186                         raise exc_info[1]
187                 finally:
188                     exc_info = None     # CPython GC bug?
189             else:
190                 raise Exception("Can only start responding once.")
191         resp[:] = status, headers
192         return write
193
194     with ashd.perf.request(env) as reqevent:
195         try:
196             respiter = handler(env, startreq)
197             try:
198                 for data in respiter:
199                     write(data)
200                 if resp:
201                     flushreq()
202             finally:
203                 if hasattr(respiter, "close"):
204                     respiter.close()
205         except closed:
206             pass
207         if resp:
208             reqevent.response(resp)
209
210 flightlock = threading.Condition()
211 inflight = 0
212
213 class reqthread(threading.Thread):
214     def __init__(self, req):
215         super().__init__(name = "Request handler")
216         self.req = req.dup()
217     
218     def run(self):
219         global inflight
220         try:
221             with flightlock:
222                 if reqlimit != 0:
223                     start = time.time()
224                     while inflight >= reqlimit:
225                         flightlock.wait(10)
226                         if time.time() - start > 10:
227                             os.abort()
228                 inflight += 1
229             try:
230                 dowsgi(self.req)
231             finally:
232                 with flightlock:
233                     inflight -= 1
234                     flightlock.notify()
235         except:
236             log.error("exception occurred in handler thread", exc_info=True)
237         finally:
238             self.req.close()
239             sys.stderr.flush()
240     
241 def handle(req):
242     reqthread(req).start()
243
244 ashd.util.serveloop(handle)