♻ SWU: etag/lastmodified => httpheader

We now have a unified version check for any kind of HTTP headers. Makes
things easier.
This commit is contained in:
Gina Häußge committed 2020-06-12 11:33:32 +02:00
1 parent 607e4b376c
commit 0a5f9dfc00
4 files changed
+25 -48

No files matched your search

+10 -4
View File
@@ -204,11 +204,16 @@ types are currently recognized:
* ``package``: (mandatory) Name of the package which to check.
* ``etag``: Checks the ``ETag`` header on a defined URL for changes. Additional
* ``httpheader``: Checks an HTTP header on a defined URL for changes. This can be used for easy checks
against things like ``ETag`` or ``Last-Modified`` headers. Additional
config parameters:
* ``etag_url`` or ``url``: (mandatory) URL to check. ``url`` can be used to avoid duplication in case of updater
* ``header_url`` or ``url``: (mandatory) URL to check. ``url`` can be used to avoid duplication in case of updater
methods such as ``single_file_plugin``.
* ``header_name``: (mandatory) HTTP header to check, case-insensitive, e.g. ``ETag`` or ``Last-Modified``.
* ``header_method``: HTTP request method to use for the check, defaults to ``HEAD``.
* ``header_prefix``: Prefix to use for the obtained value in the version display. If not provided ``header_name``
will be used. If set to an empty string, no prefix will be added.
* ``lastmodified``: Checks the ``Last-Modified`` header on a defined URL for changes. Additional
config parameters:
@@ -381,7 +386,8 @@ Single file plugin hosted in a gist ``https://gist.github.com/someUser/somegist`
softwareupdate:
checks:
some_plugin:
type: etag
type: httpheader
header_name: ETag
url: 'https://gist.github.com/someUser/somegist/raw/some_plugin.py'
method: single_file_plugin
@@ -401,7 +407,7 @@ The same but updated when a ``version.json`` hosted alongside gets updated with
Note that for gist hosted single file plugins, you need to use the "Raw" install link but should remove the
commit identifier. E.g. ``https://gist.githubusercontent.com/<user>/<gistid>/raw/my_plugin.py`` instead of
``https://gist.githubusercontent.com/<user>/<gistid>/raw/<commit>/my_plugin.py``. Note that these URLs will
be cached by Github for a bit, so update will not be immediately picked up.
be cached by Github for a bit, so an update will not be immediately picked up.
.. _sec-bundledplugins-softwareupdate-events:
@@ -6,7 +6,7 @@ __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agp
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
from . import commandline, git_commit, github_commit, github_release, bitbucket_commit, \
python_checker, never_current, always_current, pypi_release, etag, lastmodified, jsondata
python_checker, never_current, always_current, pypi_release, httpheader, jsondata
def log_github_ratelimit(logger, r):
ratelimit = r.headers["X-RateLimit-Limit"] if "X-RateLimit-Limit" in r.headers else "?"
@@ -15,20 +15,26 @@ def get_latest(target, check, online=True):
if not online:
raise CannotCheckOffline()
url = check.get("etag_url", check.get("url"))
current = check.get("current")
url = check.get("header_url", check.get("url"))
header = check.get("header_name")
if url is None:
raise ConfigurationInvalid("ETag version check needs etag_url or url set")
if url is None or header is None:
raise ConfigurationInvalid("HTTP header version check needs header_url or url and header_name set")
current = check.get("current")
method = check.get("header_method", "head")
prefix = check.get("header_prefix", header)
if prefix:
prefix = "{} ".format(prefix)
try:
with requests.head(url) as r:
latest = r.headers.get("Etag")
with requests.request(method, url) as r:
latest = r.headers.get(header)
except Exception as exc:
raise NetworkError(cause=exc)
information = dict(local=dict(name="ETag {}".format(current if current else "-"), value=current),
remote=dict(name="ETag {}".format(latest if latest else "-"), value=latest))
information = dict(local=dict(name="{}{}".format(prefix, current if current else "-"), value=current),
remote=dict(name="{}{}".format(prefix, latest if latest else "-"), value=latest))
logger.debug("Target: {}, local: {}, remote: {}".format(target, information["local"]["name"], information["remote"]["name"]))
@@ -1,35 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
import requests
import logging
from ..exceptions import ConfigurationInvalid, NetworkError, CannotCheckOffline
logger = logging.getLogger("octoprint.plugins.softwareupdate.version_checks.etag")
def get_latest(target, check, online=True):
if not online:
raise CannotCheckOffline()
url = check.get("lastmodified_url", check.get("url"))
current = check.get("current")
if url is None:
raise ConfigurationInvalid("Last-Modified version check needs either lastmodified_url or url set")
try:
with requests.head(url) as r:
latest = r.headers.get("Last-Modified")
except Exception as exc:
raise NetworkError(cause=exc)
information = dict(local=dict(name="Last-Modified {}".format(current if current else "-"), value=current),
remote=dict(name="Last-Modified {}".format(latest if latest else "-"), value=latest))
logger.debug("Target: {}, local: {}, remote: {}".format(target, information["local"]["name"], information["remote"]["name"]))
return information, current is None or current == latest or latest is None