diff --git a/src/server/h1writer.rs b/src/server/h1writer.rs index c27a4c44a..97ce6dff9 100644 --- a/src/server/h1writer.rs +++ b/src/server/h1writer.rs @@ -176,13 +176,11 @@ impl Writer for H1Writer { buffer.extend_from_slice(reason); // content length + let mut len_is_set = true; match info.length { ResponseLength::Chunked => { buffer.extend_from_slice(b"\r\ntransfer-encoding: chunked\r\n") } - ResponseLength::Zero => { - buffer.extend_from_slice(b"\r\ncontent-length: 0\r\n") - } ResponseLength::Length(len) => { helpers::write_content_length(len, &mut buffer) } @@ -191,6 +189,10 @@ impl Writer for H1Writer { write!(buffer.writer(), "{}", len)?; buffer.extend_from_slice(b"\r\n"); } + ResponseLength::Zero => { + len_is_set = false; + buffer.extend_from_slice(b"\r\n"); + } ResponseLength::None => buffer.extend_from_slice(b"\r\n"), } if let Some(ce) = info.content_encoding { @@ -212,6 +214,9 @@ impl Writer for H1Writer { }, CONTENT_LENGTH => match info.length { ResponseLength::None => (), + ResponseLength::Zero => { + len_is_set = true; + } _ => continue, }, DATE => { @@ -248,6 +253,9 @@ impl Writer for H1Writer { unsafe { buffer.advance_mut(pos); } + if !len_is_set { + buffer.extend_from_slice(b"content-length: 0\r\n") + } // optimized date header, set_date writes \r\n if !has_date { diff --git a/src/server/output.rs b/src/server/output.rs index 70c24facc..35f3c7a45 100644 --- a/src/server/output.rs +++ b/src/server/output.rs @@ -191,15 +191,13 @@ impl Output { let transfer = match resp.body() { Body::Empty => { - if !info.head { - info.length = match resp.status() { - StatusCode::NO_CONTENT - | StatusCode::CONTINUE - | StatusCode::SWITCHING_PROTOCOLS - | StatusCode::PROCESSING => ResponseLength::None, - _ => ResponseLength::Zero, - }; - } + info.length = match resp.status() { + StatusCode::NO_CONTENT + | StatusCode::CONTINUE + | StatusCode::SWITCHING_PROTOCOLS + | StatusCode::PROCESSING => ResponseLength::None, + _ => ResponseLength::Zero, + }; *self = Output::Empty(buf); return; } diff --git a/tests/test_server.rs b/tests/test_server.rs index cb19cfed0..f3c9bf9dd 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -1358,8 +1358,8 @@ fn test_ssl_handshake_timeout() { #[test] fn test_content_length() { - use http::StatusCode; use actix_web::http::header::{HeaderName, HeaderValue}; + use http::StatusCode; let mut srv = test::TestServer::new(move |app| { app.resource("/{status}", |r| { @@ -1398,4 +1398,3 @@ fn test_content_length() { assert_eq!(response.headers().get(&header), Some(&value)); } } -