From c9aa6b2889e6de7f82e5f44ab5bbcd0ef90099bd Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 5 Jan 2014 08:32:44 +0100 Subject: [PATCH] python: Converted scgi-wsgi3 as well to use modular handling. --- python3/scgi-wsgi3 | 61 +++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/python3/scgi-wsgi3 b/python3/scgi-wsgi3 index bd625c4..1f5495d 100755 --- a/python3/scgi-wsgi3 +++ b/python3/scgi-wsgi3 @@ -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() -- 2.11.0