1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use super::{DecompressionBody, DecompressionLayer, ResponseFuture};
use crate::compression_utils::AcceptEncoding;
use http::{
header::{self, ACCEPT_ENCODING},
Request, Response,
};
use http_body::Body;
use std::task::{Context, Poll};
use tower_service::Service;
#[derive(Debug, Clone)]
pub struct Decompression<S> {
pub(crate) inner: S,
pub(crate) accept: AcceptEncoding,
}
impl<S> Decompression<S> {
pub fn new(service: S) -> Self {
Self {
inner: service,
accept: AcceptEncoding::default(),
}
}
define_inner_service_accessors!();
pub fn layer() -> DecompressionLayer {
DecompressionLayer::new()
}
#[cfg(feature = "decompression-gzip")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-gzip")))]
pub fn gzip(mut self, enable: bool) -> Self {
self.accept.set_gzip(enable);
self
}
#[cfg(feature = "decompression-deflate")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-deflate")))]
pub fn deflate(mut self, enable: bool) -> Self {
self.accept.set_deflate(enable);
self
}
#[cfg(feature = "decompression-br")]
#[cfg_attr(docsrs, doc(cfg(feature = "decompression-br")))]
pub fn br(mut self, enable: bool) -> Self {
self.accept.set_br(enable);
self
}
pub fn no_gzip(mut self) -> Self {
self.accept.set_gzip(false);
self
}
pub fn no_deflate(mut self) -> Self {
self.accept.set_deflate(false);
self
}
pub fn no_br(mut self) -> Self {
self.accept.set_br(false);
self
}
}
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for Decompression<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
ResBody: Body,
{
type Response = Response<DecompressionBody<ResBody>>;
type Error = S::Error;
type Future = ResponseFuture<S::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
if let header::Entry::Vacant(entry) = req.headers_mut().entry(ACCEPT_ENCODING) {
if let Some(accept) = self.accept.to_header_value() {
entry.insert(accept);
}
}
ResponseFuture {
inner: self.inner.call(req),
accept: self.accept,
}
}
}