From 01613f334bdfe81abe4a6934e590ad7f70ba3935 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 16 Dec 2019 17:22:26 +0600 Subject: [PATCH] Move BodyEncoding to dev module #1220 --- CHANGES.md | 6 ++++++ actix-files/CHANGES.md | 6 +++++- actix-files/Cargo.toml | 4 ++-- actix-files/src/named.rs | 8 ++++---- src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++ src/middleware/compress.rs | 28 ++++--------------------- src/middleware/mod.rs | 2 +- 7 files changed, 64 insertions(+), 32 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 762a5d84..56813ce9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # Changes +## [2.0.0] - 2019-12-xx + +### Changed + +* Move `BodyEncoding` to `dev` module #1220 + ## [2.0.0-alpha.6] - 2019-12-15 ### Fixed diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index 5a33d361..5bfd937a 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -1,6 +1,10 @@ # Changes -## [0.2.0-alpha.7] - 2019-12-07 +## [0.2.0] - 2019-12-xx + +* Fix BodyEncoding trait import #1220 + +## [0.2.0-alpha.1] - 2019-12-07 * Migrate to `std::future` diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index 5a65ad1f..3eecdbab 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -18,7 +18,7 @@ name = "actix_files" path = "src/lib.rs" [dependencies] -actix-web = { version = "2.0.0-alpha.5", default-features = false } +actix-web = { version = "2.0.0-alpha.6", default-features = false } actix-http = "1.0.0" actix-service = "1.0.0" bitflags = "1" @@ -33,4 +33,4 @@ v_htmlescape = "0.4" [dev-dependencies] actix-rt = "1.0.0" -actix-web = { version = "2.0.0-alpha.5", features=["openssl"] } +actix-web = { version = "2.0.0-alpha.6", features=["openssl"] } diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index 0dcbd93b..3d9398b4 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -12,11 +12,11 @@ use mime; use mime_guess::from_path; use actix_http::body::SizedStream; +use actix_web::dev::BodyEncoding; use actix_web::http::header::{ self, Charset, ContentDisposition, DispositionParam, DispositionType, ExtendedValue, }; use actix_web::http::{ContentEncoding, StatusCode}; -use actix_web::middleware::BodyEncoding; use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder}; use futures::future::{ready, Ready}; @@ -268,7 +268,7 @@ impl NamedFile { ); }); if let Some(current_encoding) = self.encoding { - resp.encoding(current_encoding); + resp.set_encoding(current_encoding); } let reader = ChunkedReadFile { size: self.md.len(), @@ -335,7 +335,7 @@ impl NamedFile { }); // default compressing if let Some(current_encoding) = self.encoding { - resp.encoding(current_encoding); + resp.set_encoding(current_encoding); } resp.if_some(last_modified, |lm, resp| { @@ -356,7 +356,7 @@ impl NamedFile { if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) { length = rangesvec[0].length; offset = rangesvec[0].start; - resp.encoding(ContentEncoding::Identity); + resp.set_encoding(ContentEncoding::Identity); resp.header( header::CONTENT_RANGE, format!( diff --git a/src/lib.rs b/src/lib.rs index acb0da10..d2f501e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -158,6 +158,48 @@ pub mod dev { }; path } + + use crate::http::header::ContentEncoding; + use actix_http::{Response, ResponseBuilder}; + + struct Enc(ContentEncoding); + + /// Helper trait that allows to set specific encoding for response. + pub trait BodyEncoding { + fn encoding(&self) -> Option; + + fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self; + } + + impl BodyEncoding for ResponseBuilder { + fn encoding(&self) -> Option { + if let Some(ref enc) = self.extensions().get::() { + Some(enc.0) + } else { + None + } + } + + fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self { + self.extensions_mut().insert(Enc(encoding)); + self + } + } + + impl BodyEncoding for Response { + fn encoding(&self) -> Option { + if let Some(ref enc) = self.extensions().get::() { + Some(enc.0) + } else { + None + } + } + + fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self { + self.extensions_mut().insert(Enc(encoding)); + self + } + } } pub mod client { diff --git a/src/middleware/compress.rs b/src/middleware/compress.rs index 0826606b..c55c741e 100644 --- a/src/middleware/compress.rs +++ b/src/middleware/compress.rs @@ -9,34 +9,14 @@ use std::task::{Context, Poll}; use actix_http::body::MessageBody; use actix_http::encoding::Encoder; use actix_http::http::header::{ContentEncoding, ACCEPT_ENCODING}; -use actix_http::{Error, Response, ResponseBuilder}; +use actix_http::Error; use actix_service::{Service, Transform}; use futures::future::{ok, Ready}; use pin_project::pin_project; +use crate::dev::BodyEncoding; use crate::service::{ServiceRequest, ServiceResponse}; -struct Enc(ContentEncoding); - -/// Helper trait that allows to set specific encoding for response. -pub trait BodyEncoding { - fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self; -} - -impl BodyEncoding for ResponseBuilder { - fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self { - self.extensions_mut().insert(Enc(encoding)); - self - } -} - -impl BodyEncoding for Response { - fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self { - self.extensions_mut().insert(Enc(encoding)); - self - } -} - #[derive(Debug, Clone)] /// `Middleware` for compressing response body. /// @@ -155,8 +135,8 @@ where match futures::ready!(this.fut.poll(cx)) { Ok(resp) => { - let enc = if let Some(enc) = resp.response().extensions().get::() { - enc.0 + let enc = if let Some(enc) = resp.response().encoding() { + enc } else { *this.encoding }; diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index be23230c..f0d42cc2 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -3,7 +3,7 @@ #[cfg(feature = "compress")] mod compress; #[cfg(feature = "compress")] -pub use self::compress::{BodyEncoding, Compress}; +pub use self::compress::Compress; mod condition; mod defaultheaders;