rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What rho's clients for URLs someone else chose share: `tools::web`'s
//! fetches and the daemon's `rho/forge/fetch`. Each follows no redirect on
//! its own, so a 3xx can't carry a request to a host its caller never
//! checked, and all use one TLS configuration ([`TLS_CONFIG`]).

use std::sync::LazyLock;

use rustls_platform_verifier::BuilderVerifierExt;

/// One rustls client config for the process.
///
/// Building it loads the system trust store, which costs a few milliseconds,
/// and a content fetch needs its own client to pin the connection. Sharing the
/// config keeps that client build in the microseconds. `None` means TLS is
/// unavailable, and reqwest then reports the same failure it would have
/// reported for a config it built itself.
static TLS_CONFIG: LazyLock<Option<rustls::ClientConfig>> = LazyLock::new(rustls_client_config);

fn rustls_client_config() -> Option<rustls::ClientConfig> {
    rho_providers::ensure_rustls_ring_provider();
    let mut config = rustls::ClientConfig::builder()
        .with_platform_verifier()
        .ok()?
        .with_no_client_auth();
    config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
    Some(config)
}

/// A client builder that follows no redirect, with [`TLS_CONFIG`]: each
/// caller checks a redirect's URL itself, and sets its own timeouts and
/// proxies.
pub(crate) fn no_redirect_builder() -> reqwest::ClientBuilder {
    let builder = crate::reqwest_client_builder().redirect(reqwest::redirect::Policy::none());
    match TLS_CONFIG.as_ref() {
        Some(tls) => builder.tls_backend_preconfigured(tls.clone()),
        None => builder,
    }
}