callfcgi: Improved recvrec implementation.
[ashd.git] / python / scgi-wsgi
... / ...
CommitLineData
1#!/usr/bin/python
2
3import sys, os, getopt, logging, platform
4import socket
5import ashd.scgi, ashd.serve
6try:
7 import pdm.srv
8except:
9 pdm = None
10
11def usage(out):
12 out.write("usage: scgi-wsgi [-hAL] [-m PDM-SPEC] [-p MODPATH] [-t REQUEST-HANDLER[:PAR[=VAL](,PAR[=VAL])...]] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
13
14sk = None
15hspec = "free", {}
16modwsgi_compat = False
17setlog = True
18opts, args = getopt.getopt(sys.argv[1:], "+hALp:t:T:m:")
19for o, a in opts:
20 if o == "-h":
21 usage(sys.stdout)
22 sys.exit(0)
23 elif o == "-p":
24 sys.path.insert(0, a)
25 elif o == "-L":
26 setlog = False
27 elif o == "-T":
28 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29 sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
30 p = a.rfind(":")
31 if p < 0:
32 bindhost = "localhost"
33 bindport = int(a)
34 else:
35 bindhost = a[:p]
36 bindport = int(a[p + 1:])
37 sk.bind((bindhost, bindport))
38 sk.listen(32)
39 elif o == "-A":
40 modwsgi_compat = True
41 elif o == "-m":
42 if pdm is not None:
43 pdm.srv.listen(a)
44 elif o == "-t":
45 hspec = ashd.serve.parsehspec(a)
46if len(args) < 1:
47 usage(sys.stderr)
48 sys.exit(1)
49if setlog:
50 logging.basicConfig(format="scgi-wsgi(%(name)s): %(levelname)s: %(message)s")
51
52if sk is None:
53 # This is suboptimal, since the socket on stdin is not necessarily
54 # AF_UNIX, but Python does not seem to offer any way around it,
55 # that I can find.
56 sk = socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM)
57
58try:
59 handlermod = __import__(args[0], fromlist = ["dummy"])
60except ImportError, exc:
61 sys.stderr.write("scgi-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
62 sys.exit(1)
63if not modwsgi_compat:
64 if not hasattr(handlermod, "wmain"):
65 sys.stderr.write("scgi-wsgi: handler %s has no `wmain' function\n" % args[0])
66 sys.exit(1)
67 handler = handlermod.wmain(*args[1:])
68else:
69 if not hasattr(handlermod, "application"):
70 sys.stderr.write("scgi-wsgi: handler %s has no `application' object\n" % args[0])
71 sys.exit(1)
72 handler = handlermod.application
73
74def mkenv(head, sk):
75 env = dict(head)
76 env["wsgi.version"] = 1, 0
77 if "HTTP_X_ASH_PROTOCOL" in env:
78 env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
79 elif "HTTPS" in env:
80 env["wsgi.url_scheme"] = "https"
81 else:
82 env["wsgi.url_scheme"] = "http"
83 env["wsgi.input"] = sk
84 env["wsgi.errors"] = sys.stderr
85 env["wsgi.multithread"] = True
86 env["wsgi.multiprocess"] = False
87 env["wsgi.run_once"] = False
88 return env
89
90class request(ashd.serve.wsgirequest):
91 def __init__(self, sk, **kw):
92 super(request, self).__init__(**kw)
93 self.bsk = sk.dup()
94 self.sk = self.bsk.makefile("r+")
95
96 def mkenv(self):
97 return mkenv(ashd.scgi.readhead(self.sk), self.sk)
98
99 def handlewsgi(self, env, startreq):
100 return handler(env, startreq)
101
102 _onjython = None
103 @staticmethod
104 def onjython():
105 if request._onjython is None:
106 request._onjython = ("java" in platform.system().lower())
107 return request._onjython
108
109 def fileno(self):
110 if request.onjython():
111 self.bsk.setblocking(False)
112 return self.bsk.fileno()
113
114 def writehead(self, status, headers):
115 w = self.buffer.extend
116 w("Status: %s\n" % status)
117 for nm, val in headers:
118 w("%s: %s\n" % (nm, val))
119 w("\n")
120
121 def flush(self):
122 try:
123 if not request.onjython():
124 ret = self.bsk.send(self.buffer, socket.MSG_DONTWAIT)
125 else:
126 ret = self.bsk.send(str(self.buffer))
127 self.buffer[:ret] = ""
128 except IOError:
129 raise ashd.serve.closed()
130
131 def close(self):
132 self.sk.close()
133 self.bsk.close()
134
135if hspec[0] not in ashd.serve.names:
136 sys.stderr.write("scgi-wsgi: no such request handler: %s\n" % hspec[0])
137 sys.exit(1)
138hclass = ashd.serve.names[hspec[0]]
139try:
140 hargs = hclass.parseargs(**hspec[1])
141except ValueError as exc:
142 sys.stderr.write("scgi-wsgi: %s\n" % exc)
143 sys.exit(1)
144
145reqhandler = hclass(**hargs)
146try:
147 while True:
148 nsk, addr = sk.accept()
149 try:
150 reqhandler.handle(request(sk=nsk, handler=reqhandler))
151 finally:
152 nsk.close()
153finally:
154 reqhandler.close()