diff options
| author | Dennis Fink | 2026-11-13 20:08:42 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 20:08:42 +0100 |
| commit | db7748111141d9d103f335fb8a0c647bce78b2cc (patch) | |
| tree | 36ed82450b13618253a5e8b3c0dc548489345bfe /webmentions_ssg/tasks/source_parser.py | |
| parent | 87117345ed6e99e5a070d919ac17139cba7b8fe4 (diff) | |
| download | webmentions-ssg-db7748111141d9d103f335fb8a0c647bce78b2cc.tar.gz webmentions-ssg-db7748111141d9d103f335fb8a0c647bce78b2cc.zip | |
refactor(sender): flatten canonical URL parsing
Return early when no canonical link is present so canonical URL
validation can follow a linear path with less nesting and unchanged scan
behavior.
Diffstat (limited to 'webmentions_ssg/tasks/source_parser.py')
| -rw-r--r-- | webmentions_ssg/tasks/source_parser.py | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/webmentions_ssg/tasks/source_parser.py b/webmentions_ssg/tasks/source_parser.py index 1bd498c..9cbe071 100644 --- a/webmentions_ssg/tasks/source_parser.py +++ b/webmentions_ssg/tasks/source_parser.py @@ -97,30 +97,31 @@ def canonical_url( :raises SourceScanError: If the canonical URL is empty, cannot be resolved, or does not resolve to an HTTP or HTTPS URL. """ - if (link := document.select_one('link[rel~="canonical"][href]')) is not None: - href = link.get("href") + if (link := document.select_one('link[rel~="canonical"][href]')) is None: + return None - if not isinstance(href, str) or not (href := href.strip()): - raise SourceScanError("Document contains an empty canonical URL") + href = link.get("href") - if is_http_url(href): - return href + if not isinstance(href, str) or not (href := href.strip()): + raise SourceScanError("Document contains an empty canonical URL") - if base_url is None: - raise SourceScanError( - "Document contains a relative canonical URL, but " - "SOURCE_BASE_URL is not configured" - ) + if is_http_url(href): + return href - resolved = urljoin(source_url_for_path(relative_path, base_url), href) + if base_url is None: + raise SourceScanError( + "Document contains a relative canonical URL, but " + "SOURCE_BASE_URL is not configured" + ) - if not is_http_url(resolved): - raise SourceScanError( - f"Canonical URL is not a valid HTTP or HTTPS URL: {href!r}" - ) + resolved = urljoin(source_url_for_path(relative_path, base_url), href) + + if not is_http_url(resolved): + raise SourceScanError( + f"Canonical URL is not a valid HTTP or HTTPS URL: {href!r}" + ) - return resolved - return None + return resolved def property_urls(entry: dict[str, Any], property_name: str) -> Iterator[str]: |
