From 0a5f9dfc00d0d3f4fba8e05aad107e8c6f68505e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 12 Jun 2020 11:33:32 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20SWU:=20etag/lastmodified=20=3D>=20h?= =?UTF-8?q?ttpheader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now have a unified version check for any kind of HTTP headers. Makes things easier. --- docs/bundledplugins/softwareupdate.rst | 14 +++++--- .../softwareupdate/version_checks/__init__.py | 2 +- .../version_checks/{etag.py => httpheader.py} | 22 +++++++----- .../version_checks/lastmodified.py | 35 ------------------- 4 files changed, 25 insertions(+), 48 deletions(-) rename src/octoprint/plugins/softwareupdate/version_checks/{etag.py => httpheader.py} (58%) delete mode 100644 src/octoprint/plugins/softwareupdate/version_checks/lastmodified.py diff --git a/docs/bundledplugins/softwareupdate.rst b/docs/bundledplugins/softwareupdate.rst index 9453df4f4..f99e5627f 100644 --- a/docs/bundledplugins/softwareupdate.rst +++ b/docs/bundledplugins/softwareupdate.rst @@ -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///raw/my_plugin.py`` instead of ``https://gist.githubusercontent.com///raw//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: diff --git a/src/octoprint/plugins/softwareupdate/version_checks/__init__.py b/src/octoprint/plugins/softwareupdate/version_checks/__init__.py index 77ef02835..d769a224b 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/__init__.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/__init__.py @@ -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 "?" diff --git a/src/octoprint/plugins/softwareupdate/version_checks/etag.py b/src/octoprint/plugins/softwareupdate/version_checks/httpheader.py similarity index 58% rename from src/octoprint/plugins/softwareupdate/version_checks/etag.py rename to src/octoprint/plugins/softwareupdate/version_checks/httpheader.py index 42ab857ee..ec3c9898e 100644 --- a/src/octoprint/plugins/softwareupdate/version_checks/etag.py +++ b/src/octoprint/plugins/softwareupdate/version_checks/httpheader.py @@ -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"])) diff --git a/src/octoprint/plugins/softwareupdate/version_checks/lastmodified.py b/src/octoprint/plugins/softwareupdate/version_checks/lastmodified.py deleted file mode 100644 index 498ec4c2f..000000000 --- a/src/octoprint/plugins/softwareupdate/version_checks/lastmodified.py +++ /dev/null @@ -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