Skip to content

Commit 3a1a242

Browse files
committed
feat(server): allow consumer to supply an SslContext
Closes #471
1 parent fef04d2 commit 3a1a242

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/server/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::path::Path;
2626
use std::thread::{self, JoinHandle};
2727

2828
use num_cpus;
29+
use openssl::ssl::SslContext;
2930

3031
pub use self::request::Request;
3132
pub use self::response::Response;
@@ -50,14 +51,20 @@ pub mod response;
5051

5152
mod listener;
5253

54+
#[derive(Debug)]
55+
enum SslConfig<'a> {
56+
CertAndKey(&'a Path, &'a Path),
57+
Context(SslContext),
58+
}
59+
5360
/// A server can listen on a TCP socket.
5461
///
5562
/// Once listening, it will create a `Request`/`Response` pair for each
5663
/// incoming connection, and hand them to the provided handler.
5764
#[derive(Debug)]
5865
pub struct Server<'a, H: Handler, L = HttpListener> {
5966
handler: H,
60-
ssl: Option<(&'a Path, &'a Path)>,
67+
ssl: Option<SslConfig<'a>>,
6168
_marker: PhantomData<L>
6269
}
6370

@@ -90,7 +97,15 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
9097
pub fn https(handler: H, cert: &'a Path, key: &'a Path) -> Server<'a, H, HttpListener> {
9198
Server {
9299
handler: handler,
93-
ssl: Some((cert, key)),
100+
ssl: Some(SslConfig::CertAndKey(cert, key)),
101+
_marker: PhantomData
102+
}
103+
}
104+
/// Creates a new server that will handler `HttpStreams`s using a TLS connection defined by an SslContext.
105+
pub fn https_with_context(handler: H, ssl_context: SslContext) -> Server<'a, H, HttpListener> {
106+
Server {
107+
handler: handler,
108+
ssl: Some(SslConfig::Context(ssl_context)),
94109
_marker: PhantomData
95110
}
96111
}
@@ -100,7 +115,8 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
100115
/// Binds to a socket, and starts handling connections using a task pool.
101116
pub fn listen_threads<T: ToSocketAddrs>(self, addr: T, threads: usize) -> HttpResult<Listening> {
102117
let listener = try!(match self.ssl {
103-
Some((cert, key)) => HttpListener::https(addr, cert, key),
118+
Some(SslConfig::CertAndKey(cert, key)) => HttpListener::https(addr, cert, key),
119+
Some(SslConfig::Context(ssl_context)) => HttpListener::https_with_context(addr, ssl_context),
104120
None => HttpListener::http(addr)
105121
});
106122
with_listener(self.handler, listener, threads)

0 commit comments

Comments
 (0)