1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 10:42:55 +01:00

Move BodyEncoding to dev module #1220

This commit is contained in:
Nikolay Kim 2019-12-16 17:22:26 +06:00
parent b0aa9395da
commit 01613f334b
7 changed files with 64 additions and 32 deletions

View File

@ -1,5 +1,11 @@
# Changes # Changes
## [2.0.0] - 2019-12-xx
### Changed
* Move `BodyEncoding` to `dev` module #1220
## [2.0.0-alpha.6] - 2019-12-15 ## [2.0.0-alpha.6] - 2019-12-15
### Fixed ### Fixed

View File

@ -1,6 +1,10 @@
# Changes # 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` * Migrate to `std::future`

View File

@ -18,7 +18,7 @@ name = "actix_files"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [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-http = "1.0.0"
actix-service = "1.0.0" actix-service = "1.0.0"
bitflags = "1" bitflags = "1"
@ -33,4 +33,4 @@ v_htmlescape = "0.4"
[dev-dependencies] [dev-dependencies]
actix-rt = "1.0.0" 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"] }

View File

@ -12,11 +12,11 @@ use mime;
use mime_guess::from_path; use mime_guess::from_path;
use actix_http::body::SizedStream; use actix_http::body::SizedStream;
use actix_web::dev::BodyEncoding;
use actix_web::http::header::{ use actix_web::http::header::{
self, Charset, ContentDisposition, DispositionParam, DispositionType, ExtendedValue, self, Charset, ContentDisposition, DispositionParam, DispositionType, ExtendedValue,
}; };
use actix_web::http::{ContentEncoding, StatusCode}; use actix_web::http::{ContentEncoding, StatusCode};
use actix_web::middleware::BodyEncoding;
use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder}; use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder};
use futures::future::{ready, Ready}; use futures::future::{ready, Ready};
@ -268,7 +268,7 @@ impl NamedFile {
); );
}); });
if let Some(current_encoding) = self.encoding { if let Some(current_encoding) = self.encoding {
resp.encoding(current_encoding); resp.set_encoding(current_encoding);
} }
let reader = ChunkedReadFile { let reader = ChunkedReadFile {
size: self.md.len(), size: self.md.len(),
@ -335,7 +335,7 @@ impl NamedFile {
}); });
// default compressing // default compressing
if let Some(current_encoding) = self.encoding { if let Some(current_encoding) = self.encoding {
resp.encoding(current_encoding); resp.set_encoding(current_encoding);
} }
resp.if_some(last_modified, |lm, resp| { resp.if_some(last_modified, |lm, resp| {
@ -356,7 +356,7 @@ impl NamedFile {
if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) { if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) {
length = rangesvec[0].length; length = rangesvec[0].length;
offset = rangesvec[0].start; offset = rangesvec[0].start;
resp.encoding(ContentEncoding::Identity); resp.set_encoding(ContentEncoding::Identity);
resp.header( resp.header(
header::CONTENT_RANGE, header::CONTENT_RANGE,
format!( format!(

View File

@ -158,6 +158,48 @@ pub mod dev {
}; };
path 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<ContentEncoding>;
fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self;
}
impl BodyEncoding for ResponseBuilder {
fn encoding(&self) -> Option<ContentEncoding> {
if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0)
} else {
None
}
}
fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
self.extensions_mut().insert(Enc(encoding));
self
}
}
impl<B> BodyEncoding for Response<B> {
fn encoding(&self) -> Option<ContentEncoding> {
if let Some(ref enc) = self.extensions().get::<Enc>() {
Some(enc.0)
} else {
None
}
}
fn set_encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
self.extensions_mut().insert(Enc(encoding));
self
}
}
} }
pub mod client { pub mod client {

View File

@ -9,34 +9,14 @@ use std::task::{Context, Poll};
use actix_http::body::MessageBody; use actix_http::body::MessageBody;
use actix_http::encoding::Encoder; use actix_http::encoding::Encoder;
use actix_http::http::header::{ContentEncoding, ACCEPT_ENCODING}; use actix_http::http::header::{ContentEncoding, ACCEPT_ENCODING};
use actix_http::{Error, Response, ResponseBuilder}; use actix_http::Error;
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use futures::future::{ok, Ready}; use futures::future::{ok, Ready};
use pin_project::pin_project; use pin_project::pin_project;
use crate::dev::BodyEncoding;
use crate::service::{ServiceRequest, ServiceResponse}; 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<B> BodyEncoding for Response<B> {
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
self.extensions_mut().insert(Enc(encoding));
self
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
/// `Middleware` for compressing response body. /// `Middleware` for compressing response body.
/// ///
@ -155,8 +135,8 @@ where
match futures::ready!(this.fut.poll(cx)) { match futures::ready!(this.fut.poll(cx)) {
Ok(resp) => { Ok(resp) => {
let enc = if let Some(enc) = resp.response().extensions().get::<Enc>() { let enc = if let Some(enc) = resp.response().encoding() {
enc.0 enc
} else { } else {
*this.encoding *this.encoding
}; };

View File

@ -3,7 +3,7 @@
#[cfg(feature = "compress")] #[cfg(feature = "compress")]
mod compress; mod compress;
#[cfg(feature = "compress")] #[cfg(feature = "compress")]
pub use self::compress::{BodyEncoding, Compress}; pub use self::compress::Compress;
mod condition; mod condition;
mod defaultheaders; mod defaultheaders;