Fix overeager preemptive caching, even on broken protocols

This commit is contained in:
Gina Häußge 2016-07-04 19:28:20 +02:00
parent a49bc9ca75
commit 6018bf8428
3 changed files with 14 additions and 4 deletions

View file

@ -793,6 +793,12 @@ class Server(object):
additional_request_data = kwargs.get("_additional_request_data", dict())
kwargs = dict((k, v) for k, v in kwargs.items() if not k.startswith("_") and not k == "plugin")
kwargs.update(additional_request_data)
base_url = kwargs.get("base_url", "")
if not (base_url.startswith("http://") or base_url.startswith("https://")):
self._logger.info("Skipping preemptive cache entry with base url {}, neither http:// nor https:// and possibly broken".format(base_url))
continue
try:
if plugin:
self._logger.info("Preemptively caching {} (plugin {}) for {!r}".format(route, plugin, kwargs))

View file

@ -488,6 +488,12 @@ class PreemptiveCache(object):
return matched, unmatched
with self._lock:
if not self._log_access:
self._logger.debug(
"Not updating timestamp and counter for {} and {!r}, currently flagged as disabled".format(root,
data))
return
cache_data = self.get_all_data()
if not root in cache_data:
@ -509,12 +515,10 @@ class PreemptiveCache(object):
to_persist["_timestamp"] = time.time()
to_persist["_count"] = 1
self._logger.info("Adding entry for {} and {!r}".format(root, to_persist))
elif self._log_access:
else:
to_persist["_timestamp"] = time.time()
to_persist["_count"] = to_persist.get("_count", 0) + 1
self._logger.debug("Updating timestamp and counter for {} and {!r}".format(root, data))
else:
self._logger.debug("Not updating timestamp and counter for {} and {!r}, currently flagged as disabled".format(root, data))
self.set_data(root, [to_persist] + other)

View file

@ -32,7 +32,7 @@ _valid_div_re = re.compile("[a-zA-Z_-]+")
@app.route("/")
@util.flask.preemptively_cached(cache=preemptiveCache,
data=lambda: dict(path=request.path, base_url=request.url_root, query_string="l10n={}".format(g.locale.language) if g.locale else "en"),
unless=lambda: request.url_root in settings().get(["server", "preemptiveCache", "exceptions"]))
unless=lambda: request.url_root in settings().get(["server", "preemptiveCache", "exceptions"]) or not (request.url_root.startswith("http://") or request.url_root.startswith("https://")))
@util.flask.conditional(lambda: _check_etag_and_lastmodified_for_index(), NOT_MODIFIED)
@util.flask.cached(timeout=-1,
refreshif=lambda cached: _validate_cache_for_index(cached),