1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use async_trait::async_trait;
use flate2::{Compression, write::GzEncoder};
use futures::{StreamExt, stream::BoxStream};
use std::io::Write;
use std::sync::Arc;
use super::Middleware;
use crate::core::{Request, Response, response::Body, router::Handler};
/// Compression algorithms supported by the middleware
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionAlgorithm {
/// Gzip compression (widely supported, good compression)
Gzip,
}
impl CompressionAlgorithm {
fn encoding_name(&self) -> &'static str {
match self {
CompressionAlgorithm::Gzip => "gzip",
}
}
}
/// Configuration for compression middleware
#[derive(Clone)]
pub struct CompressionConfig {
/// Compression level (0-9 for gzip/deflate, 0-11 for brotli)
pub level: u32,
/// Minimum response size to compress (bytes)
pub min_size: usize,
/// Supported algorithms in order of preference
pub algorithms: Vec<CompressionAlgorithm>,
/// Content types that should be compressed (empty means compress all types)
pub compress_types: Vec<String>,
/// Whether to enable content type filtering (if false, compress all content types)
pub filter_content_types: bool,
}
impl Default for CompressionConfig {
fn default() -> Self {
Self {
level: 6, // Default compression level
min_size: 1024, // Only compress responses >= 1KB
algorithms: vec![CompressionAlgorithm::Gzip],
compress_types: vec![
"text/".to_string(),
"application/json".to_string(),
"application/javascript".to_string(),
"application/xml".to_string(),
"application/rss+xml".to_string(),
"application/atom+xml".to_string(),
"image/svg+xml".to_string(),
],
filter_content_types: true, // Enable content type filtering by default
}
}
}
impl CompressionConfig {
pub fn new() -> Self {
Self::default()
}
/// Set compression level (0-9)
pub fn level(mut self, level: u32) -> Self {
self.level = level.min(9);
self
}
/// Set minimum size threshold for compression
pub fn min_size(mut self, size: usize) -> Self {
self.min_size = size;
self
}
/// Add a content type pattern that should be compressed
pub fn compress_type<S: Into<String>>(mut self, content_type: S) -> Self {
self.compress_types.push(content_type.into());
self
}
/// Set the list of compression algorithms in order of preference
pub fn algorithms(mut self, algorithms: Vec<CompressionAlgorithm>) -> Self {
self.algorithms = algorithms;
self
}
/// Enable or disable content type filtering
pub fn filter_content_types(mut self, filter: bool) -> Self {
self.filter_content_types = filter;
self
}
/// Disable content type filtering (compress all types)
pub fn compress_all_types(mut self) -> Self {
self.filter_content_types = false;
self
}
}
/// Middleware for HTTP response compression
pub struct CompressionMiddleware {
config: CompressionConfig,
}
impl CompressionMiddleware {
/// Create new compression middleware with default configuration
pub fn new() -> Self {
Self {
config: CompressionConfig::default(),
}
}
/// Create new compression middleware with custom configuration
pub fn with_config(config: CompressionConfig) -> Self {
Self { config }
}
/// Check if the client accepts the given encoding
fn accepts_encoding(&self, req: &Request, encoding: &str) -> bool {
req.headers()
.get("accept-encoding")
.and_then(|v| v.to_str().ok())
.map(|accept_encoding| {
accept_encoding
.split(',')
.map(|s| s.trim())
.any(|enc| enc.eq_ignore_ascii_case(encoding) || enc.eq_ignore_ascii_case("*"))
})
.unwrap_or(false)
}
/// Choose the best compression algorithm based on client support
fn choose_algorithm(&self, req: &Request) -> Option<CompressionAlgorithm> {
self.config
.algorithms
.iter()
.find(|&&algorithm| self.accepts_encoding(req, algorithm.encoding_name()))
.copied()
}
/// Check if the content type should be compressed
fn should_compress_content_type(&self, content_type: &str) -> bool {
// If content type filtering is disabled, compress all types
if !self.config.filter_content_types {
return true;
}
// If no content types are specified, compress all types
if self.config.compress_types.is_empty() {
return true;
}
// Check if content type matches any pattern
for pattern in &self.config.compress_types {
if content_type.starts_with(pattern) {
return true;
}
}
false
}
/// Check if the response (alone) allows compression according to config
fn response_allows_compress(&self, res: &Response) -> bool {
// Don't compress if content-encoding is already set
if res.headers.contains_key(http::header::CONTENT_ENCODING) {
return false;
}
// Check content type
if let Some(content_type) = res.headers.get(http::header::CONTENT_TYPE) {
if let Ok(ct) = content_type.to_str() {
if !self.should_compress_content_type(ct) {
return false;
}
} else {
return false;
}
} else if self.config.filter_content_types {
// No content-type header, only compress if content type filtering is disabled
return false;
}
// For byte bodies, check size threshold
if let Body::Bytes(bytes) = &res.body
&& bytes.len() < self.config.min_size
{
return false;
}
true
}
/// Compress byte data using the specified algorithm
fn compress_bytes(
&self,
data: &[u8],
algorithm: CompressionAlgorithm,
) -> Result<Vec<u8>, std::io::Error> {
match algorithm {
CompressionAlgorithm::Gzip => {
let mut encoder = GzEncoder::new(Vec::new(), Compression::new(self.config.level));
encoder.write_all(data)?;
encoder.finish()
}
}
}
/// Create a compressed stream from an uncompressed stream
fn compress_stream(
&self,
stream: BoxStream<'static, bytes::Bytes>,
algorithm: CompressionAlgorithm,
) -> BoxStream<'static, bytes::Bytes> {
match algorithm {
CompressionAlgorithm::Gzip => {
Box::pin(futures::stream::unfold(
(stream, None, false),
|(mut stream, mut encoder_opt, finished)| async move {
if finished {
return None;
}
// Initialize encoder on first chunk
if encoder_opt.is_none() {
encoder_opt = Some(GzEncoder::new(Vec::new(), Compression::new(6)));
}
let mut encoder = encoder_opt.take().unwrap();
match stream.next().await {
Some(chunk) => {
// Write chunk to encoder
if encoder.write_all(&chunk).is_err() {
return None;
}
// Take compressed data so far without allocating
let compressed = std::mem::take(encoder.get_mut());
Some((
bytes::Bytes::from(compressed),
(stream, Some(encoder), false),
))
}
None => {
// Finish compression
match encoder.finish() {
Ok(final_data) => {
Some((bytes::Bytes::from(final_data), (stream, None, true)))
}
Err(_) => None,
}
}
}
},
))
}
}
}
}
impl Default for CompressionMiddleware {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Middleware for CompressionMiddleware {
async fn handle(&self, req: Request, next: Arc<dyn Handler>) -> Response {
// Pre-compute the best algorithm from the request, then move request downstream
let algo = self.choose_algorithm(&req);
let mut response = next.handle(req).await;
// Check if we should compress this response
if let Some(algorithm) = algo.filter(|_| self.response_allows_compress(&response)) {
match response.body {
Body::Bytes(ref bytes) => {
// Compress byte body
if let Ok(compressed) = self.compress_bytes(bytes, algorithm) {
response.body = Body::Bytes(bytes::Bytes::from(compressed));
let _ = response.headers.insert(
http::header::CONTENT_ENCODING,
http::HeaderValue::from_str(algorithm.encoding_name()).unwrap(),
);
let _ = response.headers.insert(
http::header::VARY,
http::HeaderValue::from_static("Accept-Encoding"),
);
// Remove content-length since compression changes the size
response.headers.remove(http::header::CONTENT_LENGTH);
}
}
Body::Stream(stream) => {
// Compress streaming body
let compressed_stream = self.compress_stream(stream, algorithm);
response.body = Body::Stream(compressed_stream);
let _ = response.headers.insert(
http::header::CONTENT_ENCODING,
http::HeaderValue::from_str(algorithm.encoding_name()).unwrap(),
);
let _ = response.headers.insert(
http::header::VARY,
http::HeaderValue::from_static("Accept-Encoding"),
);
// Remove content-length for streaming responses with compression
response.headers.remove(http::header::CONTENT_LENGTH);
}
}
}
response
}
}
#[cfg(test)]
mod tests;