# HG changeset patch # User Gregory Szorc # Date 2018-03-10 22:19:27 # Node ID 7ad6a275316f9ba5ec50b09bd1470f2bc8f7c084 # Parent 8ddb5c354906973a711e2734859cdbfaccfa4319 hgweb: inline caching() and port to modern mechanisms We only had one consumer of this simple function. While it could be a generic function, let's not over abstract the code. As part of inlining, we port it off wsgirequest, fix some Python 3 issues, and set a response header on our new response object so it is ready once we start using it to send responses. Differential Revision: https://phab.mercurial-scm.org/D2785 diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py --- a/mercurial/hgweb/common.py +++ b/mercurial/hgweb/common.py @@ -214,12 +214,6 @@ def get_contact(config): config("ui", "username") or encoding.environ.get("EMAIL") or "") -def caching(web, req): - tag = r'W/"%d"' % web.mtime - if req.env.get('HTTP_IF_NONE_MATCH') == tag: - raise ErrorResponse(HTTP_NOT_MODIFIED) - req.headers.append(('ETag', tag)) - def cspvalues(ui): """Obtain the Content-Security-Policy header and nonce value. 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 @@ -18,7 +18,6 @@ from .common import ( HTTP_NOT_MODIFIED, HTTP_OK, HTTP_SERVER_ERROR, - caching, cspvalues, permhooks, ) @@ -388,7 +387,13 @@ class hgweb(object): # Don't enable caching if using a CSP nonce because then it wouldn't # be a nonce. if rctx.configbool('web', 'cache') and not rctx.nonce: - caching(self, wsgireq) # sets ETag header or raises NOT_MODIFIED + tag = 'W/"%d"' % self.mtime + if req.headers.get('If-None-Match') == tag: + raise ErrorResponse(HTTP_NOT_MODIFIED) + + wsgireq.headers.append((r'ETag', pycompat.sysstr(tag))) + res.headers['ETag'] = tag + if cmd not in webcommands.__all__: msg = 'no such method: %s' % cmd raise ErrorResponse(HTTP_BAD_REQUEST, msg)