rama_http/layer/decompression/request/
layer.rs

1use super::service::RequestDecompression;
2use crate::headers::encoding::AcceptEncoding;
3use rama_core::Layer;
4
5/// Decompresses request bodies and calls its underlying service.
6///
7/// Transparently decompresses request bodies based on the `Content-Encoding` header.
8/// When the encoding in the `Content-Encoding` header is not accepted an `Unsupported Media Type`
9/// status code will be returned with the accepted encodings in the `Accept-Encoding` header.
10///
11/// Enabling pass-through of unaccepted encodings will not return an `Unsupported Media Type`. But
12/// will call the underlying service with the unmodified request if the encoding is not supported.
13/// This is disabled by default.
14///
15/// See the [module docs](crate::layer::decompression) for more details.
16#[derive(Debug, Default, Clone)]
17pub struct RequestDecompressionLayer {
18    accept: AcceptEncoding,
19    pass_through_unaccepted: bool,
20}
21
22impl<S> Layer<S> for RequestDecompressionLayer {
23    type Service = RequestDecompression<S>;
24
25    fn layer(&self, service: S) -> Self::Service {
26        RequestDecompression {
27            inner: service,
28            accept: self.accept,
29            pass_through_unaccepted: self.pass_through_unaccepted,
30        }
31    }
32}
33
34impl RequestDecompressionLayer {
35    /// Creates a new `RequestDecompressionLayer`.
36    pub fn new() -> Self {
37        Default::default()
38    }
39
40    /// Sets whether to support gzip encoding.
41    pub fn gzip(mut self, enable: bool) -> Self {
42        self.accept.set_gzip(enable);
43        self
44    }
45
46    /// Sets whether to support gzip encoding.
47    pub fn set_gzip(&mut self, enable: bool) -> &mut Self {
48        self.accept.set_gzip(enable);
49        self
50    }
51
52    /// Sets whether to support Deflate encoding.
53    pub fn deflate(mut self, enable: bool) -> Self {
54        self.accept.set_deflate(enable);
55        self
56    }
57
58    /// Sets whether to support Deflate encoding.
59    pub fn set_deflate(&mut self, enable: bool) -> &mut Self {
60        self.accept.set_deflate(enable);
61        self
62    }
63
64    /// Sets whether to support Brotli encoding.
65    pub fn br(mut self, enable: bool) -> Self {
66        self.accept.set_br(enable);
67        self
68    }
69
70    /// Sets whether to support Brotli encoding.
71    pub fn set_br(&mut self, enable: bool) -> &mut Self {
72        self.accept.set_br(enable);
73        self
74    }
75
76    /// Sets whether to support Zstd encoding.
77    pub fn zstd(mut self, enable: bool) -> Self {
78        self.accept.set_zstd(enable);
79        self
80    }
81
82    /// Sets whether to support Zstd encoding.
83    pub fn set_zstd(&mut self, enable: bool) -> &mut Self {
84        self.accept.set_zstd(enable);
85        self
86    }
87
88    /// Sets whether to pass through the request even when the encoding is not supported.
89    pub fn pass_through_unaccepted(mut self, enable: bool) -> Self {
90        self.pass_through_unaccepted = enable;
91        self
92    }
93
94    /// Sets whether to pass through the request even when the encoding is not supported.
95    pub fn set_pass_through_unaccepted(&mut self, enable: bool) -> &mut Self {
96        self.pass_through_unaccepted = enable;
97        self
98    }
99}