1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-01 08:45:10 +02: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

@ -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 {

View File

@ -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.
///
@ -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
};

View File

@ -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;