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/forms.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'webmentions_ssg/forms.py') diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index 005bd20..f5fba51 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -5,7 +5,6 @@ """Webmention forms and URL validation.""" import ipaddress -from urllib.parse import urlsplit import httpx from flask import current_app @@ -14,7 +13,7 @@ from wtforms import Field, PasswordField, StringField, SubmitField, ValidationEr from wtforms.form import BaseForm from wtforms.validators import InputRequired, StopValidation -from .net import is_public_address +from .net import hostname_of, is_public_address from .validation import is_control_character @@ -96,10 +95,7 @@ class AllowedHostname: :param field: Field containing the URL to validate. :raises ValidationError: If the URL hostname is not configured as allowed. """ - try: - hostname = (urlsplit(field.data).hostname or "").rstrip(".") - except ValueError, TypeError: - hostname = None + hostname = hostname_of(field.data) if hostname is not None and hostname in current_app.config["ALLOWED_HOSTNAMES"]: return raise ValidationError(self.message or field.gettext("Invalid input.")) @@ -109,11 +105,7 @@ class PublicIPLiteral: """Reject non-public IP literals without performing DNS in the web worker.""" def __call__(self, form: BaseForm, field: Field) -> None: - try: - hostname = urlsplit(field.data).hostname - except (ValueError, TypeError) as exc: - raise ValidationError(field.gettext("Invalid input.")) from exc - + hostname = hostname_of(field.data) if hostname is None: raise ValidationError(field.gettext("Invalid input.")) -- cgit v1.3.1