#[cfg(feature = "advanced-routing")]
mod custom_dialing;
#[cfg(feature = "high-level-url")]
mod finalize;
#[cfg(feature = "http3")]
mod h3_dispatch;
#[cfg(feature = "high-level-url")]
mod hop;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
mod hyper_dispatch;
#[cfg(all(feature = "high-level-url", not(feature = "redirects")))]
mod lean;
mod prepare;
#[cfg(feature = "proxy")]
mod proxy_dispatch;
#[cfg(all(feature = "high-level-url", feature = "redirects"))]
mod redirect;
#[cfg(all(feature = "high-level-url", feature = "logical-retry"))]
mod retry;
mod route;
#[cfg(feature = "proxy")]
pub(crate) use prepare::validate_target;
#[cfg(all(feature = "high-level-url", feature = "logical-retry"))]
pub(crate) use retry::send_with_retry;
#[cfg(all(
feature = "high-level-url",
not(feature = "redirects"),
not(feature = "logical-retry")
))]
pub(crate) use lean::send_lean;
#[cfg(all(
feature = "high-level-url",
feature = "redirects",
not(feature = "logical-retry")
))]
pub(crate) use redirect::send_with_redirects;
#[cfg(test)]
pub(crate) use prepare::apply_content_length;
use std::time::Duration;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
use bytes::Bytes;
use crate::body::{NativeRequestBody, NativeResponseBody};
use crate::client::ClientInner;
use crate::error::{Error, Result};
#[cfg(feature = "high-level-url")]
use crate::request::Request;
#[cfg(feature = "high-level-url")]
use crate::response::Response;
use crate::timeout::Timeout;
use crate::transport_hints::NativeRequestOptions;
async fn send_with_total_timeout<F, T>(
send_future: F,
remaining_total: Option<Duration>,
) -> Result<T>
where
F: std::future::Future<Output = Result<T>>,
{
use crate::timeout::TimeoutPhase;
match remaining_total {
Some(duration) => {
let started = std::time::Instant::now();
tokio::time::timeout(duration, send_future)
.await
.map_err(|_| Error::Timeout {
phase: TimeoutPhase::Total,
elapsed: started.elapsed(),
})?
}
None => send_future.await,
}
}
#[cfg(all(
feature = "high-level-url",
any(feature = "logical-retry", feature = "redirects")
))]
const DRAIN_MAX_BYTES: usize = 256 * 1024;
#[cfg(all(
feature = "high-level-url",
any(feature = "logical-retry", feature = "redirects")
))]
const DRAIN_MAX_TIME: Duration = Duration::from_secs(30);
#[cfg(all(
feature = "high-level-url",
any(feature = "logical-retry", feature = "redirects")
))]
pub(super) async fn drain_response_body(response: &mut Response, read_timeout: Option<Duration>) {
let Ok(stream) = response.raw_bytes_stream() else {
return; };
let mut remaining = DRAIN_MAX_BYTES;
let mut stream = std::pin::pin!(stream);
let cap = read_timeout.map_or(DRAIN_MAX_TIME, |read| read.min(DRAIN_MAX_TIME));
let drain = async {
while let Some(chunk) = futures_util::StreamExt::next(&mut stream).await {
match chunk {
Ok(bytes) => {
if bytes.len() >= remaining {
break;
}
remaining -= bytes.len();
}
Err(_) => break,
}
}
};
let _ = tokio::time::timeout(cap, drain).await;
}
#[allow(
clippy::too_many_lines,
reason = "the body is the prepare, route-select and finalize sequence for one request, and the order of those three phases is the behaviour"
)]
#[cfg(all(
any(feature = "transport-http1", feature = "transport-http2"),
feature = "high-level-url"
))]
pub(crate) async fn send_single_request(
inner: &ClientInner,
request: Request,
timeout: &Timeout,
) -> Result<Response> {
let (prepared, guard) = prepare::prepare_single_request(inner, request, timeout).await?;
let prepare::PreparedRequest {
method,
url,
uri,
headers,
body,
version,
transport_hints,
proxied_target,
#[cfg(feature = "proxy")]
effective_proxy,
decompression_enabled,
max_decoded_body_size,
max_decompression_ratio,
timeout: hop_timeout,
remaining_total,
deadline,
failure_context,
} = prepared;
#[cfg(not(feature = "proxy"))]
let _ = &deadline;
#[cfg(not(feature = "proxy"))]
let _ = &proxied_target;
#[cfg(all(unix, feature = "advanced-routing"))]
let has_uds = inner.uds_client.is_some();
#[cfg(all(unix, not(feature = "advanced-routing")))]
let has_uds = false;
#[cfg(not(unix))]
let has_uds = false;
#[cfg(feature = "proxy")]
let has_proxy = effective_proxy.is_some();
#[cfg(not(feature = "proxy"))]
let has_proxy = false;
#[cfg(all(feature = "proxy", feature = "advanced-routing"))]
let has_direct_no_proxy = !has_proxy
&& (transport_hints.resolved_target.is_some() || inner.direct_client.is_some())
&& !has_uds;
#[cfg(all(feature = "proxy", not(feature = "advanced-routing")))]
let has_direct_no_proxy = false;
#[cfg(all(not(feature = "proxy"), feature = "advanced-routing"))]
let has_direct_no_proxy =
!has_uds && (transport_hints.resolved_target.is_some() || inner.direct_client.is_some());
#[cfg(all(not(feature = "proxy"), not(feature = "advanced-routing")))]
let has_direct_no_proxy = false;
#[cfg(feature = "advanced-routing")]
let has_sni = transport_hints.sni_hostname.is_some();
#[cfg(not(feature = "advanced-routing"))]
let has_sni = false;
#[cfg(feature = "advanced-routing")]
let has_custom = inner.config.dialer.is_some();
#[cfg(not(feature = "advanced-routing"))]
let has_custom = false;
#[cfg(not(feature = "advanced-routing"))]
{
if transport_hints.resolved_target.is_some() {
return Err(Error::Unsupported(
"caller-supplied resolved destinations require the advanced-routing feature".into(),
));
}
if transport_hints.sni_hostname.is_some() {
return Err(Error::Unsupported(
"SNI override requires the advanced-routing feature".into(),
));
}
}
#[cfg(all(feature = "advanced-routing", feature = "proxy"))]
if has_custom {
let arms = custom_dialing::CustomDialingState {
has_resolved_target: transport_hints.resolved_target.is_some(),
has_local_address_or_socket_options: inner.direct_connector_config.is_some(),
has_uds: inner.config.uds_configured,
#[cfg(feature = "http3")]
is_http3_only: matches!(
inner.config.http_version_policy,
crate::HttpVersionPolicy::Http3Only
),
#[cfg(not(feature = "http3"))]
is_http3_only: false,
has_built_in_proxy: Some(has_proxy),
};
if let Some(conflict) = custom_dialing::custom_dialing_conflict(arms) {
return Err(conflict.error());
}
}
#[cfg(feature = "http3")]
let (use_h3, h3_alt) =
h3_dispatch::decide_h3_use(inner, &url, has_uds, has_proxy, has_sni, has_custom);
#[cfg(not(feature = "http3"))]
let use_h3 = false;
let route = route::select_route(
has_uds,
has_custom,
has_direct_no_proxy,
has_proxy,
has_sni,
use_h3,
);
#[cfg(feature = "http3")]
if transport_hints.resolved_target.is_some() && use_h3 {
return Err(Error::Unsupported(
"caller-supplied resolved destinations are incompatible with HTTP/3".into(),
));
}
#[cfg(feature = "http3")]
if use_h3 {
inner.transport_metrics.record_h3_attempted();
}
let response = match route {
#[cfg(feature = "advanced-routing")]
route::TransportRoute::Uds => {
hyper_dispatch::send_uds_route(
inner,
method,
uri,
headers,
body,
version,
url,
&transport_hints,
remaining_total,
hop_timeout.read,
)
.await?
}
#[cfg(feature = "advanced-routing")]
route::TransportRoute::Custom => {
hyper_dispatch::send_custom_route(
inner,
method,
uri,
headers,
body,
version,
url,
&transport_hints,
failure_context.as_deref(),
remaining_total,
hop_timeout.read,
)
.await?
}
#[cfg(feature = "advanced-routing")]
route::TransportRoute::Direct => {
hyper_dispatch::send_direct_route(
inner,
method,
uri,
headers,
body,
version,
url,
&transport_hints,
failure_context.as_deref(),
remaining_total,
hop_timeout.read,
)
.await?
}
route::TransportRoute::Proxy => {
#[cfg(feature = "proxy")]
{
proxy_dispatch::send_proxy_route(
inner,
&method,
&url,
headers,
body,
version,
effective_proxy.as_ref(),
&transport_hints,
proxied_target.as_ref(),
hop_timeout,
remaining_total,
deadline,
failure_context.as_deref(),
)
.await?
}
#[cfg(not(feature = "proxy"))]
{
let _ = (method, uri, headers, body, version);
return Err(Error::Unsupported("proxy support is not enabled".into()));
}
}
#[cfg(feature = "advanced-routing")]
route::TransportRoute::SniDirect => {
hyper_dispatch::send_sni_route(
inner,
method,
uri,
headers,
body,
version,
url,
&transport_hints,
failure_context.as_deref(),
remaining_total,
hop_timeout.read,
)
.await?
}
route::TransportRoute::H3 => {
#[cfg(feature = "http3")]
{
h3_dispatch::send_h3_route(
inner,
&method,
&url,
&headers,
body,
version,
uri,
h3_alt,
hop_timeout,
remaining_total,
&transport_hints,
failure_context.as_deref(),
)
.await?
}
#[cfg(not(feature = "http3"))]
{
let _ = (method, uri, headers, body, version);
return Err(Error::Unsupported("HTTP/3 support is not enabled".into()));
}
}
route::TransportRoute::Standard => {
hyper_dispatch::send_hyper_request(
inner,
method,
url,
headers,
body,
version,
remaining_total,
&transport_hints,
failure_context.as_deref(),
hop_timeout.read,
)
.await?
}
};
#[cfg(feature = "proxy")]
let via_proxy = effective_proxy.is_some();
#[cfg(not(feature = "proxy"))]
let via_proxy = false;
let total_deadline = match (deadline, hop_timeout.total) {
(Some(deadline), Some(total)) => {
Some(crate::timeout::ResponseDeadline::new(deadline, total))
}
_ => None,
};
finalize::finalize_response(
inner,
response,
route,
via_proxy,
decompression_enabled,
max_decoded_body_size,
max_decompression_ratio,
guard,
hop_timeout.read,
total_deadline,
)
}
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
#[allow(
clippy::too_many_lines,
reason = "native dispatch keeps route validation, pool admission, and the shared route matrix together"
)]
pub(crate) async fn send_native_http_body<B>(
inner: &ClientInner,
request: http::Request<B>,
options: NativeRequestOptions,
) -> Result<http::Response<NativeResponseBody>>
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: std::error::Error + Send + Sync + 'static,
{
use http_body_util::BodyExt;
use crate::headers::Headers;
use crate::http_origin::HttpOrigin;
use crate::pool::OriginKey;
use crate::timeout::TimeoutPhase;
let http_origin = HttpOrigin::from_uri(request.uri())?;
#[cfg(feature = "proxy")]
let explicit_port = request.uri().port_u16();
let NativeRequestOptions {
timeout: request_timeout,
transport_hints,
} = options;
let timeout = match inner.config.timeout {
Some(client_timeout) => client_timeout.merge(request_timeout),
None => request_timeout.unwrap_or_default(),
};
if let Some(target) = &transport_hints.resolved_target {
crate::http_origin::validate_effective_port(
crate::http_origin::EffectivePortSubject::ResolvedDestination,
target.addresses(),
http_origin.port_ref(),
)?;
}
#[cfg(feature = "proxy")]
if prepare::native_would_use_proxy(inner, &http_origin, explicit_port) {
return Err(Error::Unsupported(
"native frame bodies are not supported through the built-in proxy routes".into(),
));
}
#[cfg(feature = "advanced-routing")]
if inner.config.dialer.is_some() {
let arms = custom_dialing::CustomDialingState {
has_resolved_target: transport_hints.resolved_target.is_some(),
has_local_address_or_socket_options: inner.direct_connector_config.is_some(),
has_uds: inner.config.uds_configured,
#[cfg(feature = "http3")]
is_http3_only: matches!(
inner.config.http_version_policy,
crate::HttpVersionPolicy::Http3Only
),
#[cfg(not(feature = "http3"))]
is_http3_only: false,
has_built_in_proxy: None,
};
if let Some(conflict) = custom_dialing::custom_dialing_conflict(arms) {
return Err(conflict.error());
}
}
#[cfg(not(feature = "advanced-routing"))]
{
if transport_hints.resolved_target.is_some() {
return Err(Error::Unsupported(
"caller-supplied resolved destinations require the advanced-routing feature".into(),
));
}
if transport_hints.sni_hostname.is_some() {
return Err(Error::Unsupported(
"SNI override requires the advanced-routing feature".into(),
));
}
}
#[cfg(feature = "http3")]
if matches!(
inner.config.http_version_policy,
crate::HttpVersionPolicy::Http3Only
) {
return Err(Error::Unsupported(
"native frame bodies are not supported by the experimental HTTP/3 route".into(),
));
}
#[cfg(feature = "tls-rustls")]
if http_origin.is_https() {
if let Some(error) = &inner.tls_config_error {
return Err(Error::Tls(error.clone()));
}
}
let (request_parts, request_body) = request.into_parts();
let uri = prepare::resolve_native_request_uri(request_parts.uri, &transport_hints)?;
let method = request_parts.method;
let version = request_parts.version;
let headers = Headers::from(request_parts.headers);
let body = BodyExt::map_err(request_body, |error| {
Box::new(error) as Box<dyn std::error::Error + Send + Sync>
})
.boxed_unsync();
let body = NativeRequestBody::new(Box::pin(body), timeout.write).boxed_unsync();
let origin_key = inner
.pool
.needs_origin_key()
.then(|| OriginKey::from_origin(&http_origin));
let started = std::time::Instant::now();
let pool_deadline = match (timeout.pool, timeout.total) {
(Some(pool), Some(total)) if total <= pool => Some((total, TimeoutPhase::Total)),
(Some(pool), _) => Some((pool, TimeoutPhase::Pool)),
(None, Some(total)) => Some((total, TimeoutPhase::Total)),
(None, None) => None,
};
let guard = match pool_deadline {
Some((duration, phase)) => {
match tokio::time::timeout(duration, inner.pool.acquire(origin_key.as_ref())).await {
Ok(guard) => guard?,
Err(_) => {
return Err(Error::Timeout {
phase,
elapsed: started.elapsed(),
})
}
}
}
None => inner.pool.acquire(origin_key.as_ref()).await?,
};
let remaining_total = timeout
.total
.map(|total| total.saturating_sub(started.elapsed()));
if remaining_total.is_some_and(|remaining| remaining.is_zero()) {
return Err(Error::Timeout {
phase: crate::timeout::TimeoutPhase::Total,
elapsed: started.elapsed(),
});
}
let total_deadline = timeout.total.map(|total| {
crate::timeout::ResponseDeadline::new(
crate::timeout::saturating_add_instant(started, total),
total,
)
});
let trace = transport_hints.trace.as_deref();
#[cfg(all(unix, feature = "advanced-routing"))]
let has_uds = inner.uds_client.is_some();
#[cfg(any(not(unix), not(feature = "advanced-routing")))]
let has_uds = false;
#[cfg(feature = "advanced-routing")]
let (has_dialer, has_direct) = (
inner.config.dialer.is_some(),
transport_hints.resolved_target.is_some() || inner.direct_client.is_some(),
);
#[cfg(not(feature = "advanced-routing"))]
let (has_dialer, has_direct) = (false, false);
#[cfg(feature = "advanced-routing")]
let has_sni = transport_hints.sni_hostname.is_some();
#[cfg(not(feature = "advanced-routing"))]
let has_sni = false;
let route = route::select_route(has_uds, has_dialer, has_direct, false, has_sni, false);
let raw_response = match route {
#[cfg(feature = "advanced-routing")]
route::TransportRoute::Uds => {
#[cfg(unix)]
{
let uds_client = inner
.uds_client
.as_ref()
.ok_or_else(|| Error::Unsupported("UDS client not available".into()))?;
let hyper_request = hyper_dispatch::build_http_request_owned(
method,
uri,
version,
headers,
crate::transport::HyperRequestBody::new(body),
)?;
send_with_total_timeout(
crate::transport::direct::send_raw_request(
uds_client,
hyper_request,
trace,
timeout.read,
remaining_total,
),
remaining_total,
)
.await?
}
#[cfg(not(unix))]
{
let _ = (method, uri, headers, body, version);
return Err(Error::Unsupported(
"Unix domain sockets are not supported on this platform".into(),
));
}
}
#[cfg(feature = "advanced-routing")]
route::TransportRoute::Custom => {
let custom_client = if let Some(sni_hostname) = transport_hints.sni_hostname.as_deref()
{
inner.sni_custom_client(sni_hostname).await?
} else {
inner
.custom_client
.as_ref()
.ok_or_else(|| Error::Unsupported("custom dialer client not available".into()))?
.clone()
};
let hyper_request = hyper_dispatch::build_http_request_owned(
method,
uri,
version,
headers,
crate::transport::HyperRequestBody::new(body),
)?;
send_with_total_timeout(
crate::transport::direct::send_raw_request(
&custom_client,
hyper_request,
trace,
timeout.read,
remaining_total,
),
remaining_total,
)
.await?
}
#[cfg(feature = "advanced-routing")]
route::TransportRoute::Direct => {
let resolved_client;
let direct_client = if let Some(target) = transport_hints.resolved_target.as_ref() {
resolved_client = inner
.resolved_client(
&http_origin,
target,
transport_hints.sni_hostname.as_deref(),
)
.await?;
&resolved_client
} else {
inner
.direct_client
.as_ref()
.ok_or_else(|| Error::Unsupported("direct client not available".into()))?
};
let hyper_request = hyper_dispatch::build_http_request_owned(
method,
uri,
version,
headers,
crate::transport::HyperRequestBody::new(body),
)?;
send_with_total_timeout(
crate::transport::direct::send_raw_request(
direct_client,
hyper_request,
trace,
timeout.read,
remaining_total,
),
remaining_total,
)
.await?
}
#[cfg(feature = "advanced-routing")]
route::TransportRoute::SniDirect => {
let sni_hostname = transport_hints.sni_hostname.as_deref().ok_or_else(|| {
Error::RequestBuild("SNI route selected without sni_hostname".into())
})?;
let sni_client = inner.sni_client(sni_hostname).await?;
let hyper_request = hyper_dispatch::build_http_request_owned(
method,
uri,
version,
headers,
crate::transport::HyperRequestBody::new(body),
)?;
send_with_total_timeout(
crate::transport::direct::send_raw_request(
&sni_client,
hyper_request,
trace,
timeout.read,
remaining_total,
),
remaining_total,
)
.await?
}
route::TransportRoute::Standard => {
let hyper_client = inner.hyper_client.as_ref().ok_or_else(|| {
crate::http_version::missing_client_error(inner.config.http_version_policy)
})?;
let hyper_request = hyper_dispatch::build_http_request_owned(
method,
uri,
version,
headers,
crate::transport::HyperRequestBody::new(body),
)?;
send_with_total_timeout(
crate::transport::direct::send_raw_request(
hyper_client,
hyper_request,
trace,
timeout.read,
remaining_total,
),
remaining_total,
)
.await?
}
route::TransportRoute::Proxy | route::TransportRoute::H3 => {
return Err(Error::Unsupported(
"native frame body route is not available".into(),
));
}
};
if raw_response.status() == http::StatusCode::SWITCHING_PROTOCOLS {
return Err(Error::Unsupported(
"native frame bodies do not expose protocol upgrades; use the high-level upgrade API"
.into(),
));
}
let (parts, incoming) = raw_response.into_parts();
let lease = guard
.has_response_state()
.then(|| std::sync::Arc::new(guard));
let native_body =
NativeResponseBody::from_incoming(incoming, lease, timeout.read, total_deadline);
Ok(http::Response::from_parts(parts, native_body))
}
#[cfg(not(any(feature = "transport-http1", feature = "transport-http2")))]
pub(crate) async fn send_native_http_body<B>(
_inner: &ClientInner,
_request: http::Request<B>,
_options: NativeRequestOptions,
) -> Result<http::Response<NativeResponseBody>> {
Err(Error::Unsupported(
"no HTTP protocol feature is enabled; enable http1 or http2".into(),
))
}
#[cfg(all(
not(any(feature = "transport-http1", feature = "transport-http2")),
feature = "high-level-url"
))]
pub(crate) async fn send_single_request(
_inner: &ClientInner,
_request: Request,
_timeout: &Timeout,
) -> Result<Response> {
Err(Error::Unsupported(
"no HTTP protocol feature is enabled; enable http1 or http2".into(),
))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn total_timeout_wraps_transport_future() {
let ok = send_with_total_timeout(async { Ok::<u32, Error>(7) }, None)
.await
.expect("passthrough");
assert_eq!(ok, 7);
let err = send_with_total_timeout(
async {
tokio::time::sleep(Duration::from_secs(5)).await;
Ok::<u32, Error>(7)
},
Some(Duration::from_millis(20)),
)
.await
.unwrap_err();
assert!(
matches!(err, Error::Timeout { .. }),
"expected total timeout, got {err:?}"
);
}
}