Skip to content

Commit cbfb61f

Browse files
authored
Merge pull request #15 from fiag/tcp-options
Add TCP_NODELAY and TTL options.
2 parents b1cfe68 + bdfa648 commit cbfb61f

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//! tide tls listener built on async-tls and rustls
1+
//! tide tls listener built on async-rustls and rustls
22
//!
33
//!
44
//! # Example
55
//! ```rust
66
//! # use tide_rustls::TlsListener;
7-
//! fn main() -> tide::Result<()> { async_std::task::block_on(async {
7+
//! # fn main() -> tide::Result<()> { async_std::task::block_on(async {
88
//! let mut app = tide::new();
99
//! app.at("/").get(|_| async { Ok("Hello tls") });
1010
//! # if false {
@@ -15,8 +15,7 @@
1515
//! .key(std::env::var("TIDE_KEY_PATH").unwrap()),
1616
//! )
1717
//! .await?;
18-
//! # }
19-
//! # Ok(()) }) }
18+
//! # } Ok(()) }) }
2019
//! ```
2120
#![forbid(unsafe_code, future_incompatible)]
2221
#![deny(

src/tls_listener.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub struct TlsListener<State> {
2727
connection: TcpConnection,
2828
config: TlsListenerConfig,
2929
server: Option<Server<State>>,
30+
tcp_nodelay: Option<bool>,
31+
tcp_ttl: Option<u32>,
3032
}
3133

3234
impl<State> Debug for TlsListener<State> {
@@ -42,16 +44,25 @@ impl<State> Debug for TlsListener<State> {
4244
&"None"
4345
},
4446
)
47+
.field("tcp_ttl", &self.tcp_ttl)
48+
.field("tcp_nodelay", &self.tcp_nodelay)
4549
.finish()
4650
}
4751
}
4852

4953
impl<State> TlsListener<State> {
50-
pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig) -> Self {
54+
pub(crate) fn new(
55+
connection: TcpConnection,
56+
config: TlsListenerConfig,
57+
tcp_nodelay: Option<bool>,
58+
tcp_ttl: Option<u32>,
59+
) -> Self {
5160
Self {
5261
connection,
5362
config,
5463
server: None,
64+
tcp_nodelay,
65+
tcp_ttl,
5566
}
5667
}
5768
/// The primary entrypoint to create a TlsListener. See
@@ -203,7 +214,17 @@ impl<State: Clone + Send + Sync + 'static> Listener<State> for TlsListener<State
203214
continue;
204215
}
205216

206-
Ok(stream) => handle_tls(server.clone(), stream, acceptor.clone()),
217+
Ok(stream) => {
218+
if let Some(nodelay) = self.tcp_nodelay {
219+
stream.set_nodelay(nodelay)?;
220+
}
221+
222+
if let Some(ttl) = self.tcp_ttl {
223+
stream.set_ttl(ttl)?;
224+
}
225+
226+
handle_tls(server.clone(), stream, acceptor.clone())
227+
}
207228
};
208229
}
209230
Ok(())

src/tls_listener_builder.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,27 @@ use std::sync::Arc;
3535
/// .config(rustls::ServerConfig::new(rustls::NoClientAuth::new()))
3636
/// .finish();
3737
/// ```
38+
///
39+
/// ```rust
40+
/// # use tide_rustls::TlsListener;
41+
/// let listener = TlsListener::<()>::build()
42+
/// .addrs("localhost:4433")
43+
/// .cert("./tls/localhost-4433.cert")
44+
/// .key("./tls/localhost-4433.key")
45+
/// .tcp_ttl(60)
46+
/// .tcp_nodelay(true)
47+
/// .finish();
48+
/// ```
49+
3850
pub struct TlsListenerBuilder<State> {
3951
key: Option<PathBuf>,
4052
cert: Option<PathBuf>,
4153
config: Option<ServerConfig>,
4254
tls_acceptor: Option<Arc<dyn CustomTlsAcceptor>>,
4355
tcp: Option<TcpListener>,
4456
addrs: Option<Vec<SocketAddr>>,
57+
tcp_nodelay: Option<bool>,
58+
tcp_ttl: Option<u32>,
4559
_state: PhantomData<State>,
4660
}
4761

@@ -54,6 +68,8 @@ impl<State> Default for TlsListenerBuilder<State> {
5468
tls_acceptor: None,
5569
tcp: None,
5670
addrs: None,
71+
tcp_nodelay: None,
72+
tcp_ttl: None,
5773
_state: PhantomData,
5874
}
5975
}
@@ -82,6 +98,8 @@ impl<State> std::fmt::Debug for TlsListenerBuilder<State> {
8298
)
8399
.field("tcp", &self.tcp)
84100
.field("addrs", &self.addrs)
101+
.field("tcp_nodelay", &self.tcp_nodelay)
102+
.field("tcp_ttl", &self.tcp_ttl)
85103
.finish()
86104
}
87105
}
@@ -148,6 +166,18 @@ impl<State> TlsListenerBuilder<State> {
148166
self
149167
}
150168

169+
/// Provides a TCP_NODELAY option for this tls listener.
170+
pub fn tcp_nodelay(mut self, nodelay: bool) -> Self {
171+
self.tcp_nodelay = Some(nodelay);
172+
self
173+
}
174+
175+
/// Provides a TTL option for this tls listener, in seconds.
176+
pub fn tcp_ttl(mut self, ttl: u32) -> Self {
177+
self.tcp_ttl = Some(ttl);
178+
self
179+
}
180+
151181
/// finishes building a TlsListener from this TlsListenerBuilder.
152182
///
153183
/// # Errors
@@ -168,6 +198,8 @@ impl<State> TlsListenerBuilder<State> {
168198
tls_acceptor,
169199
tcp,
170200
addrs,
201+
tcp_nodelay,
202+
tcp_ttl,
171203
..
172204
} = self;
173205

@@ -194,6 +226,6 @@ impl<State> TlsListenerBuilder<State> {
194226
}
195227
};
196228

197-
Ok(TlsListener::new(connection, config))
229+
Ok(TlsListener::new(connection, config, tcp_nodelay, tcp_ttl))
198230
}
199231
}

0 commit comments

Comments
 (0)