From 1864be32db482df017bc7dd57bbd2ccadeec4429 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 29 May 2013 02:47:22 +0200 Subject: [PATCH] Added a simple cache-helper function. --- wrw/resp.py | 6 ++++++ wrw/util.py | 11 +++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/wrw/resp.py b/wrw/resp.py index 24896ba..159126a 100644 --- a/wrw/resp.py +++ b/wrw/resp.py @@ -71,3 +71,9 @@ class redirect(dispatch.restart): req.status(self.status, "Redirect") req.ohead["Location"] = proto.appendurl(proto.requrl(req), self.url) return [] + +class unmodified(dispatch.restart): + def handle(self, req): + req.status(304, "Not Modified") + req.ohead["Content-Length"] = "0" + return [] diff --git a/wrw/util.py b/wrw/util.py index cfc2c8c..abf865e 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -1,5 +1,5 @@ -import inspect -import req, dispatch, session, form, resp +import inspect, math +import req, dispatch, session, form, resp, proto def wsgiwrap(callable): def wrapper(env, startreq): @@ -233,3 +233,10 @@ class specdirty(sessiondata): ss[i] = specslot.unbound else: ss[i] = val + +def datecheck(req, mtime): + if "If-Modified-Since" in req.ihead: + rtime = proto.phttpdate(req.ihead["If-Modified-Since"]) + if rtime >= math.floor(mtime): + raise resp.unmodified() + req.ohead["Last-Modified"] = proto.httpdate(mtime) -- 2.11.0