//! Hyper SSL support via BoringSSL. #![warn(missing_docs)] use crate::cache::SessionKey; use boring::error::ErrorStack; use boring::ex_data::Index; use boring::ssl::Ssl; use std::fmt; use std::sync::LazyLock; use tokio_boring::SslStream; mod cache; mod v0; /// Hyper 1 support. #[cfg(feature = "hyper1")] pub mod v1; pub use self::v0::*; fn key_index() -> Result, ErrorStack> { static IDX: LazyLock> = LazyLock::new(|| Ssl::new_ex_index().unwrap()); Ok(*IDX) } /// Settings for [`HttpsLayer`] pub struct HttpsLayerSettings { session_cache_capacity: usize, } impl HttpsLayerSettings { /// Constructs an [`HttpsLayerSettingsBuilder`] for configuring settings #[must_use] pub fn builder() -> HttpsLayerSettingsBuilder { HttpsLayerSettingsBuilder(HttpsLayerSettings::default()) } } impl Default for HttpsLayerSettings { fn default() -> Self { Self { session_cache_capacity: 8, } } } /// Builder for [`HttpsLayerSettings`] pub struct HttpsLayerSettingsBuilder(HttpsLayerSettings); impl HttpsLayerSettingsBuilder { /// Sets maximum number of sessions to cache. Session capacity is per session key (domain). /// Defaults to 8. pub fn set_session_cache_capacity(&mut self, capacity: usize) { self.0.session_cache_capacity = capacity; } /// Consumes the builder, returning a new [`HttpsLayerSettings`] #[must_use] pub fn build(self) -> HttpsLayerSettings { self.0 } } /// A stream which may be wrapped with TLS. pub enum MaybeHttpsStream { /// A raw HTTP stream. Http(T), /// An SSL-wrapped HTTP stream. Https(SslStream), } impl fmt::Debug for MaybeHttpsStream { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { MaybeHttpsStream::Http(..) => f.pad("Http(..)"), MaybeHttpsStream::Https(..) => f.pad("Https(..)"), } } }