tower_http/decompression/request/layer.rs
1use super::service::RequestDecompression;
2use crate::compression_utils::AcceptEncoding;
3use tower_layer::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::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 #[cfg(feature = "decompression-gzip")]
42 pub fn gzip(mut self, enable: bool) -> Self {
43 self.accept.set_gzip(enable);
44 self
45 }
46
47 /// Sets whether to support Deflate encoding.
48 #[cfg(feature = "decompression-deflate")]
49 pub fn deflate(mut self, enable: bool) -> Self {
50 self.accept.set_deflate(enable);
51 self
52 }
53
54 /// Sets whether to support Brotli encoding.
55 #[cfg(feature = "decompression-br")]
56 pub fn br(mut self, enable: bool) -> Self {
57 self.accept.set_br(enable);
58 self
59 }
60
61 /// Sets whether to support Zstd encoding.
62 #[cfg(feature = "decompression-zstd")]
63 pub fn zstd(mut self, enable: bool) -> Self {
64 self.accept.set_zstd(enable);
65 self
66 }
67
68 /// Disables support for gzip encoding.
69 ///
70 /// This method is available even if the `gzip` crate feature is disabled.
71 pub fn no_gzip(mut self) -> Self {
72 self.accept.set_gzip(false);
73 self
74 }
75
76 /// Disables support for Deflate encoding.
77 ///
78 /// This method is available even if the `deflate` crate feature is disabled.
79 pub fn no_deflate(mut self) -> Self {
80 self.accept.set_deflate(false);
81 self
82 }
83
84 /// Disables support for Brotli encoding.
85 ///
86 /// This method is available even if the `br` crate feature is disabled.
87 pub fn no_br(mut self) -> Self {
88 self.accept.set_br(false);
89 self
90 }
91
92 /// Disables support for Zstd encoding.
93 ///
94 /// This method is available even if the `zstd` crate feature is disabled.
95 pub fn no_zstd(mut self) -> Self {
96 self.accept.set_zstd(false);
97 self
98 }
99
100 /// Sets whether to pass through the request even when the encoding is not supported.
101 pub fn pass_through_unaccepted(mut self, enable: bool) -> Self {
102 self.pass_through_unaccepted = enable;
103 self
104 }
105}