Skip to main content

hyperlane_plugin_compression/
struct.rs

1use super::*;
2
3/// Configuration for [`CompressionPlugin`].
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct CompressionConfig {
6    /// Whether inbound compressed requests (`gzip`, `deflate`, `br`) are automatically decompressed.
7    #[get(type(copy))]
8    pub(crate) decompress_requests: bool,
9    /// Whether unknown inbound `Content-Encoding` headers return `415 Unsupported Media Type`.
10    #[get(type(copy))]
11    pub(crate) reject_unknown_encoding: bool,
12    /// Upper bound in bytes on decompressed inbound request payloads (`413` when exceeded).
13    #[get(type(copy))]
14    pub(crate) max_decompressed_bytes: usize,
15    /// Whether outgoing responses are automatically negotiated via `Accept-Encoding`.
16    #[get(type(copy))]
17    pub(crate) compress_responses: bool,
18    /// Minimum outgoing body length eligible for compression.
19    #[get(type(copy))]
20    pub(crate) min_body_bytes: usize,
21    /// Enable Brotli (`br`) response compression.
22    #[get(type(copy))]
23    pub(crate) enable_brotli: bool,
24    /// Enable Gzip (`gzip`) response compression.
25    #[get(type(copy))]
26    pub(crate) enable_gzip: bool,
27    /// Enable Deflate (`deflate`) response compression.
28    #[get(type(copy))]
29    pub(crate) enable_deflate: bool,
30    /// MIME type prefixes skipped for response compression.
31    pub(crate) skip_mime_prefixes: Vec<String>,
32}
33
34/// Precompiled compression engine backing [`CompressionRequestHook`] and [`CompressionResponseHook`].
35#[derive(Clone, Debug)]
36pub struct CompressionPlugin {
37    /// Retained configuration.
38    pub(crate) config: CompressionConfig,
39    /// Lowercased excluded MIME prefixes.
40    pub(crate) skip_mime_lower: Box<[String]>,
41    /// Prebuilt `415 Unsupported Media Type` wire response.
42    pub(crate) unsupported_wire: Vec<u8>,
43    /// Prebuilt `413 Payload Too Large` wire response.
44    pub(crate) limit_wire: Vec<u8>,
45}
46
47/// Opt-in request middleware performing bounded decompression of compressed request bodies.
48#[derive(Clone, Copy, Debug, Default)]
49#[hyperlane::request_middleware(-104)]
50pub struct CompressionRequestHook;
51
52/// Opt-in response middleware negotiating `Content-Encoding` and merging `Vary: Accept-Encoding`.
53#[derive(Clone, Copy, Debug, Default)]
54#[hyperlane::response_middleware(-55)]
55pub struct CompressionResponseHook;