1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-03-20 06:25:16 +01:00

misc: improve default compress function

This commit is contained in:
William R. Arellano 2023-03-14 22:21:18 -05:00
parent e9a1aeebce
commit a3ec0ccf99

View File

@ -12,6 +12,7 @@ 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;
@ -86,7 +87,16 @@ impl fmt::Debug for Compress {
impl Default for Compress { impl Default for Compress {
fn default() -> Self { fn default() -> Self {
Compress { Compress {
compress: |_| false, compress: |content_type| match content_type {
None => true,
Some(value) => {
let response_mime: Mime = value.to_str().unwrap().parse::<Mime>().unwrap();
match response_mime.type_().as_str() {
"image" => false,
_ => true,
}
}
},
} }
} }
} }
@ -207,7 +217,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 = (self.compress)(content_type); let should_compress = (self.compress)(content_type);
if should_compress { if should_compress {
EitherBody::left(Encoder::response(enc, head, body)) EitherBody::left(Encoder::response(enc, head, body))
} else { } else {