aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/public_transport.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 19:57:08 +0100
committerDennis Fink2026-11-12 19:57:08 +0100
commit4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch)
treef7ed41a374a8a4136792039a262dc62aa8cf8c59 /webmentions_ssg/public_transport.py
parent7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff)
downloadwebmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz
webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage.
Diffstat (limited to 'webmentions_ssg/public_transport.py')
-rw-r--r--webmentions_ssg/public_transport.py144
1 files changed, 0 insertions, 144 deletions
diff --git a/webmentions_ssg/public_transport.py b/webmentions_ssg/public_transport.py
deleted file mode 100644
index 131cf55..0000000
--- a/webmentions_ssg/public_transport.py
+++ /dev/null
@@ -1,144 +0,0 @@
-# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
-#
-# SPDX-License-Identifier: BSD-3-Clause
-
-"""HTTP transport that connects only to validated DNS answers.
-
-httpcore receives the original host for Host and TLS SNI/certificate checks,
-but the socket is opened to an IP from a single, bounded DNS lookup. This
-protects the receiver and sender, including redirects and advertised URLs.
-"""
-
-import ipaddress
-import socket
-import time
-from collections.abc import Iterable
-
-import httpcore
-import httpx
-from httpcore._backends.sync import SyncStream # httpcore 1.x socket implementation
-
-from .url_security import public_addresses
-
-
-class NetworkDeadline:
- """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
-
- def timeout(self, original: float | None, error: type[Exception]) -> float:
- remaining = self.expires - time.monotonic()
- if remaining <= 0:
- raise error("Overall HTTP request deadline exceeded")
- return min(remaining, original) if original is not None else remaining
-
-
-class DeadlineStream(httpcore.NetworkStream):
- """Clip every read/write/TLS operation to the remaining overall deadline."""
-
- def __init__(self, stream: httpcore.NetworkStream, deadline: NetworkDeadline):
- 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)
- )
-
- def write(self, buffer: bytes, timeout: float | None = None) -> None:
- self._stream.write(
- buffer, timeout=self._deadline.timeout(timeout, httpcore.WriteTimeout)
- )
-
- def start_tls(
- self,
- ssl_context,
- server_hostname: str | None = None,
- timeout: float | None = None,
- ) -> httpcore.NetworkStream:
- return DeadlineStream(
- self._stream.start_tls(
- ssl_context,
- server_hostname=server_hostname,
- timeout=self._deadline.timeout(timeout, httpcore.ConnectTimeout),
- ),
- self._deadline,
- )
-
- def get_extra_info(self, info: str):
- return self._stream.get_extra_info(info)
-
- def close(self) -> None:
- self._stream.close()
-
-
-class PublicNetworkBackend(httpcore.SyncBackend):
- """Resolve and validate in the TCP connection operation, never in a hook."""
-
- def __init__(self, deadline: NetworkDeadline):
- self._deadline = deadline
-
- def connect_tcp(
- self,
- host: str,
- port: int,
- timeout: float | None = None,
- local_address: str | None = None,
- socket_options: Iterable[tuple] | None = None,
- ) -> 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)
- 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)
- )
- 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()
- 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
- raise last_error
-
-
-class PublicHTTPTransport(httpx.HTTPTransport):
- """A direct (non-proxy) httpx transport with an IP-pinning network backend.
-
- httpx 0.28 does not expose httpcore's ``network_backend`` parameter, so we
- initialize its compatible pool directly. Pin the httpx/httpcore major APIs
- and keep this integration under test when upgrading either dependency.
- """
-
- 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)),
- )