rama_http/layer/decompression/
layer.rs

1use super::Decompression;
2use crate::headers::encoding::AcceptEncoding;
3use rama_core::Layer;
4
5/// Decompresses response bodies of the underlying service.
6///
7/// This adds the `Accept-Encoding` header to requests and transparently decompresses response
8/// bodies based on the `Content-Encoding` header.
9///
10/// See the [module docs](crate::layer::decompression) for more details.
11#[derive(Debug, Default, Clone)]
12pub struct DecompressionLayer {
13    accept: AcceptEncoding,
14}
15
16impl<S> Layer<S> for DecompressionLayer {
17    type Service = Decompression<S>;
18
19    fn layer(&self, service: S) -> Self::Service {
20        Decompression {
21            inner: service,
22            accept: self.accept,
23        }
24    }
25}
26
27impl DecompressionLayer {
28    /// Creates a new `DecompressionLayer`.
29    pub fn new() -> Self {
30        Default::default()
31    }
32
33    /// Sets whether to request the gzip encoding.
34    pub fn gzip(mut self, enable: bool) -> Self {
35        self.accept.set_gzip(enable);
36        self
37    }
38
39    /// Sets whether to request the gzip encoding.
40    pub fn set_gzip(&mut self, enable: bool) -> &mut Self {
41        self.accept.set_gzip(enable);
42        self
43    }
44
45    /// Sets whether to request the Deflate encoding.
46    pub fn deflate(mut self, enable: bool) -> Self {
47        self.accept.set_deflate(enable);
48        self
49    }
50
51    /// Sets whether to request the Deflate encoding.
52    pub fn set_deflate(&mut self, enable: bool) -> &mut Self {
53        self.accept.set_deflate(enable);
54        self
55    }
56
57    /// Sets whether to request the Brotli encoding.
58    pub fn br(mut self, enable: bool) -> Self {
59        self.accept.set_br(enable);
60        self
61    }
62
63    /// Sets whether to request the Brotli encoding.
64    pub fn set_br(&mut self, enable: bool) -> &mut Self {
65        self.accept.set_br(enable);
66        self
67    }
68
69    /// Sets whether to request the Zstd encoding.
70    pub fn zstd(mut self, enable: bool) -> Self {
71        self.accept.set_zstd(enable);
72        self
73    }
74
75    /// Sets whether to request the Zstd encoding.
76    pub fn set_zstd(&mut self, enable: bool) -> &mut Self {
77        self.accept.set_zstd(enable);
78        self
79    }
80}