Merge branch 'master' of git.dolda2000.com:/srv/git/r/ashd
[ashd.git] / python3 / ashd-wsgi3
... / ...
CommitLineData
1#!/usr/bin/python3
2
3import sys, os, getopt, threading, logging, time, locale, collections
4import ashd.proto, ashd.util, ashd.perf
5try:
6 import pdm.srv
7except:
8 pdm = None
9
10def usage(out):
11 out.write("usage: ashd-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n")
12
13reqlimit = 0
14modwsgi_compat = False
15setlog = True
16opts, args = getopt.getopt(sys.argv[1:], "+hALp:l:m:")
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)
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)
32if len(args) < 1:
33 usage(sys.stderr)
34 sys.exit(1)
35if setlog:
36 logging.basicConfig(format="ashd-wsgi3(%(name)s): %(levelname)s: %(message)s")
37log = logging.getLogger("ashd-wsgi3")
38
39try:
40 handlermod = __import__(args[0], fromlist = ["dummy"])
41except ImportError as exc:
42 sys.stderr.write("ashd-wsgi3: handler %s not found: %s\n" % (args[0], exc.args[0]))
43 sys.exit(1)
44if 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:])
49else:
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
55class closed(IOError):
56 def __init__(self):
57 super().__init__("The client has closed the connection.")
58
59cwd = os.getcwd()
60def absolutify(path):
61 if path[0] != '/':
62 return os.path.join(cwd, path)
63 return path
64
65def 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
98def 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_PROTOCOL", "wsgi.url_scheme"),
135 ("HTTP_X_ASH_SERVER_ADDRESS", "SERVER_ADDR"), ("HTTP_X_ASH_SERVER_PORT", "SERVER_PORT"),
136 ("HTTP_X_ASH_ADDRESS", "REMOTE_ADDR"), ("HTTP_X_ASH_PORT", "REMOTE_PORT"),
137 ("HTTP_CONTENT_TYPE", "CONTENT_TYPE"), ("HTTP_CONTENT_LENGTH", "CONTENT_LENGTH")]:
138 if src in env: env[tgt] = env[src]
139 for key in ["HTTP_CONTENT_TYPE", "HTTP_CONTENT_LENGTH"]:
140 # The CGI specification does not strictly require this, but
141 # many actualy programs and libraries seem to.
142 if key in env: del env[key]
143 if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == b"https": env["HTTPS"] = "on"
144 if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"].decode(locale.getpreferredencoding()))
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 recode(thing):
155 if isinstance(thing, collections.ByteString):
156 return thing
157 else:
158 return str(thing).encode("latin-1")
159
160 def flushreq():
161 if not respsent:
162 if not resp:
163 raise Exception("Trying to write data before starting response.")
164 status, headers = resp
165 respsent[:] = [True]
166 buf = bytearray()
167 buf += b"HTTP/1.1 " + recode(status) + b"\n"
168 for nm, val in headers:
169 buf += recode(nm) + b": " + recode(val) + b"\n"
170 buf += b"\n"
171 try:
172 req.sk.write(buf)
173 except IOError:
174 raise closed()
175
176 def write(data):
177 if not data:
178 return
179 flushreq()
180 try:
181 req.sk.write(data)
182 req.sk.flush()
183 except IOError:
184 raise closed()
185
186 def startreq(status, headers, exc_info = None):
187 if resp:
188 if exc_info: # Interesting, this...
189 try:
190 if respsent:
191 raise exc_info[1]
192 finally:
193 exc_info = None # CPython GC bug?
194 else:
195 raise Exception("Can only start responding once.")
196 resp[:] = status, headers
197 return write
198
199 with ashd.perf.request(env) as reqevent:
200 try:
201 respiter = handler(env, startreq)
202 try:
203 for data in respiter:
204 write(data)
205 if resp:
206 flushreq()
207 finally:
208 if hasattr(respiter, "close"):
209 respiter.close()
210 except closed:
211 pass
212 if resp:
213 reqevent.response(resp)
214
215flightlock = threading.Condition()
216inflight = 0
217
218class reqthread(threading.Thread):
219 def __init__(self, req):
220 super().__init__(name = "Request handler")
221 self.req = req.dup()
222
223 def run(self):
224 global inflight
225 try:
226 with flightlock:
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 try:
235 dowsgi(self.req)
236 finally:
237 with flightlock:
238 inflight -= 1
239 flightlock.notify()
240 except:
241 log.error("exception occurred in handler thread", exc_info=True)
242 finally:
243 self.req.close()
244 sys.stderr.flush()
245
246def handle(req):
247 reqthread(req).start()
248
249ashd.util.serveloop(handle)