fix(http): Make sure not to lose the stream when CL is invalid
When the Content-Length header is invalid, the Http11Message ends up dropping the stream before returning the error. This causes a panic when the `close_connection` method of the message is used later. This commit fixes this by saving the stream onto the message instance before returning the error. A regression test is also included.
This commit is contained in:
		| @@ -225,6 +225,7 @@ impl HttpMessage for Http11Message { | ||||
|                 SizedReader(stream, len) | ||||
|             } else if headers.has::<ContentLength>() { | ||||
|                 trace!("illegal Content-Length: {:?}", headers.get_raw("Content-Length")); | ||||
|                 self.stream = Some(stream.into_inner()); | ||||
|                 return Err(Error::Header); | ||||
|             } else { | ||||
|                 trace!("neither Transfer-Encoding nor Content-Length"); | ||||
| @@ -893,10 +894,12 @@ mod tests { | ||||
|     use std::error::Error; | ||||
|     use std::io::{self, Read, Write}; | ||||
|  | ||||
|  | ||||
|     use buffer::BufReader; | ||||
|     use mock::MockStream; | ||||
|     use http::HttpMessage; | ||||
|  | ||||
|     use super::{read_chunk_size, parse_request, parse_response}; | ||||
|     use super::{read_chunk_size, parse_request, parse_response, Http11Message}; | ||||
|  | ||||
|     #[test] | ||||
|     fn test_write_chunked() { | ||||
| @@ -987,6 +990,15 @@ mod tests { | ||||
|         assert_eq!(e.description(), "early eof"); | ||||
|     } | ||||
|  | ||||
|     #[test] | ||||
|     fn test_message_get_incoming_invalid_content_length() { | ||||
|         let raw = MockStream::with_input( | ||||
|             b"HTTP/1.1 200 OK\r\nContent-Length: asdf\r\n\r\n"); | ||||
|         let mut msg = Http11Message::with_stream(Box::new(raw)); | ||||
|         assert!(msg.get_incoming().is_err()); | ||||
|         assert!(msg.close_connection().is_ok()); | ||||
|     } | ||||
|  | ||||
|     #[test] | ||||
|     fn test_parse_incoming() { | ||||
|         let mut raw = MockStream::with_input(b"GET /echo HTTP/1.1\r\nHost: hyper.rs\r\n\r\n"); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user