hyper_boring/
lib.rs

1//! Hyper SSL support via BoringSSL.
2#![warn(missing_docs)]
3#![cfg_attr(docsrs, feature(doc_auto_cfg))]
4
5use crate::cache::SessionKey;
6use boring::error::ErrorStack;
7use boring::ex_data::Index;
8use boring::ssl::Ssl;
9use std::fmt;
10use std::sync::LazyLock;
11use tokio_boring::SslStream;
12
13mod cache;
14mod v0;
15/// Hyper 1 support.
16#[cfg(feature = "hyper1")]
17pub mod v1;
18
19pub use self::v0::*;
20
21fn key_index() -> Result<Index<Ssl, SessionKey>, ErrorStack> {
22    static IDX: LazyLock<Index<Ssl, SessionKey>> = LazyLock::new(|| Ssl::new_ex_index().unwrap());
23    Ok(*IDX)
24}
25
26/// Settings for [`HttpsLayer`]
27pub struct HttpsLayerSettings {
28    session_cache_capacity: usize,
29}
30
31impl HttpsLayerSettings {
32    /// Constructs an [`HttpsLayerSettingsBuilder`] for configuring settings
33    #[must_use]
34    pub fn builder() -> HttpsLayerSettingsBuilder {
35        HttpsLayerSettingsBuilder(HttpsLayerSettings::default())
36    }
37}
38
39impl Default for HttpsLayerSettings {
40    fn default() -> Self {
41        Self {
42            session_cache_capacity: 8,
43        }
44    }
45}
46
47/// Builder for [`HttpsLayerSettings`]
48pub struct HttpsLayerSettingsBuilder(HttpsLayerSettings);
49
50impl HttpsLayerSettingsBuilder {
51    /// Sets maximum number of sessions to cache. Session capacity is per session key (domain).
52    /// Defaults to 8.
53    pub fn set_session_cache_capacity(&mut self, capacity: usize) {
54        self.0.session_cache_capacity = capacity;
55    }
56
57    /// Consumes the builder, returning a new [`HttpsLayerSettings`]
58    #[must_use]
59    pub fn build(self) -> HttpsLayerSettings {
60        self.0
61    }
62}
63
64/// A stream which may be wrapped with TLS.
65pub enum MaybeHttpsStream<T> {
66    /// A raw HTTP stream.
67    Http(T),
68    /// An SSL-wrapped HTTP stream.
69    Https(SslStream<T>),
70}
71
72impl<T> fmt::Debug for MaybeHttpsStream<T> {
73    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74        match *self {
75            MaybeHttpsStream::Http(..) => f.pad("Http(..)"),
76            MaybeHttpsStream::Https(..) => f.pad("Https(..)"),
77        }
78    }
79}