Extract etag computation so it can be customized or disabled.

This commit is contained in:
Ben Darnell 2011-05-30 01:09:11 -07:00
parent 2f8dd6e4c0
commit 619bbf7c00
1 changed files with 19 additions and 10 deletions

View File

@ -598,10 +598,8 @@ class RequestHandler(object):
if (self._status_code == 200 and
self.request.method in ("GET", "HEAD") and
"Etag" not in self._headers):
hasher = hashlib.sha1()
for part in self._write_buffer:
hasher.update(part)
etag = '"%s"' % hasher.hexdigest()
etag = self.compute_etag()
if etag is not None:
inm = self.request.headers.get("If-None-Match")
if inm and inm.find(etag) != -1:
self._write_buffer = []
@ -874,6 +872,17 @@ class RequestHandler(object):
def reverse_url(self, name, *args):
return self.application.reverse_url(name, *args)
def compute_etag(self):
"""Computes the etag header to be used for this request.
May be overridden to provide custom etag implementations,
or may return None to disable tornado's default etag support.
"""
hasher = hashlib.sha1()
for part in self._write_buffer:
hasher.update(part)
return '"%s"' % hasher.hexdigest()
def _stack_context_handle_exception(self, type, value, traceback):
try:
# For historical reasons _handle_request_exception only takes