|
1 | | -use log::{info, warn, error}; |
| 1 | +use log::{info, warn}; |
2 | 2 | use proxy_wasm::{traits::*, types::*}; |
3 | 3 | use serde::Deserialize; |
4 | 4 | use serde_json_wasm::de; |
5 | | -use std::time::Duration; |
6 | 5 |
|
7 | 6 | // ----------------------------------------------------------------------------- |
8 | 7 | // Config |
9 | 8 | // ----------------------------------------------------------------------------- |
10 | 9 |
|
11 | 10 | #[derive(Deserialize, Clone, Copy, Debug)] |
12 | 11 | 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>, |
26 | 14 | } |
27 | 15 |
|
28 | 16 | // ----------------------------------------------------------------------------- |
29 | 17 | // Root Context |
30 | 18 | // ----------------------------------------------------------------------------- |
31 | 19 |
|
32 | | -const ROOT_ID: u32 = 0; |
33 | | - |
34 | 20 | struct MyFilterRoot { |
35 | 21 | config: Option<Config>, |
36 | 22 | } |
37 | 23 |
|
38 | 24 | struct MyFilter { |
39 | 25 | context_id: u32, |
40 | | - _config: Config, |
| 26 | + config: Config, |
41 | 27 | } |
42 | 28 |
|
43 | 29 | 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 | +// } |
60 | 42 | } |
61 | 43 |
|
62 | 44 | 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 | +// } |
67 | 51 |
|
68 | 52 | 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); |
70 | 54 |
|
71 | 55 | if let Some(config_bytes) = self.get_plugin_configuration() { |
72 | | - assert!(config_bytes.len() == config_size); |
73 | 56 | match de::from_slice::<Config>(&config_bytes) { |
74 | 57 | 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 | | - |
82 | 58 | 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 | | - ); |
89 | 59 |
|
90 | 60 | true |
91 | 61 | } |
92 | 62 | Err(err) => { |
93 | 63 | 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 |
96 | 66 | ); |
97 | 67 |
|
98 | 68 | false |
99 | 69 | } |
100 | 70 | } |
101 | 71 | } else { |
102 | | - warn!("#{} on_configure: failed getting configuration", ROOT_ID); |
| 72 | + warn!("on_configure: failed getting configuration"); |
103 | 73 |
|
104 | 74 | false |
105 | 75 | } |
106 | 76 | } |
107 | 77 |
|
108 | | - fn get_type(&self) -> Option<ContextType> { |
109 | | - Some(ContextType::HttpContext) |
110 | | - } |
111 | | - |
112 | | - // Called when the host environment creates a new HTTP context |
113 | 78 | 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); |
115 | 80 |
|
116 | 81 | if let Some(config) = &self.config { |
117 | 82 | Some(Box::new(MyFilter { |
118 | 83 | context_id: context_id, |
119 | | - _config: config.clone(), |
| 84 | + config: config.clone(), |
120 | 85 | })) |
121 | 86 | } else { |
122 | 87 | None |
123 | 88 | } |
124 | 89 | } |
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 | | - } |
143 | 90 | } |
144 | 91 |
|
145 | 92 | // ----------------------------------------------------------------------------- |
146 | 93 | // Plugin Context |
147 | 94 | // ----------------------------------------------------------------------------- |
148 | 95 |
|
149 | 96 | 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 | +// ) {} |
162 | 104 | } |
163 | 105 |
|
164 | 106 | 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 | +// |
180 | 115 | 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); |
188 | 117 |
|
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 | + } |
194 | 124 |
|
195 | 125 | Action::Continue |
196 | 126 | } |
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 | +// } |
214 | 134 | } |
215 | 135 |
|
216 | 136 | proxy_wasm::main! {{ |
217 | | - |
218 | 137 | proxy_wasm::set_log_level(LogLevel::Debug); |
219 | 138 | proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { |
220 | 139 | Box::new(MyFilterRoot { |
|
0 commit comments