| 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import sys, os, getopt, logging, collections |
| 4 | import socket |
| 5 | import ashd.scgi, ashd.serve |
| 6 | try: |
| 7 | import pdm.srv |
| 8 | except: |
| 9 | pdm = None |
| 10 | |
| 11 | def usage(out): |
| 12 | out.write("usage: scgi-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-t REQUEST-HANDLER[:PAR[=VAL](,PAR[=VAL])...]] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n") |
| 13 | |
| 14 | sk = None |
| 15 | hspec = "free", {} |
| 16 | modwsgi_compat = False |
| 17 | setlog = True |
| 18 | opts, args = getopt.getopt(sys.argv[1:], "+hALp:t:T:m:") |
| 19 | for 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) |
| 46 | if len(args) < 1: |
| 47 | usage(sys.stderr) |
| 48 | sys.exit(1) |
| 49 | if setlog: |
| 50 | logging.basicConfig(format="scgi-wsgi3(%(name)s): %(levelname)s: %(message)s") |
| 51 | |
| 52 | if 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 | |
| 58 | try: |
| 59 | handlermod = __import__(args[0], fromlist = ["dummy"]) |
| 60 | except ImportError as exc: |
| 61 | sys.stderr.write("scgi-wsgi3: handler %s not found: %s\n" % (args[0], exc.args[0])) |
| 62 | sys.exit(1) |
| 63 | if not modwsgi_compat: |
| 64 | if not hasattr(handlermod, "wmain"): |
| 65 | sys.stderr.write("scgi-wsgi3: handler %s has no `wmain' function\n" % args[0]) |
| 66 | sys.exit(1) |
| 67 | handler = handlermod.wmain(*args[1:]) |
| 68 | else: |
| 69 | if not hasattr(handlermod, "application"): |
| 70 | sys.stderr.write("scgi-wsgi3: handler %s has no `application' object\n" % args[0]) |
| 71 | sys.exit(1) |
| 72 | handler = handlermod.application |
| 73 | |
| 74 | def mkenv(head, sk): |
| 75 | try: |
| 76 | env = ashd.scgi.decodehead(head, "utf-8") |
| 77 | env["wsgi.uri_encoding"] = "utf-8" |
| 78 | except UnicodeError: |
| 79 | env = ashd.scgi.decodehead(head, "latin-1") |
| 80 | env["wsgi.uri_encoding"] = "latin-1" |
| 81 | env["wsgi.version"] = 1, 0 |
| 82 | if "HTTP_X_ASH_PROTOCOL" in env: |
| 83 | env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"] |
| 84 | elif "HTTPS" in env: |
| 85 | env["wsgi.url_scheme"] = "https" |
| 86 | else: |
| 87 | env["wsgi.url_scheme"] = "http" |
| 88 | env["wsgi.input"] = sk |
| 89 | env["wsgi.errors"] = sys.stderr |
| 90 | env["wsgi.multithread"] = True |
| 91 | env["wsgi.multiprocess"] = False |
| 92 | env["wsgi.run_once"] = False |
| 93 | return env |
| 94 | |
| 95 | def recode(thing): |
| 96 | if isinstance(thing, collections.ByteString): |
| 97 | return thing |
| 98 | else: |
| 99 | return str(thing).encode("latin-1") |
| 100 | |
| 101 | class request(ashd.serve.wsgirequest): |
| 102 | def __init__(self, *, sk, **kw): |
| 103 | super().__init__(**kw) |
| 104 | self.bsk = sk.dup() |
| 105 | self.sk = self.bsk.makefile("rwb") |
| 106 | |
| 107 | def mkenv(self): |
| 108 | return mkenv(ashd.scgi.readhead(self.sk), self.sk) |
| 109 | |
| 110 | def handlewsgi(self, env, startreq): |
| 111 | return handler(env, startreq) |
| 112 | |
| 113 | def fileno(self): |
| 114 | return self.bsk.fileno() |
| 115 | |
| 116 | def writehead(self, status, headers): |
| 117 | w = self.buffer.extend |
| 118 | w(b"Status: " + recode(status) + b"\n") |
| 119 | for nm, val in headers: |
| 120 | w(recode(nm) + b": " + recode(val) + b"\n") |
| 121 | w(b"\n") |
| 122 | |
| 123 | def flush(self): |
| 124 | try: |
| 125 | ret = self.bsk.send(self.buffer, socket.MSG_DONTWAIT) |
| 126 | self.buffer[:ret] = b"" |
| 127 | except IOError: |
| 128 | raise ashd.serve.closed() |
| 129 | |
| 130 | def close(self): |
| 131 | self.sk.close() |
| 132 | self.bsk.close() |
| 133 | |
| 134 | if hspec[0] not in ashd.serve.names: |
| 135 | sys.stderr.write("scgi-wsgi3: no such request handler: %s\n" % hspec[0]) |
| 136 | sys.exit(1) |
| 137 | hclass = ashd.serve.names[hspec[0]] |
| 138 | try: |
| 139 | hargs = hclass.parseargs(**hspec[1]) |
| 140 | except ValueError as exc: |
| 141 | sys.stderr.write("scgi-wsgi3: %s\n" % exc) |
| 142 | sys.exit(1) |
| 143 | |
| 144 | reqhandler = hclass(**hargs) |
| 145 | try: |
| 146 | while True: |
| 147 | nsk, addr = sk.accept() |
| 148 | try: |
| 149 | reqhandler.handle(request(sk=nsk, handler=reqhandler)) |
| 150 | finally: |
| 151 | nsk.close() |
| 152 | finally: |
| 153 | reqhandler.close() |