mirror of
https://github.com/fafhrd91/actix-web
synced 2025-03-20 14:25:18 +01:00
feat(compress): give more control to the user
This commit is contained in:
parent
14b09e35d1
commit
e9a1aeebce
@ -12,7 +12,6 @@ use actix_http::encoding::Encoder;
|
|||||||
use actix_service::{Service, Transform};
|
use actix_service::{Service, Transform};
|
||||||
use actix_utils::future::{ok, Either, Ready};
|
use actix_utils::future::{ok, Either, Ready};
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use mime::Mime;
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
@ -76,7 +75,7 @@ use crate::{
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct Compress {
|
pub struct Compress {
|
||||||
pub compress: fn(&HeaderValue) -> bool,
|
pub compress: fn(Option<&HeaderValue>) -> bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Compress {
|
impl fmt::Debug for Compress {
|
||||||
@ -113,7 +112,7 @@ where
|
|||||||
|
|
||||||
pub struct CompressMiddleware<S> {
|
pub struct CompressMiddleware<S> {
|
||||||
service: S,
|
service: S,
|
||||||
compress: fn(&HeaderValue) -> bool,
|
compress: fn(Option<&HeaderValue>) -> bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, B> Service<ServiceRequest> for CompressMiddleware<S>
|
impl<S, B> Service<ServiceRequest> for CompressMiddleware<S>
|
||||||
@ -183,7 +182,7 @@ pin_project! {
|
|||||||
fut: S::Future,
|
fut: S::Future,
|
||||||
encoding: Encoding,
|
encoding: Encoding,
|
||||||
_phantom: PhantomData<B>,
|
_phantom: PhantomData<B>,
|
||||||
compress: fn(&HeaderValue) -> bool,
|
compress: fn(Option<&HeaderValue>) -> bool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,9 +207,7 @@ where
|
|||||||
|
|
||||||
Poll::Ready(Ok(resp.map_body(move |head, body| {
|
Poll::Ready(Ok(resp.map_body(move |head, body| {
|
||||||
let content_type = head.headers.get(header::CONTENT_TYPE);
|
let content_type = head.headers.get(header::CONTENT_TYPE);
|
||||||
let should_compress = content_type
|
let should_compress = (self.compress)(content_type);
|
||||||
.map(|value| (self.compress)(value))
|
|
||||||
.unwrap_or(true);
|
|
||||||
if should_compress {
|
if should_compress {
|
||||||
EitherBody::left(Encoder::response(enc, head, body))
|
EitherBody::left(Encoder::response(enc, head, body))
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user