aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg
diff options
context:
space:
mode:
Diffstat (limited to 'webmentions_ssg')
-rw-r--r--webmentions_ssg/net.py81
-rw-r--r--webmentions_ssg/tasks/discovery.py11
-rw-r--r--webmentions_ssg/tasks/receiver.py3
-rw-r--r--webmentions_ssg/tasks/sender.py12
4 files changed, 68 insertions, 39 deletions
diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py
index 9a6ecfb..0314810 100644
--- a/webmentions_ssg/net.py
+++ b/webmentions_ssg/net.py
@@ -8,6 +8,7 @@ import ipaddress
import socket
import time
from collections.abc import Iterable
+from contextlib import ExitStack
from urllib.parse import urlsplit
import dns.exception
@@ -29,6 +30,10 @@ class UnsafeAddressError(ValueError):
"""A hostname or IP literal identifies a non-public network address."""
+class ResponseTooLargeError(ValueError):
+ """A response body exceeds its configured size limit."""
+
+
class NetworkDeadline:
"""One monotonic deadline shared by DNS, TCP, TLS, and response streaming."""
@@ -89,6 +94,43 @@ class PublicNetworkBackend(httpcore.SyncBackend):
def __init__(self, deadline: NetworkDeadline):
self._deadline = deadline
+ def _connect_one(
+ self,
+ address: str,
+ port: int,
+ timeout: float | None,
+ local_address: str | None,
+ socket_options: Iterable[tuple] | None,
+ ) -> httpcore.NetworkStream:
+ family = (
+ socket.AF_INET6
+ if ipaddress.ip_address(address).version == 6
+ else socket.AF_INET
+ )
+
+ # Close the socket on any error, transferring ownership to SyncStream
+ # only after the connection and stream have been created successfully.
+ with ExitStack() as cleanup:
+ sock = socket.socket(family, socket.SOCK_STREAM)
+ cleanup.callback(sock.close)
+ sock.settimeout(self._deadline.timeout(timeout, httpcore.ConnectTimeout))
+
+ if local_address is not None:
+ sock.bind((local_address, 0))
+
+ for option in socket_options or ():
+ sock.setsockopt(*option)
+
+ sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
+
+ # This numeric address is the *same* address just validated;
+ # no getaddrinfo or DNS resolver runs between check and connect.
+ sock.connect((address, port))
+
+ stream = DeadlineStream(SyncStream(sock), self._deadline)
+ cleanup.pop_all()
+ return stream
+
def connect_tcp(
self,
host: str,
@@ -104,43 +146,18 @@ class PublicNetworkBackend(httpcore.SyncBackend):
last_error: httpcore.ConnectError | httpcore.ConnectTimeout | None = None
for address in addresses:
- family = (
- socket.AF_INET6
- if ipaddress.ip_address(address).version == 6
- else socket.AF_INET
- )
- sock = socket.socket(family, socket.SOCK_STREAM)
-
try:
- sock.settimeout(
- self._deadline.timeout(timeout, httpcore.ConnectTimeout)
+ return self._connect_one(
+ address, port, timeout, local_address, socket_options
)
-
- if local_address is not None:
- sock.bind((local_address, 0))
-
- for option in socket_options or ():
- sock.setsockopt(*option)
-
- sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
-
- # This numeric address is the *same* address just validated;
- # no getaddrinfo or DNS resolver runs between check and connect.
- sock.connect((address, port))
-
- return DeadlineStream(SyncStream(sock), self._deadline)
- except (TimeoutError, OSError) as exc:
- sock.close()
-
+ except OSError as exc:
if isinstance(exc, TimeoutError):
last_error = httpcore.ConnectTimeout(str(exc))
else:
last_error = httpcore.ConnectError(str(exc))
- except BaseException:
- sock.close()
- raise
- assert last_error is not None
+ if last_error is None:
+ raise httpcore.ConnectError(f"No public addresses available for {host!r}")
raise last_error
@@ -256,13 +273,13 @@ def read_limited_body(response: httpx.Response, max_bytes: int, *, label: str) -
pass
else:
if declared_length > max_bytes:
- raise ValueError(f"{label} document is too large")
+ raise ResponseTooLargeError(f"{label} document is too large")
body = bytearray()
for chunk in response.iter_bytes(chunk_size=64 * 1024):
body.extend(chunk)
if len(body) > max_bytes:
- raise ValueError(f"{label} document is too large")
+ raise ResponseTooLargeError(f"{label} document is too large")
return bytes(body)
diff --git a/webmentions_ssg/tasks/discovery.py b/webmentions_ssg/tasks/discovery.py
index 29c3e81..456e47e 100644
--- a/webmentions_ssg/tasks/discovery.py
+++ b/webmentions_ssg/tasks/discovery.py
@@ -11,7 +11,12 @@ import httpx
from bs4 import BeautifulSoup
from flask import current_app
-from ..net import is_http_url, is_temporary_status, read_limited_body
+from ..net import (
+ ResponseTooLargeError,
+ is_http_url,
+ is_temporary_status,
+ read_limited_body,
+)
LINK_SPLIT = re.compile(r",\s*(?=<)")
DISCOVERY_HEADERS = {"Accept": "text/html, application/xhtml+xml;q=0.9"}
@@ -130,7 +135,7 @@ def read_target_body(response: httpx.Response) -> bytes:
return read_limited_body(
response, current_app.config["MAX_TARGET_BYTES"], label="Target"
)
- except ValueError as exc:
+ except ResponseTooLargeError as exc:
raise PermanentSenderError(str(exc)) from exc
@@ -149,7 +154,7 @@ def discover_webmention_endpoint(client: httpx.Client, target: str) -> str | Non
"""
try:
head_response = client.head(target, headers=DISCOVERY_HEADERS)
- except httpx.RequestError, TemporarySenderError:
+ except httpx.RequestError:
# HEAD is only an optimization. Servers and intermediaries may reject
# or drop it even when fetching the document with GET would succeed.
pass
diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py
index 2963dc7..cbb42b6 100644
--- a/webmentions_ssg/tasks/receiver.py
+++ b/webmentions_ssg/tasks/receiver.py
@@ -16,6 +16,7 @@ from ..extensions import db, huey
from ..models import ReceivedStatus, ReceivedWebmention
from ..net import (
AddressResolutionError,
+ ResponseTooLargeError,
UnsafeAddressError,
build_client,
is_temporary_status,
@@ -76,7 +77,7 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]:
return response, read_limited_body(
response, current_app.config["MAX_SOURCE_BYTES"], label="Source"
)
- except ValueError as exc:
+ except ResponseTooLargeError as exc:
raise VerificationError(str(exc)) from exc
except UnsafeAddressError as exc:
diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py
index db99476..cd6c64a 100644
--- a/webmentions_ssg/tasks/sender.py
+++ b/webmentions_ssg/tasks/sender.py
@@ -210,10 +210,8 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
httpx.InvalidURL,
httpx.UnsupportedProtocol,
httpx.LocalProtocolError,
- ValueError,
- UnicodeError,
) as exc:
- # Invalid URLs and local protocol errors cannot improve with retries.
+ # Known permanent failures cannot improve with retries.
outcome = SentWebmentionStatus.FAILED
reason = str(exc) or type(exc).__name__
except (TemporarySenderError, AddressResolutionError, httpx.RequestError) as exc:
@@ -221,6 +219,14 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
reason = str(exc) or "Webmention request failed"
terminal = attempt >= max_attempts
temporary_failure = True
+ except Exception as exc:
+ # Unexpected errors must not disappear into a generic permanent-failure
+ # handler without a traceback (including programming ValueErrors).
+ current_app.logger.exception(
+ "Unexpected error sending SentWebmention %s", webmention_uuid
+ )
+ outcome = SentWebmentionStatus.FAILED
+ reason = str(exc) or type(exc).__name__
# A scanner may have reopened or superseded the revision during I/O.
# Refresh only once, and persist one outcome atomically with its metadata.