swadhinbiswas WIP save-point: production hardening, H3 removal, fmt gate, and accumulated phase work · 7/15/2026
a293d8c
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
322
use std::io::Write;
use anyhow::Result;
use async_trait::async_trait;
use flate2::write::{DeflateEncoder, GzEncoder};
use flate2::Compression;
use http_body_util::combinators::UnsyncBoxBody;
use http_body_util::BodyExt;
use hyper::body::Bytes;
use hyper::{Request, Response};
use crate::ResponseBody;
// ---------------------------------------------------------------------------
// Compression algorithm
// ---------------------------------------------------------------------------
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
Gzip,
Deflate,
#[cfg(feature = "brotli-compression")]
Brotli,
#[cfg(feature = "zstd-compression")]
Zstd,
Identity,
}
impl Encoding {
/// Parse the best encoding from the `Accept-Encoding` header.
/// Returns the first supported encoding found, preferring Brotli > Zstd > Gzip > Deflate.
pub fn from_accept_encoding(header_value: &str) -> Self {
// Simple preference order: br > zstd > gzip > deflate
let lower = header_value.to_ascii_lowercase();
let parts: Vec<&str> = lower.split(',').map(|s| s.trim()).collect();
for part in &parts {
let encoding_name = part.split(';').next().unwrap_or("").trim();
match encoding_name {
#[cfg(feature = "brotli-compression")]
"br" => return Encoding::Brotli,
#[cfg(feature = "zstd-compression")]
"zstd" => return Encoding::Zstd,
"gzip" | "x-gzip" => return Encoding::Gzip,
"deflate" => return Encoding::Deflate,
"*" => {
// Wildcard: return the best we support
#[cfg(feature = "brotli-compression")]
return Encoding::Brotli;
#[cfg(feature = "zstd-compression")]
return Encoding::Zstd;
return Encoding::Gzip;
}
_ => continue,
}
}
Encoding::Identity
}
/// Returns the `Content-Encoding` header value for this encoding.
pub fn as_header_value(&self) -> &'static str {
match self {
Encoding::Gzip => "gzip",
Encoding::Deflate => "deflate",
#[cfg(feature = "brotli-compression")]
Encoding::Brotli => "br",
#[cfg(feature = "zstd-compression")]
Encoding::Zstd => "zstd",
Encoding::Identity => "identity",
}
}
/// Returns true if this encoding actually compresses data.
pub fn is_compression(&self) -> bool {
*self != Encoding::Identity
}
}
// ---------------------------------------------------------------------------
// Compress bytes
// ---------------------------------------------------------------------------
pub fn compress_bytes(data: &[u8], encoding: Encoding) -> Result<Vec<u8>> {
match encoding {
Encoding::Gzip => {
let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
encoder.write_all(data)?;
Ok(encoder.finish()?)
}
Encoding::Deflate => {
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::fast());
encoder.write_all(data)?;
Ok(encoder.finish()?)
}
#[cfg(feature = "brotli-compression")]
Encoding::Brotli => {
let mut encoder = brotli::CompressorWriter::new(Vec::new(), 4096, 6, 22);
std::io::Write::write_all(&mut encoder, data)?;
Ok(encoder.into_inner())
}
#[cfg(feature = "zstd-compression")]
Encoding::Zstd => {
let mut output = Vec::new();
zstd::stream::copy_encode(std::io::Cursor::new(data), &mut output, 3)?;
Ok(output)
}
Encoding::Identity => Ok(data.to_vec()),
}
}
// ---------------------------------------------------------------------------
// Compression middleware
// ---------------------------------------------------------------------------
/// Middleware that compresses response bodies based on the client's
/// `Accept-Encoding` header. Only compresses responses that are large
/// enough to benefit (> 1KB) and have compressible content types.
pub struct CompressionMiddleware {
min_size: usize,
}
impl CompressionMiddleware {
pub fn new() -> Self {
Self { min_size: 1024 }
}
pub fn with_min_size(min_size: usize) -> Self {
Self { min_size }
}
}
impl Default for CompressionMiddleware {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl<B: Send + 'static> crate::middleware::Middleware<B> for CompressionMiddleware {
async fn handle(
&self,
req: Request<B>,
next: crate::middleware::Next<'_, B>,
) -> Result<Response<ResponseBody>> {
// Determine client's accepted encoding
let accept_encoding =
req.headers().get("accept-encoding").and_then(|v| v.to_str().ok()).unwrap_or("");
let encoding = Encoding::from_accept_encoding(accept_encoding);
// If client doesn't support compression, just pass through
if !encoding.is_compression() {
return next.run(req).await;
}
let resp = next.run(req).await?;
// Check if the response is worth compressing
let content_type =
resp.headers().get("content-type").and_then(|v| v.to_str().ok()).unwrap_or("");
if !is_compressible_content_type(content_type) {
return Ok(resp);
}
// Don't compress if already encoded
if resp.headers().contains_key("content-encoding") {
return Ok(resp);
}
// Decompose response into parts and body
let (parts, body) = resp.into_parts();
// Collect the response body
let body_bytes = body.collect().await?.to_bytes();
// Don't compress small responses
if body_bytes.len() < self.min_size {
let new_body = UnsyncBoxBody::new(
http_body_util::Full::new(body_bytes)
.map_err(|e: std::convert::Infallible| -> anyhow::Error { match e {} }),
);
return Ok(Response::from_parts(parts, new_body));
}
// Compress
let compressed = compress_bytes(&body_bytes, encoding)?;
// Only use compression if it actually reduced size
if compressed.len() >= body_bytes.len() {
let new_body = UnsyncBoxBody::new(
http_body_util::Full::new(body_bytes)
.map_err(|e: std::convert::Infallible| -> anyhow::Error { match e {} }),
);
return Ok(Response::from_parts(parts, new_body));
}
let mut parts = parts;
parts.headers.insert("content-encoding", encoding.as_header_value().parse().unwrap());
parts.headers.insert("content-length", compressed.len().to_string().parse().unwrap());
// Add Vary header so caches know the response varies by Accept-Encoding
parts.headers.insert("vary", "accept-encoding".parse().unwrap());
let new_body = UnsyncBoxBody::new(
http_body_util::Full::new(Bytes::from(compressed))
.map_err(|e: std::convert::Infallible| -> anyhow::Error { match e {} }),
);
Ok(Response::from_parts(parts, new_body))
}
}
// ---------------------------------------------------------------------------
// Content type check
// ---------------------------------------------------------------------------
fn is_compressible_content_type(ct: &str) -> bool {
// Text-based and common compressible types
ct.starts_with("text/")
|| ct.contains("json")
|| ct.contains("javascript")
|| ct.contains("xml")
|| ct.contains("svg")
|| ct.contains("html")
|| ct.contains("css")
|| ct.contains("wasm")
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encoding_from_gzip() {
assert_eq!(Encoding::from_accept_encoding("gzip, deflate"), Encoding::Gzip);
}
#[test]
fn test_encoding_from_deflate() {
assert_eq!(Encoding::from_accept_encoding("deflate"), Encoding::Deflate);
}
#[test]
fn test_encoding_from_identity() {
assert_eq!(Encoding::from_accept_encoding("identity"), Encoding::Identity);
}
#[test]
fn test_encoding_from_wildcard() {
assert_eq!(Encoding::from_accept_encoding("*"), Encoding::Gzip);
}
#[test]
fn test_encoding_preference_order() {
// Gzip should be preferred over deflate since gzip appears later in the list
// but our parser picks the first compressible encoding it finds.
// Actually gzip is preferred because we check in order: br > zstd > gzip > deflate
// and "deflate, gzip" has deflate first, so deflate would be matched first.
// Wait - the parser iterates parts and matches each. For "deflate, gzip":
// part 0 = "deflate" -> matches Deflate, returns immediately.
assert_eq!(Encoding::from_accept_encoding("deflate, gzip"), Encoding::Deflate);
}
#[test]
fn test_encoding_gzip_preferred_over_deflate_in_header() {
// When gzip appears before deflate, gzip wins
assert_eq!(Encoding::from_accept_encoding("gzip, deflate"), Encoding::Gzip);
}
#[test]
fn test_compress_gzip() {
let data = b"hello world hello world hello world";
let compressed = compress_bytes(data, Encoding::Gzip).unwrap();
// Gzip adds overhead for small data, but still works
assert!(!compressed.is_empty());
}
#[test]
fn test_compress_deflate() {
let data = b"hello world hello world hello world";
let compressed = compress_bytes(data, Encoding::Deflate).unwrap();
assert!(!compressed.is_empty());
}
#[test]
fn test_compress_identity() {
let data = b"hello world";
let result = compress_bytes(data, Encoding::Identity).unwrap();
assert_eq!(result, data);
}
#[test]
fn test_compressible_content_type() {
assert!(is_compressible_content_type("text/html"));
assert!(is_compressible_content_type("application/json"));
assert!(is_compressible_content_type("application/javascript"));
assert!(is_compressible_content_type("image/svg+xml"));
assert!(!is_compressible_content_type("image/png"));
assert!(!is_compressible_content_type("image/jpeg"));
assert!(!is_compressible_content_type("application/octet-stream"));
}
#[test]
fn test_encoding_as_header_value() {
assert_eq!(Encoding::Gzip.as_header_value(), "gzip");
assert_eq!(Encoding::Deflate.as_header_value(), "deflate");
assert_eq!(Encoding::Identity.as_header_value(), "identity");
}
#[test]
fn test_is_compression() {
assert!(Encoding::Gzip.is_compression());
assert!(Encoding::Deflate.is_compression());
assert!(!Encoding::Identity.is_compression());
}
}