python: Make scgi-wsgi{,3} use ashd.serve.
authorFredrik Tolf <fredrik@dolda2000.com>
Fri, 18 Jan 2013 22:04:15 +0000 (23:04 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Fri, 18 Jan 2013 22:04:15 +0000 (23:04 +0100)
python/ashd/scgi.py
python/scgi-wsgi
python3/ashd/scgi.py
python3/scgi-wsgi3

index f7ba3a8..1f0c5ab 100644 (file)
@@ -1,13 +1,6 @@
-import sys
-import threading
-
 class protoerr(Exception):
     pass
 
-class closed(IOError):
-    def __init__(self):
-        super(closed, self).__init__("The client has closed the connection.")
-
 def readns(sk):
     hln = 0
     while True:
@@ -33,100 +26,3 @@ def readhead(sk):
         ret[parts[i]] = parts[i + 1]
         i += 2
     return ret
-
-class reqthread(threading.Thread):
-    def __init__(self, sk, handler):
-        super(reqthread, self).__init__(name = "SCGI request handler")
-        self.bsk = sk.dup()
-        self.sk = self.bsk.makefile("r+")
-        self.handler = handler
-
-    def run(self):
-        try:
-            head = readhead(self.sk)
-            self.handler(head, self.sk)
-        finally:
-            self.sk.close()
-            self.bsk.close()
-
-def handlescgi(sk, handler):
-    t = reqthread(sk, handler)
-    t.start()
-
-def servescgi(socket, handler):
-    while True:
-        nsk, addr = socket.accept()
-        try:
-            handlescgi(nsk, handler)
-        finally:
-            nsk.close()
-
-def wrapwsgi(handler):
-    def handle(head, sk):
-        env = dict(head)
-        env["wsgi.version"] = 1, 0
-        if "HTTP_X_ASH_PROTOCOL" in env:
-            env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
-        elif "HTTPS" in env:
-            env["wsgi.url_scheme"] = "https"
-        else:
-            env["wsgi.url_scheme"] = "http"
-        env["wsgi.input"] = sk
-        env["wsgi.errors"] = sys.stderr
-        env["wsgi.multithread"] = True
-        env["wsgi.multiprocess"] = False
-        env["wsgi.run_once"] = False
-
-        resp = []
-        respsent = []
-
-        def flushreq():
-            if not respsent:
-                if not resp:
-                    raise Exception, "Trying to write data before starting response."
-                status, headers = resp
-                respsent[:] = [True]
-                try:
-                    sk.write("Status: %s\n" % status)
-                    for nm, val in headers:
-                        sk.write("%s: %s\n" % (nm, val))
-                    sk.write("\n")
-                except IOError:
-                    raise closed()
-
-        def write(data):
-            if not data:
-                return
-            flushreq()
-            try:
-                sk.write(data)
-                sk.flush()
-            except IOError:
-                raise closed()
-
-        def startreq(status, headers, exc_info = None):
-            if resp:
-                if exc_info:                # Interesting, this...
-                    try:
-                        if respsent:
-                            raise exc_info[0], exc_info[1], exc_info[2]
-                    finally:
-                        exc_info = None     # CPython GC bug?
-                else:
-                    raise Exception, "Can only start responding once."
-            resp[:] = status, headers
-            return write
-
-        respiter = handler(env, startreq)
-        try:
-            try:
-                for data in respiter:
-                    write(data)
-                if resp:
-                    flushreq()
-            except closed:
-                pass
-        finally:
-            if hasattr(respiter, "close"):
-                respiter.close()
-    return handle
index e2689d4..cb67db5 100755 (executable)
@@ -2,7 +2,7 @@
 
 import sys, os, getopt, logging
 import socket
-import ashd.scgi
+import ashd.scgi, ashd.serve
 
 def usage(out):
     out.write("usage: scgi-wsgi [-hAL] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
@@ -61,4 +61,62 @@ else:
         sys.exit(1)
     handler = handlermod.application
 
-ashd.scgi.servescgi(sk, ashd.scgi.wrapwsgi(handler))
+def mkenv(head, sk):
+    env = dict(head)
+    env["wsgi.version"] = 1, 0
+    if "HTTP_X_ASH_PROTOCOL" in env:
+        env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
+    elif "HTTPS" in env:
+        env["wsgi.url_scheme"] = "https"
+    else:
+        env["wsgi.url_scheme"] = "http"
+    env["wsgi.input"] = sk
+    env["wsgi.errors"] = sys.stderr
+    env["wsgi.multithread"] = True
+    env["wsgi.multiprocess"] = False
+    env["wsgi.run_once"] = False
+    return env
+
+class reqthread(ashd.serve.wsgithread):
+    def __init__(self, sk):
+        super(reqthread, self).__init__()
+        self.bsk = sk.dup()
+        self.sk = self.bsk.makefile("r+")
+
+    def handlewsgi(self):
+        return handler(self.env, self.startreq)
+
+    def writehead(self, status, headers):
+        try:
+            self.sk.write("Status: %s\n" % status)
+            for nm, val in headers:
+                self.sk.write("%s: %s\n" % (nm, val))
+            self.sk.write("\n")
+        except IOError:
+            raise ashd.serve.closed()
+
+    def writedata(self, data):
+        try:
+            self.sk.write(data)
+            self.sk.flush()
+        except IOError:
+            raise ashd.serve.closed()
+
+    def handle(self):
+        head = ashd.scgi.readhead(self.sk)
+        self.env = mkenv(head, self.sk)
+        super(reqthread, self).handle()
+
+    def run(self):
+        try:
+            super(reqthread, self).run()
+        finally:
+            self.sk.close()
+            self.bsk.close()
+
+while True:
+    nsk, addr = sk.accept()
+    try:
+        reqthread(nsk).start()
+    finally:
+        nsk.close()
index 8fa5767..c00c5a3 100644 (file)
@@ -1,13 +1,6 @@
-import sys, collections
-import threading
-
 class protoerr(Exception):
     pass
 
-class closed(IOError):
-    def __init__(self):
-        super(closed, self).__init__("The client has closed the connection.")
-
 def readns(sk):
     hln = 0
     while True:
@@ -34,115 +27,5 @@ def readhead(sk):
         i += 2
     return ret
 
-class reqthread(threading.Thread):
-    def __init__(self, sk, handler):
-        super(reqthread, self).__init__(name = "SCGI request handler")
-        self.bsk = sk.dup()
-        self.sk = self.bsk.makefile("rwb")
-        self.handler = handler
-
-    def run(self):
-        try:
-            head = readhead(self.sk)
-            self.handler(head, self.sk)
-        finally:
-            self.sk.close()
-            self.bsk.close()
-
-def handlescgi(sk, handler):
-    t = reqthread(sk, handler)
-    t.start()
-
-def servescgi(socket, handler):
-    while True:
-        nsk, addr = socket.accept()
-        try:
-            handlescgi(nsk, handler)
-        finally:
-            nsk.close()
-
 def decodehead(head, coding):
     return {k.decode(coding): v.decode(coding) for k, v in head.items()}
-
-def wrapwsgi(handler):
-    def handle(head, sk):
-        try:
-            env = decodehead(head, "utf-8")
-            env["wsgi.uri_encoding"] = "utf-8"
-        except UnicodeError:
-            env = decodehead(head, "latin-1")
-            env["wsgi.uri_encoding"] = "latin-1"
-        env["wsgi.version"] = 1, 0
-        if "HTTP_X_ASH_PROTOCOL" in env:
-            env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
-        elif "HTTPS" in env:
-            env["wsgi.url_scheme"] = "https"
-        else:
-            env["wsgi.url_scheme"] = "http"
-        env["wsgi.input"] = sk
-        env["wsgi.errors"] = sys.stderr
-        env["wsgi.multithread"] = True
-        env["wsgi.multiprocess"] = False
-        env["wsgi.run_once"] = False
-
-        resp = []
-        respsent = []
-
-        def recode(thing):
-            if isinstance(thing, collections.ByteString):
-                return thing
-            else:
-                return str(thing).encode("latin-1")
-
-        def flushreq():
-            if not respsent:
-                if not resp:
-                    raise Exception("Trying to write data before starting response.")
-                status, headers = resp
-                respsent[:] = [True]
-                buf = bytearray()
-                buf += b"Status: " + recode(status) + b"\n"
-                for nm, val in headers:
-                    buf += recode(nm) + b": " + recode(val) + b"\n"
-                buf += b"\n"
-                try:
-                    sk.write(buf)
-                except IOError:
-                    raise closed()
-
-        def write(data):
-            if not data:
-                return
-            flushreq()
-            try:
-                sk.write(data)
-                sk.flush()
-            except IOError:
-                raise closed()
-
-        def startreq(status, headers, exc_info = None):
-            if resp:
-                if exc_info:                # Interesting, this...
-                    try:
-                        if respsent:
-                            raise exc_info[1]
-                    finally:
-                        exc_info = None     # CPython GC bug?
-                else:
-                    raise Exception("Can only start responding once.")
-            resp[:] = status, headers
-            return write
-
-        respiter = handler(env, startreq)
-        try:
-            try:
-                for data in respiter:
-                    write(data)
-                if resp:
-                    flushreq()
-            except closed:
-                pass
-        finally:
-            if hasattr(respiter, "close"):
-                respiter.close()
-    return handle
index c66f6e3..0dfe9a1 100755 (executable)
@@ -1,8 +1,8 @@
 #!/usr/bin/python3
 
-import sys, os, getopt, logging
+import sys, os, getopt, logging, collections
 import socket
-import ashd.scgi
+import ashd.scgi, ashd.serve
 
 def usage(out):
     out.write("usage: scgi-wsgi3 [-hAL] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
@@ -61,4 +61,75 @@ else:
         sys.exit(1)
     handler = handlermod.application
 
-ashd.scgi.servescgi(sk, ashd.scgi.wrapwsgi(handler))
+def mkenv(head, sk):
+    try:
+        env = ashd.scgi.decodehead(head, "utf-8")
+        env["wsgi.uri_encoding"] = "utf-8"
+    except UnicodeError:
+        env = ashd.scgi.decodehead(head, "latin-1")
+        env["wsgi.uri_encoding"] = "latin-1"
+    env["wsgi.version"] = 1, 0
+    if "HTTP_X_ASH_PROTOCOL" in env:
+        env["wsgi.url_scheme"] = env["HTTP_X_ASH_PROTOCOL"]
+    elif "HTTPS" in env:
+        env["wsgi.url_scheme"] = "https"
+    else:
+        env["wsgi.url_scheme"] = "http"
+    env["wsgi.input"] = sk
+    env["wsgi.errors"] = sys.stderr
+    env["wsgi.multithread"] = True
+    env["wsgi.multiprocess"] = False
+    env["wsgi.run_once"] = False
+    return env
+
+def recode(thing):
+    if isinstance(thing, collections.ByteString):
+        return thing
+    else:
+        return str(thing).encode("latin-1")
+
+class reqthread(ashd.serve.wsgithread):
+    def __init__(self, sk):
+        super().__init__()
+        self.bsk = sk.dup()
+        self.sk = self.bsk.makefile("rwb")
+
+    def handlewsgi(self):
+        return handler(self.env, self.startreq)
+
+    def writehead(self, status, headers):
+        buf = bytearray()
+        buf += b"Status: " + recode(status) + b"\n"
+        for nm, val in headers:
+            buf += recode(nm) + b": " + recode(val) + b"\n"
+        buf += b"\n"
+        try:
+            self.sk.write(buf)
+        except IOError:
+            raise ashd.serve.closed()
+
+    def writedata(self, data):
+        try:
+            self.sk.write(data)
+            self.sk.flush()
+        except IOError:
+            raise ashd.serve.closed()
+
+    def handle(self):
+        head = ashd.scgi.readhead(self.sk)
+        self.env = mkenv(head, self.sk)
+        super().handle()
+
+    def run(self):
+        try:
+            super().run()
+        finally:
+            self.sk.close()
+            self.bsk.close()
+
+while True:
+    nsk, addr = sk.accept()
+    try:
+        reqthread(nsk).start()
+    finally:
+        nsk.close()