@@ -67,10 +67,10 @@ pub struct ResponseCompressionCtx(CtxInner);
67
67
68
68
enum CtxInner {
69
69
HeaderPhase {
70
- decompress_enable : bool ,
71
70
// Store the preferred list to compare with content-encoding
72
71
accept_encoding : Vec < Algorithm > ,
73
72
encoding_levels : [ u32 ; Algorithm :: COUNT ] ,
73
+ decompress_enable : [ bool ; Algorithm :: COUNT ] ,
74
74
} ,
75
75
BodyPhase ( Option < Box < dyn Encode + Send + Sync > > ) ,
76
76
}
@@ -81,9 +81,9 @@ impl ResponseCompressionCtx {
81
81
/// The `decompress_enable` flag will tell the ctx to decompress if needed.
82
82
pub fn new ( compression_level : u32 , decompress_enable : bool ) -> Self {
83
83
Self ( CtxInner :: HeaderPhase {
84
- decompress_enable,
85
84
accept_encoding : Vec :: new ( ) ,
86
85
encoding_levels : [ compression_level; Algorithm :: COUNT ] ,
86
+ decompress_enable : [ decompress_enable; Algorithm :: COUNT ] ,
87
87
} )
88
88
}
89
89
@@ -93,9 +93,9 @@ impl ResponseCompressionCtx {
93
93
match & self . 0 {
94
94
CtxInner :: HeaderPhase {
95
95
decompress_enable,
96
- accept_encoding : _,
97
96
encoding_levels : levels,
98
- } => levels. iter ( ) . any ( |l| * l != 0 ) || * decompress_enable,
97
+ ..
98
+ } => levels. iter ( ) . any ( |l| * l != 0 ) || decompress_enable. iter ( ) . any ( |d| * d) ,
99
99
CtxInner :: BodyPhase ( c) => c. is_some ( ) ,
100
100
}
101
101
}
@@ -104,11 +104,7 @@ impl ResponseCompressionCtx {
104
104
/// algorithm name, in bytes, out bytes, time took for the compression
105
105
pub fn get_info ( & self ) -> Option < ( & ' static str , usize , usize , Duration ) > {
106
106
match & self . 0 {
107
- CtxInner :: HeaderPhase {
108
- decompress_enable : _,
109
- accept_encoding : _,
110
- encoding_levels : _,
111
- } => None ,
107
+ CtxInner :: HeaderPhase { .. } => None ,
112
108
CtxInner :: BodyPhase ( c) => c. as_ref ( ) . map ( |c| c. stat ( ) ) ,
113
109
}
114
110
}
@@ -119,9 +115,8 @@ impl ResponseCompressionCtx {
119
115
pub fn adjust_level ( & mut self , new_level : u32 ) {
120
116
match & mut self . 0 {
121
117
CtxInner :: HeaderPhase {
122
- decompress_enable : _,
123
- accept_encoding : _,
124
118
encoding_levels : levels,
119
+ ..
125
120
} => {
126
121
* levels = [ new_level; Algorithm :: COUNT ] ;
127
122
}
@@ -135,27 +130,38 @@ impl ResponseCompressionCtx {
135
130
pub fn adjust_algorithm_level ( & mut self , algorithm : Algorithm , new_level : u32 ) {
136
131
match & mut self . 0 {
137
132
CtxInner :: HeaderPhase {
138
- decompress_enable : _,
139
- accept_encoding : _,
140
133
encoding_levels : levels,
134
+ ..
141
135
} => {
142
136
levels[ algorithm. index ( ) ] = new_level;
143
137
}
144
138
CtxInner :: BodyPhase ( _) => panic ! ( "Wrong phase: BodyPhase" ) ,
145
139
}
146
140
}
147
141
148
- /// Adjust the decompression flag.
142
+ /// Adjust the decompression flag for all compression algorithms .
149
143
/// # Panic
150
144
/// This function will panic if it has already started encoding the response body.
151
145
pub fn adjust_decompression ( & mut self , enabled : bool ) {
152
146
match & mut self . 0 {
153
147
CtxInner :: HeaderPhase {
154
- decompress_enable,
155
- accept_encoding : _,
156
- encoding_levels : _,
148
+ decompress_enable, ..
157
149
} => {
158
- * decompress_enable = enabled;
150
+ * decompress_enable = [ enabled; Algorithm :: COUNT ] ;
151
+ }
152
+ CtxInner :: BodyPhase ( _) => panic ! ( "Wrong phase: BodyPhase" ) ,
153
+ }
154
+ }
155
+
156
+ /// Adjust the decompression flag for a specific algorithm.
157
+ /// # Panic
158
+ /// This function will panic if it has already started encoding the response body.
159
+ pub fn adjust_algorithm_decompression ( & mut self , algorithm : Algorithm , enabled : bool ) {
160
+ match & mut self . 0 {
161
+ CtxInner :: HeaderPhase {
162
+ decompress_enable, ..
163
+ } => {
164
+ decompress_enable[ algorithm. index ( ) ] = enabled;
159
165
}
160
166
CtxInner :: BodyPhase ( _) => panic ! ( "Wrong phase: BodyPhase" ) ,
161
167
}
@@ -208,7 +214,9 @@ impl ResponseCompressionCtx {
208
214
let encoder = match action {
209
215
Action :: Noop => None ,
210
216
Action :: Compress ( algorithm) => algorithm. compressor ( levels[ algorithm. index ( ) ] ) ,
211
- Action :: Decompress ( algorithm) => algorithm. decompressor ( * decompress_enable) ,
217
+ Action :: Decompress ( algorithm) => {
218
+ algorithm. decompressor ( decompress_enable[ algorithm. index ( ) ] )
219
+ }
212
220
} ;
213
221
if encoder. is_some ( ) {
214
222
adjust_response_header ( resp, & action) ;
@@ -317,6 +325,7 @@ impl Algorithm {
317
325
None
318
326
} else {
319
327
match self {
328
+ Self :: Gzip => Some ( Box :: new ( gzip:: Decompressor :: new ( ) ) ) ,
320
329
Self :: Brotli => Some ( Box :: new ( brotli:: Decompressor :: new ( ) ) ) ,
321
330
_ => None , // not implemented
322
331
}
0 commit comments