From a285fdf29e59c3b957247ea9241943c7202e9a28 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 13 Nov 2026 21:20:40 +0100 Subject: refactor(core): consolidate repeated helper logic Centralize SQL row counts, hostname parsing, and content-type normalization so tasks, forms, and scanners share consistent implementations. Simplify periodic task registration and admin lookups with table-driven setup and Flask-SQLAlchemy's built-in 404 helper. --- webmentions_ssg/net.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'webmentions_ssg/net.py') diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py index 7c29edc..02c7f2b 100644 --- a/webmentions_ssg/net.py +++ b/webmentions_ssg/net.py @@ -7,7 +7,7 @@ import ipaddress import socket import time -from collections.abc import Iterable +from collections.abc import Iterable, Mapping from contextlib import ExitStack from http import HTTPStatus from urllib.parse import urlsplit @@ -252,6 +252,20 @@ def public_addresses( return tuple(str(address) for address in addresses) +def hostname_of(url: str) -> str | None: + """Return a normalized URL hostname, or None for malformed URLs.""" + try: + hostname = urlsplit(url).hostname + except (TypeError, ValueError): + return None + return hostname.rstrip(".") if hostname is not None else None + + +def content_type_of(headers: Mapping[str, str]) -> str: + """Return a normalized media type without Content-Type parameters.""" + return headers.get("Content-Type", "").partition(";")[0].strip().lower() + + def is_http_url(url: str) -> bool: """Check whether a URL is an absolute HTTP or HTTPS URL. -- cgit v1.3.1