@@ -9,7 +9,7 @@ use std::sync::Arc;
9
9
10
10
use openssl:: ssl:: { Ssl , SslStream , SslContext , SSL_VERIFY_NONE } ;
11
11
use openssl:: ssl:: SslMethod :: Sslv23 ;
12
- use openssl:: ssl:: error:: { SslError , StreamError , OpenSslErrors , SslSessionClosed } ;
12
+ use openssl:: ssl:: error:: StreamError as SslIoError ;
13
13
use openssl:: x509:: X509FileType ;
14
14
15
15
use typeable:: Typeable ;
@@ -29,7 +29,7 @@ pub trait NetworkListener: Clone {
29
29
//fn listen<To: ToSocketAddrs>(&mut self, addr: To) -> io::Result<Self::Acceptor>;
30
30
31
31
/// Returns an iterator of streams.
32
- fn accept ( & mut self ) -> io :: Result < Self :: Stream > ;
32
+ fn accept ( & mut self ) -> :: Result < Self :: Stream > ;
33
33
34
34
/// Get the address this Listener ended up listening on.
35
35
fn local_addr ( & mut self ) -> io:: Result < SocketAddr > ;
@@ -47,8 +47,8 @@ pub trait NetworkListener: Clone {
47
47
pub struct NetworkConnections < ' a , N : NetworkListener + ' a > ( & ' a mut N ) ;
48
48
49
49
impl < ' a , N : NetworkListener + ' a > Iterator for NetworkConnections < ' a , N > {
50
- type Item = io :: Result < N :: Stream > ;
51
- fn next ( & mut self ) -> Option < io :: Result < N :: Stream > > {
50
+ type Item = :: Result < N :: Stream > ;
51
+ fn next ( & mut self ) -> Option < :: Result < N :: Stream > > {
52
52
Some ( self . 0 . accept ( ) )
53
53
}
54
54
}
@@ -58,6 +58,7 @@ pub trait NetworkStream: Read + Write + Any + Send + Typeable {
58
58
/// Get the remote address of the underlying connection.
59
59
fn peer_addr ( & mut self ) -> io:: Result < SocketAddr > ;
60
60
/// This will be called when Stream should no longer be kept alive.
61
+ #[ inline]
61
62
fn close ( & mut self , _how : Shutdown ) -> io:: Result < ( ) > {
62
63
Ok ( ( ) )
63
64
}
@@ -68,7 +69,7 @@ pub trait NetworkConnector {
68
69
/// Type of Stream to create
69
70
type Stream : Into < Box < NetworkStream + Send > > ;
70
71
/// Connect to a remote address.
71
- fn connect ( & mut self , host : & str , port : u16 , scheme : & str ) -> io :: Result < Self :: Stream > ;
72
+ fn connect ( & mut self , host : & str , port : u16 , scheme : & str ) -> :: Result < Self :: Stream > ;
72
73
}
73
74
74
75
impl < T : NetworkStream + Send > From < T > for Box < NetworkStream + Send > {
@@ -158,22 +159,22 @@ impl Clone for HttpListener {
158
159
impl HttpListener {
159
160
160
161
/// Start listening to an address over HTTP.
161
- pub fn http < To : ToSocketAddrs > ( addr : To ) -> io :: Result < HttpListener > {
162
+ pub fn http < To : ToSocketAddrs > ( addr : To ) -> :: Result < HttpListener > {
162
163
Ok ( HttpListener :: Http ( try!( TcpListener :: bind ( addr) ) ) )
163
164
}
164
165
165
166
/// Start listening to an address over HTTPS.
166
- pub fn https < To : ToSocketAddrs > ( addr : To , cert : & Path , key : & Path ) -> io :: Result < HttpListener > {
167
- let mut ssl_context = try!( SslContext :: new ( Sslv23 ) . map_err ( lift_ssl_error ) ) ;
168
- try!( ssl_context. set_cipher_list ( "DEFAULT" ) . map_err ( lift_ssl_error ) ) ;
169
- try!( ssl_context. set_certificate_file ( cert, X509FileType :: PEM ) . map_err ( lift_ssl_error ) ) ;
170
- try!( ssl_context. set_private_key_file ( key, X509FileType :: PEM ) . map_err ( lift_ssl_error ) ) ;
167
+ pub fn https < To : ToSocketAddrs > ( addr : To , cert : & Path , key : & Path ) -> :: Result < HttpListener > {
168
+ let mut ssl_context = try!( SslContext :: new ( Sslv23 ) ) ;
169
+ try!( ssl_context. set_cipher_list ( "DEFAULT" ) ) ;
170
+ try!( ssl_context. set_certificate_file ( cert, X509FileType :: PEM ) ) ;
171
+ try!( ssl_context. set_private_key_file ( key, X509FileType :: PEM ) ) ;
171
172
ssl_context. set_verify ( SSL_VERIFY_NONE , None ) ;
172
173
HttpListener :: https_with_context ( addr, ssl_context)
173
174
}
174
175
175
176
/// Start listening to an address of HTTPS using the given SslContext
176
- pub fn https_with_context < To : ToSocketAddrs > ( addr : To , ssl_context : SslContext ) -> io :: Result < HttpListener > {
177
+ pub fn https_with_context < To : ToSocketAddrs > ( addr : To , ssl_context : SslContext ) -> :: Result < HttpListener > {
177
178
Ok ( HttpListener :: Https ( try!( TcpListener :: bind ( addr) ) , Arc :: new ( ssl_context) ) )
178
179
}
179
180
}
@@ -182,21 +183,20 @@ impl NetworkListener for HttpListener {
182
183
type Stream = HttpStream ;
183
184
184
185
#[ inline]
185
- fn accept ( & mut self ) -> io :: Result < HttpStream > {
186
- Ok ( match * self {
187
- HttpListener :: Http ( ref mut tcp) => HttpStream :: Http ( CloneTcpStream ( try!( tcp. accept ( ) ) . 0 ) ) ,
186
+ fn accept ( & mut self ) -> :: Result < HttpStream > {
187
+ match * self {
188
+ HttpListener :: Http ( ref mut tcp) => Ok ( HttpStream :: Http ( CloneTcpStream ( try!( tcp. accept ( ) ) . 0 ) ) ) ,
188
189
HttpListener :: Https ( ref mut tcp, ref ssl_context) => {
189
190
let stream = CloneTcpStream ( try!( tcp. accept ( ) ) . 0 ) ;
190
191
match SslStream :: new_server ( & * * ssl_context, stream) {
191
- Ok ( ssl_stream) => HttpStream :: Https ( ssl_stream) ,
192
- Err ( StreamError ( e) ) => {
193
- return Err ( io:: Error :: new ( io:: ErrorKind :: ConnectionAborted ,
194
- e) ) ;
192
+ Ok ( ssl_stream) => Ok ( HttpStream :: Https ( ssl_stream) ) ,
193
+ Err ( SslIoError ( e) ) => {
194
+ Err ( io:: Error :: new ( io:: ErrorKind :: ConnectionAborted , e) . into ( ) )
195
195
} ,
196
- Err ( e) => return Err ( lift_ssl_error ( e ) )
196
+ Err ( e) => Err ( e . into ( ) )
197
197
}
198
198
}
199
- } )
199
+ }
200
200
}
201
201
202
202
#[ inline]
@@ -308,40 +308,30 @@ pub type ContextVerifier = Box<FnMut(&mut SslContext) -> () + Send>;
308
308
impl NetworkConnector for HttpConnector {
309
309
type Stream = HttpStream ;
310
310
311
- fn connect ( & mut self , host : & str , port : u16 , scheme : & str ) -> io :: Result < HttpStream > {
311
+ fn connect ( & mut self , host : & str , port : u16 , scheme : & str ) -> :: Result < HttpStream > {
312
312
let addr = & ( host, port) ;
313
- match scheme {
313
+ Ok ( try! ( match scheme {
314
314
"http" => {
315
315
debug ! ( "http scheme" ) ;
316
316
Ok ( HttpStream :: Http ( CloneTcpStream ( try!( TcpStream :: connect ( addr) ) ) ) )
317
317
} ,
318
318
"https" => {
319
319
debug ! ( "https scheme" ) ;
320
320
let stream = CloneTcpStream ( try!( TcpStream :: connect ( addr) ) ) ;
321
- let mut context = try!( SslContext :: new ( Sslv23 ) . map_err ( lift_ssl_error ) ) ;
321
+ let mut context = try!( SslContext :: new ( Sslv23 ) ) ;
322
322
if let Some ( ref mut verifier) = self . 0 {
323
323
verifier ( & mut context) ;
324
324
}
325
- let ssl = try!( Ssl :: new ( & context) . map_err ( lift_ssl_error ) ) ;
326
- try!( ssl. set_hostname ( host) . map_err ( lift_ssl_error ) ) ;
327
- let stream = try!( SslStream :: new ( & context, stream) . map_err ( lift_ssl_error ) ) ;
325
+ let ssl = try!( Ssl :: new ( & context) ) ;
326
+ try!( ssl. set_hostname ( host) ) ;
327
+ let stream = try!( SslStream :: new ( & context, stream) ) ;
328
328
Ok ( HttpStream :: Https ( stream) )
329
329
} ,
330
330
_ => {
331
331
Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
332
332
"Invalid scheme for Http" ) )
333
333
}
334
- }
335
- }
336
- }
337
-
338
- fn lift_ssl_error ( ssl : SslError ) -> io:: Error {
339
- debug ! ( "lift_ssl_error: {:?}" , ssl) ;
340
- match ssl {
341
- StreamError ( err) => err,
342
- SslSessionClosed => io:: Error :: new ( io:: ErrorKind :: ConnectionAborted ,
343
- "SSL Connection Closed" ) ,
344
- e@OpenSslErrors ( ..) => io:: Error :: new ( io:: ErrorKind :: Other , e)
334
+ } ) )
345
335
}
346
336
}
347
337
0 commit comments