python: Converted scgi-wsgi3 as well to use modular handling.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Jan 2014 07:32:44 +0000 (08:32 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Jan 2014 07:32:44 +0000 (08:32 +0100)
python3/scgi-wsgi3

index bd625c4..1f5495d 100755 (executable)
@@ -95,51 +95,46 @@ def recode(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")
+reqhandler = ashd.serve.freethread()
 
-    def handlewsgi(self):
-        return handler(self.env, self.startreq)
+class request(ashd.serve.wsgirequest):
+    def __init__(self, *, sk, **kw):
+        super().__init__(**kw)
+        self.sk = sk.dup()
+
+    def mkenv(self):
+        fsk = self.sk.dup().makefile("rwb")
+        try:
+            return mkenv(ashd.scgi.readhead(fsk), fsk)
+        finally:
+            fsk.close()
+
+    def handlewsgi(self, env, startreq):
+        return handler(env, startreq)
+
+    def fileno(self):
+        return self.sk.fileno()
 
     def writehead(self, status, headers):
-        buf = bytearray()
-        buf += b"Status: " + recode(status) + b"\n"
+        w = self.buffer.extend
+        w(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()
+            w(recode(nm) + b": " + recode(val) + b"\n")
+        w(b"\n")
 
-    def writedata(self, data):
+    def flush(self):
         try:
-            self.sk.write(data)
-            self.sk.flush()
+            ret = self.sk.send(self.buffer, socket.MSG_DONTWAIT)
+            self.buffer[:ret] = b""
         except IOError:
             raise ashd.serve.closed()
 
-    def handle(self):
-        head = ashd.scgi.readhead(self.sk)
-        self.env = mkenv(head, self.sk)
-        with ashd.perf.request(self.env) as reqevent:
-            super().handle()
-            if self.status:
-                reqevent.response([self.status, self.headers])
-
-    def run(self):
-        try:
-            super().run()
-        finally:
-            self.sk.close()
-            self.bsk.close()
+    def close(self):
+        self.sk.close()
 
 while True:
     nsk, addr = sk.accept()
     try:
-        reqthread(nsk).start()
+        reqhandler.handle(request(sk=nsk, handler=reqhandler))
     finally:
         nsk.close()