X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fdispatch.py;h=5276892ac9e1abbc8250a92f07d78cdc0722e052;hb=2a5a8ce70a0107b7bfd5bd3459ae4aae3a7558e2;hp=e51559e1fbe4b477a847af6bc1ea985a588393f2;hpb=825ac8f0cafe69dfb61c127556ef50f0f92e6168;p=wrw.git diff --git a/wrw/dispatch.py b/wrw/dispatch.py index e51559e..5276892 100644 --- a/wrw/dispatch.py +++ b/wrw/dispatch.py @@ -1,5 +1,5 @@ import sys, traceback -from . import env +from . import env, req, proto __all__ = ["restart"] @@ -16,32 +16,6 @@ def mangle(result): return result return [str(result)] -class iterproxy(object): - # Makes sure iter(real).next() is called immediately, in order to - # let generator code run. - def __init__(self, real): - self.bk = real - self.bki = iter(real) - self._next = [None] - self.__next__() - - def __iter__(self): - return self - - def __next__(self): - if self._next is None: - raise StopIteration() - ret = self._next[0] - try: - self._next[:] = [self.bki.__next__()] - except StopIteration: - self._next = None - return ret - - def close(self): - if hasattr(self.bk, "close"): - self.bk.close() - def defaulterror(req, excinfo): from . import resp traceback.print_exception(*excinfo) @@ -50,6 +24,7 @@ def defaulterror(req, excinfo): def wraphandler(handler, excinfo): def wrapped(req): return handler(req, excinfo) + wrapped.__wrapped__ = handler return wrapped errorhandler = env.var(defaulterror) @@ -60,7 +35,7 @@ def handle(req, startreq, handler): resp = [""] while True: try: - resp = iterproxy(handler(req)) + resp = handler(req) break except restart as i: handler = i.handle @@ -73,3 +48,9 @@ def handle(req, startreq, handler): return resp finally: req.cleanup() + +def handleenv(env, startreq, handler): + if not "HTTP_HOST" in env: + return proto.simpleerror(env, startreq, 400, "Bad Request", "Request must include Host header.") + r = req.origrequest(env) + return handle(r, startreq, handler)