From e5cc6150ebe82eae6daa3ec61c91296b19200a81 Mon Sep 17 00:00:00 2001 From: Thomas Orozco Date: Mon, 25 Jan 2021 13:16:20 +0000 Subject: [PATCH] Don't send Content-Length on HTTP/1.1 100 Continue Backported from https://github.com/hyperium/hyper/pull/2216. --- src/proto/h1/role.rs | 23 ++++++++++++++++------- tests/server.rs | 5 +++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index f188efa5a5..cd0e6c78b5 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -550,13 +550,13 @@ impl Http1Transaction for Server { } } None | Some(BodyLength::Known(0)) => { - if msg.head.subject != StatusCode::NOT_MODIFIED { + if Server::can_have_content_length(msg.req_method, msg.head.subject) { extend(dst, b"content-length: 0\r\n"); } Encoder::length(0) } Some(BodyLength::Known(len)) => { - if msg.head.subject == StatusCode::NOT_MODIFIED { + if !Server::can_have_content_length(msg.req_method, msg.head.subject) { Encoder::length(0) } else { extend(dst, b"content-length: "); @@ -625,13 +625,22 @@ impl Server { if method == &Some(Method::HEAD) || method == &Some(Method::CONNECT) && status.is_success() { false + } else if status.is_informational() { + false + } else { + match status { + StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false, + _ => true, + } + } + } + + fn can_have_content_length(method: &Option, status: StatusCode) -> bool { + if status.is_informational() || method == &Some(Method::CONNECT) && status.is_success() { + false } else { match status { - // TODO: support for 1xx codes needs improvement everywhere - // would be 100...199 => false - StatusCode::SWITCHING_PROTOCOLS - | StatusCode::NO_CONTENT - | StatusCode::NOT_MODIFIED => false, + StatusCode::NO_CONTENT | StatusCode::NOT_MODIFIED => false, _ => true, } } diff --git a/tests/server.rs b/tests/server.rs index bb465126fa..7b011768d9 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1327,8 +1327,9 @@ async fn upgrades_new() { let mut buf = [0; 256]; tcp.read(&mut buf).expect("read 1"); - let expected = "HTTP/1.1 101 Switching Protocols\r\n"; - assert_eq!(s(&buf[..expected.len()]), expected); + let response = s(&buf); + assert!(response.starts_with("HTTP/1.1 101 Switching Protocols\r\n")); + assert!(!has_header(&response, "content-length")); let _ = read_101_tx.send(()); let n = tcp.read(&mut buf).expect("read 2");