Skip to content

Commit

Permalink
Move BodyEncoding to dev module actix#1220
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 16, 2019
1 parent b0aa939 commit 01613f3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion actix-files/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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`

Expand Down
4 changes: 2 additions & 2 deletions actix-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
8 changes: 4 additions & 4 deletions actix-files/src/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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| {
Expand All @@ -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!(
Expand Down
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 {
Expand Down
28 changes: 4 additions & 24 deletions src/middleware/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B> BodyEncoding for Response<B> {
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self {
self.extensions_mut().insert(Enc(encoding));
self
}
}

#[derive(Debug, Clone)]
/// `Middleware` for compressing response body.
///
Expand Down Expand Up @@ -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>() {
enc.0
let enc = if let Some(enc) = resp.response().encoding() {
enc
} else {
*this.encoding
};
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 01613f3

Please sign in to comment.