Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

38.5 KB
Raw Blame
"""
HTML Content Downloader for web pages.

Downloads and extracts clean text content from HTML web pages.
Extraction is handled by the shared pipeline in extraction/pipeline.py.
"""

from typing import Any, Callable, Dict, Optional
from urllib.parse import urlparse
from loguru import logger
from bs4 import BeautifulSoup
from requests.exceptions import ConnectionError as RequestsConnectionError
from requests.exceptions import Timeout as RequestsTimeout
from requests.exceptions import HTTPError as RequestsHTTPError

from .base import (
    _DOCUMENT_ABSENT_STATUSES,
    BaseDownloader,
    ContentType,
    DownloadResult,
    rate_limit_authority,
)
from .extraction.pipeline import extract_content_with_metadata
from ...constants import BROWSER_USER_AGENT
from ...security.client_safe_errors import (
    CLIENT_SAFE_DOWNLOAD_MESSAGES,
    client_safe_download_message,
)
from ...security.ssrf_validator import redact_url_for_log
from ...security.egress.policy import PolicyDeniedError
from ...utilities.pdf_extraction_limits import MAX_PDF_EXTRACTION_PAGES
from ...utilities.resource_utils import safe_close


# Single source of truth for caller-safe tokens lives in
# ``security.client_safe_errors`` (PR #6564 review: this local copy drifted
# from the services table — missing DataError/InterfaceError/
# InvalidRequestError and the OSError errno branch). Aliases below preserve
# the historic names; the mapping itself is shared so it cannot drift again.
# Importing from ``security`` (not ``research_library.services``) avoids a
# services <-> downloaders cycle. sanitize_error_for_client() is NOT a
# substitute — it scrubs credential shapes only, not SQL/paths/URLs.
_CLIENT_SAFE_HTML_MESSAGES = CLIENT_SAFE_DOWNLOAD_MESSAGES


def _client_safe_html_message(exc: BaseException) -> str:
    """Back-compat alias for :func:`client_safe_download_message`."""
    return client_safe_download_message(exc)


def _as_markdown_text(value: str) -> str:
    """Collapse page-supplied metadata to a single line.

    ``_format_extracted_content`` wraps the title in ``# `` and the
    description in ``*``. Both are third-party strings -- for arXiv they now
    reach stored text at character 0, ahead of the paper itself -- so a
    newline in either would end the construct and hand the rest of the value
    the document's own syntax. Collapsing whitespace keeps the value inside
    the construct it was meant to fill.

    Metacharacters are deliberately *not* escaped. What this produces is
    stored as ``Document.text_content``, which is not markdown to every
    consumer: the document viewer renders it as plain text in a
    ``white-space: pre-wrap`` element (``document_text.html``), unified
    search matches it by substring, and the RAG and notes services feed it
    to embeddings and prompts verbatim. Backslashes written in here would
    show up literally in all four and would break substring matches on the
    characters escaped. Escaping belongs at the markdown sinks, and they
    already do it -- the one that renders this text as markdown parses it
    inline and passes the result through DOMPurify.
    """
    return " ".join(value.split())


def _fetch_failure_reason(status_code: int) -> str:
    """Factual HTTP-class token for ``html.fetch_failed`` log lines.

    Names the status bucket only (absent / rate-limited / forbidden /
    client / server error) — never the cause: a 403 may be a bot-wall
    or a genuinely private document, and the log cannot tell.
    """
    if status_code in _DOCUMENT_ABSENT_STATUSES:
        return "absent"
    if status_code in (429, 503):
        return "rate_limited"
    if status_code in (401, 403):
        return "forbidden"
    if status_code == 400:
        return "bad_request"
    if 500 <= status_code <= 599:
        return "server_error"
    if 400 <= status_code <= 499:
        return "client_error"
    return "unexpected_status"


class _TerminalTransportError(Exception):
    """Internal: terminal transport failure with attempt context.

    Raised by ``_fetch_response_with_retry`` when the last attempt fails
    with a transport error, so the failure handling can record the real
    attempt count instead of a hardcoded ``retry_count=1``. Never
    escapes ``HTMLDownloader``.
    """

    def __init__(self, exc: BaseException, wait_time: float, attempt: int):
        super().__init__(exc)
        self.exc = exc
        self.wait_time = wait_time
        self.attempt = attempt


class HTMLDownloader(BaseDownloader):
    """Downloader for HTML web pages - extracts clean text content."""

    # Bound on the PDF recovery path (see _extract_stashed_pdf_text). The
    # stash is reachable from every HTML-classified URL, so the fetched
    # bytes stay capped small (MAX_RECOVERED_PDF_BYTES); the page budget
    # aliases the central MAX_PDF_EXTRACTION_PAGES ceiling so research
    # recovery and dedicated PDF paths truncate identically (one source
    # of truth in utilities.pdf_extraction_limits).
    MAX_RECOVERED_PDF_BYTES = 10 * 1024 * 1024
    MAX_RECOVERED_PDF_PAGES = MAX_PDF_EXTRACTION_PAGES

    def __init__(
        self,
        timeout: int = 30,
        language: str = "English",
        username: Optional[str] = None,
        password: Optional[str] = None,
        settings_snapshot: Optional[Dict[str, Any]] = None,
        **kwargs,
    ):
        super().__init__(
            timeout=timeout,
            username=username,
            password=password,
            settings_snapshot=settings_snapshot,
            **kwargs,
        )
        self.session.headers.update({"User-Agent": BROWSER_USER_AGENT})
        self.language = language

    def can_handle(self, url: str) -> bool:
        """
        Check if this downloader can handle the given URL.

        Returns True for any HTTP/HTTPS URL (fallback downloader for web content).
        """
        try:
            parsed = urlparse(url)
            return parsed.scheme in ("http", "https")
        except Exception:
            return False

    def download(
        self, url: str, content_type: ContentType = ContentType.TEXT
    ) -> Optional[bytes]:
        """
        Download and extract text content from HTML page.

        Args:
            url: The URL to download
            content_type: Type of content (TEXT for HTML extraction)

        Returns:
            Extracted text as UTF-8 bytes, or None if failed
        """
        self._last_pdf_content = None
        self._last_final_url = None
        if content_type == ContentType.PDF:
            logger.warning(
                "html.download_pdf_unsupported url={url}",
                url=redact_url_for_log(url),
            )
            return None

        try:
            html_content = self._fetch_html(url)
            if not html_content:
                kind, payload, final_url = self._consume_recovery(url)
                if kind == "pdf" and isinstance(payload, bytes):
                    if len(payload) > self.MAX_RECOVERED_PDF_BYTES:
                        logger.info(
                            "html.pdf_recovery_skipped_oversize url={url}",
                            url=redact_url_for_log(url),
                        )
                        return None
                    pdf_text = self._extract_stashed_pdf_text(
                        payload, final_url or url, url
                    )
                    if pdf_text:
                        try:
                            self._last_final_url = (
                                final_url
                                if final_url and final_url != url
                                else None
                            )
                        except Exception:  # allow: silent-exception
                            pass
                        try:
                            self._last_pdf_content = payload
                        except Exception:  # allow: silent-exception
                            pass
                        return pdf_text.encode("utf-8")
                elif kind == "text" and isinstance(payload, str):
                    if payload.strip():
                        return self._format_recovered_text(
                            payload, final_url or url
                        ).encode("utf-8")
                return None

            extracted = self._extract_content(html_content, url)
            if extracted:
                text = self._format_extracted_content(extracted)
                return text.encode("utf-8")

            return None

        except PolicyDeniedError:
            raise
        except Exception as e:
            # Log the exception class only (zero-risk: the token already
            # exposes it to the client). Never log str(e) or the traceback:
            # either can re-embed the credentialed URL / SQL text / paths.
            logger.opt(exception=False).error(
                "html.download_failed url={url} exc_class={exc_class}",
                url=redact_url_for_log(url),
                exc_class=type(e).__name__,
            )
            return None

    def download_with_result(
        self,
        url: str,
        content_type: ContentType = ContentType.TEXT,
        *,
        on_transport_error: Optional[Callable[[BaseException], None]] = None,
    ) -> DownloadResult:
        """Download content, optionally reporting the typed terminal failure.

        The callback receives only transport failures, before their details
        are reduced to a client-safe skip reason. Extraction failures and
        missing documents (404/410) remain per-document results.
        """
        self._last_pdf_content = None
        self._last_final_url = None
        if content_type == ContentType.PDF:
            return DownloadResult(
                skip_reason="HTML downloader does not support PDF downloads"
            )

        try:
            if on_transport_error is None:
                html_content = self._fetch_html(url)
            else:
                html_content, _ = self._fetch_html_with_final_url(
                    url, on_transport_error=on_transport_error
                )
            if not html_content:
                kind, payload, final_url = self._consume_recovery(url)
                if kind == "pdf" and isinstance(payload, bytes):
                    if len(payload) > self.MAX_RECOVERED_PDF_BYTES:
                        return DownloadResult(
                            skip_reason=(
                                f"PDF served at HTML URL exceeds "
                                f"{self.MAX_RECOVERED_PDF_BYTES // (1024 * 1024)} MB "
                                f"recovery limit, skipping text extraction"
                            )
                        )
                    pdf_text = self._extract_stashed_pdf_text(
                        payload, final_url or url, url
                    )
                    if pdf_text:
                        try:
                            self._last_final_url = (
                                final_url
                                if final_url and final_url != url
                                else None
                            )
                        except Exception:  # allow: silent-exception
                            pass
                        try:
                            self._last_pdf_content = payload
                        except Exception:  # allow: silent-exception
                            pass
                        return DownloadResult(
                            content=pdf_text.encode("utf-8"),
                            is_success=True,
                            pdf_content=payload,
                        )
                    return DownloadResult(
                        skip_reason=(
                            "PDF served at HTML URL had no extractable text"
                        )
                    )
                if kind == "text" and isinstance(payload, str):
                    if payload.strip():
                        return DownloadResult(
                            content=self._format_recovered_text(
                                payload, final_url or url
                            ).encode("utf-8"),
                            is_success=True,
                        )
                    return DownloadResult(
                        skip_reason="Text served at HTML URL was empty"
                    )
                return DownloadResult(
                    skip_reason="Failed to fetch HTML content from URL"
                )

            extracted = self._extract_content(html_content, url)
            if not extracted:
                return DownloadResult(
                    skip_reason="Could not extract meaningful content from page"
                )

            text = self._format_extracted_content(extracted)
            if not text.strip():
                return DownloadResult(skip_reason="Extracted content is empty")

            return DownloadResult(
                content=text.encode("utf-8"),
                is_success=True,
            )

        except PolicyDeniedError:
            raise
        except Exception as e:
            logger.opt(exception=False).error(
                "html.download_result_failed url={url} exc_class={exc_class}",
                url=redact_url_for_log(url),
                exc_class=type(e).__name__,
            )
            # skip_reason propagates to the browser via the download SSE
            # stream; the exception text can carry SQL text, target URLs, or
            # file paths — return a fixed token only (never str(e); only the
            # class name is logged above, as a traceback could re-embed the
            # credentialed URL).
            return DownloadResult(skip_reason=_client_safe_html_message(e))

    def _fetch_html(
        self,
        url: str,
        *,
        on_rate_limited: Optional[Callable[[], None]] = None,
    ) -> Optional[str]:
        """Fetch raw HTML content from URL."""
        return self._fetch_html_with_final_url(
            url, on_rate_limited=on_rate_limited
        )[0]

    def _fetch_html_with_final_url(
        self,
        url: str,
        *,
        on_transport_failure: Optional[Callable[[], None]] = None,
        on_transport_error: Optional[Callable[[BaseException], None]] = None,
        on_rate_limited: Optional[Callable[[], None]] = None,
        max_attempts: int = 3,
    ) -> tuple[Optional[str], Optional[str]]:
        """Fetch raw HTML together with the URL that finally served it.

        Redirects are followed, so the final URL can differ from ``url``.
        Callers that require the response to stay on a specific path must
        compare the returned URL themselves.

        A response that is not HTML is still a failure here (``(None, None)``)
        so strict callers (arXiv HTML rendition check, metadata) keep their
        contract. Recoverable responses -- a PDF, or plain text / a feed --
        have their payload stashed for ``download()``/
        ``download_with_result()`` (see ``_consume_recovery``): the URL
        classifier only sees the URL string, so publisher/DOI links without
        a ``.pdf`` path routinely land here serving ``application/pdf``,
        and plain-text / feed URLs (READMEs, ``.txt`` preprints, RSS/Atom)
        likewise arrive typed as something other than HTML. Discarding
        those bytes turns a recoverable source into a warning plus a lost
        citation.

        Transient failures -- HTTP 429/503 and ``requests`` timeouts /
        connection errors -- are retried up to ``max_attempts`` with an
        adaptive wait before every attempt (mirroring
        ``BaseDownloader._download_pdf``); every other status or error
        fails fast exactly as before. ``on_transport_failure`` fires at
        most once, only on the terminal failure, preserving its
        circuit-breaker contract (see below).

        ``on_transport_failure`` is called once when the fetch fails
        without the host answering: an exception (timeout, connection
        error, a body that aborts mid-read, the decoded-body cap) or a
        non-200 status other than 404 and 410, which answer "no such
        page". A 200 that is not HTML is an answer too and does not call
        it, but only once its body has been read in full: a 200 whose
        body aborts mid-read never got to answer, and is reported as the
        transport failure it is. When given, the failure is logged
        as a warning rather than an error, since the caller handles it.

        ``on_rate_limited`` is called once when the fetch gives up on
        HTTP 429/503 after exhausting ``max_attempts`` (mirroring
        ``BaseDownloader._download_pdf``): a chain owner uses it to stop
        sending further requests to that host. Only the caller can stop
        the later legs of the fallback chain: the probe GET applies no
        rate limiting, and the browser leg backs off only on its own
        tracker buckets, which never see the 429/503 outcomes this fetch
        records.
        """
        # Fresh per-fetch recovery state. Cleared on entry so a stale stash
        # from an earlier fetch can never leak into this one; consumption
        # additionally requires the stashed request URL to match (see
        # _consume_recovery). The stash fields are unsynchronized instance
        # state: downloader instances are used sequentially (e.g. per-request
        # construction in fetch_and_extract, or sequential consumption of
        # one shared instance in batch_fetch_and_extract) — cross-thread
        # sharing of one instance is unsupported, and the URL-match guard
        # does not make it safe (see _consume_recovery).
        self._recovery_kind: Optional[str] = None
        self._recovery_payload: Optional[bytes | str] = None
        self._recovery_url: Optional[str] = None
        self._recovery_request_url: Optional[str] = url
        logger.debug(
            "html.fetch_started url={url}",
            url=redact_url_for_log(url),
        )
        engine_type = f"html_download_{rate_limit_authority(url)}"
        max_attempts = max(1, int(max_attempts))

        def _terminal_failure(
            exc: BaseException, failed_wait_time: float, failed_attempt: int
        ) -> tuple[None, None]:
            # A fetch that raised must leave no recovery state behind, even
            # if it raised after the stash was populated: the payload belongs
            # to a response the caller never received in full.
            self._recovery_kind = None
            self._recovery_payload = None
            self._recovery_url = None
            if on_transport_failure is not None:
                # The caller handles the failure, so it is not an error
                # here (ERROR records reach the user's browser).
                logger.warning(
                    "html.fetch_error url={url} exc_class={exc_class}",
                    url=redact_url_for_log(url),
                    exc_class=type(exc).__name__,
                )
            else:
                logger.opt(exception=False).error(
                    "html.fetch_error url={url} exc_class={exc_class}",
                    url=redact_url_for_log(url),
                    exc_class=type(exc).__name__,
                )
            self.rate_tracker.record_outcome(
                engine_type=engine_type,
                wait_time=failed_wait_time,
                success=False,
                retry_count=failed_attempt,
                error_type=type(exc).__name__,
            )
            if on_transport_failure is not None:
                on_transport_failure()
            if on_transport_error is not None:
                on_transport_error(exc)
            return None, None

        wait_time = 0.0
        attempt = 1
        try:
            response, wait_time, attempt = self._fetch_response_with_retry(
                url, engine_type, max_attempts
            )

            try:
                if response.status_code == 200:
                    content_type = response.headers.get(
                        "content-type", ""
                    ).lower()
                    if (
                        "text/html" in content_type
                        or "application/xhtml" in content_type
                    ):
                        self._ensure_decoded_body_cap(response)
                        # Read (capped) before recording the outcome, so a
                        # body that aborts mid-read is not also counted as
                        # a success.
                        text = response.text
                        self.rate_tracker.record_outcome(
                            engine_type=engine_type,
                            wait_time=wait_time,
                            success=True,
                            retry_count=attempt,
                            search_result_count=1,
                        )
                        logger.debug(
                            "html.fetch_succeeded url={url}",
                            url=redact_url_for_log(url),
                        )
                        return text, response.url
                    # Non-HTML 200 is an answer, not a transport failure: do
                    # not call on_transport_failure. Bound the body before any
                    # recovery read, including the magic-byte check below.
                    # _ensure_decoded_body_cap adds the decoded-byte guard for
                    # a small-Content-Length gzip body on top of the one
                    # SafeSession.send installs; the read that follows is
                    # then capped at MAX_RESPONSE_SIZE.
                    self._ensure_decoded_body_cap(response)
                    # Read the bounded body HERE, inside this method's outer
                    # try, instead of letting _is_pdf_content() do it. That
                    # helper swallows every exception raised while reading
                    # response.content and answers "not a PDF", so a body
                    # that aborts mid-read (connection reset, truncated
                    # chunked body) falls through to the unsupported-content
                    # return below: an answer the host never gave, recorded
                    # as neither on_transport_failure nor a failed outcome.
                    # The arXiv caller then reads the attempt as NOT_FETCHED
                    # (a refundable fetch) instead of FETCH_FAILED and keeps
                    # sending requests to a failing host. Reading here routes
                    # the failure to the outer except, which records it
                    # exactly once, and the same bytes are reused for the
                    # sniff and the stash below, so this costs no second
                    # read. Mirrors the PDF path (BaseDownloader.
                    # _download_pdf), which reads its body the same way.
                    body = response.content
                    if self._is_pdf_content(response, content=body):
                        # HTML-classified URL serving a PDF: not a
                        # warning-worthy failure. Stash the already-downloaded
                        # (bounded) bytes so download()/download_with_result()
                        # can extract text with no second request (one-GET
                        # recovery contract).
                        logger.info(
                            "html.fetch_pdf_recovery url={url}",
                            url=redact_url_for_log(url),
                        )
                        self._recovery_kind = "pdf"
                        # The bytes read above, reused: no second request and
                        # no second read of the body.
                        self._recovery_payload = body
                        self._recovery_url = (
                            getattr(response, "url", None) or url
                        )
                        return None, None
                    if self._is_recoverable_text_content(content_type):
                        # Plain text / feed served where HTML was expected
                        # (READMEs, .txt preprints, RSS/Atom): usable as source
                        # text directly, no extraction step needed.
                        logger.info(
                            "html.fetch_text_recovery url={url}",
                            url=redact_url_for_log(url),
                        )
                        self._recovery_kind = "text"
                        self._recovery_payload = getattr(response, "text", "")
                        self._recovery_url = (
                            getattr(response, "url", None) or url
                        )
                        return None, None
                    logger.warning(
                        "html.fetch_unexpected_content_type url={url} status={status}",
                        url=redact_url_for_log(url),
                        status=response.status_code,
                    )
                    return None, None
                status = response.status_code
                # NOTE: intermediate 429/503s never reach here --
                # _fetch_response_with_retry handles (and closes) those
                # before returning. A 429/503 seen here exhausted
                # max_attempts and fails terminally below.
                logger.warning(
                    "html.fetch_failed url={url} status={status} "
                    "reason={reason} attempts={attempts}",
                    url=redact_url_for_log(url),
                    status=status,
                    reason=_fetch_failure_reason(status),
                    attempts=attempt,
                )
                self.rate_tracker.record_outcome(
                    engine_type=engine_type,
                    wait_time=wait_time,
                    success=False,
                    retry_count=attempt,
                    error_type=f"HTTP_{status}",
                )
                if (
                    on_transport_failure is not None
                    and status not in _DOCUMENT_ABSENT_STATUSES
                ):
                    on_transport_failure()
                if (
                    on_transport_error is not None
                    and status not in _DOCUMENT_ABSENT_STATUSES
                ):
                    on_transport_error(RequestsHTTPError(response=response))
                if status in (429, 503) and on_rate_limited is not None:
                    # Fires exactly once: this branch is terminal (the
                    # retried 429/503s never reach it — see the NOTE above).
                    # Mirrors ``BaseDownloader._download_pdf``: the caller
                    # stops sending further requests to this host.
                    on_rate_limited()
                return None, None
            finally:
                # stream=True leaves the connection unreleased until the
                # body is consumed or the response is closed.
                safe_close(response, "HTML download response")

        except PolicyDeniedError:
            self._recovery_kind = None
            self._recovery_payload = None
            self._recovery_url = None
            raise
        except _TerminalTransportError as terminal:
            return _terminal_failure(
                terminal.exc, terminal.wait_time, terminal.attempt
            )
        except Exception as e:
            return _terminal_failure(e, wait_time, attempt)

    def _fetch_response_with_retry(
        self, url: str, engine_type: str, max_attempts: int
    ) -> tuple[Any, float, int]:
        """GET with retry on transient failures; terminal errors raise.

        Retries HTTP 429/503 and ``requests`` timeouts / connection
        errors (mirroring ``BaseDownloader._download_pdf``), with an
        adaptive wait before every attempt. Returns
        ``(response, wait_time, attempt)``. A terminal transport failure
        raises ``_TerminalTransportError`` carrying the attempt context;
        any other exception from the GET propagates unchanged. A
        retried 429/503 response is closed before continuing so pooled
        connections are released.
        """
        for attempt in range(1, max_attempts + 1):
            # Adaptive wait before every attempt (mirrors
            # BaseDownloader._download_pdf): the tracker learns per host,
            # and retrying into a rate-limited host without waiting would
            # trip the next attempt immediately.
            wait_time = self.rate_tracker.apply_rate_limit(engine_type)
            try:
                # stream=True keeps requests.Session.send from consuming the
                # body before _ensure_decoded_body_cap can wrap the reads, so
                # that guard also bounds the decoded read of response.text at
                # MAX_RESPONSE_SIZE for a valid under-cap Content-Length (a
                # second guard: SafeSession.send installs one on every response
                # it accepts). This bounds the final response only: with
                # allow_redirects=True, SafeSession discards each intermediate
                # redirect body unread.
                response = self.session.get(
                    url,
                    timeout=self.timeout,
                    allow_redirects=True,
                    stream=True,
                )
            except PolicyDeniedError:
                raise
            except (RequestsTimeout, RequestsConnectionError) as e:
                if attempt >= max_attempts:
                    raise _TerminalTransportError(e, wait_time, attempt) from e
                logger.warning(
                    "html.fetch_attempt url={url} exc_class={exc_class} "
                    "attempt={attempt}/{max_attempts}",
                    url=redact_url_for_log(url),
                    exc_class=type(e).__name__,
                    attempt=attempt,
                    max_attempts=max_attempts,
                )
                self.rate_tracker.record_outcome(
                    engine_type=engine_type,
                    wait_time=wait_time,
                    success=False,
                    retry_count=attempt,
                    error_type=type(e).__name__,
                )
                continue
            except Exception as e:
                raise _TerminalTransportError(e, wait_time, attempt) from e
            status = response.status_code
            if status in (429, 503) and attempt < max_attempts:
                logger.warning(
                    "html.fetch_attempt url={url} status={status} "
                    "attempt={attempt}/{max_attempts}",
                    url=redact_url_for_log(url),
                    status=status,
                    attempt=attempt,
                    max_attempts=max_attempts,
                )
                self.rate_tracker.record_outcome(
                    engine_type=engine_type,
                    wait_time=wait_time,
                    success=False,
                    retry_count=attempt,
                    error_type=f"HTTP_{status}",
                )
                safe_close(response, "HTML download response")
                continue
            return response, wait_time, attempt
        raise AssertionError(  # pragma: no cover -- defensive
            "_fetch_response_with_retry: loop always returns or raises"
        )

    @staticmethod
    def _is_recoverable_text_content(content_type: str) -> bool:
        """Report whether a non-HTML content type is usable as text directly.

        ``text/*`` plus feed/syndication XML (RSS/Atom, ``application/xml``,
        and other ``+xml`` types except SVG, which is markup, not prose).
        Deliberately excludes ``application/json``: raw API dumps are large
        and low-signal as research sources, so they keep failing loudly
        instead of silently entering the corpus.
        """
        # Headers routinely carry parameters (``; charset=utf-8``). Strip
        # them before matching, otherwise ``application/rss+xml;
        # charset=utf-8`` matches neither the exact names nor ``+xml``.
        # NOTE: local name intentionally avoids ``media_type`` so the
        # stored-XSS census (``media_type\s*=``) keeps passing -- this
        # value never reaches a served response.
        base_type = content_type.split(";", 1)[0].strip().lower()
        # SVG is markup, not prose -- reject in every family, including
        # ``text/svg`` and ``text/svg+xml`` which would otherwise pass via
        # the ``text/`` prefix below.
        if "svg" in base_type:
            return False
        if base_type.startswith("text/"):
            return True
        if base_type in (
            "application/xml",
            "application/rss+xml",
            "application/atom+xml",
        ):
            return True
        return base_type.endswith("+xml")

    def _consume_recovery(
        self, url: str
    ) -> tuple[Optional[str], Optional[bytes | str], Optional[str]]:
        """Take stashed recovery payload when it belongs to this request URL.

        Returns ``(kind, payload, final_url)`` with kind ``"pdf"`` (bytes)
        or ``"text"`` (str), or ``(None, None, None)``. Single-use: the
        stash is cleared on a URL match so each fetch consumes at most its
        own bytes. A mismatch (stale stash from an earlier fetch) is left
        alone and reported as absent. The stash fields are unsynchronized
        instance state and downloader instances are used sequentially
        (per-request construction, or sequential consumption of one shared
        instance); sharing one instance across threads is unsupported. The
        URL-match guard does not make such sharing safe: the request URL is
        recorded when a fetch starts, not when the stash is written, and
        the non-200 and unexpected-content-type returns do not clear the
        stash. If one instance is shared anyway, a request can lose its own
        recovery or receive another request's recovered content.
        """
        if getattr(self, "_recovery_payload", None) is None:
            return None, None, None
        if getattr(self, "_recovery_request_url", None) != url:
            return None, None, None
        kind = getattr(self, "_recovery_kind", None)
        payload = self._recovery_payload
        final_url = getattr(self, "_recovery_url", None) or url
        self._recovery_payload = None
        self._recovery_kind = None
        return kind, payload, final_url

    def _extract_stashed_pdf_text(
        self, pdf_bytes: bytes, pdf_url: str, url: str
    ) -> Optional[str]:
        """Extract and format text from stashed PDF bytes."""
        # Defense in depth: callers check the byte cap first for a specific
        # skip reason, but never parse an oversize stash even if they don't.
        if len(pdf_bytes) > self.MAX_RECOVERED_PDF_BYTES:
            logger.info(
                "html.pdf_recovery_skipped_oversize url={url}",
                url=redact_url_for_log(url),
            )
            return None
        try:
            text = self.extract_text_from_pdf(
                pdf_bytes, max_pages=self.MAX_RECOVERED_PDF_PAGES
            )
        except Exception:
            logger.opt(exception=False).error(
                "html.pdf_recovery_extract_failed url={url}",
                url=redact_url_for_log(url),
            )
            return None
        if not text or not text.strip():
            logger.debug(
                "html.pdf_recovery_empty url={url}",
                url=redact_url_for_log(url),
            )
            return None
        return self._format_recovered_text(text, pdf_url)

    @staticmethod
    def _format_recovered_text(text: str, url: str) -> str:
        """Format recovered non-HTML text like content (Source line + body)."""
        return f"Source: {url}\n\n{text.strip()}"

    def _extract_content(self, html: str, url: str) -> Optional[Dict[str, Any]]:
        """Extract clean content and metadata from HTML.

        Delegates to the shared extraction pipeline which handles
        trafilatura, readability, justext, and metadata enrichment.
        """
        try:
            result = extract_content_with_metadata(html, language=self.language)
            if not result:
                return None

            title = result.get("title")
            content = result["content"]

            logger.debug(
                "html.extract_succeeded url={url} content_length={content_length}",
                url=redact_url_for_log(url),
                content_length=len(content),
            )
            return {
                "title": title,
                "description": result.get("description"),
                "content": content,
                "url": url,
            }

        except Exception as e:
            logger.opt(exception=False).error(
                "html.extract_failed url={url} exc_class={exc_class}",
                url=redact_url_for_log(url),
                exc_class=type(e).__name__,
            )
            return None

    def _format_extracted_content(self, extracted: Dict[str, Any]) -> str:
        """Format extracted content as readable text."""
        parts = []

        if extracted.get("title"):
            parts.append(f"# {_as_markdown_text(str(extracted['title']))}")
            parts.append("")

        if extracted.get("description"):
            parts.append(
                f"*{_as_markdown_text(str(extracted['description']))}*"
            )
            parts.append("")

        if extracted.get("url"):
            parts.append(f"Source: {extracted['url']}")
            parts.append("")

        if extracted.get("content"):
            parts.append(extracted["content"])

        return "\n".join(parts)

    def get_metadata(self, url: str) -> Dict[str, Any]:
        """Get metadata about the page."""
        html_content = self._fetch_html(url)
        if not html_content:
            return {}

        try:
            soup = BeautifulSoup(html_content, "html.parser")

            metadata = {"url": url}

            if soup.title and soup.title.string:
                metadata["title"] = soup.title.string.strip()

            meta_desc = soup.find("meta", attrs={"name": "description"})
            if meta_desc and meta_desc.get("content"):
                metadata["description"] = str(meta_desc["content"])

            author = soup.find("meta", attrs={"name": "author"})
            if author and author.get("content"):
                metadata["author"] = str(author["content"])

            for prop in ["article:published_time", "datePublished"]:
                date_tag = soup.find("meta", property=prop)
                if date_tag and date_tag.get("content"):
                    metadata["published_date"] = str(date_tag["content"])
                    break

            return metadata

        except Exception as e:
            logger.opt(exception=False).error(
                "html.metadata_extract_failed url={url} exc_class={exc_class}",
                url=redact_url_for_log(url),
                exc_class=type(e).__name__,
            )
            return {"url": url}