Skip to main content

wae_https/middleware/
compression.rs

1//! 压缩中间件
2//!
3//! 提供响应压缩功能,支持 gzip、brotli、deflate 压缩算法。
4//! 根据 Accept-Encoding 请求头自动选择最优压缩算法。
5
6use tower_http::compression::CompressionLayer as TowerCompressionLayer;
7
8/// 压缩算法类型
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum CompressionAlgorithm {
11    /// gzip 压缩算法
12    Gzip,
13
14    /// brotli 压缩算法
15    Brotli,
16
17    /// deflate 压缩算法
18    Deflate,
19}
20
21/// 压缩配置
22#[derive(Debug, Clone)]
23pub struct CompressionConfig {
24    /// 是否启用 gzip 压缩
25    pub enable_gzip: bool,
26
27    /// 是否启用 brotli 压缩
28    pub enable_brotli: bool,
29
30    /// 是否启用 deflate 压缩
31    pub enable_deflate: bool,
32}
33
34impl Default for CompressionConfig {
35    fn default() -> Self {
36        Self { enable_gzip: true, enable_brotli: true, enable_deflate: true }
37    }
38}
39
40impl CompressionConfig {
41    /// 创建默认压缩配置
42    pub fn new() -> Self {
43        Self::default()
44    }
45
46    /// 设置 gzip 压缩开关
47    pub fn with_gzip(mut self, enable: bool) -> Self {
48        self.enable_gzip = enable;
49        self
50    }
51
52    /// 设置 brotli 压缩开关
53    pub fn with_brotli(mut self, enable: bool) -> Self {
54        self.enable_brotli = enable;
55        self
56    }
57
58    /// 设置 deflate 压缩开关
59    pub fn with_deflate(mut self, enable: bool) -> Self {
60        self.enable_deflate = enable;
61        self
62    }
63}
64
65/// 压缩中间件层
66///
67/// 封装 tower-http 的 CompressionLayer,提供响应压缩功能。
68/// 根据 Accept-Encoding 请求头自动选择客户端支持的压缩算法。
69pub struct Compression {
70    config: CompressionConfig,
71}
72
73impl Compression {
74    /// 使用默认配置创建压缩中间件
75    pub fn new() -> Self {
76        Self { config: CompressionConfig::new() }
77    }
78
79    /// 使用指定配置创建压缩中间件
80    pub fn with_config(config: CompressionConfig) -> Self {
81        Self { config }
82    }
83
84    /// 获取当前配置
85    pub fn config(&self) -> &CompressionConfig {
86        &self.config
87    }
88
89    /// 构建 CompressionLayer
90    pub fn build(&self) -> TowerCompressionLayer {
91        let mut layer = TowerCompressionLayer::new();
92
93        if self.config.enable_gzip {
94            layer = layer.gzip(true);
95        }
96
97        if self.config.enable_brotli {
98            layer = layer.br(true);
99        }
100
101        if self.config.enable_deflate {
102            layer = layer.deflate(true);
103        }
104
105        layer
106    }
107}
108
109impl Default for Compression {
110    fn default() -> Self {
111        Self::new()
112    }
113}
114
115/// 压缩中间件构建器
116///
117/// 提供链式 API 构建压缩中间件配置。
118pub struct CompressionBuilder {
119    config: CompressionConfig,
120}
121
122impl CompressionBuilder {
123    /// 创建新的构建器
124    pub fn new() -> Self {
125        Self { config: CompressionConfig::new() }
126    }
127
128    /// 启用 gzip 压缩
129    pub fn gzip(mut self) -> Self {
130        self.config.enable_gzip = true;
131        self
132    }
133
134    /// 禁用 gzip 压缩
135    pub fn no_gzip(mut self) -> Self {
136        self.config.enable_gzip = false;
137        self
138    }
139
140    /// 启用 brotli 压缩
141    pub fn brotli(mut self) -> Self {
142        self.config.enable_brotli = true;
143        self
144    }
145
146    /// 禁用 brotli 压缩
147    pub fn no_brotli(mut self) -> Self {
148        self.config.enable_brotli = false;
149        self
150    }
151
152    /// 启用 deflate 压缩
153    pub fn deflate(mut self) -> Self {
154        self.config.enable_deflate = true;
155        self
156    }
157
158    /// 禁用 deflate 压缩
159    pub fn no_deflate(mut self) -> Self {
160        self.config.enable_deflate = false;
161        self
162    }
163
164    /// 构建压缩中间件
165    pub fn build(self) -> Compression {
166        Compression::with_config(self.config)
167    }
168}
169
170impl Default for CompressionBuilder {
171    fn default() -> Self {
172        Self::new()
173    }
174}