use std::sync::Arc;
use futures_util::{stream::BoxStream, StreamExt};
use http::Request;
use hyper::body::Incoming;
use rustls::{ServerConfig, ServerConnection};
pub trait TlsConfigurer {
fn is_dynamic(&self) -> bool;
fn into_stream(self) -> BoxStream<'static, Arc<ServerConfig>>;
}
impl TlsConfigurer for () {
fn is_dynamic(&self) -> bool {
false
}
fn into_stream(self) -> BoxStream<'static, Arc<ServerConfig>> {
futures_util::stream::empty().boxed()
}
}
impl TlsConfigurer for rustls::server::ServerConfig {
fn is_dynamic(&self) -> bool {
false
}
fn into_stream(self) -> BoxStream<'static, Arc<ServerConfig>> {
futures_util::stream::iter([Arc::new(self)]).boxed()
}
}
impl TlsConfigurer for Arc<rustls::server::ServerConfig> {
fn is_dynamic(&self) -> bool {
false
}
fn into_stream(self) -> BoxStream<'static, Arc<ServerConfig>> {
futures_util::stream::iter([self]).boxed()
}
}
impl TlsConfigurer for Arc<dyn Fn() -> Arc<rustls::server::ServerConfig> + Send + Sync> {
fn is_dynamic(&self) -> bool {
false
}
fn into_stream(self) -> BoxStream<'static, Arc<ServerConfig>> {
futures_util::stream::once(async move { (self)() }).boxed()
}
}
impl TlsConfigurer for BoxStream<'static, Arc<ServerConfig>> {
fn is_dynamic(&self) -> bool {
true
}
fn into_stream(self) -> BoxStream<'static, Arc<ServerConfig>> {
self
}
}
pub trait TlsConnectionMiddleware: Clone + Send + 'static {
type Data: Send;
fn data(&self, connection: &ServerConnection) -> Self::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) {}
}