X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fresp.py;h=00c7045ffdd03799b62502212c43ec8b569c7dd8;hb=782b0af56fb0ba7bc0afee9c26e24014ca43def3;hp=7fae787f81a92c0f4447c81be0ffcdcc042c2265;hpb=9bc70dab473c7057a20d182ce8566ad0a70a7c5d;p=wrw.git diff --git a/wrw/resp.py b/wrw/resp.py index 7fae787..00c7045 100644 --- a/wrw/resp.py +++ b/wrw/resp.py @@ -1,5 +1,6 @@ -import dispatch, proto, env -from sp import xhtml +import os +from . import dispatch, proto, env +from .sp import xhtml h = xhtml.cons() __all__ = ["skeleton", "skelfor", "setskel", "usererror"] @@ -28,7 +29,7 @@ def setskel(req, skel): class usererror(dispatch.restart): def __init__(self, message, *detail): - super(usererror, self).__init__() + super().__init__() self.message = message self.detail = detail @@ -36,9 +37,9 @@ class usererror(dispatch.restart): return skelfor(req).error(req, self.message, *self.detail) class message(dispatch.restart): - def __init__(self, msg, *detail): - super(message, self).__init__() - self.message = msg + def __init__(self, message, *detail): + super().__init__() + self.message = message self.detail = detail def handle(self, req): @@ -50,16 +51,16 @@ class httperror(usererror): message = proto.statusinfo[status][0] if detail is None: detail = (proto.statusinfo[status][1],) - super(httperror, self).__init__(message, *detail) + super().__init__(message, *detail) self.status = status def handle(self, req): req.status(self.status, self.message) - return super(httperror, self).handle(req) + return super().handle(req) class notfound(httperror): def __init__(self): - return super(notfound, self).__init__(404) + return super().__init__(404) class redirect(dispatch.restart): bases = {"url": proto.requrl, @@ -67,7 +68,7 @@ class redirect(dispatch.restart): "site": proto.siteurl} def __init__(self, url, status=303, base="url"): - super(redirect, self).__init__() + super().__init__() self.url = url self.status = status self.bases[base] @@ -84,3 +85,38 @@ class unmodified(dispatch.restart): req.status(304, "Not Modified") req.ohead["Content-Length"] = "0" return [] + +class fileiter(object): + def __init__(self, fp): + self.fp = fp + + def __iter__(self): + return self + + def __next__(self): + if self.fp is None: + raise StopIteration() + data = self.fp.read(16384) + if data == b"": + self.fp.close() + self.fp = None + raise StopIteration() + return data + + def close(self): + if self.fp is not None: + self.fp.close() + self.fp = None + +class fileresp(dispatch.restart): + def __init__(self, fp, ctype): + self.fp = fp + self.ctype = ctype + + def handle(self, req): + req.ohead["Content-Type"] = self.ctype + if hasattr(self.fp, "fileno"): + sz = os.fstat(self.fp.fileno()).st_size + if sz > 0: + req.ohead["Content-Length"] = str(sz) + return fileiter(self.fp)