htparser: Added support for handing a cipher priority specification to GnuTLS.
[ashd.git] / python / scgi-wsgi
CommitLineData
173e0e9e 1#!/usr/bin/python
c06db49a 2
8f410ce7 3import sys, os, getopt, logging, platform
c06db49a 4import socket
8f410ce7 5import ashd.scgi, ashd.serve
0cd97ae2
FT
6try:
7 import pdm.srv
8except:
9 pdm = None
c06db49a
FT
10
11def usage(out):
8f410ce7 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")
c06db49a
FT
13
14sk = None
8f410ce7 15hspec = "free", {}
c06db49a 16modwsgi_compat = False
78c8462c 17setlog = True
8f410ce7 18opts, args = getopt.getopt(sys.argv[1:], "+hALp:t:T:m:")
c06db49a
FT
19for o, a in opts:
20 if o == "-h":
21 usage(sys.stdout)
22 sys.exit(0)
23 elif o == "-p":
e4769c65 24 sys.path.insert(0, a)
78c8462c
FT
25 elif o == "-L":
26 setlog = False
c06db49a
FT
27 elif o == "-T":
28 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
78c8462c 29 sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
c06db49a
FT
30 p = a.rfind(":")
31 if p < 0:
075a379e 32 bindhost = "localhost"
c06db49a
FT
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
0cd97ae2
FT
41 elif o == "-m":
42 if pdm is not None:
43 pdm.srv.listen(a)
8f410ce7
FT
44 elif o == "-t":
45 hspec = ashd.serve.parsehspec(a)
c06db49a
FT
46if len(args) < 1:
47 usage(sys.stderr)
48 sys.exit(1)
78c8462c 49if setlog:
b327e4c1 50 logging.basicConfig(format="scgi-wsgi(%(name)s): %(levelname)s: %(message)s")
c06db49a
FT
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"])
173e0e9e
FT
60except ImportError, exc:
61 sys.stderr.write("scgi-wsgi: handler %s not found: %s\n" % (args[0], exc.message))
c06db49a
FT
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)
adb11d5f 67 handler = handlermod.wmain(*args[1:])
c06db49a
FT
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
79662f8c
FT
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
8f410ce7
FT
90class request(ashd.serve.wsgirequest):
91 def __init__(self, sk, **kw):
92 super(request, self).__init__(**kw)
79662f8c
FT
93 self.bsk = sk.dup()
94 self.sk = self.bsk.makefile("r+")
95
8f410ce7
FT
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()
79662f8c
FT
113
114 def writehead(self, status, headers):
8f410ce7
FT
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")
79662f8c 120
8f410ce7 121 def flush(self):
79662f8c 122 try:
8f410ce7 123 if not request.onjython():
4364a29e 124 ret = self.bsk.send(self.buffer, socket.MSG_DONTWAIT)
8f410ce7 125 else:
4364a29e 126 ret = self.bsk.send(str(self.buffer))
8f410ce7 127 self.buffer[:ret] = ""
79662f8c
FT
128 except IOError:
129 raise ashd.serve.closed()
130
8f410ce7
FT
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)
79662f8c 144
8f410ce7
FT
145reqhandler = hclass(**hargs)
146try:
147 while True:
148 nsk, addr = sk.accept()
79662f8c 149 try:
8f410ce7 150 reqhandler.handle(request(sk=nsk, handler=reqhandler))
79662f8c 151 finally:
8f410ce7
FT
152 nsk.close()
153finally:
154 reqhandler.close()