From 2bfc170fb0c1acb11f8851370df382c84e734f1a Mon Sep 17 00:00:00 2001 From: "William R. Arellano" Date: Tue, 14 Mar 2023 16:38:14 -0500 Subject: [PATCH] feat(compress): add compress function to middleware --- actix-web/src/middleware/compress.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/actix-web/src/middleware/compress.rs b/actix-web/src/middleware/compress.rs index 2c81698ef..dce9fc378 100644 --- a/actix-web/src/middleware/compress.rs +++ b/actix-web/src/middleware/compress.rs @@ -13,6 +13,7 @@ use actix_utils::future::{ok, Either, Ready}; use futures_core::ready; use once_cell::sync::Lazy; use pin_project_lite::pin_project; +use mime::Mime; use crate::{ body::{EitherBody, MessageBody}, @@ -71,9 +72,19 @@ use crate::{ /// ``` /// /// [feature flags]: ../index.html#crate-features -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] #[non_exhaustive] -pub struct Compress; +pub struct Compress { + pub compress: fn(Mime) -> bool, +} + +impl Default for Compress { + fn default() -> Self { + Compress { + compress: |_| { true } + } + } +} impl Transform for Compress where @@ -87,12 +98,13 @@ where type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { - ok(CompressMiddleware { service }) + ok(CompressMiddleware { service, compress: self.compress }) } } pub struct CompressMiddleware { service: S, + compress: fn(Mime) -> bool, } impl Service for CompressMiddleware