diff options
| author | Dennis Fink | 2026-10-30 19:47:19 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-30 19:47:19 +0100 |
| commit | a2992533873cb53acb052cdcd4288430b24af587 (patch) | |
| tree | 2be1c48d2f3d99b0d4c884f9db41e90fad883d22 /webmentions_ssg | |
| parent | 616ab6a8daaae6334b832fee12d9e8e335f0aba6 (diff) | |
| download | webmentions-ssg-a2992533873cb53acb052cdcd4288430b24af587.tar.gz webmentions-ssg-a2992533873cb53acb052cdcd4288430b24af587.zip | |
fix(sender): make endpoint discovery more resilient
Fall back to GET when HEAD fails so targets that reject or mishandle
HEAD can still advertise a Webmention endpoint.
Ignore malformed or unsupported endpoint candidates and continue
searching for the first valid HTTP endpoint.
Diffstat (limited to 'webmentions_ssg')
| -rw-r--r-- | webmentions_ssg/tasks/sender.py | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py index a4a8367..725b5c8 100644 --- a/webmentions_ssg/tasks/sender.py +++ b/webmentions_ssg/tasks/sender.py @@ -73,7 +73,10 @@ def resolve_endpoint(response: httpx.Response, href: str) -> str: :raises PermanentSenderError: If the resolved endpoint is not an HTTP or HTTPS URL. """ - endpoint = urljoin(str(response.url), href.strip()) + try: + endpoint = urljoin(str(response.url), href.strip()) + except ValueError as exc: + raise PermanentSenderError(f"Invalid Webmention endpoint: {href!r}") from exc if not is_http_url(endpoint): raise PermanentSenderError(f"Invalid Webmention endpoint: {endpoint!r}") @@ -116,8 +119,8 @@ def endpoint_from_headers(response: httpx.Response) -> str | None: Find a Webmention endpoint in the response Link headers. :param response: HTTP response whose headers should be inspected. - :return: First advertised Webmention endpoint, or ``None`` if none is found. - :raises PermanentSenderError: If a discovered endpoint is invalid. + :return: First valid advertised Webmention endpoint, or ``None`` if none + is found. Invalid candidates are ignored. """ for header in response.headers.get_list("Link"): for value in LINK_SPLIT.split(header): @@ -127,7 +130,10 @@ def endpoint_from_headers(response: httpx.Response) -> str | None: href, relations = link if "webmention" in relations: - return resolve_endpoint(response, href) + try: + return resolve_endpoint(response, href) + except PermanentSenderError: + continue return None @@ -140,8 +146,8 @@ def endpoint_from_html(response: httpx.Response, body: bytes) -> str | None: :param response: HTTP response from which the document was retrieved. :param body: HTML document body. - :return: First advertised Webmention endpoint, or ``None`` if none is found. - :raises PermanentSenderError: If a discovered endpoint is invalid. + :return: First valid advertised Webmention endpoint, or ``None`` if none + is found. Invalid candidates are ignored. """ document = BeautifulSoup(body, "html5lib") @@ -165,7 +171,10 @@ def endpoint_from_html(response: httpx.Response, body: bytes) -> str | None: if not isinstance(href, str): continue - return resolve_endpoint(response, href) + try: + return resolve_endpoint(response, href) + except PermanentSenderError: + continue return None @@ -211,13 +220,17 @@ def discover_webmention_endpoint(client: httpx.Client, target: str) -> str | Non :param target: Target URL whose Webmention endpoint should be discovered. :return: Discovered Webmention endpoint, or ``None`` if none is advertised. :raises TemporarySenderError: If the target returns a temporary HTTP failure. - :raises PermanentSenderError: If the target returns a permanent HTTP failure - or advertises an invalid endpoint. + :raises PermanentSenderError: If GET returns a permanent HTTP failure. """ - head_response = client.head(target, headers=DISCOVERY_HEADERS) - - if endpoint := endpoint_from_headers(head_response): - return endpoint + try: + head_response = client.head(target, headers=DISCOVERY_HEADERS) + except httpx.RequestError, TemporarySenderError: + # HEAD is only an optimization. Servers and intermediaries may reject + # or drop it even when fetching the document with GET would succeed. + pass + else: + if endpoint := endpoint_from_headers(head_response): + return endpoint with client.stream("GET", target, headers=DISCOVERY_HEADERS) as response: if endpoint := endpoint_from_headers(response): |
