@@ -51,12 +51,15 @@ impl<'a, N: NetworkListener + 'a> Iterator for NetworkConnections<'a, N> {
51
51
pub trait NetworkStream : Read + Write + Any + Send + Typeable {
52
52
/// Get the remote address of the underlying connection.
53
53
fn peer_addr ( & mut self ) -> io:: Result < SocketAddr > ;
54
+
54
55
/// Set the maximum time to wait for a read to complete.
55
56
#[ cfg( feature = "timeouts" ) ]
56
57
fn set_read_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > ;
58
+
57
59
/// Set the maximum time to wait for a write to complete.
58
60
#[ cfg( feature = "timeouts" ) ]
59
61
fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > ;
62
+
60
63
/// This will be called when Stream should no longer be kept alive.
61
64
#[ inline]
62
65
fn close ( & mut self , _how : Shutdown ) -> io:: Result < ( ) > {
@@ -66,9 +69,8 @@ pub trait NetworkStream: Read + Write + Any + Send + Typeable {
66
69
// Unsure about name and implementation...
67
70
68
71
#[ doc( hidden) ]
69
- fn set_previous_response_expected_no_content ( & mut self , _expected : bool ) {
70
-
71
- }
72
+ fn set_previous_response_expected_no_content ( & mut self , _expected : bool ) { }
73
+
72
74
#[ doc( hidden) ]
73
75
fn previous_response_expected_no_content ( & self ) -> bool {
74
76
false
@@ -79,6 +81,7 @@ pub trait NetworkStream: Read + Write + Any + Send + Typeable {
79
81
pub trait NetworkConnector {
80
82
/// Type of Stream to create
81
83
type Stream : Into < Box < NetworkStream + Send > > ;
84
+
82
85
/// Connect to a remote address.
83
86
fn connect ( & self , host : & str , port : u16 , scheme : & str ) -> :: Result < Self :: Stream > ;
84
87
}
@@ -215,13 +218,17 @@ impl Clone for HttpListener {
215
218
}
216
219
}
217
220
218
- impl HttpListener {
221
+ impl From < TcpListener > for HttpListener {
222
+ fn from ( listener : TcpListener ) -> HttpListener {
223
+ HttpListener ( listener)
224
+ }
225
+ }
219
226
227
+ impl HttpListener {
220
228
/// Start listening to an address over HTTP.
221
229
pub fn new < To : ToSocketAddrs > ( addr : To ) -> :: Result < HttpListener > {
222
230
Ok ( HttpListener ( try!( TcpListener :: bind ( addr) ) ) )
223
231
}
224
-
225
232
}
226
233
227
234
impl NetworkListener for HttpListener {
@@ -382,17 +389,17 @@ impl NetworkConnector for HttpConnector {
382
389
/// A closure as a connector used to generate TcpStreams per request
383
390
///
384
391
/// # Example
385
- ///
392
+ ///
386
393
/// Basic example:
387
- ///
394
+ ///
388
395
/// ```norun
389
396
/// Client::with_connector(|addr: &str, port: u16, scheme: &str| {
390
397
/// TcpStream::connect(&(addr, port))
391
398
/// });
392
399
/// ```
393
- ///
400
+ ///
394
401
/// Example using TcpBuilder from the net2 crate if you want to configure your source socket:
395
- ///
402
+ ///
396
403
/// ```norun
397
404
/// Client::with_connector(|addr: &str, port: u16, scheme: &str| {
398
405
/// let b = try!(TcpBuilder::new_v4());
@@ -499,7 +506,6 @@ pub struct HttpsListener<S: Ssl> {
499
506
}
500
507
501
508
impl < S : Ssl > HttpsListener < S > {
502
-
503
509
/// Start listening to an address over HTTPS.
504
510
pub fn new < To : ToSocketAddrs > ( addr : To , ssl : S ) -> :: Result < HttpsListener < S > > {
505
511
HttpListener :: new ( addr) . map ( |l| HttpsListener {
@@ -508,6 +514,13 @@ impl<S: Ssl> HttpsListener<S> {
508
514
} )
509
515
}
510
516
517
+ /// Construct an HttpsListener from a bound `TcpListener`.
518
+ pub fn with_listener ( listener : HttpListener , ssl : S ) -> HttpsListener < S > {
519
+ HttpsListener {
520
+ listener : listener,
521
+ ssl : ssl
522
+ }
523
+ }
511
524
}
512
525
513
526
impl < S : Ssl + Clone > NetworkListener for HttpsListener < S > {
@@ -576,7 +589,6 @@ mod openssl {
576
589
use openssl:: x509:: X509FileType ;
577
590
use super :: { NetworkStream , HttpStream } ;
578
591
579
-
580
592
/// An implementation of `Ssl` for OpenSSL.
581
593
///
582
594
/// # Example
@@ -678,7 +690,6 @@ mod tests {
678
690
679
691
let mock = stream. downcast :: < MockStream > ( ) . ok ( ) . unwrap ( ) ;
680
692
assert_eq ! ( mock, Box :: new( MockStream :: new( ) ) ) ;
681
-
682
693
}
683
694
684
695
#[ test]
@@ -688,6 +699,6 @@ mod tests {
688
699
689
700
let mock = unsafe { stream. downcast_unchecked :: < MockStream > ( ) } ;
690
701
assert_eq ! ( mock, Box :: new( MockStream :: new( ) ) ) ;
691
-
692
702
}
693
703
}
704
+
0 commit comments