Added a simple cache-helper function.
authorFredrik Tolf <fredrik@dolda2000.com>
Wed, 29 May 2013 00:47:22 +0000 (02:47 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Wed, 29 May 2013 00:47:22 +0000 (02:47 +0200)
wrw/resp.py
wrw/util.py

index 24896ba..159126a 100644 (file)
@@ -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 []
index cfc2c8c..abf865e 100644 (file)
@@ -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)