From 77adab4ebf0fadd9ccd014d24ff0bcec1bce1e8b Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 16 Feb 2018 15:22:06 -0800 Subject: [PATCH] fix(client): prevent empty bodies sending transfer-encoding for GET, HEAD --- src/proto/h1/role.rs | 14 +++++++++----- tests/client.rs | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index b0f5df171f..40324e1a7f 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -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 @@ -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::(); if can_have_body { @@ -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::(); head.headers.remove::(); @@ -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::().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::() { Some(&mut header::TransferEncoding(ref mut encodings)) => { if encodings.last() != Some(&header::Encoding::Chunked) { diff --git a/tests/client.rs b/tests/client.rs index 39fab3d2d7..c935e5e792 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -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,