accesslog: Fixed up stdout logging and related FD_CLOEXEC usage.
[ashd.git] / python / ashd-wsgi
1 #!/usr/bin/python
2
3 import sys, os, getopt, threading, time
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, exc:
29     sys.stderr.write("ashd-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
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(closed, self).__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 = ""
54     i = 0
55     while i < len(url):
56         c = url[i]
57         i += 1
58         if c == '%':
59             if len(url) >= i + 2:
60                 c = 0
61                 if '0' <= url[i] <= '9':
62                     c |= (ord(url[i]) - ord('0')) << 4
63                 elif 'a' <= url[i] <= 'f':
64                     c |= (ord(url[i]) - ord('a') + 10) << 4
65                 elif 'A' <= url[i] <= 'F':
66                     c |= (ord(url[i]) - ord('A') + 10) << 4
67                 else:
68                     raise ValueError("Illegal URL escape character")
69                 if '0' <= url[i + 1] <= '9':
70                     c |= ord(url[i + 1]) - ord('0')
71                 elif 'a' <= url[i + 1] <= 'f':
72                     c |= ord(url[i + 1]) - ord('a') + 10
73                 elif 'A' <= url[i + 1] <= 'F':
74                     c |= ord(url[i + 1]) - ord('A') + 10
75                 else:
76                     raise ValueError("Illegal URL escape character")
77                 buf += chr(c)
78                 i += 2
79             else:
80                 raise ValueError("Incomplete URL escape character")
81         else:
82             buf += 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("-", "_")] = val
90     env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
91     env["GATEWAY_INTERFACE"] = "CGI/1.1"
92     env["SERVER_PROTOCOL"] = req.ver
93     env["REQUEST_METHOD"] = req.method
94     env["REQUEST_URI"] = req.url
95     try:
96         env["PATH_INFO"] = unquoteurl(req.rest)
97     except:
98         env["PATH_INFO"] = req.rest
99     name = req.url
100     p = name.find('?')
101     if p >= 0:
102         env["QUERY_STRING"] = name[p + 1:]
103         name = name[:p]
104     else:
105         env["QUERY_STRING"] = ""
106     if name[-len(req.rest):] == req.rest:
107         name = name[:-len(req.rest)]
108     env["SCRIPT_NAME"] = name
109     if "Host" in req: env["SERVER_NAME"] = req["Host"]
110     if "X-Ash-Server-Port" in req: env["SERVER_PORT"] = req["X-Ash-Server-Port"]
111     if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == "https": env["HTTPS"] = "on"
112     if "X-Ash-Address" in req: env["REMOTE_ADDR"] = req["X-Ash-Address"]
113     if "Content-Type" in req: env["CONTENT_TYPE"] = req["Content-Type"]
114     if "Content-Length" in req: env["CONTENT_LENGTH"] = req["Content-Length"]
115     if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"])
116     if "X-Ash-Protocol" in req: env["wsgi.url_scheme"] = req["X-Ash-Protocol"]
117     env["wsgi.input"] = req.sk
118     env["wsgi.errors"] = sys.stderr
119     env["wsgi.multithread"] = True
120     env["wsgi.multiprocess"] = False
121     env["wsgi.run_once"] = False
122
123     resp = []
124     respsent = []
125
126     def flushreq():
127         if not respsent:
128             if not resp:
129                 raise Exception, "Trying to write data before starting response."
130             status, headers = resp
131             respsent[:] = [True]
132             try:
133                 req.sk.write("HTTP/1.1 %s\n" % status)
134                 for nm, val in headers:
135                     req.sk.write("%s: %s\n" % (nm, val))
136                 req.sk.write("\n")
137             except IOError:
138                 raise closed()
139
140     def write(data):
141         if not data:
142             return
143         flushreq()
144         try:
145             req.sk.write(data)
146             req.sk.flush()
147         except IOError:
148             raise closed()
149
150     def startreq(status, headers, exc_info = None):
151         if resp:
152             if exc_info:                # Interesting, this...
153                 try:
154                     if respsent:
155                         raise exc_info[0], exc_info[1], exc_info[2]
156                 finally:
157                     exc_info = None     # CPython GC bug?
158             else:
159                 raise Exception, "Can only start responding once."
160         resp[:] = status, headers
161         return write
162
163     respiter = handler(env, startreq)
164     try:
165         try:
166             for data in respiter:
167                 write(data)
168             if resp:
169                 flushreq()
170         except closed:
171             pass
172     finally:
173         if hasattr(respiter, "close"):
174             respiter.close()
175
176 flightlock = threading.Condition()
177 inflight = 0
178
179 class reqthread(threading.Thread):
180     def __init__(self, req):
181         super(reqthread, self).__init__(name = "Request handler")
182         self.req = req.dup()
183     
184     def run(self):
185         global inflight
186         try:
187             flightlock.acquire()
188             try:
189                 if reqlimit != 0:
190                     start = time.time()
191                     while inflight >= reqlimit:
192                         flightlock.wait(10)
193                         if time.time() - start > 10:
194                             os.abort()
195                 inflight += 1
196             finally:
197                 flightlock.release()
198             try:
199                 dowsgi(self.req)
200             finally:
201                 flightlock.acquire()
202                 try:
203                     inflight -= 1
204                     flightlock.notify()
205                 finally:
206                     flightlock.release()
207         finally:
208             self.req.close()
209     
210 def handle(req):
211     reqthread(req).start()
212
213 ashd.util.serveloop(handle)