From 0404b78b54bc80658630b1af588562aa7245d217 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 15 Feb 2021 11:24:46 +0000 Subject: [PATCH] improve body size docs --- actix-http/src/body.rs | 26 ++++++++++++++++++++++++++ actix-http/src/client/h2proto.rs | 1 + actix-http/src/encoding/encoder.rs | 1 + 3 files changed, 28 insertions(+) diff --git a/actix-http/src/body.rs b/actix-http/src/body.rs index 0dbe93a4a..4fd578c87 100644 --- a/actix-http/src/body.rs +++ b/actix-http/src/body.rs @@ -15,13 +15,39 @@ use crate::error::Error; /// Body size hint. #[derive(Debug, PartialEq, Copy, Clone)] pub enum BodySize { + /// Absence of body can be assumed from method or status code. + /// + /// Will skip writing Content-Length header. None, + + /// Zero size body. + /// + /// Will write `Content-Length: 0` header. Empty, + + /// Known size body. + /// + /// Will write `Content-Length: N` header. `Sized(0)` is treated the same as `Empty`. Sized(u64), + + /// Unknown size body. + /// + /// Will not write Content-Length header. Can be used with chunked Transfer-Encoding. Stream, } impl BodySize { + /// Returns true if size hint indicates no or empty body. + /// + /// ``` + /// # use actix_http::body::BodySize; + /// assert!(BodySize::None.is_eof()); + /// assert!(BodySize::Empty.is_eof()); + /// assert!(BodySize::Sized(0).is_eof()); + /// + /// assert!(!BodySize::Sized(64).is_eof()); + /// assert!(!BodySize::Stream.is_eof()); + /// ``` pub fn is_eof(&self) -> bool { matches!(self, BodySize::None | BodySize::Empty | BodySize::Sized(0)) } diff --git a/actix-http/src/client/h2proto.rs b/actix-http/src/client/h2proto.rs index a70bc1738..105e04c73 100644 --- a/actix-http/src/client/h2proto.rs +++ b/actix-http/src/client/h2proto.rs @@ -36,6 +36,7 @@ where B: MessageBody, { trace!("Sending client request: {:?} {:?}", head, body.size()); + let head_req = head.as_ref().method == Method::HEAD; let length = body.size(); let eof = matches!( diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index 366ecb8c4..ee0587fbd 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -78,6 +78,7 @@ impl Encoder { }); } } + ResponseBody::Body(Encoder { body, eof: false,