use bytes::Bytes;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::task::{Context, Poll};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ConnectFailureKind {
Dns,
ConnectionRefused,
Connect,
}
#[cfg(feature = "tls-rustls")]
pub(crate) type Connector = hyper_rustls::HttpsConnector<
hyper_util::client::legacy::connect::HttpConnector<standard_resolver::DefaultResolver>,
>;
#[cfg(not(feature = "tls-rustls"))]
pub(crate) type Connector =
hyper_util::client::legacy::connect::HttpConnector<standard_resolver::DefaultResolver>;
pub(crate) struct HyperRequestBody {
inner: Pin<
Box<
http_body_util::combinators::UnsyncBoxBody<
Bytes,
Box<dyn std::error::Error + Send + Sync>,
>,
>,
>,
completion_tx: Mutex<Option<tokio::sync::oneshot::Sender<tokio::time::Instant>>>,
completion_rx: Option<tokio::sync::oneshot::Receiver<tokio::time::Instant>>,
complete: AtomicBool,
}
impl HyperRequestBody {
pub(crate) fn new(
inner: http_body_util::combinators::UnsyncBoxBody<
Bytes,
Box<dyn std::error::Error + Send + Sync>,
>,
) -> Self {
let (completion_tx, completion_rx) = tokio::sync::oneshot::channel();
Self {
inner: Box::pin(inner),
completion_tx: Mutex::new(Some(completion_tx)),
completion_rx: Some(completion_rx),
complete: AtomicBool::new(false),
}
}
pub(crate) fn take_completion_receiver(
&mut self,
) -> Option<tokio::sync::oneshot::Receiver<tokio::time::Instant>> {
self.completion_rx.take()
}
fn mark_complete(&self) {
if !self.complete.swap(true, Ordering::AcqRel) {
if let Ok(mut sender) = self.completion_tx.lock() {
if let Some(sender) = sender.take() {
let _ = sender.send(tokio::time::Instant::now());
}
}
}
}
}
impl http_body::Body for HyperRequestBody {
type Data = Bytes;
type Error = Box<dyn std::error::Error + Send + Sync>;
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
let this = self.as_mut().get_mut();
match this.inner.as_mut().poll_frame(cx) {
Poll::Ready(Some(Ok(frame))) => {
if this.inner.as_ref().get_ref().is_end_stream() {
this.mark_complete();
}
Poll::Ready(Some(Ok(frame)))
}
Poll::Ready(None) => {
this.mark_complete();
Poll::Ready(None)
}
Poll::Ready(Some(Err(error))) => {
if let Ok(mut sender) = this.completion_tx.lock() {
sender.take();
}
Poll::Ready(Some(Err(error)))
}
other => other,
}
}
fn is_end_stream(&self) -> bool {
let is_end_stream = self.inner.as_ref().get_ref().is_end_stream();
if is_end_stream {
self.mark_complete();
}
is_end_stream
}
fn size_hint(&self) -> http_body::SizeHint {
self.inner.as_ref().get_ref().size_hint()
}
}
#[cfg(feature = "advanced-routing")]
pub(crate) type CustomConnector = custom_connector::CustomConnector;
#[cfg(feature = "advanced-routing")]
pub(crate) type TimeoutCustomClient = hyper_util::client::legacy::Client<
lifecycle::LifecycleConnector<connect_timeout::ConnectTimeout<CustomConnector>>,
HyperRequestBody,
>;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
pub(crate) type TimeoutHyperClient = hyper_util::client::legacy::Client<
lifecycle::LifecycleConnector<connect_timeout::ConnectTimeout<Connector>>,
HyperRequestBody,
>;
#[cfg(feature = "advanced-routing")]
pub(crate) type TimeoutDirectClient = hyper_util::client::legacy::Client<
lifecycle::LifecycleConnector<
connect_timeout::ConnectTimeout<direct_connector::DirectConnector>,
>,
HyperRequestBody,
>;
#[cfg(unix)]
#[cfg(feature = "advanced-routing")]
pub(crate) type TimeoutUdsClient = hyper_util::client::legacy::Client<
lifecycle::LifecycleConnector<connect_timeout::ConnectTimeout<uds::UdsConnector>>,
HyperRequestBody,
>;
#[cfg(feature = "proxy")]
pub(crate) type TimeoutSocksClient = hyper_util::client::legacy::Client<
lifecycle::LifecycleConnector<connect_timeout::ConnectTimeout<socks::SocksConnector>>,
HyperRequestBody,
>;
#[cfg(feature = "proxy")]
pub(crate) type TimeoutForwardClient = hyper_util::client::legacy::Client<
lifecycle::LifecycleConnector<connect_timeout::ConnectTimeout<proxy::ForwardProxyConnector>>,
HyperRequestBody,
>;
#[cfg(feature = "proxy")]
pub(crate) type TimeoutConnectClient = hyper_util::client::legacy::Client<
lifecycle::LifecycleConnector<connect_timeout::ConnectTimeout<connect::ConnectProxyConnector>>,
HyperRequestBody,
>;
pub mod alt_svc;
pub(crate) mod connect_timeout;
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing",
feature = "proxy"
))]
pub(crate) mod hyper_client;
#[cfg(feature = "advanced-routing")]
pub(crate) mod custom_connector {
#[cfg(feature = "tls-rustls")]
pub(crate) type CustomConnector = hyper_rustls::HttpsConnector<super::dialer::DialerConnector>;
#[cfg(not(feature = "tls-rustls"))]
pub(crate) type CustomConnector = super::dialer::DialerConnector;
}
#[cfg(feature = "http3")]
pub mod datagram;
pub mod dialer;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
pub(crate) mod direct;
pub mod direct_connector;
pub mod lifecycle;
pub mod metrics;
pub(crate) mod standard_resolver;
#[cfg(all(unix, feature = "advanced-routing"))]
pub(crate) mod uds;
#[cfg(feature = "proxy")]
pub(crate) mod connect;
#[cfg(feature = "proxy")]
pub(crate) mod proxy;
#[cfg(feature = "proxy")]
pub(crate) mod socks;
#[cfg(feature = "http3")]
pub(crate) mod http3;
pub(crate) const ALPN_HTTP2: &[u8] = b"h2";
pub(crate) fn negotiated_http2(alpn: Option<&[u8]>) -> bool {
alpn.is_some_and(|protocol| protocol == ALPN_HTTP2)
}
#[cfg(test)]
mod tests {
use super::{negotiated_http2, ALPN_HTTP2};
#[test]
fn negotiated_http2_detects_only_the_h2_protocol() {
assert!(negotiated_http2(Some(ALPN_HTTP2)));
assert!(!negotiated_http2(Some(b"http/1.1")));
assert!(!negotiated_http2(Some(b"http/1.0")));
assert!(!negotiated_http2(Some(b"")));
assert!(!negotiated_http2(None));
}
#[test]
fn negotiated_http2_matches_the_previous_inline_expression() {
for candidate in [
Some(ALPN_HTTP2),
Some(&b"http/1.1"[..]),
Some(&b""[..]),
None,
] {
let expected = matches!(candidate, Some(protocol) if protocol == ALPN_HTTP2);
assert_eq!(negotiated_http2(candidate), expected);
}
}
}