#[cfg(any(feature = "client", feature = "server"))]
use crate::error::{Error, ErrorKind};
pub use karmaio::tls::rustls;
pub const HTTP_11_ALPN: &[u8] = b"http/1.1";
#[derive(Clone, Debug)]
pub struct TlsInfo {
alpn_http_11: bool,
protocol_version: Option<rustls::ProtocolVersion>,
cipher_suite: Option<rustls::CipherSuite>,
handshake_kind: Option<rustls::HandshakeKind>,
server_name: Option<Box<str>>,
}
impl TlsInfo {
#[cfg(any(feature = "client", feature = "server"))]
pub(crate) fn new(
alpn_protocol: Option<&[u8]>,
protocol_version: Option<rustls::ProtocolVersion>,
cipher_suite: Option<rustls::SupportedCipherSuite>,
handshake_kind: Option<rustls::HandshakeKind>,
server_name: Option<&str>,
) -> Self {
Self {
alpn_http_11: alpn_protocol == Some(HTTP_11_ALPN),
protocol_version,
cipher_suite: cipher_suite.map(|suite| suite.suite()),
handshake_kind,
server_name: server_name.map(Into::into),
}
}
#[inline]
pub fn alpn_protocol(&self) -> Option<&'static [u8]> {
self.alpn_http_11.then_some(HTTP_11_ALPN)
}
#[inline]
pub const fn protocol_version(&self) -> Option<rustls::ProtocolVersion> {
self.protocol_version
}
#[inline]
pub const fn cipher_suite(&self) -> Option<rustls::CipherSuite> {
self.cipher_suite
}
#[inline]
pub const fn handshake_kind(&self) -> Option<rustls::HandshakeKind> {
self.handshake_kind
}
#[inline]
pub fn server_name(&self) -> Option<&str> {
self.server_name.as_deref()
}
}
#[cfg(any(feature = "client", feature = "server"))]
pub(crate) fn validate_alpn(protocol: Option<&[u8]>) -> Result<(), Error> {
match protocol {
None | Some(HTTP_11_ALPN) => Ok(()),
Some(_) => Err(Error::new(
ErrorKind::Unsupported,
"negotiated TLS protocol is not HTTP/1.1",
)),
}
}