diff options
| author | Dennis Fink | 2026-11-13 21:20:40 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 21:20:40 +0100 |
| commit | a285fdf29e59c3b957247ea9241943c7202e9a28 (patch) | |
| tree | 17ceeb703de23af1e67081d99a8864bbe353fb46 /webmentions_ssg/net.py | |
| parent | 78ecd8bc227bdcbbd0e45eea025f7223252ed0da (diff) | |
| download | webmentions-ssg-a285fdf29e59c3b957247ea9241943c7202e9a28.tar.gz webmentions-ssg-a285fdf29e59c3b957247ea9241943c7202e9a28.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/net.py | 16 |
1 files changed, 15 insertions, 1 deletions
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. |
