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<Result<Index<Ssl, SessionKey>, ErrorStack>> =
19        LazyLock::new(Ssl::new_ex_index);
20    IDX.clone()
21}
22
23/// Settings for [`HttpsLayer`]
24pub struct HttpsLayerSettings {
25    session_cache_capacity: usize,
26}
27
28impl HttpsLayerSettings {
29    /// Constructs an [`HttpsLayerSettingsBuilder`] for configuring settings
30    #[must_use]
31    pub fn builder() -> HttpsLayerSettingsBuilder {
32        HttpsLayerSettingsBuilder(HttpsLayerSettings::default())
33    }
34}
35
36impl Default for HttpsLayerSettings {
37    fn default() -> Self {
38        Self {
39            session_cache_capacity: 8,
40        }
41    }
42}
43
44/// Builder for [`HttpsLayerSettings`]
45pub struct HttpsLayerSettingsBuilder(HttpsLayerSettings);
46
47impl HttpsLayerSettingsBuilder {
48    /// Sets maximum number of sessions to cache. Session capacity is per session key (domain).
49    /// Defaults to 8.
50    pub fn set_session_cache_capacity(&mut self, capacity: usize) {
51        self.0.session_cache_capacity = capacity;
52    }
53
54    /// Consumes the builder, returning a new [`HttpsLayerSettings`]
55    #[must_use]
56    pub fn build(self) -> HttpsLayerSettings {
57        self.0
58    }
59}
60
61/// A stream which may be wrapped with TLS.
62pub enum MaybeHttpsStream<T> {
63    /// A raw HTTP stream.
64    Http(T),
65    /// An SSL-wrapped HTTP stream.
66    Https(SslStream<T>),
67}
68
69impl<T> fmt::Debug for MaybeHttpsStream<T> {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        match *self {
72            MaybeHttpsStream::Http(..) => f.pad("Http(..)"),
73            MaybeHttpsStream::Https(..) => f.pad("Https(..)"),
74        }
75    }
76}