Skip to content

Commit a36e44a

Browse files
committed
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.
1 parent 32e09a0 commit a36e44a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/http/h1.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl HttpMessage for Http11Message {
225225
SizedReader(stream, len)
226226
} else if headers.has::<ContentLength>() {
227227
trace!("illegal Content-Length: {:?}", headers.get_raw("Content-Length"));
228+
self.stream = Some(stream.into_inner());
228229
return Err(Error::Header);
229230
} else {
230231
trace!("neither Transfer-Encoding nor Content-Length");
@@ -893,10 +894,12 @@ mod tests {
893894
use std::error::Error;
894895
use std::io::{self, Read, Write};
895896

897+
896898
use buffer::BufReader;
897899
use mock::MockStream;
900+
use http::HttpMessage;
898901

899-
use super::{read_chunk_size, parse_request, parse_response};
902+
use super::{read_chunk_size, parse_request, parse_response, Http11Message};
900903

901904
#[test]
902905
fn test_write_chunked() {
@@ -987,6 +990,15 @@ mod tests {
987990
assert_eq!(e.description(), "early eof");
988991
}
989992

993+
#[test]
994+
fn test_message_get_incoming_invalid_content_length() {
995+
let raw = MockStream::with_input(
996+
b"HTTP/1.1 200 OK\r\nContent-Length: asdf\r\n\r\n");
997+
let mut msg = Http11Message::with_stream(Box::new(raw));
998+
assert!(msg.get_incoming().is_err());
999+
assert!(msg.close_connection().is_ok());
1000+
}
1001+
9901002
#[test]
9911003
fn test_parse_incoming() {
9921004
let mut raw = MockStream::with_input(b"GET /echo HTTP/1.1\r\nHost: hyper.rs\r\n\r\n");

0 commit comments

Comments
 (0)