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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use super::{Compression, Predicate};
use crate::compression::predicate::DefaultPredicate;
use crate::compression_utils::AcceptEncoding;
use tower_layer::Layer;
#[derive(Clone, Debug, Default)]
pub struct CompressionLayer<P = DefaultPredicate> {
accept: AcceptEncoding,
predicate: P,
}
impl<S, P> Layer<S> for CompressionLayer<P>
where
P: Predicate,
{
type Service = Compression<S, P>;
fn layer(&self, inner: S) -> Self::Service {
Compression {
inner,
accept: self.accept,
predicate: self.predicate.clone(),
}
}
}
impl CompressionLayer {
pub fn new() -> Self {
Self::default()
}
#[cfg(feature = "compression-gzip")]
pub fn gzip(mut self, enable: bool) -> Self {
self.accept.set_gzip(enable);
self
}
#[cfg(feature = "compression-deflate")]
pub fn deflate(mut self, enable: bool) -> Self {
self.accept.set_deflate(enable);
self
}
#[cfg(feature = "compression-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
}
pub fn compress_when<C>(self, predicate: C) -> CompressionLayer<C>
where
C: Predicate,
{
CompressionLayer {
accept: self.accept,
predicate,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::{header::ACCEPT_ENCODING, Request, Response};
use http_body::Body as _;
use hyper::Body;
use tokio::fs::File;
use bytes::{Bytes, BytesMut};
use std::convert::Infallible;
use tokio_util::io::ReaderStream;
use tower::{Service, ServiceBuilder, ServiceExt};
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let file = File::open("Cargo.toml").await.expect("file missing");
let stream = ReaderStream::new(file);
let body = Body::wrap_stream(stream);
Ok(Response::new(body))
}
#[tokio::test]
async fn accept_encoding_configuration_works() -> Result<(), crate::BoxError> {
let deflate_only_layer = CompressionLayer::new().no_br().no_gzip();
let mut service = ServiceBuilder::new()
.layer(deflate_only_layer)
.service_fn(handle);
let request = Request::builder()
.header(ACCEPT_ENCODING, "gzip, deflate, br")
.body(Body::empty())?;
let response = service.ready().await?.call(request).await?;
assert_eq!(response.headers()["content-encoding"], "deflate");
let mut body = response.into_body();
let mut bytes = BytesMut::new();
while let Some(chunk) = body.data().await {
let chunk = chunk?;
bytes.extend_from_slice(&chunk[..]);
}
let bytes: Bytes = bytes.freeze();
let deflate_bytes_len = bytes.len();
let br_only_layer = CompressionLayer::new().no_gzip().no_deflate();
let mut service = ServiceBuilder::new()
.layer(br_only_layer)
.service_fn(handle);
let request = Request::builder()
.header(ACCEPT_ENCODING, "gzip, deflate, br")
.body(Body::empty())?;
let response = service.ready().await?.call(request).await?;
assert_eq!(response.headers()["content-encoding"], "br");
let mut body = response.into_body();
let mut bytes = BytesMut::new();
while let Some(chunk) = body.data().await {
let chunk = chunk?;
bytes.extend_from_slice(&chunk[..]);
}
let bytes: Bytes = bytes.freeze();
let br_byte_length = bytes.len();
assert!(br_byte_length < deflate_bytes_len * 9 / 10);
Ok(())
}
}