@@ -26,6 +26,7 @@ use std::path::Path;
26
26
use std:: thread:: { self , JoinHandle } ;
27
27
28
28
use num_cpus;
29
+ use openssl:: ssl:: SslContext ;
29
30
30
31
pub use self :: request:: Request ;
31
32
pub use self :: response:: Response ;
@@ -50,14 +51,20 @@ pub mod response;
50
51
51
52
mod listener;
52
53
54
+ #[ derive( Debug ) ]
55
+ enum SslConfig < ' a > {
56
+ CertAndKey ( & ' a Path , & ' a Path ) ,
57
+ Context ( SslContext ) ,
58
+ }
59
+
53
60
/// A server can listen on a TCP socket.
54
61
///
55
62
/// Once listening, it will create a `Request`/`Response` pair for each
56
63
/// incoming connection, and hand them to the provided handler.
57
64
#[ derive( Debug ) ]
58
65
pub struct Server < ' a , H : Handler , L = HttpListener > {
59
66
handler : H ,
60
- ssl : Option < ( & ' a Path , & ' a Path ) > ,
67
+ ssl : Option < SslConfig < ' a > > ,
61
68
_marker : PhantomData < L >
62
69
}
63
70
@@ -90,7 +97,15 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
90
97
pub fn https ( handler : H , cert : & ' a Path , key : & ' a Path ) -> Server < ' a , H , HttpListener > {
91
98
Server {
92
99
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) ) ,
94
109
_marker : PhantomData
95
110
}
96
111
}
@@ -100,7 +115,8 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
100
115
/// Binds to a socket, and starts handling connections using a task pool.
101
116
pub fn listen_threads < T : ToSocketAddrs > ( self , addr : T , threads : usize ) -> HttpResult < Listening > {
102
117
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) ,
104
120
None => HttpListener :: http ( addr)
105
121
} ) ;
106
122
with_listener ( self . handler , listener, threads)
0 commit comments