3 import sys, os, getopt, threading, time
4 import ashd.proto, ashd.util
7 out.write("usage: ashd-wsgi [-hA] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n")
10 modwsgi_compat = False
11 opts, args = getopt.getopt(sys.argv[1:], "+hAp:l:")
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))
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])
35 handler = handlermod.wmain(*args[1:])
37 if not hasattr(handlermod, "application"):
38 sys.stderr.write("ashd-wsgi: handler %s has no `application' object\n" % args[0])
40 handler = handlermod.application
42 class closed(IOError):
44 super(closed, self).__init__("The client has closed the connection.")
49 return os.path.join(cwd, path)
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
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
76 raise ValueError("Illegal URL escape character")
80 raise ValueError("Incomplete URL escape character")
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
96 env["PATH_INFO"] = unquoteurl(req.rest)
98 env["PATH_INFO"] = req.rest
102 env["QUERY_STRING"] = name[p + 1:]
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
129 raise Exception, "Trying to write data before starting response."
130 status, headers = resp
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))
150 def startreq(status, headers, exc_info = None):
152 if exc_info: # Interesting, this...
155 raise exc_info[0], exc_info[1], exc_info[2]
157 exc_info = None # CPython GC bug?
159 raise Exception, "Can only start responding once."
160 resp[:] = status, headers
163 respiter = handler(env, startreq)
166 for data in respiter:
173 if hasattr(respiter, "close"):
176 flightlock = threading.Condition()
179 class reqthread(threading.Thread):
180 def __init__(self, req):
181 super(reqthread, self).__init__(name = "Request handler")
191 while inflight >= reqlimit:
193 if time.time() - start > 10:
211 reqthread(req).start()
213 ashd.util.serveloop(handle)