Skip to content

Commit

Permalink
Merge pull request #15 from fiag/tcp-options
Browse files Browse the repository at this point in the history
Add TCP_NODELAY and TTL options.
  • Loading branch information
jbr authored May 25, 2021
2 parents b1cfe68 + bdfa648 commit cbfb61f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! tide tls listener built on async-tls and rustls
//! tide tls listener built on async-rustls and rustls
//!
//!
//! # Example
//! ```rust
//! # use tide_rustls::TlsListener;
//! fn main() -> tide::Result<()> { async_std::task::block_on(async {
//! # fn main() -> tide::Result<()> { async_std::task::block_on(async {
//! let mut app = tide::new();
//! app.at("/").get(|_| async { Ok("Hello tls") });
//! # if false {
Expand All @@ -15,8 +15,7 @@
//! .key(std::env::var("TIDE_KEY_PATH").unwrap()),
//! )
//! .await?;
//! # }
//! # Ok(()) }) }
//! # } Ok(()) }) }
//! ```
#![forbid(unsafe_code, future_incompatible)]
#![deny(
Expand Down
25 changes: 23 additions & 2 deletions src/tls_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub struct TlsListener<State> {
connection: TcpConnection,
config: TlsListenerConfig,
server: Option<Server<State>>,
tcp_nodelay: Option<bool>,
tcp_ttl: Option<u32>,
}

impl<State> Debug for TlsListener<State> {
Expand All @@ -42,16 +44,25 @@ impl<State> Debug for TlsListener<State> {
&"None"
},
)
.field("tcp_ttl", &self.tcp_ttl)
.field("tcp_nodelay", &self.tcp_nodelay)
.finish()
}
}

impl<State> TlsListener<State> {
pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig) -> Self {
pub(crate) fn new(
connection: TcpConnection,
config: TlsListenerConfig,
tcp_nodelay: Option<bool>,
tcp_ttl: Option<u32>,
) -> Self {
Self {
connection,
config,
server: None,
tcp_nodelay,
tcp_ttl,
}
}
/// The primary entrypoint to create a TlsListener. See
Expand Down Expand Up @@ -203,7 +214,17 @@ impl<State: Clone + Send + Sync + 'static> Listener<State> for TlsListener<State
continue;
}

Ok(stream) => handle_tls(server.clone(), stream, acceptor.clone()),
Ok(stream) => {
if let Some(nodelay) = self.tcp_nodelay {
stream.set_nodelay(nodelay)?;
}

if let Some(ttl) = self.tcp_ttl {
stream.set_ttl(ttl)?;
}

handle_tls(server.clone(), stream, acceptor.clone())
}
};
}
Ok(())
Expand Down
34 changes: 33 additions & 1 deletion src/tls_listener_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,27 @@ use std::sync::Arc;
/// .config(rustls::ServerConfig::new(rustls::NoClientAuth::new()))
/// .finish();
/// ```
///
/// ```rust
/// # use tide_rustls::TlsListener;
/// let listener = TlsListener::<()>::build()
/// .addrs("localhost:4433")
/// .cert("./tls/localhost-4433.cert")
/// .key("./tls/localhost-4433.key")
/// .tcp_ttl(60)
/// .tcp_nodelay(true)
/// .finish();
/// ```
pub struct TlsListenerBuilder<State> {
key: Option<PathBuf>,
cert: Option<PathBuf>,
config: Option<ServerConfig>,
tls_acceptor: Option<Arc<dyn CustomTlsAcceptor>>,
tcp: Option<TcpListener>,
addrs: Option<Vec<SocketAddr>>,
tcp_nodelay: Option<bool>,
tcp_ttl: Option<u32>,
_state: PhantomData<State>,
}

Expand All @@ -54,6 +68,8 @@ impl<State> Default for TlsListenerBuilder<State> {
tls_acceptor: None,
tcp: None,
addrs: None,
tcp_nodelay: None,
tcp_ttl: None,
_state: PhantomData,
}
}
Expand Down Expand Up @@ -82,6 +98,8 @@ impl<State> std::fmt::Debug for TlsListenerBuilder<State> {
)
.field("tcp", &self.tcp)
.field("addrs", &self.addrs)
.field("tcp_nodelay", &self.tcp_nodelay)
.field("tcp_ttl", &self.tcp_ttl)
.finish()
}
}
Expand Down Expand Up @@ -148,6 +166,18 @@ impl<State> TlsListenerBuilder<State> {
self
}

/// Provides a TCP_NODELAY option for this tls listener.
pub fn tcp_nodelay(mut self, nodelay: bool) -> Self {
self.tcp_nodelay = Some(nodelay);
self
}

/// Provides a TTL option for this tls listener, in seconds.
pub fn tcp_ttl(mut self, ttl: u32) -> Self {
self.tcp_ttl = Some(ttl);
self
}

/// finishes building a TlsListener from this TlsListenerBuilder.
///
/// # Errors
Expand All @@ -168,6 +198,8 @@ impl<State> TlsListenerBuilder<State> {
tls_acceptor,
tcp,
addrs,
tcp_nodelay,
tcp_ttl,
..
} = self;

Expand All @@ -194,6 +226,6 @@ impl<State> TlsListenerBuilder<State> {
}
};

Ok(TlsListener::new(connection, config))
Ok(TlsListener::new(connection, config, tcp_nodelay, tcp_ttl))
}
}

0 comments on commit cbfb61f

Please sign in to comment.