tower_http/decompression/
layer.rs

1use super::Decompression;
2use crate::compression_utils::AcceptEncoding;
3use tower_layer::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::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    #[cfg(feature = "decompression-gzip")]
35    pub fn gzip(mut self, enable: bool) -> Self {
36        self.accept.set_gzip(enable);
37        self
38    }
39
40    /// Sets whether to request the Deflate encoding.
41    #[cfg(feature = "decompression-deflate")]
42    pub fn deflate(mut self, enable: bool) -> Self {
43        self.accept.set_deflate(enable);
44        self
45    }
46
47    /// Sets whether to request the Brotli encoding.
48    #[cfg(feature = "decompression-br")]
49    pub fn br(mut self, enable: bool) -> Self {
50        self.accept.set_br(enable);
51        self
52    }
53
54    /// Sets whether to request the Zstd encoding.
55    #[cfg(feature = "decompression-zstd")]
56    pub fn zstd(mut self, enable: bool) -> Self {
57        self.accept.set_zstd(enable);
58        self
59    }
60
61    /// Disables the gzip encoding.
62    ///
63    /// This method is available even if the `gzip` crate feature is disabled.
64    pub fn no_gzip(mut self) -> Self {
65        self.accept.set_gzip(false);
66        self
67    }
68
69    /// Disables the Deflate encoding.
70    ///
71    /// This method is available even if the `deflate` crate feature is disabled.
72    pub fn no_deflate(mut self) -> Self {
73        self.accept.set_deflate(false);
74        self
75    }
76
77    /// Disables the Brotli encoding.
78    ///
79    /// This method is available even if the `br` crate feature is disabled.
80    pub fn no_br(mut self) -> Self {
81        self.accept.set_br(false);
82        self
83    }
84
85    /// Disables the Zstd encoding.
86    ///
87    /// This method is available even if the `zstd` crate feature is disabled.
88    pub fn no_zstd(mut self) -> Self {
89        self.accept.set_zstd(false);
90        self
91    }
92}