diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py --- a/mercurial/hgweb/common.py +++ b/mercurial/hgweb/common.py @@ -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 @@ def get_contact(config): 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 --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -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 @@ class hgweb(object): 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 --git a/tests/test-hgweb-no-path-info b/tests/test-hgweb-no-path-info --- a/tests/test-hgweb-no-path-info +++ b/tests/test-hgweb-no-path-info @@ -23,7 +23,7 @@ def startrsp(status, headers): print '---- STATUS' print status print '---- HEADERS' - print headers + print [i for i in headers if i[0] != 'ETag'] print '---- DATA' return output.write diff --git a/tests/test-hgweb-no-request-uri b/tests/test-hgweb-no-request-uri --- a/tests/test-hgweb-no-request-uri +++ b/tests/test-hgweb-no-request-uri @@ -23,7 +23,7 @@ def startrsp(status, headers): print '---- STATUS' print status print '---- HEADERS' - print headers + print [i for i in headers if i[0] != 'ETag'] print '---- DATA' return output.write diff --git a/tests/test-hgweb-non-interactive b/tests/test-hgweb-non-interactive --- a/tests/test-hgweb-non-interactive +++ b/tests/test-hgweb-non-interactive @@ -40,7 +40,7 @@ def startrsp(status, headers): print '---- STATUS' print status print '---- HEADERS' - print headers + print [i for i in headers if i[0] != 'ETag'] print '---- DATA' return output.write diff --git a/tests/test-hgweb.out b/tests/test-hgweb.out --- a/tests/test-hgweb.out +++ b/tests/test-hgweb.out @@ -364,4 +364,6 @@ ul#graphnodes li .info { top: -3px; font-style: italic; } +304 Not Modified + % errors