Skip to main content

sark_http/payload/compress/
mod.rs

1use miniz_oxide::deflate::core::TDEFLStatus;
2use o3::buffer::pool::owners;
3
4#[doc(hidden)]
5pub mod dynamic;
6mod encoder;
7pub mod inflate;
8
9use std::num;
10
11use o3::{
12    alloc,
13    buffer::pool::{self, state},
14};
15
16/// Compression storage selected when constructing a protocol connection.
17pub trait Compression: Sized {
18    fn try_new() -> Result<Self, alloc::Error>;
19
20    /// Returns a smaller gzip body or keeps the original bytes unchanged.
21    fn encode(&mut self, bytes: &[u8]) -> Output;
22
23    fn try_encode(&mut self, bytes: &[u8]) -> Result<Frozen, Error>;
24}
25
26#[doc(hidden)]
27pub type Frozen = pool::Span<
28    owners::Plain<pool::Fixed<{ Gzip::SLOTS.get() }, { Gzip::CAPACITY.get() }>>,
29    pool::Owned,
30    pool::Frozen,
31>;
32
33pub enum Output {
34    Plain,
35    Compressed(Frozen),
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum Error {
40    Unavailable,
41    Input {
42        actual: usize,
43        limit: usize,
44    },
45    Exhausted,
46    Deflate {
47        status: TDEFLStatus,
48        read: usize,
49        wrote: usize,
50    },
51    Extent {
52        wrote: usize,
53        capacity: usize,
54        maximum: usize,
55    },
56}
57
58pub struct Gzip {
59    encoder: encoder::Encoder,
60    pool: pool::Pool<
61        state::Initialized,
62        owners::Plain<pool::Fixed<{ Gzip::SLOTS.get() }, { Gzip::CAPACITY.get() }>>,
63    >,
64}
65
66impl Gzip {
67    pub const SLOTS: num::NonZero<usize> = num::NonZero::<usize>::MIN.saturating_add(31);
68    pub const CAPACITY: num::NonZero<usize> =
69        num::NonZero::<usize>::MIN.saturating_add(256 * 1024 - 1);
70
71    fn try_compress(&mut self, src: &[u8], maximum: usize) -> Result<Frozen, Error> {
72        let mut lease = self.pool.acquire().ok_or(Error::Exhausted)?;
73        self.encoder.encode(src, lease.output(), maximum)?;
74        Ok(lease.freeze())
75    }
76}
77
78impl Compression for Gzip {
79    fn try_new() -> Result<Self, alloc::Error> {
80        let encoder = encoder::Encoder::try_new()?;
81        let pool = pool::Pool::try_from(pool::Fixed::new())?;
82        Ok(Self { encoder, pool })
83    }
84
85    fn encode(&mut self, src: &[u8]) -> Output {
86        if !(encoder::FRAMING + 1..=encoder::MAX_INPUT).contains(&src.len()) {
87            return Output::Plain;
88        }
89        match self.try_compress(src, src.len() - 1) {
90            Ok(frozen) => Output::Compressed(frozen),
91            Err(_) => Output::Plain,
92        }
93    }
94
95    fn try_encode(&mut self, src: &[u8]) -> Result<Frozen, Error> {
96        if src.len() > encoder::MAX_INPUT {
97            return Err(Error::Input {
98                actual: src.len(),
99                limit: encoder::MAX_INPUT,
100            });
101        }
102        self.try_compress(src, Self::CAPACITY.get())
103    }
104}
105
106impl Compression for () {
107    fn try_new() -> Result<Self, alloc::Error> {
108        Ok(())
109    }
110
111    fn encode(&mut self, _: &[u8]) -> Output {
112        Output::Plain
113    }
114
115    fn try_encode(&mut self, _: &[u8]) -> Result<Frozen, Error> {
116        Err(Error::Unavailable)
117    }
118}