Skip to content

Commit

Permalink
fix(http): Make sure not to lose the stream when CL is invalid
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mlalic committed Sep 4, 2015
1 parent 32e09a0 commit a36e44a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/http/h1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit a36e44a

Please sign in to comment.