use std::net::IpAddr;
use std::time::Duration;
use crate::http::client_identity::ClientTlsConfig;
#[allow(dead_code)]
pub(crate) fn build_tracker_client_with_tls_and_timeouts(
request_timeout_secs: u64,
connect_timeout_secs: u64,
tls: &ClientTlsConfig,
) -> Result<reqwest::Client, String> {
build_tracker_client_with_source(request_timeout_secs, connect_timeout_secs, tls, None)
}
pub(crate) fn build_tracker_client_with_source(
request_timeout_secs: u64,
connect_timeout_secs: u64,
tls: &ClientTlsConfig,
local_address: Option<IpAddr>,
) -> Result<reqwest::Client, String> {
crate::http::client_pool::ensure_rustls_provider();
let mut builder = reqwest::Client::builder()
.timeout(Duration::from_secs(request_timeout_secs))
.connect_timeout(Duration::from_secs(connect_timeout_secs))
.gzip(false);
if let Some(address) = local_address {
builder = builder.local_address(address);
}
let builder =
crate::http::client_identity::apply(builder, tls).map_err(|error| error.to_string())?;
builder
.build()
.map_err(|e| format!("Failed to build HTTP client: {}", e))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tracker_client_applies_custom_tls_configuration() {
let options = crate::request::request_group::DownloadOptions {
ca_certificate: Some("missing-tracker-ca.pem".into()),
..Default::default()
};
let tls = ClientTlsConfig::from_download_options(&options);
let client = build_tracker_client_with_tls_and_timeouts(30, 30, &tls);
assert!(
client
.expect_err("missing tracker CA must reject client construction")
.contains("Failed to read CA certificate")
);
}
}