use crate::error::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HttpVersionPolicy {
Http1Only,
Http2Only,
Http3Only,
Auto {
allow_http3: bool,
},
}
impl Default for HttpVersionPolicy {
fn default() -> Self {
Self::Auto { allow_http3: false }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Http3Capability {
Compiled,
NotCompiled,
}
impl Http3Capability {
pub(crate) const CURRENT: Self = if cfg!(feature = "http3") {
Self::Compiled
} else {
Self::NotCompiled
};
pub(crate) const fn is_available(self) -> bool {
matches!(self, Self::Compiled)
}
}
pub(crate) fn missing_client_error(policy: HttpVersionPolicy) -> Error {
if matches!(policy, HttpVersionPolicy::Http3Only) && !Http3Capability::CURRENT.is_available() {
return Error::Unsupported(
"HTTP/3 is not supported by this build: the http3 feature was not compiled in".into(),
);
}
Error::Unsupported("HTTP client not available for this protocol".into())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct HttpVersionPolicyEnabler(HttpVersionPolicy);
impl HttpVersionPolicyEnabler {
#[must_use]
pub(crate) fn from_policy(policy: HttpVersionPolicy) -> Self {
match policy {
#[cfg(not(feature = "transport-http2"))]
HttpVersionPolicy::Http2Only => Self(HttpVersionPolicy::Http1Only),
#[cfg(not(feature = "http3"))]
HttpVersionPolicy::Http3Only => Self(HttpVersionPolicy::Http1Only),
#[allow(
unused_variables,
reason = "the `Auto` arm ignores the caller's HTTP/3 flag when the http2 or http3 feature is compiled out, so the binding is unused in those builds"
)]
HttpVersionPolicy::Auto { allow_http3 } => {
#[cfg(not(feature = "transport-http2"))]
return Self(HttpVersionPolicy::Http1Only);
#[cfg(all(feature = "transport-http2", not(feature = "http3")))]
return Self(HttpVersionPolicy::Auto { allow_http3: false });
#[cfg(all(feature = "transport-http2", feature = "http3"))]
return Self(HttpVersionPolicy::Auto { allow_http3 });
}
_ => Self(policy),
}
}
#[must_use]
#[allow(
dead_code,
reason = "the HTTP/1.1 switch is only read by the ALPN and connector builders, which are compiled out when neither tls-rustls nor transport-http2 is enabled"
)]
pub(crate) const fn enable_http1(self) -> bool {
matches!(
self.0,
HttpVersionPolicy::Http1Only | HttpVersionPolicy::Auto { .. }
)
}
#[must_use]
#[allow(
dead_code,
reason = "the HTTP/2 switch is only read by the ALPN and connector builders, which are compiled out when neither tls-rustls nor transport-http2 is enabled"
)]
pub(crate) const fn enable_http2(self) -> bool {
matches!(
self.0,
HttpVersionPolicy::Http2Only | HttpVersionPolicy::Auto { .. }
)
}
#[must_use]
#[allow(
dead_code,
reason = "the HTTP/3 switch is only read by the HTTP/3 connector setup, which is compiled out when the http3 feature is disabled"
)]
pub(crate) fn use_http3(self) -> bool {
match self.0 {
HttpVersionPolicy::Http3Only => true,
HttpVersionPolicy::Auto { allow_http3 } => allow_http3,
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_auto() {
assert_eq!(
HttpVersionPolicy::default(),
HttpVersionPolicy::Auto { allow_http3: false }
);
}
#[test]
fn http1_only_enables_http1_only() {
let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http1Only);
assert!(enabler.enable_http1());
assert!(!enabler.enable_http2());
assert!(!enabler.use_http3());
}
#[test]
#[cfg(feature = "transport-http2")]
fn auto_enables_both() {
let enabler =
HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto { allow_http3: false });
assert!(enabler.enable_http1());
assert!(enabler.enable_http2());
assert!(!enabler.use_http3());
}
#[test]
#[cfg(not(feature = "transport-http2"))]
fn auto_downgrades_without_feature() {
let enabler =
HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto { allow_http3: false });
assert!(enabler.enable_http1());
assert!(!enabler.enable_http2());
}
#[test]
#[cfg(feature = "transport-http2")]
fn http2_only_enables_http2_only() {
let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http2Only);
assert!(!enabler.enable_http1());
assert!(enabler.enable_http2());
}
#[test]
#[cfg(not(feature = "transport-http2"))]
fn http2_only_downgrades_without_feature() {
let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http2Only);
assert!(enabler.enable_http1());
assert!(!enabler.enable_http2());
}
#[test]
#[cfg(feature = "http3")]
fn http3_only_enables_http3_only() {
let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http3Only);
assert!(!enabler.enable_http1());
assert!(!enabler.enable_http2());
assert!(enabler.use_http3());
}
#[test]
#[cfg(not(feature = "http3"))]
fn http3_only_downgrades_without_feature() {
let enabler = HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Http3Only);
assert!(enabler.enable_http1());
assert!(!enabler.enable_http2());
assert!(!enabler.use_http3());
}
#[test]
#[cfg(not(feature = "http3"))]
fn missing_client_error_names_the_http3_capability_gap() {
let error = missing_client_error(HttpVersionPolicy::Http3Only);
let message = error.to_string();
assert!(
message.contains("HTTP/3"),
"the failure must name HTTP/3: {message}"
);
assert!(
!message.contains("HTTP client not available"),
"the failure must not reuse the generic missing-client wording: {message}"
);
}
#[test]
#[cfg(feature = "http3")]
fn missing_client_error_keeps_the_generic_wording_when_http3_is_compiled() {
let error = missing_client_error(HttpVersionPolicy::Http3Only);
let message = error.to_string();
assert!(
message.contains("HTTP client not available"),
"a compiled-in HTTP/3 build has no capability gap to report: {message}"
);
}
#[test]
fn missing_client_error_keeps_the_generic_wording_for_other_policies() {
for policy in [
HttpVersionPolicy::Http1Only,
HttpVersionPolicy::Http2Only,
HttpVersionPolicy::Auto { allow_http3: false },
HttpVersionPolicy::Auto { allow_http3: true },
] {
let message = missing_client_error(policy).to_string();
assert!(
message.contains("HTTP client not available"),
"unexpected classification for {policy:?}: {message}"
);
}
}
#[test]
fn classification_is_typed_and_does_not_render_the_policy() {
let message = missing_client_error(HttpVersionPolicy::Http3Only).to_string();
assert!(
!message.contains("Http3Only"),
"the classification must not embed a rendered policy: {message}"
);
assert!(
!message.contains("Auto"),
"the classification must not embed a rendered policy: {message}"
);
}
#[test]
fn http3_capability_matches_the_compiled_feature_set() {
assert_eq!(
Http3Capability::CURRENT.is_available(),
cfg!(feature = "http3"),
"the typed capability must track the compiled feature set"
);
}
}