#![cfg(feature = "tls")]
use crate::pem::{load_certs, load_key};
use std::io;
use std::sync::Arc;
use tokio_rustls::rustls::ServerConfig;
use tokio_rustls::TlsAcceptor;
pub fn acceptor_from_pem(cert_path: &str, key_path: &str) -> io::Result<TlsAcceptor> {
let certs = load_certs(cert_path)?;
let key = load_key(key_path)?;
let mut config = ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(certs, key)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
Ok(TlsAcceptor::from(Arc::new(config)))
}
#[cfg(all(test, feature = "tls"))]
mod tests {
use super::*;
#[test]
fn missing_files_error_cleanly() {
match acceptor_from_pem("/nonexistent/cert.pem", "/nonexistent/key.pem") {
Ok(_) => panic!("expected an error for nonexistent cert/key files"),
Err(err) => assert_eq!(err.kind(), std::io::ErrorKind::NotFound),
}
}
}