Skip to content

Commit

Permalink
fix(client): prevent empty bodies sending transfer-encoding for GET, …
Browse files Browse the repository at this point in the history
…HEAD
  • Loading branch information
seanmonstar committed Feb 16, 2018
1 parent dd79a4a commit 77adab4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use proto::{MessageHead, RawStatus, Http1Transaction, ParseResult,
use proto::h1::{Encoder, Decoder, date};
use method::Method;
use status::StatusCode;
use version::HttpVersion::{self, Http10, Http11};
use version::HttpVersion::{Http10, Http11};

const MAX_HEADERS: usize = 100;
const AVERAGE_HEADER_SIZE: usize = 30; // totally scientific
Expand Down Expand Up @@ -203,7 +203,7 @@ impl ServerTransaction {
};

if has_body && can_have_body {
set_length(head.version, &mut head.headers)
set_length(&mut head.headers, head.version == Http11)
} else {
head.headers.remove::<TransferEncoding>();
if can_have_body {
Expand Down Expand Up @@ -354,7 +354,11 @@ impl Http1Transaction for ClientTransaction {
impl ClientTransaction {
fn set_length(head: &mut RequestHead, has_body: bool) -> Encoder {
if has_body {
set_length(head.version, &mut head.headers)
let can_chunked = head.version == Http11
&& (head.subject.0 != Method::Head)
&& (head.subject.0 != Method::Get)
&& (head.subject.0 != Method::Connect);
set_length(&mut head.headers, can_chunked)
} else {
head.headers.remove::<ContentLength>();
head.headers.remove::<TransferEncoding>();
Expand All @@ -363,12 +367,12 @@ impl ClientTransaction {
}
}

fn set_length(version: HttpVersion, headers: &mut Headers) -> Encoder {
fn set_length(headers: &mut Headers, can_chunked: bool) -> Encoder {
let len = headers.get::<header::ContentLength>().map(|n| **n);

if let Some(len) = len {
Encoder::length(len)
} else if version == Http11 {
} else if can_chunked {
let encodings = match headers.get_mut::<header::TransferEncoding>() {
Some(&mut header::TransferEncoding(ref mut encodings)) => {
if encodings.last() != Some(&header::Encoding::Chunked) {
Expand Down
22 changes: 22 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ test! {
body: None,
}

test! {
name: client_get_implicitly_empty,

server:
expected: "GET / HTTP/1.1\r\nHost: {addr}\r\n\r\n",
reply: REPLY_OK,

client:
request:
method: Get,
url: "http://{addr}/",
headers: [],
body: Some(""),
proxy: false,
response:
status: Ok,
headers: [
ContentLength(0),
],
body: None,
}

test! {
name: client_post_sized,

Expand Down

0 comments on commit 77adab4

Please sign in to comment.