|
| 1 | +use envoy_proxy_dynamic_modules_rust_sdk::*; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +/// This implements the [`envoy_proxy_dynamic_modules_rust_sdk::HttpFilterConfig`] trait. |
| 5 | +/// |
| 6 | +/// The trait corresponds to a Envoy filter chain configuration. |
| 7 | +#[derive(Serialize, Deserialize, Debug)] |
| 8 | +pub struct FilterConfig { |
| 9 | + request_headers: Vec<(String, String)>, |
| 10 | + response_headers: Vec<(String, String)>, |
| 11 | +} |
| 12 | + |
| 13 | +impl FilterConfig { |
| 14 | + /// This is the constructor for the [`FilterConfig`]. |
| 15 | + /// |
| 16 | + /// filter_config is the filter config from the Envoy config here: |
| 17 | + /// https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/dynamic_modules/v3/dynamic_modules.proto#envoy-v3-api-msg-extensions-dynamic-modules-v3-dynamicmoduleconfig |
| 18 | + pub fn new(filter_config: &str) -> Option<Self> { |
| 19 | + let filter_config: FilterConfig = match serde_json::from_str(filter_config) { |
| 20 | + Ok(cfg) => cfg, |
| 21 | + Err(err) => { |
| 22 | + eprintln!("Error parsing filter config: {}", err); |
| 23 | + return None; |
| 24 | + } |
| 25 | + }; |
| 26 | + Some(filter_config) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl<EC: EnvoyHttpFilterConfig, EHF: EnvoyHttpFilter> HttpFilterConfig<EC, EHF> for FilterConfig { |
| 31 | + /// This is called for each new HTTP filter. |
| 32 | + fn new_http_filter(&mut self, _envoy: &mut EC) -> Box<dyn HttpFilter<EHF>> { |
| 33 | + Box::new(Filter { |
| 34 | + request_headers: self.request_headers.clone(), |
| 35 | + response_headers: self.response_headers.clone(), |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// This implements the [`envoy_proxy_dynamic_modules_rust_sdk::HttpFilter`] trait. |
| 41 | +/// |
| 42 | +/// This sets the request and response headers to the values specified in the filter config. |
| 43 | +pub struct Filter { |
| 44 | + request_headers: Vec<(String, String)>, |
| 45 | + response_headers: Vec<(String, String)>, |
| 46 | +} |
| 47 | + |
| 48 | +/// This implements the [`envoy_proxy_dynamic_modules_rust_sdk::HttpFilter`] trait. |
| 49 | +impl<EHF: EnvoyHttpFilter> HttpFilter<EHF> for Filter { |
| 50 | + fn on_request_headers( |
| 51 | + &mut self, |
| 52 | + envoy_filter: &mut EHF, |
| 53 | + _end_of_stream: bool, |
| 54 | + ) -> abi::envoy_dynamic_module_type_on_http_filter_request_headers_status { |
| 55 | + for (key, value) in &self.request_headers { |
| 56 | + envoy_filter.set_request_header(key, value.as_bytes()); |
| 57 | + } |
| 58 | + abi::envoy_dynamic_module_type_on_http_filter_request_headers_status::Continue |
| 59 | + } |
| 60 | + |
| 61 | + fn on_response_headers( |
| 62 | + &mut self, |
| 63 | + envoy_filter: &mut EHF, |
| 64 | + _end_of_stream: bool, |
| 65 | + ) -> abi::envoy_dynamic_module_type_on_http_filter_response_headers_status { |
| 66 | + for (key, value) in &self.response_headers { |
| 67 | + envoy_filter.set_response_header(key, value.as_bytes()); |
| 68 | + } |
| 69 | + abi::envoy_dynamic_module_type_on_http_filter_response_headers_status::Continue |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::*; |
| 76 | + |
| 77 | + #[test] |
| 78 | + /// This demonstrates how to write a test without Envoy using a mock provided by the SDK. |
| 79 | + fn test_filter() { |
| 80 | + let mut envoy_filter = envoy_proxy_dynamic_modules_rust_sdk::MockEnvoyHttpFilter::new(); |
| 81 | + let mut filter = Filter { |
| 82 | + request_headers: vec![("X-Foo".to_string(), "bar".to_string())], |
| 83 | + response_headers: vec![("X-Bar".to_string(), "foo".to_string())], |
| 84 | + }; |
| 85 | + |
| 86 | + envoy_filter |
| 87 | + .expect_set_request_header() |
| 88 | + .returning(|key, value| { |
| 89 | + assert_eq!(key, "X-Foo"); |
| 90 | + assert_eq!(value, b"bar"); |
| 91 | + return true; |
| 92 | + }); |
| 93 | + envoy_filter |
| 94 | + .expect_set_response_header() |
| 95 | + .returning(|key, value| { |
| 96 | + assert_eq!(key, "X-Bar"); |
| 97 | + assert_eq!(value, b"foo"); |
| 98 | + return true; |
| 99 | + }); |
| 100 | + assert_eq!( |
| 101 | + filter.on_request_headers(&mut envoy_filter, false), |
| 102 | + abi::envoy_dynamic_module_type_on_http_filter_request_headers_status::Continue |
| 103 | + ); |
| 104 | + assert_eq!( |
| 105 | + filter.on_response_headers(&mut envoy_filter, false), |
| 106 | + abi::envoy_dynamic_module_type_on_http_filter_response_headers_status::Continue |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
0 commit comments