# HG changeset patch # User Dirkjan Ochtman # Date 1283952228 -7200 # Node ID f64b416b0ac88cccf4e83cc6059d4686e9365f85 # Parent 1121af2397618d17e288c4a849aba1edc59d7214 hgweb: support very simple caching model (issue1845) diff -r 1121af239761 -r f64b416b0ac8 mercurial/hgweb/common.py --- 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)) diff -r 1121af239761 -r f64b416b0ac8 mercurial/hgweb/hgweb_mod.py --- 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) diff -r 1121af239761 -r f64b416b0ac8 tests/test-hgweb-no-path-info --- 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 diff -r 1121af239761 -r f64b416b0ac8 tests/test-hgweb-no-request-uri --- 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 diff -r 1121af239761 -r f64b416b0ac8 tests/test-hgweb-non-interactive --- 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 diff -r 1121af239761 -r f64b416b0ac8 tests/test-hgweb.out --- 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