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