mirror of https://github.com/fafhrd91/actix-web
remove `BodySize::Empty`
This commit is contained in:
parent
9905df9a60
commit
7c8fc2facf
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `AnyBody::Empty`; an empty body can now only be represented as a zero-length `Bytes` variant. [#????]
|
* `AnyBody::Empty`; an empty body can now only be represented as a zero-length `Bytes` variant. [#????]
|
||||||
|
* `BodySize::Empty`; an empty body can now only be represented as a `Sized(0)` variant. [#????]
|
||||||
|
|
||||||
[#????]: https://github.com/actix/actix-web/pull/????
|
[#????]: https://github.com/actix/actix-web/pull/????
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,6 @@ impl MessageBody for AnyBody {
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
match self {
|
match self {
|
||||||
AnyBody::None => BodySize::None,
|
AnyBody::None => BodySize::None,
|
||||||
AnyBody::Bytes(ref bin) if bin.is_empty() => BodySize::Empty,
|
|
||||||
AnyBody::Bytes(ref bin) => BodySize::Sized(bin.len() as u64),
|
AnyBody::Bytes(ref bin) => BodySize::Sized(bin.len() as u64),
|
||||||
AnyBody::Stream(ref body) => body.size(),
|
AnyBody::Stream(ref body) => body.size(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ impl MessageBody for () {
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
|
|
||||||
fn size(&self) -> BodySize {
|
fn size(&self) -> BodySize {
|
||||||
BodySize::Empty
|
BodySize::Sized(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_next(
|
fn poll_next(
|
||||||
|
|
|
@ -44,8 +44,9 @@ pub use self::sized_stream::SizedStream;
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn to_bytes<B: MessageBody>(body: B) -> Result<Bytes, B::Error> {
|
pub async fn to_bytes<B: MessageBody>(body: B) -> Result<Bytes, B::Error> {
|
||||||
let cap = match body.size() {
|
let cap = match body.size() {
|
||||||
BodySize::None | BodySize::Empty | BodySize::Sized(0) => return Ok(Bytes::new()),
|
BodySize::None | BodySize::Sized(0) => return Ok(Bytes::new()),
|
||||||
BodySize::Sized(size) => size as usize,
|
BodySize::Sized(size) => size as usize,
|
||||||
|
// good enough first guess for chunk size
|
||||||
BodySize::Stream => 32_768,
|
BodySize::Stream => 32_768,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -184,7 +185,7 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_unit() {
|
async fn test_unit() {
|
||||||
assert_eq!(().size(), BodySize::Empty);
|
assert_eq!(().size(), BodySize::Sized(0));
|
||||||
assert!(poll_fn(|cx| Pin::new(&mut ()).poll_next(cx))
|
assert!(poll_fn(|cx| Pin::new(&mut ()).poll_next(cx))
|
||||||
.await
|
.await
|
||||||
.is_none());
|
.is_none());
|
||||||
|
@ -194,11 +195,11 @@ mod tests {
|
||||||
async fn test_box_and_pin() {
|
async fn test_box_and_pin() {
|
||||||
let val = Box::new(());
|
let val = Box::new(());
|
||||||
pin!(val);
|
pin!(val);
|
||||||
assert_eq!(val.size(), BodySize::Empty);
|
assert_eq!(val.size(), BodySize::Sized(0));
|
||||||
assert!(poll_fn(|cx| val.as_mut().poll_next(cx)).await.is_none());
|
assert!(poll_fn(|cx| val.as_mut().poll_next(cx)).await.is_none());
|
||||||
|
|
||||||
let mut val = Box::pin(());
|
let mut val = Box::pin(());
|
||||||
assert_eq!(val.size(), BodySize::Empty);
|
assert_eq!(val.size(), BodySize::Sized(0));
|
||||||
assert!(poll_fn(|cx| val.as_mut().poll_next(cx)).await.is_none());
|
assert!(poll_fn(|cx| val.as_mut().poll_next(cx)).await.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,9 @@ pub enum BodySize {
|
||||||
/// Will skip writing Content-Length header.
|
/// Will skip writing Content-Length header.
|
||||||
None,
|
None,
|
||||||
|
|
||||||
/// Zero size body.
|
|
||||||
///
|
|
||||||
/// Will write `Content-Length: 0` header.
|
|
||||||
Empty,
|
|
||||||
|
|
||||||
/// Known size body.
|
/// Known size body.
|
||||||
///
|
///
|
||||||
/// Will write `Content-Length: N` header. `Sized(0)` is treated the same as `Empty`.
|
/// Will write `Content-Length: N` header.
|
||||||
Sized(u64),
|
Sized(u64),
|
||||||
|
|
||||||
/// Unknown size body.
|
/// Unknown size body.
|
||||||
|
@ -25,6 +20,8 @@ pub enum BodySize {
|
||||||
impl BodySize {
|
impl BodySize {
|
||||||
/// Returns true if size hint indicates no or empty body.
|
/// Returns true if size hint indicates no or empty body.
|
||||||
///
|
///
|
||||||
|
/// Streams will return false because it cannot be known without reading the stream.
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use actix_http::body::BodySize;
|
/// # use actix_http::body::BodySize;
|
||||||
/// assert!(BodySize::None.is_eof());
|
/// assert!(BodySize::None.is_eof());
|
||||||
|
@ -35,6 +32,6 @@ impl BodySize {
|
||||||
/// assert!(!BodySize::Stream.is_eof());
|
/// assert!(!BodySize::Stream.is_eof());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn is_eof(&self) -> bool {
|
pub fn is_eof(&self) -> bool {
|
||||||
matches!(self, BodySize::None | BodySize::Empty | BodySize::Sized(0))
|
matches!(self, BodySize::None | BodySize::Sized(0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -325,7 +325,7 @@ where
|
||||||
) -> Result<(), DispatchError> {
|
) -> Result<(), DispatchError> {
|
||||||
let size = self.as_mut().send_response_inner(message, &body)?;
|
let size = self.as_mut().send_response_inner(message, &body)?;
|
||||||
let state = match size {
|
let state = match size {
|
||||||
BodySize::None | BodySize::Empty => State::None,
|
BodySize::None | BodySize::Sized(0) => State::None,
|
||||||
_ => State::SendPayload(body),
|
_ => State::SendPayload(body),
|
||||||
};
|
};
|
||||||
self.project().state.set(state);
|
self.project().state.set(state);
|
||||||
|
@ -339,7 +339,7 @@ where
|
||||||
) -> Result<(), DispatchError> {
|
) -> Result<(), DispatchError> {
|
||||||
let size = self.as_mut().send_response_inner(message, &body)?;
|
let size = self.as_mut().send_response_inner(message, &body)?;
|
||||||
let state = match size {
|
let state = match size {
|
||||||
BodySize::None | BodySize::Empty => State::None,
|
BodySize::None | BodySize::Sized(0) => State::None,
|
||||||
_ => State::SendErrorPayload(body),
|
_ => State::SendErrorPayload(body),
|
||||||
};
|
};
|
||||||
self.project().state.set(state);
|
self.project().state.set(state);
|
||||||
|
|
|
@ -93,13 +93,10 @@ pub(crate) trait MessageType: Sized {
|
||||||
dst.put_slice(b"\r\n");
|
dst.put_slice(b"\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BodySize::Empty => {
|
BodySize::Sized(0) if camel_case => {
|
||||||
if camel_case {
|
dst.put_slice(b"\r\nContent-Length: 0\r\n")
|
||||||
dst.put_slice(b"\r\nContent-Length: 0\r\n");
|
|
||||||
} else {
|
|
||||||
dst.put_slice(b"\r\ncontent-length: 0\r\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
BodySize::Sized(0) => dst.put_slice(b"\r\ncontent-length: 0\r\n"),
|
||||||
BodySize::Sized(len) => helpers::write_content_length(len, dst),
|
BodySize::Sized(len) => helpers::write_content_length(len, dst),
|
||||||
BodySize::None => dst.put_slice(b"\r\n"),
|
BodySize::None => dst.put_slice(b"\r\n"),
|
||||||
}
|
}
|
||||||
|
@ -336,7 +333,7 @@ impl<T: MessageType> MessageEncoder<T> {
|
||||||
// transfer encoding
|
// transfer encoding
|
||||||
if !head {
|
if !head {
|
||||||
self.te = match length {
|
self.te = match length {
|
||||||
BodySize::Empty => TransferEncoding::empty(),
|
BodySize::Sized(0) => TransferEncoding::empty(),
|
||||||
BodySize::Sized(len) => TransferEncoding::length(len),
|
BodySize::Sized(len) => TransferEncoding::length(len),
|
||||||
BodySize::Stream => {
|
BodySize::Stream => {
|
||||||
if message.chunked() && !stream {
|
if message.chunked() && !stream {
|
||||||
|
@ -553,7 +550,7 @@ mod tests {
|
||||||
let _ = head.encode_headers(
|
let _ = head.encode_headers(
|
||||||
&mut bytes,
|
&mut bytes,
|
||||||
Version::HTTP_11,
|
Version::HTTP_11,
|
||||||
BodySize::Empty,
|
BodySize::Sized(0),
|
||||||
ConnectionType::Close,
|
ConnectionType::Close,
|
||||||
&ServiceConfig::default(),
|
&ServiceConfig::default(),
|
||||||
);
|
);
|
||||||
|
@ -624,7 +621,7 @@ mod tests {
|
||||||
let _ = head.encode_headers(
|
let _ = head.encode_headers(
|
||||||
&mut bytes,
|
&mut bytes,
|
||||||
Version::HTTP_11,
|
Version::HTTP_11,
|
||||||
BodySize::Empty,
|
BodySize::Sized(0),
|
||||||
ConnectionType::Close,
|
ConnectionType::Close,
|
||||||
&ServiceConfig::default(),
|
&ServiceConfig::default(),
|
||||||
);
|
);
|
||||||
|
|
|
@ -285,9 +285,11 @@ fn prepare_response(
|
||||||
|
|
||||||
let _ = match size {
|
let _ = match size {
|
||||||
BodySize::None | BodySize::Stream => None,
|
BodySize::None | BodySize::Stream => None,
|
||||||
BodySize::Empty => res
|
|
||||||
|
BodySize::Sized(0) => res
|
||||||
.headers_mut()
|
.headers_mut()
|
||||||
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
||||||
|
|
||||||
BodySize::Sized(len) => {
|
BodySize::Sized(len) => {
|
||||||
let mut buf = itoa::Buffer::new();
|
let mut buf = itoa::Buffer::new();
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ where
|
||||||
// RFC: https://tools.ietf.org/html/rfc7231#section-5.1.1
|
// RFC: https://tools.ietf.org/html/rfc7231#section-5.1.1
|
||||||
let is_expect = if head.as_ref().headers.contains_key(EXPECT) {
|
let is_expect = if head.as_ref().headers.contains_key(EXPECT) {
|
||||||
match body.size() {
|
match body.size() {
|
||||||
BodySize::None | BodySize::Empty | BodySize::Sized(0) => {
|
BodySize::None | BodySize::Sized(0) => {
|
||||||
let keep_alive = framed.codec_ref().keepalive();
|
let keep_alive = framed.codec_ref().keepalive();
|
||||||
framed.io_mut().on_release(keep_alive);
|
framed.io_mut().on_release(keep_alive);
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ where
|
||||||
if do_send {
|
if do_send {
|
||||||
// send request body
|
// send request body
|
||||||
match body.size() {
|
match body.size() {
|
||||||
BodySize::None | BodySize::Empty | BodySize::Sized(0) => {}
|
BodySize::None | BodySize::Sized(0) => {}
|
||||||
_ => send_body(body, pin_framed.as_mut()).await?,
|
_ => send_body(body, pin_framed.as_mut()).await?,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -36,10 +36,7 @@ where
|
||||||
|
|
||||||
let head_req = head.as_ref().method == Method::HEAD;
|
let head_req = head.as_ref().method == Method::HEAD;
|
||||||
let length = body.size();
|
let length = body.size();
|
||||||
let eof = matches!(
|
let eof = matches!(length, BodySize::None | BodySize::Sized(0));
|
||||||
length,
|
|
||||||
BodySize::None | BodySize::Empty | BodySize::Sized(0)
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut req = Request::new(());
|
let mut req = Request::new(());
|
||||||
*req.uri_mut() = head.as_ref().uri.clone();
|
*req.uri_mut() = head.as_ref().uri.clone();
|
||||||
|
@ -52,13 +49,11 @@ where
|
||||||
// Content length
|
// Content length
|
||||||
let _ = match length {
|
let _ = match length {
|
||||||
BodySize::None => None,
|
BodySize::None => None,
|
||||||
BodySize::Stream => {
|
|
||||||
skip_len = false;
|
BodySize::Sized(0) => req
|
||||||
None
|
|
||||||
}
|
|
||||||
BodySize::Empty => req
|
|
||||||
.headers_mut()
|
.headers_mut()
|
||||||
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0")),
|
||||||
|
|
||||||
BodySize::Sized(len) => {
|
BodySize::Sized(len) => {
|
||||||
let mut buf = itoa::Buffer::new();
|
let mut buf = itoa::Buffer::new();
|
||||||
|
|
||||||
|
@ -67,6 +62,11 @@ where
|
||||||
HeaderValue::from_str(buf.format(len)).unwrap(),
|
HeaderValue::from_str(buf.format(len)).unwrap(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BodySize::Stream => {
|
||||||
|
skip_len = false;
|
||||||
|
None
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extracting extra headers from RequestHeadType. HeaderMap::new() does not allocate.
|
// Extracting extra headers from RequestHeadType. HeaderMap::new() does not allocate.
|
||||||
|
|
Loading…
Reference in New Issue