1
+ use brotli:: enc:: BrotliEncoderParams ;
1
2
use bytes:: { Bytes , BytesMut } ;
3
+ use flate2:: Compression ;
2
4
use futures:: { Stream , StreamExt } ;
3
5
use tokio:: sync:: mpsc;
4
6
use tokio_stream:: wrappers:: ReceiverStream ;
5
7
use tower:: BoxError ;
6
8
7
9
use self :: {
8
- codec:: { DeflateEncoder , Encode , GzipEncoder } ,
10
+ codec:: { BrotliEncoder , DeflateEncoder , Encode , GzipEncoder , ZstdEncoder } ,
9
11
util:: PartialBuffer ,
10
12
} ;
11
13
@@ -14,16 +16,41 @@ pub(crate) mod unshared;
14
16
pub ( crate ) mod util;
15
17
16
18
pub ( crate ) enum Compressor {
17
- //Identity,
18
19
Deflate ( DeflateEncoder ) ,
19
20
Gzip ( GzipEncoder ) ,
20
- //Brotli(BrotliEncoder),
21
- //Zstd,
22
- //others?
21
+ Brotli ( BrotliEncoder ) ,
22
+ Zstd ( ZstdEncoder ) ,
23
23
}
24
24
25
25
//FIXME: we should call finish at the end
26
26
impl Compressor {
27
+ pub ( crate ) fn new < ' a , It : ' a > ( it : It ) -> Option < Self >
28
+ where
29
+ It : Iterator < Item = & ' a str > ,
30
+ {
31
+ for s in it {
32
+ match s {
33
+ "gzip" => return Some ( Compressor :: Gzip ( GzipEncoder :: new ( Compression :: fast ( ) ) ) ) ,
34
+ "deflate" => {
35
+ return Some ( Compressor :: Deflate (
36
+ DeflateEncoder :: new ( Compression :: fast ( ) ) ,
37
+ ) )
38
+ }
39
+ // FIXME: find the "fast" brotli encoder params
40
+ "br" => {
41
+ return Some ( Compressor :: Brotli ( BrotliEncoder :: new (
42
+ BrotliEncoderParams :: default ( ) ,
43
+ ) ) )
44
+ }
45
+ "zstd" => {
46
+ return Some ( Compressor :: Zstd ( ZstdEncoder :: new ( zstd_safe:: min_c_level ( ) ) ) )
47
+ }
48
+ _ => { }
49
+ }
50
+ }
51
+ None
52
+ }
53
+
27
54
pub ( crate ) fn process (
28
55
mut self ,
29
56
mut stream : hyper:: Body ,
@@ -110,6 +137,8 @@ impl Encode for Compressor {
110
137
match self {
111
138
Compressor :: Deflate ( e) => e. encode ( input, output) ,
112
139
Compressor :: Gzip ( e) => e. encode ( input, output) ,
140
+ Compressor :: Brotli ( e) => e. encode ( input, output) ,
141
+ Compressor :: Zstd ( e) => e. encode ( input, output) ,
113
142
}
114
143
}
115
144
@@ -120,6 +149,8 @@ impl Encode for Compressor {
120
149
match self {
121
150
Compressor :: Deflate ( e) => e. flush ( output) ,
122
151
Compressor :: Gzip ( e) => e. flush ( output) ,
152
+ Compressor :: Brotli ( e) => e. flush ( output) ,
153
+ Compressor :: Zstd ( e) => e. flush ( output) ,
123
154
}
124
155
}
125
156
@@ -130,6 +161,8 @@ impl Encode for Compressor {
130
161
match self {
131
162
Compressor :: Deflate ( e) => e. finish ( output) ,
132
163
Compressor :: Gzip ( e) => e. finish ( output) ,
164
+ Compressor :: Brotli ( e) => e. finish ( output) ,
165
+ Compressor :: Zstd ( e) => e. finish ( output) ,
133
166
}
134
167
}
135
168
}
0 commit comments