From 9611172b885a47e1f6cc48d7c8f08da446658c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 29 Sep 2020 14:26:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20caching=20wrapper=20on=20e?= =?UTF-8?q?ndpoints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At some point the last modified implementation broke, and for some reason current Firefox also appears to be even sending an If-Modified-Since header if an ETag is present. This fixes caching again so that if both If-Modified-Since and If-None-Match are present, Last-Modified and ETag will be checked, if onlye one is present only that one will be checked, and if neither is present the cache will always be considered stale. Possible (at least partial) solution for #1241 --- src/octoprint/server/util/flask.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 3ac5913cd..022e3e88c 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -1100,7 +1100,18 @@ def with_revalidation_checking(etag_factory=None, if etag is None: etag = etag_factory(lm=lm) - return check_lastmodified(lm) and check_etag(etag) + if flask.request.if_none_match and flask.request.if_modified_since: + # use both + return check_lastmodified(lm) and check_etag(etag) + elif flask.request.if_none_match: + # use only ETag + return check_etag(etag) + elif flask.request.if_modified_since: + # use only Last-Modified + return check_lastmodified(lm) + else: + # assume stale cache + return False if unless is None: def unless(): @@ -1140,7 +1151,6 @@ def check_etag(etag): return False return flask.request.method in ("GET", "HEAD") and \ - flask.request.if_none_match is not None and \ etag in flask.request.if_none_match @@ -1161,8 +1171,7 @@ def check_lastmodified(lastmodified): raise ValueError("lastmodified must be a datetime or float or int instance but, got {} instead".format(lastmodified.__class__)) return flask.request.method in ("GET", "HEAD") and \ - flask.request.if_modified_since is not None and \ - lastmodified >= flask.request.if_modified_since + lastmodified <= flask.request.if_modified_since def add_revalidation_response_headers(response):