Skip to main content

hyper_boring/
lib.rs

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