etc: Added an explicit .wsgi2 extension in the Python configuration.
[ashd.git] / python / ashd-wsgi
1 #!/usr/bin/python
2
3 import sys, os, getopt, threading, logging, time
4 import ashd.proto, ashd.util, ashd.perf, ashd.serve
5 try:
6     import pdm.srv
7 except:
8     pdm = None
9
10 def usage(out):
11     out.write("usage: ashd-wsgi [-hAL] [-m PDM-SPEC] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n")
12
13 reqlimit = 0
14 modwsgi_compat = False
15 setlog = True
16 opts, args = getopt.getopt(sys.argv[1:], "+hAp:l:m:")
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)
23     elif o == "-L":
24         setlog = False
25     elif o == "-A":
26         modwsgi_compat = True
27     elif o == "-l":
28         reqlimit = int(a)
29     elif o == "-m":
30         if pdm is not None:
31             pdm.srv.listen(a)
32 if len(args) < 1:
33     usage(sys.stderr)
34     sys.exit(1)
35 if setlog:
36     logging.basicConfig(format="ashd-wsgi(%(name)s): %(levelname)s: %(message)s")
37 log = logging.getLogger("ashd-wsgi")
38
39 try:
40     handlermod = __import__(args[0], fromlist = ["dummy"])
41 except ImportError, exc:
42     sys.stderr.write("ashd-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
43     sys.exit(1)
44 if not modwsgi_compat:
45     if not hasattr(handlermod, "wmain"):
46         sys.stderr.write("ashd-wsgi: handler %s has no `wmain' function\n" % args[0])
47         sys.exit(1)
48     handler = handlermod.wmain(*args[1:])
49 else:
50     if not hasattr(handlermod, "application"):
51         sys.stderr.write("ashd-wsgi: handler %s has no `application' object\n" % args[0])
52         sys.exit(1)
53     handler = handlermod.application
54
55 cwd = os.getcwd()
56 def absolutify(path):
57     if path[0] != '/':
58         return os.path.join(cwd, path)
59     return path
60
61 def unquoteurl(url):
62     buf = ""
63     i = 0
64     while i < len(url):
65         c = url[i]
66         i += 1
67         if c == '%':
68             if len(url) >= i + 2:
69                 c = 0
70                 if '0' <= url[i] <= '9':
71                     c |= (ord(url[i]) - ord('0')) << 4
72                 elif 'a' <= url[i] <= 'f':
73                     c |= (ord(url[i]) - ord('a') + 10) << 4
74                 elif 'A' <= url[i] <= 'F':
75                     c |= (ord(url[i]) - ord('A') + 10) << 4
76                 else:
77                     raise ValueError("Illegal URL escape character")
78                 if '0' <= url[i + 1] <= '9':
79                     c |= ord(url[i + 1]) - ord('0')
80                 elif 'a' <= url[i + 1] <= 'f':
81                     c |= ord(url[i + 1]) - ord('a') + 10
82                 elif 'A' <= url[i + 1] <= 'F':
83                     c |= ord(url[i + 1]) - ord('A') + 10
84                 else:
85                     raise ValueError("Illegal URL escape character")
86                 buf += chr(c)
87                 i += 2
88             else:
89                 raise ValueError("Incomplete URL escape character")
90         else:
91             buf += c
92     return buf
93
94 def mkenv(req):
95     env = {}
96     env["wsgi.version"] = 1, 0
97     for key, val in req.headers:
98         env["HTTP_" + key.upper().replace("-", "_")] = val
99     env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
100     env["GATEWAY_INTERFACE"] = "CGI/1.1"
101     env["SERVER_PROTOCOL"] = req.ver
102     env["REQUEST_METHOD"] = req.method
103     env["REQUEST_URI"] = req.url
104     name = req.url
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(req.rest):] == req.rest:
112         # This is the same hack used in call*cgi.
113         name = name[:-len(req.rest)]
114     try:
115         pi = unquoteurl(req.rest)
116     except:
117         pi = req.rest
118     if name == '/':
119         # This seems to be normal CGI behavior, but see callcgi.c for
120         # details.
121         pi = "/" + pi
122         name = ""
123     env["SCRIPT_NAME"] = name
124     env["PATH_INFO"] = pi
125     if "Host" in req: env["SERVER_NAME"] = req["Host"]
126     if "X-Ash-Server-Address" in req: env["SERVER_ADDR"] = req["X-Ash-Server-Address"]
127     if "X-Ash-Server-Port" in req: env["SERVER_PORT"] = req["X-Ash-Server-Port"]
128     if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == "https": env["HTTPS"] = "on"
129     if "X-Ash-Address" in req: env["REMOTE_ADDR"] = req["X-Ash-Address"]
130     if "X-Ash-Port" in req: env["REMOTE_PORT"] = req["X-Ash-Port"]
131     if "Content-Type" in req:
132         env["CONTENT_TYPE"] = req["Content-Type"]
133         # The CGI specification does not strictly require this, but
134         # many actualy programs and libraries seem to.
135         del env["HTTP_CONTENT_TYPE"]
136     if "Content-Length" in req:
137         env["CONTENT_LENGTH"] = req["Content-Length"]
138         del env["HTTP_CONTENT_LENGTH"]
139     if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"])
140     if "X-Ash-Protocol" in req: env["wsgi.url_scheme"] = req["X-Ash-Protocol"]
141     env["wsgi.input"] = req.sk
142     env["wsgi.errors"] = sys.stderr
143     env["wsgi.multithread"] = True
144     env["wsgi.multiprocess"] = False
145     env["wsgi.run_once"] = False
146     return env
147
148 if reqlimit != 0:
149     guard = ashd.serve.abortlimiter(reqlimit).call
150 else:
151     guard = lambda fun: fun()
152
153 class reqthread(ashd.serve.wsgithread):
154     def __init__(self, req):
155         super(reqthread, self).__init__()
156         self.req = req.dup()
157     
158     def handlewsgi(self):
159         return handler(self.env, self.startreq)
160
161     def writehead(self, status, headers):
162         try:
163             self.req.sk.write("HTTP/1.1 %s\n" % status)
164             for nm, val in headers:
165                 self.req.sk.write("%s: %s\n" % (nm, val))
166             self.req.sk.write("\n")
167         except IOError:
168             raise ashd.serve.closed()
169
170     def writedata(self, data):
171         try:
172             self.req.sk.write(data)
173             self.req.sk.flush()
174         except IOError:
175             raise ashd.serve.closed()
176
177     def handle(self):
178         self.env = mkenv(self.req)
179         reqevent = ashd.perf.request(self.env)
180         exc = (None, None, None)
181         try:
182             super(reqthread, self).handle()
183             if self.status:
184                 reqevent.response([self.status, self.headers])
185         except:
186             exc = sys.exc_info()
187             raise
188         finally:
189             reqevent.__exit__(*exc)
190
191     def run(self):
192         try:
193             guard(super(reqthread, self).run)
194         finally:
195             self.req.close()
196     
197 def handle(req):
198     reqthread(req).start()
199
200 ashd.util.serveloop(handle)