github-copilot-sdk 1.0.35

Deprecated GitHub Copilot SDK. Migrate to copilot-sdk.
// Copyright (c) Microsoft Corporation. All rights reserved.

// ureq 3 has no per-read timeout; allow slow archives while bounding stalled transfers.
pub(super) const BODY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15 * 60);

pub(super) fn agent() -> ureq::Agent {
    let tls = ureq::tls::TlsConfig::builder().root_certs(ureq::tls::RootCerts::PlatformVerifier);
    // macOS native-tls uses Secure Transport (TLS <= 1.2); keep TLS 1.3 for
    // archive acquisition instead of silently lowering the supported protocol.
    // ureq's built-in TLS choices are native-tls and Rustls, not Apple's
    // Network.framework. Removing this branch needs a native TLS-1.3 download
    // adapter preserving platform trust, redirects, proxy and timeout behavior.
    // AWS-LC is still software crypto: an OS-backed Rustls provider could be a
    // partial migration, but the standalone SDK does not package one. See
    // src/network_tls.rs for provider limits. Archive hashing is independently
    // OS-backed in archive_hash.rs and does not require this TLS provider.
    #[cfg(target_os = "macos")]
    let tls = tls
        .provider(ureq::tls::TlsProvider::Rustls)
        .unversioned_rustls_crypto_provider(std::sync::Arc::new(
            rustls::crypto::aws_lc_rs::default_provider(),
        ));
    #[cfg(not(target_os = "macos"))]
    let tls = tls.provider(ureq::tls::TlsProvider::NativeTls);
    ureq::Agent::config_builder()
        .tls_config(tls.build())
        .timeout_connect(Some(std::time::Duration::from_secs(30)))
        .timeout_recv_response(Some(std::time::Duration::from_secs(120)))
        .timeout_recv_body(Some(BODY_TIMEOUT))
        .build()
        .new_agent()
}

pub(super) fn is_transient(error: &ureq::Error) -> bool {
    match error {
        ureq::Error::StatusCode(code) => (500..600).contains(code),
        ureq::Error::BadUri(_)
        | ureq::Error::Http(_)
        | ureq::Error::InvalidProxyUrl
        | ureq::Error::TooManyRedirects
        | ureq::Error::RedirectFailed
        | ureq::Error::RequireHttpsOnly(_)
        | ureq::Error::TlsRequired
        | ureq::Error::Tls(_)
        | ureq::Error::Pem(_)
        | ureq::Error::BodyExceedsLimit(_)
        | ureq::Error::LargeResponseHeader(..) => false,
        #[cfg(not(target_os = "macos"))]
        ureq::Error::NativeTls(_) | ureq::Error::Der(_) => false,
        #[cfg(target_os = "macos")]
        ureq::Error::Rustls(_) => false,
        _ => true,
    }
}

pub(super) fn message(error: &ureq::Error) -> String {
    match error {
        ureq::Error::StatusCode(code) => {
            let reason = ureq::http::StatusCode::from_u16(*code)
                .ok()
                .and_then(|status| status.canonical_reason());
            match reason {
                Some(reason) => format!("HTTP {code} {reason}"),
                None => format!("HTTP {code}"),
            }
        }
        _ => format!("download error: {error}"),
    }
}