Skip to main content

leyline/core/config/
compression.rs

1pub(crate) const DEFAULT_MAX_BODY_SIZE: usize = 100 * 1024 * 1024;
2pub(crate) const DEFAULT_MAX_HEADER_LIST_BYTES: usize = 256 * 1024;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub struct CompressionConfig {
7    pub(crate) gzip: bool,
8    pub(crate) brotli: bool,
9    pub(crate) deflate: bool,
10    pub(crate) zstd: bool,
11    pub(crate) max_body_size: usize,
12}
13
14impl Default for CompressionConfig {
15    fn default() -> Self {
16        Self {
17            gzip: true,
18            brotli: true,
19            deflate: true,
20            zstd: true,
21            max_body_size: DEFAULT_MAX_BODY_SIZE,
22        }
23    }
24}
25
26impl CompressionConfig {
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    pub fn none() -> Self {
32        Self {
33            gzip: false,
34            brotli: false,
35            deflate: false,
36            zstd: false,
37            ..Self::default()
38        }
39    }
40
41    pub fn gzip(mut self, on: bool) -> Self {
42        self.gzip = on;
43        self
44    }
45
46    pub fn brotli(mut self, on: bool) -> Self {
47        self.brotli = on;
48        self
49    }
50
51    pub fn deflate(mut self, on: bool) -> Self {
52        self.deflate = on;
53        self
54    }
55
56    pub fn zstd(mut self, on: bool) -> Self {
57        self.zstd = on;
58        self
59    }
60
61    pub fn max_body_size(mut self, bytes: usize) -> Self {
62        self.max_body_size = bytes;
63        self
64    }
65
66    pub(crate) fn allows(&self, encoding: &str) -> bool {
67        match encoding {
68            "gzip" | "x-gzip" => self.gzip,
69            "br" => self.brotli,
70            "deflate" => self.deflate,
71            "zstd" => self.zstd,
72            "identity" | "" => true,
73            _ => false,
74        }
75    }
76}