dirplex: Correct config resolution for base directory.
[ashd.git] / python3 / ashd-wsgi3
1 #!/usr/bin/python3
2
3 import sys, os, getopt, threading, 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 [-hA] [-m PDM-SPEC] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n")
12
13 reqlimit = 0
14 modwsgi_compat = False
15 opts, args = getopt.getopt(sys.argv[1:], "+hAp:l:m:")
16 for 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
24     elif o == "-l":
25         reqlimit = int(a)
26     elif o == "-m":
27         if pdm is not None:
28             pdm.srv.listen(a)
29 if len(args) < 1:
30     usage(sys.stderr)
31     sys.exit(1)
32
33 try:
34     handlermod = __import__(args[0], fromlist = ["dummy"])
35 except ImportError as exc:
36     sys.stderr.write("ashd-wsgi3: handler %s not found: %s\n" % (args[0], exc.args[0]))
37     sys.exit(1)
38 if not modwsgi_compat:
39     if not hasattr(handlermod, "wmain"):
40         sys.stderr.write("ashd-wsgi3: handler %s has no `wmain' function\n" % args[0])
41         sys.exit(1)
42     handler = handlermod.wmain(*args[1:])
43 else:
44     if not hasattr(handlermod, "application"):
45         sys.stderr.write("ashd-wsgi3: handler %s has no `application' object\n" % args[0])
46         sys.exit(1)
47     handler = handlermod.application
48
49 class closed(IOError):
50     def __init__(self):
51         super().__init__("The client has closed the connection.")
52
53 cwd = os.getcwd()
54 def absolutify(path):
55     if path[0] != '/':
56         return os.path.join(cwd, path)
57     return path
58
59 def unquoteurl(url):
60     buf = bytearray()
61     i = 0
62     while i < len(url):
63         c = url[i]
64         i += 1
65         if c == ord(b'%'):
66             if len(url) >= i + 2:
67                 c = 0
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
74                 else:
75                     raise ValueError("Illegal URL escape character")
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
82                 else:
83                     raise ValueError("Illegal URL escape character")
84                 buf.append(c)
85                 i += 2
86             else:
87                 raise ValueError("Incomplete URL escape character")
88         else:
89             buf.append(c)
90     return buf
91
92 def dowsgi(req):
93     env = {}
94     env["wsgi.version"] = 1, 0
95     for key, val in req.headers:
96         env["HTTP_" + key.upper().replace(b"-", b"_").decode("latin-1")] = val.decode("latin-1")
97     env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
98     env["GATEWAY_INTERFACE"] = "CGI/1.1"
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
112     p = name.find('?')
113     if p >= 0:
114         env["QUERY_STRING"] = name[p + 1:]
115         name = name[:p]
116     else:
117         env["QUERY_STRING"] = ""
118     if name[-len(rest):] == rest:
119         # This is the same hack used in call*cgi.
120         name = name[:-len(rest)]
121     if name == "/":
122         # This seems to be normal CGI behavior, but see callcgi.c for
123         # details.
124         pi = "/" + pi
125         name = ""
126     env["SCRIPT_NAME"] = name
127     env["PATH_INFO"] = pi
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()))
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
143     def recode(thing):
144         if isinstance(thing, collections.ByteString):
145             return thing
146         else:
147             return str(thing).encode("latin-1")
148
149     def flushreq():
150         if not respsent:
151             if not resp:
152                 raise Exception("Trying to write data before starting response.")
153             status, headers = resp
154             respsent[:] = [True]
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"
160             try:
161                 req.sk.write(buf)
162             except IOError:
163                 raise closed()
164
165     def write(data):
166         if not data:
167             return
168         flushreq()
169         try:
170             req.sk.write(data)
171             req.sk.flush()
172         except IOError:
173             raise closed()
174
175     def startreq(status, headers, exc_info = None):
176         if resp:
177             if exc_info:                # Interesting, this...
178                 try:
179                     if respsent:
180                         raise exc_info[1]
181                 finally:
182                     exc_info = None     # CPython GC bug?
183             else:
184                 raise Exception("Can only start responding once.")
185         resp[:] = status, headers
186         return write
187
188     with ashd.perf.request(env) as reqevent:
189         respiter = handler(env, startreq)
190         try:
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)
203
204 flightlock = threading.Condition()
205 inflight = 0
206
207 class reqthread(threading.Thread):
208     def __init__(self, req):
209         super().__init__(name = "Request handler")
210         self.req = req.dup()
211     
212     def run(self):
213         global inflight
214         try:
215             with flightlock:
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
223             try:
224                 dowsgi(self.req)
225             finally:
226                 with flightlock:
227                     inflight -= 1
228                     flightlock.notify()
229         finally:
230             self.req.close()
231             sys.stderr.flush()
232     
233 def handle(req):
234     reqthread(req).start()
235
236 ashd.util.serveloop(handle)