aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/net.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-15 20:42:20 +0100
committerDennis Fink2026-11-15 20:42:20 +0100
commit509d93b2976e353d29441a7a7b7e78ca466dc4fd (patch)
tree753e637aefabd4293f808addbf9c310eaf0aa552 /webmentions_ssg/net.py
parentc74feda222745ab7cca8993df83d9ea5f9d07e5b (diff)
downloadwebmentions-ssg-509d93b2976e353d29441a7a7b7e78ca466dc4fd.tar.gz
webmentions-ssg-509d93b2976e353d29441a7a7b7e78ca466dc4fd.zip
refactor(core)!: standardize application terminology
Rename configuration keys, task helpers, forms, network utilities, and view endpoints to make their responsibilities and domains more explicit. Align source-management routes and templates with the new terminology and rename the target-matching module and related task abstractions consistently. BREAKING CHANGE: configuration keys have been renamed and sent-source routes move from `/sent` to `/sources`, with corresponding blueprint endpoint changes.
Diffstat (limited to 'webmentions_ssg/net.py')
-rw-r--r--webmentions_ssg/net.py62
1 files changed, 37 insertions, 25 deletions
diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py
index 262434a..b827180 100644
--- a/webmentions_ssg/net.py
+++ b/webmentions_ssg/net.py
@@ -21,7 +21,7 @@ from httpcore._backends.sync import SyncStream
NAT64_PREFIX = ipaddress.IPv6Network("64:ff9b::/96")
IPV4_COMPATIBLE_PREFIX = ipaddress.IPv6Network("::/96")
-NETWORK_CONNECT_TIMOUT_ERROR = 599
+NETWORK_CONNECT_TIMEOUT_ERROR = 599
class AddressResolutionError(Exception):
@@ -36,16 +36,18 @@ class ResponseTooLargeError(ValueError):
"""A response body exceeds its configured size limit."""
-class NetworkDeadline:
+class RequestDeadline:
"""One monotonic deadline shared by DNS, TCP, TLS, and response streaming."""
def __init__(self, seconds: float) -> None:
if seconds <= 0:
- raise ValueError("REQUEST_TOTAL_TIMEOUT must be greater than zero")
- self.expires = time.monotonic() + seconds
+ raise ValueError("REQUEST_TOTAL_TIMEOUT_SECONDS must be greater than zero")
+ self.expires_at = time.monotonic() + seconds
- def timeout(self, original: float | None, error: type[Exception]) -> float:
- remaining = self.expires - time.monotonic()
+ def remaining_timeout(
+ self, original: float | None, error: type[Exception]
+ ) -> float:
+ remaining = self.expires_at - time.monotonic()
if remaining <= 0:
raise error("Overall HTTP request deadline exceeded")
return min(remaining, original) if original is not None else remaining
@@ -54,18 +56,20 @@ class NetworkDeadline:
class DeadlineStream(httpcore.NetworkStream):
"""Clip every read/write/TLS operation to the remaining overall deadline."""
- def __init__(self, stream: httpcore.NetworkStream, deadline: NetworkDeadline):
+ def __init__(self, stream: httpcore.NetworkStream, deadline: RequestDeadline):
self._stream = stream
self._deadline = deadline
def read(self, max_bytes: int, timeout: float | None = None) -> bytes:
return self._stream.read(
- max_bytes, timeout=self._deadline.timeout(timeout, httpcore.ReadTimeout)
+ max_bytes,
+ timeout=self._deadline.remaining_timeout(timeout, httpcore.ReadTimeout),
)
def write(self, buffer: bytes, timeout: float | None = None) -> None:
self._stream.write(
- buffer, timeout=self._deadline.timeout(timeout, httpcore.WriteTimeout)
+ buffer,
+ timeout=self._deadline.remaining_timeout(timeout, httpcore.WriteTimeout),
)
def start_tls(
@@ -78,7 +82,9 @@ class DeadlineStream(httpcore.NetworkStream):
self._stream.start_tls(
ssl_context,
server_hostname=server_hostname,
- timeout=self._deadline.timeout(timeout, httpcore.ConnectTimeout),
+ timeout=self._deadline.remaining_timeout(
+ timeout, httpcore.ConnectTimeout
+ ),
),
self._deadline,
)
@@ -93,10 +99,10 @@ class DeadlineStream(httpcore.NetworkStream):
class PublicNetworkBackend(httpcore.SyncBackend):
"""Resolve and validate in the TCP connection operation, never in a hook."""
- def __init__(self, deadline: NetworkDeadline):
+ def __init__(self, deadline: RequestDeadline):
self._deadline = deadline
- def _connect_one(
+ def _connect_to_address(
self,
address: str,
port: int,
@@ -115,7 +121,9 @@ class PublicNetworkBackend(httpcore.SyncBackend):
with ExitStack() as cleanup:
sock = socket.socket(family, socket.SOCK_STREAM)
cleanup.callback(sock.close)
- sock.settimeout(self._deadline.timeout(timeout, httpcore.ConnectTimeout))
+ sock.settimeout(
+ self._deadline.remaining_timeout(timeout, httpcore.ConnectTimeout)
+ )
if local_address is not None:
sock.bind((local_address, 0))
@@ -143,13 +151,13 @@ class PublicNetworkBackend(httpcore.SyncBackend):
) -> httpcore.NetworkStream:
# No second lookup of `host` is permitted. Even a DNS response that
# changes immediately after this check cannot change the socket peer.
- dns_timeout = self._deadline.timeout(timeout, httpcore.ConnectTimeout)
- addresses = public_addresses(host, lifetime=dns_timeout)
+ dns_timeout = self._deadline.remaining_timeout(timeout, httpcore.ConnectTimeout)
+ addresses = resolve_public_addresses(host, lifetime=dns_timeout)
last_error: httpcore.ConnectError | httpcore.ConnectTimeout | None = None
for address in addresses:
try:
- return self._connect_one(
+ return self._connect_to_address(
address, port, timeout, local_address, socket_options
)
except OSError as exc:
@@ -174,11 +182,13 @@ class PublicHTTPTransport(httpx.HTTPTransport):
def __init__(self, total_timeout: float):
self._pool = httpcore.ConnectionPool(
ssl_context=httpx.create_ssl_context(verify=True, trust_env=False),
- network_backend=PublicNetworkBackend(NetworkDeadline(total_timeout)),
+ network_backend=PublicNetworkBackend(RequestDeadline(total_timeout)),
)
-def build_client(user_agent: str, *, accept: str | None = None) -> httpx.Client:
+def build_public_http_client(
+ user_agent: str, *, accept: str | None = None
+) -> httpx.Client:
"""Return the hardened HTTP client shared by receiver and sender.
:param user_agent: User-Agent header sent with each request.
@@ -192,11 +202,13 @@ def build_client(user_agent: str, *, accept: str | None = None) -> httpx.Client:
return httpx.Client(
headers=headers,
- timeout=httpx.Timeout(current_app.config["REQUEST_TIMEOUT"]),
+ timeout=httpx.Timeout(current_app.config["REQUEST_TIMEOUT_SECONDS"]),
follow_redirects=True,
max_redirects=current_app.config["MAX_REDIRECTS"],
trust_env=False,
- transport=PublicHTTPTransport(current_app.config["REQUEST_TOTAL_TIMEOUT"]),
+ transport=PublicHTTPTransport(
+ current_app.config["REQUEST_TOTAL_TIMEOUT_SECONDS"]
+ ),
)
@@ -237,7 +249,7 @@ def resolve_hostname(
) from exc
-def public_addresses(
+def resolve_public_addresses(
hostname: str, *, lifetime: float | None = None
) -> tuple[str, ...]:
"""Resolve *once* and return only checked, globally routable IP addresses.
@@ -267,7 +279,7 @@ def public_addresses(
return tuple(str(address) for address in addresses)
-def hostname_of(url: str) -> str | None:
+def normalized_hostname(url: str) -> str | None:
"""Return a normalized URL hostname, or None for malformed URLs.
:param url: URL from which to extract a hostname.
@@ -280,7 +292,7 @@ def hostname_of(url: str) -> str | None:
return hostname.rstrip(".") if hostname is not None else None
-def content_type_of(headers: Mapping[str, str]) -> str:
+def media_type_of(headers: Mapping[str, str]) -> str:
"""Return a normalized media type without Content-Type parameters.
:param headers: HTTP response headers to inspect.
@@ -330,7 +342,7 @@ def read_limited_body(response: httpx.Response, max_bytes: int, *, label: str) -
return bytes(body)
-def is_temporary_status(status: int) -> bool:
+def is_retryable_status(status: int) -> bool:
"""Return whether an HTTP status is normally retryable.
:param status: HTTP response status code to classify.
@@ -343,5 +355,5 @@ def is_temporary_status(status: int) -> bool:
HTTPStatus.TOO_EARLY,
HTTPStatus.TOO_MANY_REQUESTS,
}
- or HTTPStatus.INTERNAL_SERVER_ERROR <= status <= NETWORK_CONNECT_TIMOUT_ERROR
+ or HTTPStatus.INTERNAL_SERVER_ERROR <= status <= NETWORK_CONNECT_TIMEOUT_ERROR
)