hgweb: support very simple caching model (issue1845)
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Wed, 08 Sep 2010 15:23:48 +0200
changeset 12183 f64b416b0ac8
parent 12182 1121af239761
child 12187 4a854122a1c7
hgweb: support very simple caching model (issue1845)
mercurial/hgweb/common.py
mercurial/hgweb/hgweb_mod.py
tests/test-hgweb-no-path-info
tests/test-hgweb-no-request-uri
tests/test-hgweb-non-interactive
tests/test-hgweb.out
--- a/mercurial/hgweb/common.py	Wed Sep 08 15:11:35 2010 +0200
+++ b/mercurial/hgweb/common.py	Wed Sep 08 15:23:48 2010 +0200
@@ -9,6 +9,7 @@
 import errno, mimetypes, os
 
 HTTP_OK = 200
+HTTP_NOT_MODIFIED = 304
 HTTP_BAD_REQUEST = 400
 HTTP_UNAUTHORIZED = 401
 HTTP_FORBIDDEN = 403
@@ -152,3 +153,9 @@
     return (config("web", "contact") or
             config("ui", "username") or
             os.environ.get("EMAIL") or "")
+
+def caching(web, req):
+    tag = str(web.mtime)
+    if req.env.get('HTTP_IF_NONE_MATCH') == tag:
+        raise ErrorResponse(HTTP_NOT_MODIFIED)
+    req.headers.append(('ETag', tag))
--- a/mercurial/hgweb/hgweb_mod.py	Wed Sep 08 15:11:35 2010 +0200
+++ b/mercurial/hgweb/hgweb_mod.py	Wed Sep 08 15:23:48 2010 +0200
@@ -8,7 +8,7 @@
 
 import os
 from mercurial import ui, hg, hook, error, encoding, templater
-from common import get_mtime, ErrorResponse, permhooks
+from common import get_mtime, ErrorResponse, permhooks, caching
 from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
 from request import wsgirequest
 import webcommands, protocol, webutil
@@ -178,6 +178,7 @@
                 req.form['cmd'] = [tmpl.cache['default']]
                 cmd = req.form['cmd'][0]
 
+            caching(self, req) # sets ETag header or raises NOT_MODIFIED
             if cmd not in webcommands.__all__:
                 msg = 'no such method: %s' % cmd
                 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
--- a/tests/test-hgweb-no-path-info	Wed Sep 08 15:11:35 2010 +0200
+++ b/tests/test-hgweb-no-path-info	Wed Sep 08 15:23:48 2010 +0200
@@ -23,7 +23,7 @@
 	print '---- STATUS'
 	print status
 	print '---- HEADERS'
-	print headers
+	print [i for i in headers if i[0] != 'ETag']
 	print '---- DATA'
 	return output.write
 
--- a/tests/test-hgweb-no-request-uri	Wed Sep 08 15:11:35 2010 +0200
+++ b/tests/test-hgweb-no-request-uri	Wed Sep 08 15:23:48 2010 +0200
@@ -23,7 +23,7 @@
 	print '---- STATUS'
 	print status
 	print '---- HEADERS'
-	print headers
+	print [i for i in headers if i[0] != 'ETag']
 	print '---- DATA'
 	return output.write
 
--- a/tests/test-hgweb-non-interactive	Wed Sep 08 15:11:35 2010 +0200
+++ b/tests/test-hgweb-non-interactive	Wed Sep 08 15:23:48 2010 +0200
@@ -40,7 +40,7 @@
 	print '---- STATUS'
 	print status
 	print '---- HEADERS'
-	print headers
+	print [i for i in headers if i[0] != 'ETag']
 	print '---- DATA'
 	return output.write
 
--- a/tests/test-hgweb.out	Wed Sep 08 15:11:35 2010 +0200
+++ b/tests/test-hgweb.out	Wed Sep 08 15:23:48 2010 +0200
@@ -364,4 +364,6 @@
 	top: -3px;
 	font-style: italic;
 }
+304 Not Modified
+
 % errors