Skip to content

Commit 5ce1b1a

Browse files
committed
refactor(*) simplify code structure.
1 parent 601cba6 commit 5ce1b1a

File tree

1 file changed

+57
-138
lines changed

1 file changed

+57
-138
lines changed

src/filter.rs

Lines changed: 57 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,220 +1,139 @@
1-
use log::{info, warn, error};
1+
use log::{info, warn};
22
use proxy_wasm::{traits::*, types::*};
33
use serde::Deserialize;
44
use serde_json_wasm::de;
5-
use std::time::Duration;
65

76
// -----------------------------------------------------------------------------
87
// Config
98
// -----------------------------------------------------------------------------
109

1110
#[derive(Deserialize, Clone, Copy, Debug)]
1211
struct Config {
13-
#[serde(default = "default_600")]
14-
tick_period: u64,
15-
16-
#[serde(default = "default_negative_1")]
17-
required_parameter: i32,
18-
}
19-
20-
fn default_600() -> u64 {
21-
600
22-
}
23-
24-
fn default_negative_1() -> i32 {
25-
-1
12+
#[serde(skip_serializing_if = "Option::is_none")]
13+
my_status_code: Option<u32>,
2614
}
2715

2816
// -----------------------------------------------------------------------------
2917
// Root Context
3018
// -----------------------------------------------------------------------------
3119

32-
const ROOT_ID: u32 = 0;
33-
3420
struct MyFilterRoot {
3521
config: Option<Config>,
3622
}
3723

3824
struct MyFilter {
3925
context_id: u32,
40-
_config: Config,
26+
config: Config,
4127
}
4228

4329
impl Context for MyFilterRoot {
44-
fn on_http_call_response(
45-
&mut self,
46-
token_id: u32,
47-
num_headers: usize,
48-
body_size: usize,
49-
_num_trailers: usize,
50-
) {
51-
info!("#{} on_http_call_response [root], token_id: {}, num_headers: {}, body_size: {}",
52-
ROOT_ID, token_id, num_headers, body_size
53-
);
54-
}
55-
56-
fn on_done(&mut self) -> bool {
57-
info!("#{} on_done", ROOT_ID);
58-
true
59-
}
30+
// fn on_http_call_response(
31+
// &mut self,
32+
// token_id: u32,
33+
// num_headers: usize,
34+
// body_size: usize,
35+
// _num_trailers: usize,
36+
// ) {
37+
// }
38+
//
39+
// fn on_done(&mut self) -> bool {
40+
// true
41+
// }
6042
}
6143

6244
impl RootContext for MyFilterRoot {
63-
fn on_vm_start(&mut self, config_size: usize) -> bool {
64-
info!("#{} on_vm_start, config_size: {}", ROOT_ID, config_size);
65-
true
66-
}
45+
// fn on_vm_start(&mut self, config_size: usize) -> bool {
46+
// true
47+
// }
48+
//
49+
// fn on_tick(&mut self) {
50+
// }
6751

6852
fn on_configure(&mut self, config_size: usize) -> bool {
69-
info!("#{} on_configure, config_size: {}", ROOT_ID, config_size);
53+
info!("on_configure, config_size: {}", config_size);
7054

7155
if let Some(config_bytes) = self.get_plugin_configuration() {
72-
assert!(config_bytes.len() == config_size);
7356
match de::from_slice::<Config>(&config_bytes) {
7457
Ok(config) => {
75-
if config.required_parameter == -1 {
76-
error!(
77-
"#{} on_configure: required_parameter not found {:?}",
78-
ROOT_ID, self.config.unwrap()
79-
);
80-
}
81-
8258
self.config = Some(config);
83-
self.set_tick_period(Duration::from_secs(config.tick_period));
84-
85-
info!(
86-
"#{} on_configure: loaded configuration: {:?}",
87-
ROOT_ID, self.config.unwrap()
88-
);
8959

9060
true
9161
}
9262
Err(err) => {
9363
warn!(
94-
"#{} on_configure: failed parsing configuration: {}: {}",
95-
ROOT_ID, String::from_utf8(config_bytes).unwrap(), err
64+
"on_configure: failed parsing configuration: {}: {}",
65+
String::from_utf8(config_bytes).unwrap(), err
9666
);
9767

9868
false
9969
}
10070
}
10171
} else {
102-
warn!("#{} on_configure: failed getting configuration", ROOT_ID);
72+
warn!("on_configure: failed getting configuration");
10373

10474
false
10575
}
10676
}
10777

108-
fn get_type(&self) -> Option<ContextType> {
109-
Some(ContextType::HttpContext)
110-
}
111-
112-
// Called when the host environment creates a new HTTP context
11378
fn create_http_context(&self, context_id: u32) -> Option<Box<dyn HttpContext>> {
114-
info!("#{} create_http_context: context_id: {}", ROOT_ID, context_id);
79+
info!("create_http_context: context_id: {}", context_id);
11580

11681
if let Some(config) = &self.config {
11782
Some(Box::new(MyFilter {
11883
context_id: context_id,
119-
_config: config.clone(),
84+
config: config.clone(),
12085
}))
12186
} else {
12287
None
12388
}
12489
}
125-
126-
fn on_tick(&mut self) {
127-
info!("#{} on_tick", ROOT_ID);
128-
129-
self.dispatch_http_call(
130-
"mockbin.org:80",
131-
vec![
132-
(":path", "/request/foo"),
133-
(":method", "GET"),
134-
(":scheme", "http"),
135-
(":authority", "mockbin.org:80"),
136-
],
137-
None,
138-
vec![],
139-
Duration::from_secs(4),
140-
)
141-
.unwrap();
142-
}
14390
}
14491

14592
// -----------------------------------------------------------------------------
14693
// Plugin Context
14794
// -----------------------------------------------------------------------------
14895

14996
impl Context for MyFilter {
150-
fn on_http_call_response(
151-
&mut self,
152-
token_id: u32,
153-
nheaders: usize,
154-
body_size: usize,
155-
_num_trailers: usize,
156-
) {
157-
info!(
158-
"#{} on_http_call_response, token_id: {}, num_headers: {}, body_size: {}",
159-
self.context_id, token_id, nheaders, body_size
160-
);
161-
}
97+
// fn on_http_call_response(
98+
// &mut self,
99+
// token_id: u32,
100+
// nheaders: usize,
101+
// body_size: usize,
102+
// _num_trailers: usize,
103+
// ) {}
162104
}
163105

164106
impl HttpContext for MyFilter {
165-
fn on_http_request_headers(&mut self, nheaders: usize, _eof: bool) -> Action {
166-
info!("#{} on_request_headers, headers: {}", self.context_id, nheaders);
167-
168-
Action::Pause
169-
}
170-
171-
fn on_http_request_body(&mut self, body_size: usize, eof: bool) -> Action {
172-
info!(
173-
"#{} on_request_body, body_size: {}, eof: {}",
174-
self.context_id, body_size, eof
175-
);
176-
177-
Action::Continue
178-
}
179-
107+
// fn on_http_request_headers(&mut self, nheaders: usize, _eof: bool) -> Action {
108+
// Action::Continue
109+
// }
110+
//
111+
// fn on_http_request_body(&mut self, body_size: usize, eof: bool) -> Action {
112+
// Action::Continue
113+
// }
114+
//
180115
fn on_http_response_headers(&mut self, nheaders: usize, _eof: bool) -> Action {
181-
info!(
182-
"#{} on_response_headers, headers: {}",
183-
self.context_id, nheaders
184-
);
185-
186-
Action::Continue
187-
}
116+
info!("#{} on_response_headers, headers: {}", self.context_id, nheaders);
188117

189-
fn on_http_response_body(&mut self, body_size: usize, eof: bool) -> Action {
190-
info!(
191-
"#{} on_response_body, body_size: {}, eof: {}",
192-
self.context_id, body_size, eof
193-
);
118+
match self.config.my_status_code {
119+
Some(status) => {
120+
self.set_http_response_header("status", Some(&status.to_string()))
121+
},
122+
None => ()
123+
}
194124

195125
Action::Continue
196126
}
197-
198-
fn on_log(&mut self) {
199-
info!("#{} on_log", self.context_id);
200-
201-
self.dispatch_http_call(
202-
"127.0.0.1:9000",
203-
vec![
204-
(":method", "GET"),
205-
(":path", "/foo"),
206-
(":authority", "127.0.0.1:9000"),
207-
],
208-
None,
209-
vec![],
210-
Duration::from_secs(5),
211-
)
212-
.unwrap();
213-
}
127+
//
128+
// fn on_http_response_body(&mut self, body_size: usize, eof: bool) -> Action {
129+
// Action::Continue
130+
// }
131+
//
132+
// fn on_log(&mut self) {
133+
// }
214134
}
215135

216136
proxy_wasm::main! {{
217-
218137
proxy_wasm::set_log_level(LogLevel::Debug);
219138
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
220139
Box::new(MyFilterRoot {

0 commit comments

Comments
 (0)