3 import sys, os, getopt, threading
4 import ashd.proto, ashd.util
7 out.write("usage: ashd-wsgi [-hA] [-p MODPATH] HANDLER-MODULE [ARGS...]\n")
10 opts, args = getopt.getopt(sys.argv[1:], "+hAp:")
24 handlermod = __import__(args[0], fromlist = ["dummy"])
25 except ImportError, exc:
26 sys.stderr.write("ashd-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
28 if not modwsgi_compat:
29 if not hasattr(handlermod, "wmain"):
30 sys.stderr.write("ashd-wsgi: handler %s has no `wmain' function\n" % args[0])
32 handler = handlermod.wmain(*args[1:])
34 if not hasattr(handlermod, "application"):
35 sys.stderr.write("ashd-wsgi: handler %s has no `application' object\n" % args[0])
37 handler = handlermod.application
42 return os.path.join(cwd, path)
54 if '0' <= url[i] <= '9':
55 c |= (ord(url[i]) - ord('0')) << 4
56 elif 'a' <= url[i] <= 'f':
57 c |= (ord(url[i]) - ord('a') + 10) << 4
58 elif 'A' <= url[i] <= 'F':
59 c |= (ord(url[i]) - ord('A') + 10) << 4
61 raise ValueError("Illegal URL escape character")
62 if '0' <= url[i + 1] <= '9':
63 c |= ord(url[i + 1]) - ord('0')
64 elif 'a' <= url[i + 1] <= 'f':
65 c |= ord(url[i + 1]) - ord('a') + 10
66 elif 'A' <= url[i + 1] <= 'F':
67 c |= ord(url[i + 1]) - ord('A') + 10
69 raise ValueError("Illegal URL escape character")
73 raise ValueError("Incomplete URL escape character")
79 env["wsgi.version"] = 1, 0
80 for key, val in req.headers:
81 env["HTTP_" + key.upper().replace("-", "_")] = val
82 env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
83 env["GATEWAY_INTERFACE"] = "CGI/1.1"
84 env["SERVER_PROTOCOL"] = req.ver
85 env["REQUEST_METHOD"] = req.method
86 env["REQUEST_URI"] = req.url
88 env["PATH_INFO"] = unquoteurl(req.rest)
90 env["PATH_INFO"] = req.rest
94 env["QUERY_STRING"] = name[p + 1:]
97 env["QUERY_STRING"] = ""
98 if name[-len(req.rest):] == req.rest:
99 name = name[:-len(req.rest)]
100 env["SCRIPT_NAME"] = name
101 if "Host" in req: env["SERVER_NAME"] = req["Host"]
102 if "X-Ash-Server-Port" in req: env["SERVER_PORT"] = req["X-Ash-Server-Port"]
103 if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == "https": env["HTTPS"] = "on"
104 if "X-Ash-Address" in req: env["REMOTE_ADDR"] = req["X-Ash-Address"]
105 if "Content-Type" in req: env["CONTENT_TYPE"] = req["Content-Type"]
106 if "Content-Length" in req: env["CONTENT_LENGTH"] = req["Content-Length"]
107 if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"])
108 if "X-Ash-Protocol" in req: env["wsgi.url_scheme"] = req["X-Ash-Protocol"]
109 env["wsgi.input"] = req.sk
110 env["wsgi.errors"] = sys.stderr
111 env["wsgi.multithread"] = True
112 env["wsgi.multiprocess"] = False
113 env["wsgi.run_once"] = False
121 raise Exception, "Trying to write data before starting response."
122 status, headers = resp
124 req.sk.write("HTTP/1.1 %s\n" % status)
125 for nm, val in headers:
126 req.sk.write("%s: %s\n" % (nm, val))
136 def startreq(status, headers, exc_info = None):
138 if exc_info: # Interesting, this...
141 raise exc_info[0], exc_info[1], exc_info[2]
143 exc_info = None # CPython GC bug?
145 raise Exception, "Can only start responding once."
146 resp[:] = status, headers
149 respiter = handler(env, startreq)
151 for data in respiter:
156 if hasattr(respiter, "close"):
159 class reqthread(threading.Thread):
160 def __init__(self, req):
161 super(reqthread, self).__init__(name = "Request handler")
171 reqthread(req).start()
173 ashd.util.serveloop(handle)