document new items

This commit is contained in:
Rob Ede 2022-06-27 03:04:57 +01:00
parent d64f526e6a
commit f1b3b532b7
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 27 additions and 17 deletions

View File

@ -25,6 +25,7 @@
)] )]
#![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_logo_url = "https://actix.rs/img/logo.png")]
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub use ::http::{uri, uri::Uri}; pub use ::http::{uri, uri::Uri};
pub use ::http::{Method, StatusCode, Version}; pub use ::http::{Method, StatusCode, Version};

View File

@ -181,17 +181,17 @@ where
} }
} }
/// Configuration options used when accepting TLS connection.
#[cfg(any(feature = "openssl", feature = "rustls"))] #[cfg(any(feature = "openssl", feature = "rustls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "openssl", feature = "rustls"))))]
#[derive(Debug, Default)]
pub struct TlsAcceptorConfig { pub struct TlsAcceptorConfig {
pub(crate) handshake_timeout: Option<std::time::Duration>, pub(crate) handshake_timeout: Option<std::time::Duration>,
} }
#[cfg(any(feature = "openssl", feature = "rustls"))] #[cfg(any(feature = "openssl", feature = "rustls"))]
impl TlsAcceptorConfig { impl TlsAcceptorConfig {
pub fn new(handshake_timeout: Option<std::time::Duration>) -> Self { /// Set TLS handshake timeout duration.
Self { handshake_timeout }
}
pub fn handshake_timeout(self, dur: std::time::Duration) -> Self { pub fn handshake_timeout(self, dur: std::time::Duration) -> Self {
Self { Self {
handshake_timeout: Some(dur), handshake_timeout: Some(dur),
@ -249,10 +249,10 @@ mod openssl {
Error = TlsError<SslError, DispatchError>, Error = TlsError<SslError, DispatchError>,
InitError = (), InitError = (),
> { > {
self.openssl_with_config(acceptor, TlsAcceptorConfig::new(None)) self.openssl_with_config(acceptor, TlsAcceptorConfig::default())
} }
/// Create OpenSSL based service with configuration. /// Create OpenSSL based service with custom TLS acceptor configuration.
pub fn openssl_with_config( pub fn openssl_with_config(
self, self,
acceptor: SslAcceptor, acceptor: SslAcceptor,
@ -265,6 +265,7 @@ mod openssl {
InitError = (), InitError = (),
> { > {
let mut acceptor = Acceptor::new(acceptor); let mut acceptor = Acceptor::new(acceptor);
if let Some(handshake_timeout) = tls_acceptor_config.handshake_timeout { if let Some(handshake_timeout) = tls_acceptor_config.handshake_timeout {
acceptor.set_handshake_timeout(handshake_timeout); acceptor.set_handshake_timeout(handshake_timeout);
} }
@ -341,10 +342,10 @@ mod rustls {
Error = TlsError<io::Error, DispatchError>, Error = TlsError<io::Error, DispatchError>,
InitError = (), InitError = (),
> { > {
self.rustls_with_config(config, TlsAcceptorConfig::new(None)) self.rustls_with_config(config, TlsAcceptorConfig::default())
} }
/// Create Rustls based service with configuration. /// Create Rustls based service with custom TLS acceptor configuration.
pub fn rustls_with_config( pub fn rustls_with_config(
self, self,
mut config: ServerConfig, mut config: ServerConfig,
@ -361,9 +362,11 @@ mod rustls {
config.alpn_protocols = protos; config.alpn_protocols = protos;
let mut acceptor = Acceptor::new(config); let mut acceptor = Acceptor::new(config);
if let Some(handshake_timeout) = tls_acceptor_config.handshake_timeout { if let Some(handshake_timeout) = tls_acceptor_config.handshake_timeout {
acceptor.set_handshake_timeout(handshake_timeout); acceptor.set_handshake_timeout(handshake_timeout);
} }
acceptor acceptor
.map_init_err(|_| { .map_init_err(|_| {
unreachable!("TLS acceptor service factory does not error on init") unreachable!("TLS acceptor service factory does not error on init")

View File

@ -232,19 +232,21 @@ where
self self
} }
#[cfg(any(feature = "openssl", feature = "rustls"))]
/// Set TLS handshake timeout. /// Set TLS handshake timeout.
/// ///
/// Defines a timeout for TLS handshake. If the TLS handshake does not complete /// Defines a timeout for TLS handshake. If the TLS handshake does not complete
/// within this time, the connection is closed. /// within this time, the connection is closed.
/// ///
/// By default handshake timeout is set to 3000 milliseconds. /// By default handshake timeout is set to 3000 milliseconds.
#[cfg(any(feature = "openssl", feature = "rustls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "openssl", feature = "rustls"))))]
pub fn tls_handshake_timeout(self, dur: Duration) -> Self { pub fn tls_handshake_timeout(self, dur: Duration) -> Self {
self.config self.config
.lock() .lock()
.unwrap() .unwrap()
.tls_handshake_timeout .tls_handshake_timeout
.replace(dur); .replace(dur);
self self
} }
@ -399,13 +401,15 @@ where
.into_factory() .into_factory()
.map_err(|err| err.into().error_response()); .map_err(|err| err.into().error_response());
let acceptor_config = match c.tls_handshake_timeout {
Some(dur) => TlsAcceptorConfig::default().handshake_timeout(dur),
None => TlsAcceptorConfig::default(),
};
svc.finish(map_config(fac, move |_| { svc.finish(map_config(fac, move |_| {
AppConfig::new(true, host.clone(), addr) AppConfig::new(true, host.clone(), addr)
})) }))
.openssl_with_config( .openssl_with_config(acceptor.clone(), acceptor_config)
acceptor.clone(),
TlsAcceptorConfig::new(c.tls_handshake_timeout),
)
})?; })?;
Ok(self) Ok(self)
@ -460,13 +464,15 @@ where
.into_factory() .into_factory()
.map_err(|err| err.into().error_response()); .map_err(|err| err.into().error_response());
let acceptor_config = match c.tls_handshake_timeout {
Some(dur) => TlsAcceptorConfig::default().handshake_timeout(dur),
None => TlsAcceptorConfig::default(),
};
svc.finish(map_config(fac, move |_| { svc.finish(map_config(fac, move |_| {
AppConfig::new(true, host.clone(), addr) AppConfig::new(true, host.clone(), addr)
})) }))
.rustls_with_config( .rustls_with_config(config.clone(), acceptor_config)
config.clone(),
TlsAcceptorConfig::new(c.tls_handshake_timeout),
)
})?; })?;
Ok(self) Ok(self)