Commit | Line | Data |
---|---|---|
55fa3f63 | 1 | #!/usr/bin/python3 |
c270f222 | 2 | |
14640dcc | 3 | import sys, os, getopt, threading, logging, time, locale, collections |
d5ee5cde FT |
4 | import ashd.proto, ashd.util, ashd.perf |
5 | try: | |
6 | import pdm.srv | |
7 | except: | |
8 | pdm = None | |
c270f222 FT |
9 | |
10 | def usage(out): | |
14640dcc | 11 | out.write("usage: ashd-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n") |
c270f222 | 12 | |
3e11d7ed | 13 | reqlimit = 0 |
c270f222 | 14 | modwsgi_compat = False |
14640dcc FT |
15 | setlog = True |
16 | opts, args = getopt.getopt(sys.argv[1:], "+hALp:l:m:") | |
c270f222 FT |
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) | |
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 |
32 | if len(args) < 1: |
33 | usage(sys.stderr) | |
34 | sys.exit(1) | |
14640dcc FT |
35 | if setlog: |
36 | logging.basicConfig(format="ashd-wsgi3(%(name)s): %(levelname)s: %(message)s") | |
64a8cd9f | 37 | log = logging.getLogger("ashd-wsgi3") |
c270f222 FT |
38 | |
39 | try: | |
40 | handlermod = __import__(args[0], fromlist = ["dummy"]) | |
55fa3f63 | 41 | except ImportError as exc: |
1f3d7aa3 | 42 | sys.stderr.write("ashd-wsgi3: handler %s not found: %s\n" % (args[0], exc.args[0])) |
c270f222 FT |
43 | sys.exit(1) |
44 | if not modwsgi_compat: | |
45 | if not hasattr(handlermod, "wmain"): | |
1f3d7aa3 | 46 | sys.stderr.write("ashd-wsgi3: handler %s has no `wmain' function\n" % args[0]) |
c270f222 | 47 | sys.exit(1) |
adb11d5f | 48 | handler = handlermod.wmain(*args[1:]) |
c270f222 FT |
49 | else: |
50 | if not hasattr(handlermod, "application"): | |
1f3d7aa3 | 51 | sys.stderr.write("ashd-wsgi3: handler %s has no `application' object\n" % args[0]) |
c270f222 FT |
52 | sys.exit(1) |
53 | handler = handlermod.application | |
54 | ||
81a0ca30 FT |
55 | class closed(IOError): |
56 | def __init__(self): | |
55fa3f63 | 57 | super().__init__("The client has closed the connection.") |
81a0ca30 | 58 | |
70d942a7 FT |
59 | cwd = os.getcwd() |
60 | def absolutify(path): | |
61 | if path[0] != '/': | |
62 | return os.path.join(cwd, path) | |
63 | return path | |
64 | ||
09c82f9c | 65 | def unquoteurl(url): |
55fa3f63 | 66 | buf = bytearray() |
09c82f9c FT |
67 | i = 0 |
68 | while i < len(url): | |
69 | c = url[i] | |
70 | i += 1 | |
55fa3f63 | 71 | if c == ord(b'%'): |
370d235f | 72 | if len(url) >= i + 2: |
09c82f9c | 73 | c = 0 |
55fa3f63 FT |
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 | |
09c82f9c FT |
80 | else: |
81 | raise ValueError("Illegal URL escape character") | |
55fa3f63 FT |
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 | |
09c82f9c FT |
88 | else: |
89 | raise ValueError("Illegal URL escape character") | |
55fa3f63 | 90 | buf.append(c) |
09c82f9c FT |
91 | i += 2 |
92 | else: | |
93 | raise ValueError("Incomplete URL escape character") | |
94 | else: | |
55fa3f63 | 95 | buf.append(c) |
09c82f9c | 96 | return buf |
81a0ca30 | 97 | |
c270f222 FT |
98 | def dowsgi(req): |
99 | env = {} | |
100 | env["wsgi.version"] = 1, 0 | |
101 | for key, val in req.headers: | |
55fa3f63 | 102 | env["HTTP_" + key.upper().replace(b"-", b"_").decode("latin-1")] = val.decode("latin-1") |
c270f222 FT |
103 | env["SERVER_SOFTWARE"] = "ashd-wsgi/1" |
104 | env["GATEWAY_INTERFACE"] = "CGI/1.1" | |
55fa3f63 FT |
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 | |
c270f222 FT |
118 | p = name.find('?') |
119 | if p >= 0: | |
c270f222 | 120 | env["QUERY_STRING"] = name[p + 1:] |
8498ab28 | 121 | name = name[:p] |
c270f222 FT |
122 | else: |
123 | env["QUERY_STRING"] = "" | |
55fa3f63 | 124 | if name[-len(rest):] == rest: |
53d666ca | 125 | # This is the same hack used in call*cgi. |
55fa3f63 FT |
126 | name = name[:-len(rest)] |
127 | if name == "/": | |
53d666ca FT |
128 | # This seems to be normal CGI behavior, but see callcgi.c for |
129 | # details. | |
130 | pi = "/" + pi | |
131 | name = "" | |
c270f222 | 132 | env["SCRIPT_NAME"] = name |
53d666ca | 133 | env["PATH_INFO"] = pi |
1af656d2 FT |
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")]: | |
55fa3f63 | 138 | if src in env: env[tgt] = env[src] |
0bf0720d FT |
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] | |
55fa3f63 FT |
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())) | |
c270f222 FT |
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 | ||
55fa3f63 FT |
154 | def recode(thing): |
155 | if isinstance(thing, collections.ByteString): | |
156 | return thing | |
157 | else: | |
158 | return str(thing).encode("latin-1") | |
159 | ||
699754de | 160 | def flushreq(): |
c270f222 FT |
161 | if not respsent: |
162 | if not resp: | |
55fa3f63 | 163 | raise Exception("Trying to write data before starting response.") |
c270f222 FT |
164 | status, headers = resp |
165 | respsent[:] = [True] | |
55fa3f63 FT |
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" | |
8bb0e3c1 | 171 | try: |
55fa3f63 | 172 | req.sk.write(buf) |
8bb0e3c1 FT |
173 | except IOError: |
174 | raise closed() | |
699754de FT |
175 | |
176 | def write(data): | |
177 | if not data: | |
178 | return | |
8bb0e3c1 | 179 | flushreq() |
81a0ca30 FT |
180 | try: |
181 | req.sk.write(data) | |
182 | req.sk.flush() | |
183 | except IOError: | |
184 | raise closed() | |
c270f222 FT |
185 | |
186 | def startreq(status, headers, exc_info = None): | |
187 | if resp: | |
188 | if exc_info: # Interesting, this... | |
189 | try: | |
190 | if respsent: | |
55fa3f63 | 191 | raise exc_info[1] |
c270f222 FT |
192 | finally: |
193 | exc_info = None # CPython GC bug? | |
194 | else: | |
55fa3f63 | 195 | raise Exception("Can only start responding once.") |
c270f222 FT |
196 | resp[:] = status, headers |
197 | return write | |
198 | ||
d5ee5cde | 199 | with ashd.perf.request(env) as reqevent: |
8bb0e3c1 | 200 | try: |
64a8cd9f | 201 | respiter = handler(env, startreq) |
d5ee5cde FT |
202 | try: |
203 | for data in respiter: | |
204 | write(data) | |
205 | if resp: | |
206 | flushreq() | |
64a8cd9f FT |
207 | finally: |
208 | if hasattr(respiter, "close"): | |
209 | respiter.close() | |
210 | except closed: | |
211 | pass | |
d5ee5cde FT |
212 | if resp: |
213 | reqevent.response(resp) | |
c270f222 | 214 | |
3e11d7ed FT |
215 | flightlock = threading.Condition() |
216 | inflight = 0 | |
217 | ||
c270f222 FT |
218 | class reqthread(threading.Thread): |
219 | def __init__(self, req): | |
55fa3f63 | 220 | super().__init__(name = "Request handler") |
c270f222 FT |
221 | self.req = req.dup() |
222 | ||
223 | def run(self): | |
3e11d7ed | 224 | global inflight |
c270f222 | 225 | try: |
55fa3f63 | 226 | with flightlock: |
3e11d7ed FT |
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 | |
3e11d7ed FT |
234 | try: |
235 | dowsgi(self.req) | |
236 | finally: | |
55fa3f63 | 237 | with flightlock: |
3e11d7ed FT |
238 | inflight -= 1 |
239 | flightlock.notify() | |
64a8cd9f FT |
240 | except: |
241 | log.error("exception occurred in handler thread", exc_info=True) | |
c270f222 FT |
242 | finally: |
243 | self.req.close() | |
a83cfbbc | 244 | sys.stderr.flush() |
c270f222 FT |
245 | |
246 | def handle(req): | |
247 | reqthread(req).start() | |
248 | ||
4e7888f7 | 249 | ashd.util.serveloop(handle) |