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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2024-2025 Tree xie.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::{
Error, accepts_encoding, get_bool_conf, get_hash_key, get_str_slice_conf,
};
use async_trait::async_trait;
use pingap_config::PluginConf;
use pingap_core::{Ctx, Plugin, PluginStep, RequestPluginResult};
use pingora::proxy::Session;
use smallvec::SmallVec;
use std::borrow::Cow;
use tracing::debug;
type Result<T, E = Error> = std::result::Result<T, E>;
/// A plugin that filters and modifies the Accept-Encoding header of incoming HTTP requests.
/// It ensures that only supported compression encodings are passed to upstream servers.
/// And the order of encoding is the same as customized encodings.
pub struct AcceptEncoding {
/// List of supported compression encodings (e.g., "gzip", "br", "zstd").
/// These encodings are matched against the client's Accept-Encoding header.
/// Only encodings present in this list will be preserved in the request.
encodings: Vec<String>,
/// Controls whether multiple encodings can be forwarded to the upstream server.
only_one_encoding: bool,
/// A unique identifier for this plugin instance.
/// Used for internal tracking and debugging purposes.
hash_value: String,
/// Specifies the phase in the request processing pipeline when this plugin should execute.
/// This plugin typically runs in the EarlyRequest phase to modify headers before forwarding.
plugin_step: PluginStep,
}
impl TryFrom<&PluginConf> for AcceptEncoding {
type Error = Error;
fn try_from(value: &PluginConf) -> Result<Self> {
let hash_value = get_hash_key(value);
let only_one_encoding = get_bool_conf(value, "only_one_encoding");
// `"zstd, br, gzip"` or `["zstd", "br", "gzip"]`. Only the first
// was read, and the list - the form the sample configuration shows
// - counted as no encoding at all: `Accept-Encoding` was removed
// from every request.
let encodings: Vec<String> = get_str_slice_conf(value, "encodings")
.iter()
.flat_map(|item| item.split(','))
.map(str::trim)
.filter(|item| !item.is_empty())
.map(String::from)
.collect();
Ok(Self {
encodings,
only_one_encoding,
hash_value,
plugin_step: PluginStep::EarlyRequest,
})
}
}
impl AcceptEncoding {
/// Creates a new AcceptEncoding plugin instance from the provided configuration.
///
/// # Arguments
/// * `params` - Plugin configuration containing encoding settings
///
/// # Returns
/// * `Result<Self>` - A new AcceptEncoding instance or an error if configuration is invalid
pub fn new(params: &PluginConf) -> Result<Self> {
debug!(
params = pingap_config::masked_toml(params),
"new accept encoding plugin"
);
Self::try_from(params)
}
}
#[async_trait]
impl Plugin for AcceptEncoding {
/// Returns the unique hash key for this plugin instance
#[inline]
fn config_key(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.hash_value)
}
/// Processes the HTTP request by filtering the Accept-Encoding header.
///
/// # Arguments
/// * `step` - Current plugin processing step
/// * `session` - HTTP session containing request/response data
/// * `_ctx` - Ctx context (unused in this implementation)
///
/// # Returns
/// * `pingora::Result<Option<HttpResponse>>` - None if processing should continue,
/// or a response if the request should be terminated
#[inline]
async fn handle_request(
&self,
step: PluginStep,
session: &mut Session,
_ctx: &mut Ctx,
) -> pingora::Result<RequestPluginResult> {
// Skip if not in the correct plugin step
if step != self.plugin_step {
return Ok(RequestPluginResult::Skipped);
}
let header = session.req_header_mut();
// Get the Accept-Encoding header from the request
let Some(accept_encoding) =
header.headers.get(http::header::ACCEPT_ENCODING)
else {
return Ok(RequestPluginResult::Skipped);
};
let accept_encoding = accept_encoding.to_str().unwrap_or_default();
// The kept encodings borrow the configured names; only the joined
// header value is built per request.
let mut new_accept_encodings: SmallVec<[&str; 3]> = SmallVec::new();
// Filter the accepted encodings based on our supported list
for encoding in self.encodings.iter() {
// If only_one_encoding is true, stop after finding the first match
if self.only_one_encoding && !new_accept_encodings.is_empty() {
break;
}
// Add encoding if the client actually listed it (token match, not
// a substring: `x-gzip` must not match `gzip`).
if accepts_encoding(accept_encoding, encoding) {
new_accept_encodings.push(encoding.as_str());
}
}
// Remove the header if no supported encodings found
// Otherwise, set the header to our filtered list
if new_accept_encodings.is_empty() {
header.remove_header(&http::header::ACCEPT_ENCODING);
} else {
let _ = header.insert_header(
http::header::ACCEPT_ENCODING,
new_accept_encodings.join(", "),
);
}
Ok(RequestPluginResult::Continue)
}
}
register_plugin!("accept_encoding", AcceptEncoding);
#[cfg(test)]
mod tests {
use super::{AcceptEncoding, Plugin};
use pingap_config::PluginConf;
use pingap_core::{Ctx, PluginStep};
use pingora::modules::http::HttpModules;
use pingora::proxy::Session;
use pretty_assertions::assert_eq;
use tokio_test::io::Builder;
#[test]
fn test_accept_encoding_takes_a_list() {
// Regression: the list, as the sample configuration has it, was
// read as no encoding at all.
for conf in [
"encodings = [\"zstd\", \"br\", \"gzip\"]",
"encodings = \"zstd, br, gzip\"",
"encodings = [\"zstd, br\", \"gzip\"]",
] {
let params = AcceptEncoding::try_from(
&toml::from_str::<PluginConf>(conf).unwrap(),
)
.unwrap();
assert_eq!(vec!["zstd", "br", "gzip"], params.encodings, "{conf}");
}
}
#[test]
fn test_accept_encoding_params() {
let params = AcceptEncoding::try_from(
&toml::from_str::<PluginConf>(
r###"
encodings = "zstd, br, gzip"
only_one_encoding = true
"###,
)
.unwrap(),
)
.unwrap();
assert_eq!("zstd,br,gzip", params.encodings.join(","));
assert_eq!(true, params.only_one_encoding);
}
#[tokio::test]
async fn test_accept_conding() {
let accept_encoding = AcceptEncoding::try_from(
&toml::from_str::<PluginConf>(
r###"
encodings = "zstd, br, gzip"
only_one_encoding = true
"###,
)
.unwrap(),
)
.unwrap();
// zstd
let headers = ["Accept-Encoding: gzip, zstd"].join("\r\n");
let input_header =
format!("GET /vicanso/pingap?size=1 HTTP/1.1\r\n{headers}\r\n\r\n");
let mock_io = Builder::new().read(input_header.as_bytes()).build();
let mut session = Session::new_h1_with_modules(
Box::new(mock_io),
&HttpModules::new(),
);
session.read_request().await.unwrap();
let _ = accept_encoding
.handle_request(
PluginStep::EarlyRequest,
&mut session,
&mut Ctx::default(),
)
.await
.unwrap();
assert_eq!(
"zstd",
session
.req_header()
.headers
.get("Accept-Encoding")
.unwrap()
.to_str()
.unwrap()
);
// multi accept encoding
let accept_encoding = AcceptEncoding::try_from(
&toml::from_str::<PluginConf>(
r###"
encodings = "zstd, br, gzip"
"###,
)
.unwrap(),
)
.unwrap();
let headers = ["Accept-Encoding: gzip, br, zstd"].join("\r\n");
let input_header =
format!("GET /vicanso/pingap?size=1 HTTP/1.1\r\n{headers}\r\n\r\n");
let mock_io = Builder::new().read(input_header.as_bytes()).build();
let mut session = Session::new_h1_with_modules(
Box::new(mock_io),
&HttpModules::new(),
);
session.read_request().await.unwrap();
let _ = accept_encoding
.handle_request(
PluginStep::EarlyRequest,
&mut session,
&mut Ctx::default(),
)
.await
.unwrap();
assert_eq!(
"zstd, br, gzip",
session
.req_header()
.headers
.get("Accept-Encoding")
.unwrap()
.to_str()
.unwrap()
);
let headers = ["Accept-Encoding: snappy"].join("\r\n");
let input_header =
format!("GET /vicanso/pingap?size=1 HTTP/1.1\r\n{headers}\r\n\r\n");
let mock_io = Builder::new().read(input_header.as_bytes()).build();
let mut session = Session::new_h1_with_modules(
Box::new(mock_io),
&HttpModules::new(),
);
session.read_request().await.unwrap();
let _ = accept_encoding
.handle_request(
PluginStep::EarlyRequest,
&mut session,
&mut Ctx::default(),
)
.await
.unwrap();
assert_eq!(
true,
session
.req_header()
.headers
.get("Accept-Encoding")
.is_none()
);
}
}