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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use tide::http::cache::{CacheControl, CacheDirective};
use tide::http::conditional::Vary;
use tide::http::content::{AcceptEncoding, ContentEncoding, Encoding};
use tide::http::{headers, Body, Method};
use tide::{Middleware, Next, Request, Response};
#[cfg(any(feature = "brotli", feature = "deflate", feature = "gzip"))]
use async_compression::Level;
#[cfg(any(feature = "brotli", feature = "deflate", feature = "gzip"))]
use futures_lite::io::BufReader;
#[cfg(feature = "brotli")]
use async_compression::futures::bufread::BrotliEncoder;
#[cfg(feature = "deflate")]
use async_compression::futures::bufread::DeflateEncoder;
#[cfg(feature = "gzip")]
use async_compression::futures::bufread::GzipEncoder;
#[cfg(feature = "regex-check")]
use http_types::content::ContentType;
#[cfg(feature = "regex-check")]
use regex::{Regex, RegexBuilder};
const THRESHOLD: usize = 1024;
#[cfg(feature = "regex-check")]
const CONTENT_TYPE_CHECK_PATTERN: &str = r"^text/|\+(?:json|text|xml)$";
#[cfg(feature = "regex-check")]
const EXTRACT_TYPE_PATTERN: &str = r"^\s*([^;\s]*)(?:;|\s|$)";
#[derive(Clone, Debug)]
pub struct CompressMiddleware {
threshold: usize,
#[cfg(feature = "regex-check")]
content_type_check: Option<Regex>,
#[cfg(feature = "regex-check")]
extract_type_regex: Regex,
#[cfg(feature = "brotli")]
brotli_quality: Level,
#[cfg(any(feature = "gzip", feature = "deflate"))]
deflate_quality: Level,
}
impl Default for CompressMiddleware {
fn default() -> Self {
CompressMiddlewareBuilder::default().into()
}
}
impl CompressMiddleware {
pub fn new() -> Self {
Self::default()
}
pub fn builder() -> CompressMiddlewareBuilder {
CompressMiddlewareBuilder::new()
}
pub fn set_threshold(&mut self, threshold: usize) {
self.threshold = threshold
}
pub fn threshold(&self) -> usize {
self.threshold
}
#[cfg(feature = "regex-check")]
pub fn set_content_type_check(&mut self, content_type_check: Option<Regex>) {
self.content_type_check = content_type_check
}
#[cfg(feature = "regex-check")]
pub fn content_type_check(&self) -> Option<&Regex> {
self.content_type_check.as_ref()
}
}
#[tide::utils::async_trait]
impl<State: Clone + Send + Sync + 'static> Middleware<State> for CompressMiddleware {
async fn handle(&self, req: Request<State>, next: Next<'_, State>) -> tide::Result {
let is_head = req.method() == Method::Head;
let accepts = AcceptEncoding::from_headers(&req)?;
let mut res: Response = next.run(req).await;
if is_head || accepts.is_none() {
return Ok(res);
}
let mut accepts = accepts.expect("checked directly above");
if let Some(cache_control) = CacheControl::from_headers(&res)? {
if cache_control
.iter()
.any(|directive| directive == &CacheDirective::NoTransform)
{
return Ok(res);
}
}
let mut vary = Vary::new();
vary.push(headers::ACCEPT_ENCODING)?;
vary.apply(&mut res);
if let Some(previous_encoding) = ContentEncoding::from_headers(&res)? {
if previous_encoding != Encoding::Identity {
return Ok(res);
}
}
if let Some(body_len) = res.len() {
if body_len < self.threshold {
return Ok(res);
}
}
#[cfg(feature = "regex-check")]
if let Some(ref content_type_check) = self.content_type_check {
if let Some(content_type) = ContentType::from_headers(&res)? {
if let Some(extension_match) = self
.extract_type_regex
.captures(content_type.value().as_str())
.and_then(|captures| captures.get(1))
{
#[cfg(feature = "db-check")]
if !crate::codegen_database::MIME_DB.contains(extension_match.as_str())
&& !content_type_check.is_match(extension_match.as_str())
{
return Ok(res);
}
#[cfg(not(feature = "db-check"))]
if !content_type_check.is_match(extension_match.as_str()) {
return Ok(res);
}
}
}
}
let encoding = accepts.negotiate(&[
#[cfg(feature = "brotli")]
Encoding::Brotli,
#[cfg(feature = "gzip")]
Encoding::Gzip,
#[cfg(feature = "deflate")]
Encoding::Deflate,
Encoding::Identity, ])?;
if encoding == Encoding::Identity {
res.remove_header(headers::CONTENT_ENCODING);
return Ok(res);
}
let body = res.take_body();
res.set_body(get_encoder(
body,
&encoding,
#[cfg(feature = "brotli")]
self.brotli_quality,
#[cfg(any(feature = "gzip", feature = "deflate"))]
self.deflate_quality,
));
encoding.apply(&mut res);
res.remove_header(headers::CONTENT_LENGTH);
Ok(res)
}
}
#[cfg_attr(
not(any(feature = "brotli", feature = "deflate", feature = "gzip")),
allow(unused_variables)
)]
fn get_encoder(
body: Body,
encoding: &ContentEncoding,
#[cfg(feature = "brotli")] brotli_quality: Level,
#[cfg(any(feature = "gzip", feature = "deflate"))] deflate_quality: Level,
) -> Body {
#[cfg(feature = "brotli")]
{
if *encoding == Encoding::Brotli {
return Body::from_reader(
BufReader::new(BrotliEncoder::with_quality(body, brotli_quality)),
None,
);
}
}
#[cfg(feature = "gzip")]
{
if *encoding == Encoding::Gzip {
return Body::from_reader(
BufReader::new(GzipEncoder::with_quality(body, deflate_quality)),
None,
);
}
}
#[cfg(feature = "deflate")]
{
if *encoding == Encoding::Deflate {
return Body::from_reader(
BufReader::new(DeflateEncoder::with_quality(body, deflate_quality)),
None,
);
}
}
body
}
#[derive(Clone, Debug)]
pub struct CompressMiddlewareBuilder {
pub threshold: usize,
#[cfg(feature = "regex-check")]
pub content_type_check: Option<Regex>,
#[cfg(feature = "brotli")]
pub brotli_quality: Level,
#[cfg(any(feature = "gzip", feature = "deflate"))]
pub deflate_quality: Level,
}
impl Default for CompressMiddlewareBuilder {
fn default() -> Self {
Self {
threshold: THRESHOLD,
#[cfg(feature = "regex-check")]
content_type_check: Some(
RegexBuilder::new(CONTENT_TYPE_CHECK_PATTERN)
.case_insensitive(true)
.build()
.expect("Constant regular expression defined in Tide-Compress's source code"),
),
#[cfg(feature = "brotli")]
brotli_quality: Level::Fastest,
#[cfg(any(feature = "gzip", feature = "deflate"))]
deflate_quality: Level::Default,
}
}
}
impl CompressMiddlewareBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn threshold(mut self, threshold: usize) -> Self {
self.threshold = threshold;
self
}
#[cfg(feature = "regex-check")]
pub fn content_type_check(mut self, content_type_check: Option<Regex>) -> Self {
self.content_type_check = content_type_check;
self
}
#[cfg(feature = "brotli")]
pub fn brotli_quality(mut self, quality: Level) -> Self {
self.brotli_quality = quality;
self
}
#[cfg(any(feature = "gzip", feature = "deflate"))]
pub fn deflate_quality(mut self, quality: Level) -> Self {
self.deflate_quality = quality;
self
}
pub fn build(self) -> CompressMiddleware {
self.into()
}
}
impl From<CompressMiddlewareBuilder> for CompressMiddleware {
fn from(builder: CompressMiddlewareBuilder) -> Self {
Self {
threshold: builder.threshold,
#[cfg(feature = "regex-check")]
content_type_check: builder.content_type_check,
#[cfg(feature = "regex-check")]
extract_type_regex: Regex::new(EXTRACT_TYPE_PATTERN)
.expect("Constant regular expression defined in Tide-Compress's source code"),
#[cfg(feature = "brotli")]
brotli_quality: builder.brotli_quality,
#[cfg(any(feature = "gzip", feature = "deflate"))]
deflate_quality: builder.deflate_quality,
}
}
}