tower_server/
tls.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use http::Request;
use hyper::body::Incoming;
use rustls::ServerConnection;

/// A middleware for Tls connections.
///
/// This middleware is implemented in two steps, first a data extraction step taking a [ServerConnection],
/// then a HTTP request middleware runs with that data as a parameter.
pub trait TlsConnectionMiddleware: Clone + Send + 'static {
    /// The data extracted from the rustls ServerConnection.
    type Data: Send;

    /// Extract data from a [ServerConnection].
    fn data(&self, connection: &ServerConnection) -> Self::Data;

    /// Call middleware with extracted data.
    fn call(&self, req: &mut Request<Incoming>, data: &Self::Data);
}

#[derive(Clone, Copy)]
pub struct NoOpTlsConnectionMiddleware;

impl TlsConnectionMiddleware for NoOpTlsConnectionMiddleware {
    type Data = ();

    fn data(&self, _connection: &ServerConnection) -> Self::Data {}

    fn call(&self, _req: &mut Request<Incoming>, _data: &Self::Data) {}
}