From 7e8ea44d5c7882f47161d248eed8abed1e90ba7d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 18 May 2020 00:42:51 +0100 Subject: [PATCH] remove needless BodySize::Sized64 variant --- MIGRATION.md | 4 ++++ actix-http/CHANGES.md | 3 +++ actix-http/src/body.rs | 20 +++++++++----------- actix-http/src/client/h2proto.rs | 4 ---- actix-http/src/h1/encoder.rs | 27 ++------------------------- actix-http/src/h2/dispatcher.rs | 4 ---- actix-http/src/helpers.rs | 8 ++++---- 7 files changed, 22 insertions(+), 48 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 672aa253..cb79e664 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -3,9 +3,13 @@ * Setting a cookie's SameSite property, explicitly, to `SameSite::None` will now result in `SameSite=None` being sent with the response Set-Cookie header. To create a cookie without a SameSite attribute, remove any calls setting same_site. + * actix-http support for Actors messages was moved to actix-http crate and is enabled with feature `actors` +* `BodySize::Sized64` variant has been removed. `BodySize::Sized` now receives a + `u64` instead of a `usize`. + ## 2.0.0 * `HttpServer::start()` renamed to `HttpServer::run()`. It also possible to diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index e96b0451..a383b6a1 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -6,6 +6,9 @@ * Bump minimum supported Rust version to 1.40 +* `BodySize::Sized64` variant has been removed. `BodySize::Sized` now receives a + `u64` instead of a `usize`. + ### Fixed * Support parsing of `SameSite=None` [#1503] diff --git a/actix-http/src/body.rs b/actix-http/src/body.rs index f887b53b..b821e777 100644 --- a/actix-http/src/body.rs +++ b/actix-http/src/body.rs @@ -15,8 +15,7 @@ use crate::error::Error; pub enum BodySize { None, Empty, - Sized(usize), - Sized64(u64), + Sized(u64), Stream, } @@ -25,8 +24,7 @@ impl BodySize { match self { BodySize::None | BodySize::Empty - | BodySize::Sized(0) - | BodySize::Sized64(0) => true, + | BodySize::Sized(0) => true, _ => false, } } @@ -170,7 +168,7 @@ impl MessageBody for Body { match self { Body::None => BodySize::None, Body::Empty => BodySize::Empty, - Body::Bytes(ref bin) => BodySize::Sized(bin.len()), + Body::Bytes(ref bin) => BodySize::Sized(bin.len() as u64), Body::Message(ref body) => body.size(), } } @@ -297,7 +295,7 @@ where impl MessageBody for Bytes { fn size(&self) -> BodySize { - BodySize::Sized(self.len()) + BodySize::Sized(self.len() as u64) } fn poll_next( @@ -314,7 +312,7 @@ impl MessageBody for Bytes { impl MessageBody for BytesMut { fn size(&self) -> BodySize { - BodySize::Sized(self.len()) + BodySize::Sized(self.len() as u64) } fn poll_next( @@ -331,7 +329,7 @@ impl MessageBody for BytesMut { impl MessageBody for &'static str { fn size(&self) -> BodySize { - BodySize::Sized(self.len()) + BodySize::Sized(self.len() as u64) } fn poll_next( @@ -350,7 +348,7 @@ impl MessageBody for &'static str { impl MessageBody for Vec { fn size(&self) -> BodySize { - BodySize::Sized(self.len()) + BodySize::Sized(self.len() as u64) } fn poll_next( @@ -367,7 +365,7 @@ impl MessageBody for Vec { impl MessageBody for String { fn size(&self) -> BodySize { - BodySize::Sized(self.len()) + BodySize::Sized(self.len() as u64) } fn poll_next( @@ -458,7 +456,7 @@ where S: Stream> + Unpin, { fn size(&self) -> BodySize { - BodySize::Sized64(self.size) + BodySize::Sized(self.size as u64) } /// Attempts to pull out the next value of the underlying [`Stream`]. diff --git a/actix-http/src/client/h2proto.rs b/actix-http/src/client/h2proto.rs index 2afd2d80..48ab9fe4 100644 --- a/actix-http/src/client/h2proto.rs +++ b/actix-http/src/client/h2proto.rs @@ -64,10 +64,6 @@ where CONTENT_LENGTH, HeaderValue::try_from(format!("{}", len)).unwrap(), ), - BodySize::Sized64(len) => req.headers_mut().insert( - CONTENT_LENGTH, - HeaderValue::try_from(format!("{}", len)).unwrap(), - ), }; // Extracting extra headers from RequestHeadType. HeaderMap::new() does not allocate. diff --git a/actix-http/src/h1/encoder.rs b/actix-http/src/h1/encoder.rs index 4689906b..eb8c337d 100644 --- a/actix-http/src/h1/encoder.rs +++ b/actix-http/src/h1/encoder.rs @@ -4,7 +4,7 @@ use std::ptr::copy_nonoverlapping; use std::slice::from_raw_parts_mut; use std::{cmp, io}; -use bytes::{buf::BufMutExt, BufMut, BytesMut}; +use bytes::{BufMut, BytesMut}; use crate::body::BodySize; use crate::config::ServiceConfig; @@ -95,15 +95,6 @@ pub(crate) trait MessageType: Sized { } } BodySize::Sized(len) => helpers::write_content_length(len, dst), - BodySize::Sized64(len) => { - if camel_case { - dst.put_slice(b"\r\nContent-Length: "); - } else { - dst.put_slice(b"\r\ncontent-length: "); - } - #[allow(clippy::write_with_newline)] - write!(dst.writer(), "{}\r\n", len)?; - } BodySize::None => dst.put_slice(b"\r\n"), } @@ -338,8 +329,7 @@ impl MessageEncoder { if !head { self.te = match length { BodySize::Empty => TransferEncoding::empty(), - BodySize::Sized(len) => TransferEncoding::length(len as u64), - BodySize::Sized64(len) => TransferEncoding::length(len), + BodySize::Sized(len) => TransferEncoding::length(len), BodySize::Stream => { if message.chunked() && !stream { TransferEncoding::chunked() @@ -582,19 +572,6 @@ mod tests { assert!(data.contains("Content-Type: plain/text\r\n")); assert!(data.contains("Date: date\r\n")); - let _ = head.encode_headers( - &mut bytes, - Version::HTTP_11, - BodySize::Sized64(100), - ConnectionType::KeepAlive, - &ServiceConfig::default(), - ); - let data = - String::from_utf8(Vec::from(bytes.split().freeze().as_ref())).unwrap(); - assert!(data.contains("Content-Length: 100\r\n")); - assert!(data.contains("Content-Type: plain/text\r\n")); - assert!(data.contains("Date: date\r\n")); - let mut head = RequestHead::default(); head.set_camel_case_headers(false); head.headers.insert(DATE, HeaderValue::from_static("date")); diff --git a/actix-http/src/h2/dispatcher.rs b/actix-http/src/h2/dispatcher.rs index b07764a0..a189697a 100644 --- a/actix-http/src/h2/dispatcher.rs +++ b/actix-http/src/h2/dispatcher.rs @@ -210,10 +210,6 @@ where CONTENT_LENGTH, HeaderValue::try_from(format!("{}", len)).unwrap(), ), - BodySize::Sized64(len) => res.headers_mut().insert( - CONTENT_LENGTH, - HeaderValue::try_from(format!("{}", len)).unwrap(), - ), }; // copy headers diff --git a/actix-http/src/helpers.rs b/actix-http/src/helpers.rs index ff647e72..5ac6c9be 100644 --- a/actix-http/src/helpers.rs +++ b/actix-http/src/helpers.rs @@ -30,7 +30,7 @@ pub(crate) fn write_status_line(version: Version, n: u16, bytes: &mut BytesMut) } /// NOTE: bytes object has to contain enough space -pub fn write_content_length(n: usize, bytes: &mut BytesMut) { +pub fn write_content_length(n: u64, bytes: &mut BytesMut) { bytes.put_slice(b"\r\ncontent-length: "); if n < 10 { @@ -96,16 +96,16 @@ pub fn write_content_length(n: usize, bytes: &mut BytesMut) { bytes.put_u8(DIGITS_START + d10); bytes.put_u8(DIGITS_START + d1); } else { - write_usize(n, bytes); + write_u64(n, bytes); } bytes.put_slice(b"\r\n"); } -pub(crate) fn write_usize(n: usize, bytes: &mut BytesMut) { +pub(crate) fn write_u64(n: u64, bytes: &mut BytesMut) { let mut n = n; - // 20 chars is max length of a usize (2^64) + // 20 chars is max length of a u64 (2^64) // digits will be added to the buffer from lsd to msd let mut buf = BytesMut::with_capacity(20);