X-Git-Url: http://dolda2000.com/gitweb/?p=wrw.git;a=blobdiff_plain;f=wrw%2Fresp.py;h=00c7045ffdd03799b62502212c43ec8b569c7dd8;hp=965df6c8c18a76e41e659f92eccf08ef57f4fe30;hb=782b0af56fb0ba7bc0afee9c26e24014ca43def3;hpb=d30502c8fe37bedb30ad9f3ddecd5191c5b077fb diff --git a/wrw/resp.py b/wrw/resp.py index 965df6c..00c7045 100644 --- a/wrw/resp.py +++ b/wrw/resp.py @@ -1,3 +1,4 @@ +import os from . import dispatch, proto, env from .sp import xhtml h = xhtml.cons() @@ -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)