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
39 class closed(IOError):
41 super(closed, self).__init__("The client has closed the connection.")
46 return os.path.join(cwd, path)
58 if '0' <= url[i] <= '9':
59 c |= (ord(url[i]) - ord('0')) << 4
60 elif 'a' <= url[i] <= 'f':
61 c |= (ord(url[i]) - ord('a') + 10) << 4
62 elif 'A' <= url[i] <= 'F':
63 c |= (ord(url[i]) - ord('A') + 10) << 4
65 raise ValueError("Illegal URL escape character")
66 if '0' <= url[i + 1] <= '9':
67 c |= ord(url[i + 1]) - ord('0')
68 elif 'a' <= url[i + 1] <= 'f':
69 c |= ord(url[i + 1]) - ord('a') + 10
70 elif 'A' <= url[i + 1] <= 'F':
71 c |= ord(url[i + 1]) - ord('A') + 10
73 raise ValueError("Illegal URL escape character")
77 raise ValueError("Incomplete URL escape character")
84 env["wsgi.version"] = 1, 0
85 for key, val in req.headers:
86 env["HTTP_" + key.upper().replace("-", "_")] = val
87 env["SERVER_SOFTWARE"] = "ashd-wsgi/1"
88 env["GATEWAY_INTERFACE"] = "CGI/1.1"
89 env["SERVER_PROTOCOL"] = req.ver
90 env["REQUEST_METHOD"] = req.method
91 env["REQUEST_URI"] = req.url
93 env["PATH_INFO"] = unquoteurl(req.rest)
95 env["PATH_INFO"] = req.rest
99 env["QUERY_STRING"] = name[p + 1:]
102 env["QUERY_STRING"] = ""
103 if name[-len(req.rest):] == req.rest:
104 name = name[:-len(req.rest)]
105 env["SCRIPT_NAME"] = name
106 if "Host" in req: env["SERVER_NAME"] = req["Host"]
107 if "X-Ash-Server-Port" in req: env["SERVER_PORT"] = req["X-Ash-Server-Port"]
108 if "X-Ash-Protocol" in req and req["X-Ash-Protocol"] == "https": env["HTTPS"] = "on"
109 if "X-Ash-Address" in req: env["REMOTE_ADDR"] = req["X-Ash-Address"]
110 if "Content-Type" in req: env["CONTENT_TYPE"] = req["Content-Type"]
111 if "Content-Length" in req: env["CONTENT_LENGTH"] = req["Content-Length"]
112 if "X-Ash-File" in req: env["SCRIPT_FILENAME"] = absolutify(req["X-Ash-File"])
113 if "X-Ash-Protocol" in req: env["wsgi.url_scheme"] = req["X-Ash-Protocol"]
114 env["wsgi.input"] = req.sk
115 env["wsgi.errors"] = sys.stderr
116 env["wsgi.multithread"] = True
117 env["wsgi.multiprocess"] = False
118 env["wsgi.run_once"] = False
126 raise Exception, "Trying to write data before starting response."
127 status, headers = resp
130 req.sk.write("HTTP/1.1 %s\n" % status)
131 for nm, val in headers:
132 req.sk.write("%s: %s\n" % (nm, val))
147 def startreq(status, headers, exc_info = None):
149 if exc_info: # Interesting, this...
152 raise exc_info[0], exc_info[1], exc_info[2]
154 exc_info = None # CPython GC bug?
156 raise Exception, "Can only start responding once."
157 resp[:] = status, headers
160 respiter = handler(env, startreq)
163 for data in respiter:
170 if hasattr(respiter, "close"):
173 class reqthread(threading.Thread):
174 def __init__(self, req):
175 super(reqthread, self).__init__(name = "Request handler")
185 reqthread(req).start()
187 ashd.util.serveloop(handle)