Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support etag headers #355

Merged
merged 9 commits into from Feb 14, 2019
27 changes: 27 additions & 0 deletions web_monitoring/diffing_server.py
Expand Up @@ -115,8 +115,35 @@ def options(self):
class DiffHandler(BaseHandler):
# subclass must define `differs` attribute

# Compute our own ETag header values.
# We're not actually hashing content for this, since that is expensive.
def compute_etag(self):
query_params = {k: v[-1].decode() for k, v in
self.request.arguments.items()}
jsnshrmn marked this conversation as resolved.
Show resolved Hide resolved
etag = str('W/"' + hashlib.sha256(
jsnshrmn marked this conversation as resolved.
Show resolved Hide resolved
web_monitoring.__version__.encode('utf-8') + self.request.path.encode('utf-8') + str(query_params).encode('utf-8')
).hexdigest() + '"').encode('utf-8')
jsnshrmn marked this conversation as resolved.
Show resolved Hide resolved
return etag

def head(self, differ):

self.set_etag_header()
if self.check_etag_header():
self.set_status(304)
self.finish()
return


@tornado.gen.coroutine
def get(self, differ):

# Skip a whole bunch of work if possible.
self.set_etag_header()
if self.check_etag_header():
self.set_status(304)
self.finish()
return

# Find the diffing function registered with the name given by `differ`.
try:
func = self.differs[differ]
Expand Down