#[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 }
}
}
impl HttpVersionPolicy {
#[cfg(any(feature = "http3", test))]
#[allow(dead_code)]
pub(crate) fn use_http3(self) -> bool {
matches!(self, Self::Http3Only) || matches!(self, Self::Auto { allow_http3: true })
}
}
#[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)]
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)]
pub(crate) const fn enable_http1(self) -> bool {
matches!(
self.0,
HttpVersionPolicy::Http1Only | HttpVersionPolicy::Auto { .. }
)
}
#[must_use]
#[allow(dead_code)]
pub(crate) const fn enable_http2(self) -> bool {
matches!(
self.0,
HttpVersionPolicy::Http2Only | HttpVersionPolicy::Auto { .. }
)
}
#[must_use]
#[allow(dead_code)]
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());
}
}