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