From a36e44af7d4e665a122c1498011ff10035f7376f Mon Sep 17 00:00:00 2001 From: Marko Lalic Date: Sat, 5 Sep 2015 00:42:50 +0200 Subject: [PATCH] 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. --- src/http/h1.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/http/h1.rs b/src/http/h1.rs index 14f25b78d8..239f96708b 100644 --- a/src/http/h1.rs +++ b/src/http/h1.rs @@ -225,6 +225,7 @@ impl HttpMessage for Http11Message { SizedReader(stream, len) } else if headers.has::() { 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");