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
/*!
Body compression for trillium.rs

Currently, this crate only supports compressing outbound bodies with
the zstd, brotli, and gzip algorithms (in order of preference),
although more algorithms may be added in the future. The correct
algorithm will be selected based on the Accept-Encoding header sent by
the client, if one exists.
*/
#![forbid(unsafe_code)]
#![deny(
    missing_copy_implementations,
    rustdoc::missing_crate_level_docs,
    missing_debug_implementations,
    nonstandard_style,
    unused_qualifications
)]
#![warn(missing_docs)]

use async_compression::futures::bufread::{BrotliEncoder, GzipEncoder, ZstdEncoder};
use futures_lite::{
    io::{BufReader, Cursor},
    AsyncReadExt,
};
use std::{
    collections::BTreeSet,
    fmt::{self, Display, Formatter},
    str::FromStr,
};
use trillium::{
    async_trait, conn_try, conn_unwrap, Body, Conn, Handler, HeaderValues,
    KnownHeaderName::{AcceptEncoding, ContentEncoding, Vary},
};

/// Algorithms supported by this crate
#[derive(PartialEq, Eq, Clone, Copy, Debug, Ord, PartialOrd)]
#[non_exhaustive]
pub enum CompressionAlgorithm {
    /// Brotli algorithm
    Brotli,

    /// Gzip algorithm
    Gzip,

    /// Zstd algorithm
    Zstd,
}

impl CompressionAlgorithm {
    fn as_str(&self) -> &'static str {
        match self {
            CompressionAlgorithm::Brotli => "br",
            CompressionAlgorithm::Gzip => "gzip",
            CompressionAlgorithm::Zstd => "zstd",
        }
    }

    fn from_str_exact(s: &str) -> Option<Self> {
        match s {
            "br" => Some(CompressionAlgorithm::Brotli),
            "gzip" => Some(CompressionAlgorithm::Gzip),
            "x-gzip" => Some(CompressionAlgorithm::Gzip),
            "zstd" => Some(CompressionAlgorithm::Zstd),
            _ => None,
        }
    }
}

impl AsRef<str> for CompressionAlgorithm {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Display for CompressionAlgorithm {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for CompressionAlgorithm {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_str_exact(s)
            .or_else(|| Self::from_str_exact(&s.to_ascii_lowercase()))
            .ok_or_else(|| format!("unrecognized coding {s}"))
    }
}

/**
Trillium handler for compression
*/
#[derive(Clone, Debug)]
pub struct Compression {
    algorithms: BTreeSet<CompressionAlgorithm>,
}

impl Default for Compression {
    fn default() -> Self {
        use CompressionAlgorithm::*;
        Self {
            algorithms: [Zstd, Brotli, Gzip].into_iter().collect(),
        }
    }
}

impl Compression {
    /// constructs a new compression handler
    pub fn new() -> Self {
        Self::default()
    }

    fn set_algorithms(&mut self, algos: &[CompressionAlgorithm]) {
        self.algorithms = algos.iter().copied().collect();
    }

    /**
    sets the compression algorithms that this handler will
    use. the default of Zstd, Brotli, Gzip is recommended. Note that the
    order is ignored.
    */
    pub fn with_algorithms(mut self, algorithms: &[CompressionAlgorithm]) -> Self {
        self.set_algorithms(algorithms);
        self
    }

    fn negotiate(&self, header: &str) -> Option<CompressionAlgorithm> {
        parse_accept_encoding(header)
            .into_iter()
            .find_map(|(algo, _)| {
                if self.algorithms.contains(&algo) {
                    Some(algo)
                } else {
                    None
                }
            })
    }
}

fn parse_accept_encoding(header: &str) -> Vec<(CompressionAlgorithm, u8)> {
    let mut vec = header
        .split(',')
        .filter_map(|s| {
            let mut iter = s.trim().split(';');
            let (algo, q) = (iter.next()?, iter.next());
            let algo = algo.trim().parse().ok()?;
            let q = q
                .and_then(|q| {
                    q.trim()
                        .strip_prefix("q=")
                        .and_then(|q| q.parse::<f32>().map(|f| (f * 100.0) as u8).ok())
                })
                .unwrap_or(100u8);
            Some((algo, q))
        })
        .collect::<Vec<(CompressionAlgorithm, u8)>>();

    vec.sort_by(|(algo_a, a), (algo_b, b)| match b.cmp(a) {
        std::cmp::Ordering::Equal => algo_a.cmp(algo_b),
        other => other,
    });

    vec
}

#[async_trait]
impl Handler for Compression {
    async fn run(&self, mut conn: Conn) -> Conn {
        if let Some(header) = conn
            .headers()
            .get_str(AcceptEncoding)
            .and_then(|h| self.negotiate(h))
        {
            conn.set_state(header);
        }
        conn
    }

    async fn before_send(&self, mut conn: Conn) -> Conn {
        if let Some(algo) = conn.state::<CompressionAlgorithm>().copied() {
            let mut body = conn_unwrap!(conn.inner_mut().take_response_body(), conn);
            let mut compression_used = false;

            if body.is_static() {
                match algo {
                    CompressionAlgorithm::Zstd => {
                        let bytes = body.static_bytes().unwrap();
                        let mut data = vec![];
                        let mut encoder = ZstdEncoder::new(Cursor::new(bytes));
                        conn_try!(encoder.read_to_end(&mut data).await, conn);
                        if data.len() < bytes.len() {
                            log::trace!("zstd body from {} to {}", bytes.len(), data.len());
                            compression_used = true;
                            body = Body::new_static(data);
                        }
                    }

                    CompressionAlgorithm::Brotli => {
                        let bytes = body.static_bytes().unwrap();
                        let mut data = vec![];
                        let mut encoder = BrotliEncoder::new(Cursor::new(bytes));
                        conn_try!(encoder.read_to_end(&mut data).await, conn);
                        if data.len() < bytes.len() {
                            log::trace!("brotli'd body from {} to {}", bytes.len(), data.len());
                            compression_used = true;
                            body = Body::new_static(data);
                        }
                    }

                    CompressionAlgorithm::Gzip => {
                        let bytes = body.static_bytes().unwrap();
                        let mut data = vec![];
                        let mut encoder = GzipEncoder::new(Cursor::new(bytes));
                        conn_try!(encoder.read_to_end(&mut data).await, conn);
                        if data.len() < bytes.len() {
                            log::trace!("gzipped body from {} to {}", bytes.len(), data.len());
                            body = Body::new_static(data);
                            compression_used = true;
                        }
                    }
                }
            } else if body.is_streaming() {
                compression_used = true;
                match algo {
                    CompressionAlgorithm::Zstd => {
                        body = Body::new_streaming(
                            ZstdEncoder::new(BufReader::new(body.into_reader())),
                            None,
                        );
                    }

                    CompressionAlgorithm::Brotli => {
                        body = Body::new_streaming(
                            BrotliEncoder::new(BufReader::new(body.into_reader())),
                            None,
                        );
                    }

                    CompressionAlgorithm::Gzip => {
                        body = Body::new_streaming(
                            GzipEncoder::new(BufReader::new(body.into_reader())),
                            None,
                        );
                    }
                }
            }

            if compression_used {
                let vary = conn
                    .headers_mut()
                    .get_str(Vary)
                    .map(|vary| HeaderValues::from(format!("{vary}, Accept-Encoding")))
                    .unwrap_or_else(|| HeaderValues::from("Accept-Encoding"));

                conn.headers_mut().extend([
                    (ContentEncoding, HeaderValues::from(algo.as_str())),
                    (Vary, vary),
                ]);
            }

            conn.with_body(body)
        } else {
            conn
        }
    }
}

/// Alias for [`Compression::new`](crate::Compression::new)
pub fn compression() -> Compression {
    Compression::new()
}