+ return super().__init__(404)
+
+class redirect(dispatch.restart):
+ bases = {"url": proto.requrl,
+ "script": proto.scripturl,
+ "site": proto.siteurl}
+
+ def __init__(self, url, status=303, base="url"):
+ super().__init__()
+ self.url = url
+ self.status = status
+ self.bases[base]
+ self.base = base
+
+ def handle(self, req):
+ req.status(self.status, "Redirect")
+ req.ohead["Location"] = proto.appendurl(self.bases[self.base](req), self.url)
+ req.ohead["Content-Length"] = 0
+ return []
+
+class unmodified(dispatch.restart):
+ def handle(self, req):
+ 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)