Skip to content

Commit

Permalink
fix(http2): don't add client content-length if method doesn't require it
Browse files Browse the repository at this point in the history
  • Loading branch information
clouduol authored and seanmonstar committed Dec 26, 2019
1 parent 35825c4 commit fb90d30
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/headers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bytes::BytesMut;
use http::header::{HeaderValue, OccupiedEntry, ValueIter};
use http::header::{CONTENT_LENGTH, TRANSFER_ENCODING};
use http::method::Method;
use http::HeaderMap;

pub fn connection_keep_alive(value: &HeaderValue) -> bool {
Expand Down Expand Up @@ -57,6 +58,13 @@ pub fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue>) -> Op
}
}

pub fn method_has_defined_payload_semantics(method: &Method) -> bool {
match *method {
Method::GET | Method::HEAD | Method::DELETE | Method::CONNECT => false,
_ => true,
}
}

pub fn set_content_length_if_missing(headers: &mut HeaderMap, len: u64) {
headers
.entry(CONTENT_LENGTH)
Expand Down
4 changes: 3 additions & 1 deletion src/proto/h2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ where
let mut req = ::http::Request::from_parts(head, ());
super::strip_connection_headers(req.headers_mut(), true);
if let Some(len) = body.size_hint().exact() {
headers::set_content_length_if_missing(req.headers_mut(), len);
if len != 0 || headers::method_has_defined_payload_semantics(req.method()) {
headers::set_content_length_if_missing(req.headers_mut(), len);
}
}
let eos = body.is_end_stream();
let (fut, body_tx) = match self.h2_tx.send_request(req, eos) {
Expand Down

0 comments on commit fb90d30

Please sign in to comment.