@@ -19,6 +19,7 @@ pub struct Response {
1919 pub version : version:: HttpVersion ,
2020 status_raw : RawStatus ,
2121 message : Box < HttpMessage > ,
22+ is_drained : bool ,
2223}
2324
2425impl Response {
@@ -43,41 +44,54 @@ impl Response {
4344 headers : headers,
4445 message : message,
4546 status_raw : raw_status,
47+ is_drained : false ,
4648 } )
4749 }
4850
4951 /// Get the raw status code and reason.
5052 pub fn status_raw ( & self ) -> & RawStatus {
5153 & self . status_raw
5254 }
55+
5356}
5457
5558impl Read for Response {
5659 #[ inline]
5760 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
58- let count = try!( self . message . read ( buf) ) ;
61+ match self . message . read ( buf) {
62+ Ok ( 0 ) => {
63+ self . is_drained = true ;
64+ Ok ( 0 )
65+ } ,
66+ r => r
67+ }
68+ }
69+ }
5970
60- if count == 0 {
61- if !http:: should_keep_alive ( self . version , & self . headers ) {
62- try!( self . message . close_connection ( )
63- . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: Other ,
64- "Error closing connection" ) ) ) ;
71+ impl Drop for Response {
72+ fn drop ( & mut self ) {
73+ // if not drained, theres old bits in the Reader. we can't reuse this,
74+ // since those old bits would end up in new Responses
75+ //
76+ // otherwise, the response has been drained. we should check that the
77+ // server has agreed to keep the connection open
78+ trace ! ( "Response.is_drained = {:?}" , self . is_drained) ;
79+ if !( self . is_drained && http:: should_keep_alive ( self . version , & self . headers ) ) {
80+ trace ! ( "closing connection" ) ;
81+ if let Err ( e) = self . message . close_connection ( ) {
82+ error ! ( "error closing connection: {}" , e) ;
6583 }
6684 }
67-
68- Ok ( count)
6985 }
7086}
7187
7288#[ cfg( test) ]
7389mod tests {
74- use std:: borrow:: Cow :: Borrowed ;
7590 use std:: io:: { self , Read } ;
7691
77- use header:: Headers ;
7892 use header:: TransferEncoding ;
7993 use header:: Encoding ;
80- use http:: RawStatus ;
94+ use http:: HttpMessage ;
8195 use mock:: MockStream ;
8296 use status;
8397 use version;
@@ -94,18 +108,10 @@ mod tests {
94108
95109 #[ test]
96110 fn test_into_inner ( ) {
97- let res = Response {
98- status : status:: StatusCode :: Ok ,
99- headers : Headers :: new ( ) ,
100- version : version:: HttpVersion :: Http11 ,
101- message : Box :: new ( Http11Message :: with_stream ( Box :: new ( MockStream :: new ( ) ) ) ) ,
102- status_raw : RawStatus ( 200 , Borrowed ( "OK" ) ) ,
103- } ;
104-
105- let message = res. message . downcast :: < Http11Message > ( ) . ok ( ) . unwrap ( ) ;
111+ let message: Box < HttpMessage > = Box :: new ( Http11Message :: with_stream ( Box :: new ( MockStream :: new ( ) ) ) ) ;
112+ let message = message. downcast :: < Http11Message > ( ) . ok ( ) . unwrap ( ) ;
106113 let b = message. into_inner ( ) . downcast :: < MockStream > ( ) . ok ( ) . unwrap ( ) ;
107114 assert_eq ! ( b, Box :: new( MockStream :: new( ) ) ) ;
108-
109115 }
110116
111117 #[ test]
0 commit comments