From d4ab10d345849b45e54d97b6b3ac46835f6c4473 Mon Sep 17 00:00:00 2001 From: jaymie9019 Date: Tue, 10 Sep 2024 11:45:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(wasm):=20=E4=BC=98=E5=8C=96=20PluginHt?= =?UTF-8?q?tpWrapper=20=E4=B8=AD=20http=20header=20=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 PluginHttpWrapper req_header 与 resp_header 的获取方方式,避免因为不规范的 http header 导致 plugin panic,并且打印不规范的 header 日志 --- plugins/wasm-rust/src/plugin_wrapper.rs | 34 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/plugins/wasm-rust/src/plugin_wrapper.rs b/plugins/wasm-rust/src/plugin_wrapper.rs index 85b556c6bc..76e8d1a05b 100644 --- a/plugins/wasm-rust/src/plugin_wrapper.rs +++ b/plugins/wasm-rust/src/plugin_wrapper.rs @@ -17,6 +17,9 @@ use multimap::MultiMap; use proxy_wasm::traits::{Context, HttpContext, RootContext}; use proxy_wasm::types::{Action, Bytes, DataAction, HeaderAction}; use serde::de::DeserializeOwned; +use proxy_wasm::hostcalls::log; +use proxy_wasm::types::LogLevel; + pub trait RootContextWrapper: RootContext where @@ -143,9 +146,21 @@ where fn on_http_request_headers(&mut self, num_headers: usize, end_of_stream: bool) -> HeaderAction { let binding = self.rule_matcher.borrow(); self.config = binding.get_match_config().map(|config| config.1.clone()); - for (k, v) in self.get_http_request_headers() { - self.req_headers.insert(k, v); + + for (k, v) in self.get_http_request_headers_bytes() { + match String::from_utf8(v) { + Ok(header_value) => { + self.req_headers.insert(k, header_value); + } + Err(_) => { + log( + LogLevel::Warn, + format!("request http header contains non-ASCII characters header: {}", k ).as_str(), + ).unwrap(); + } + } } + if let Some(config) = &self.config { self.http_content.on_config(config); } @@ -187,9 +202,20 @@ where num_headers: usize, end_of_stream: bool, ) -> HeaderAction { - for (k, v) in self.get_http_response_headers() { - self.res_headers.insert(k, v); + for (k, v) in self.get_http_response_headers_bytes() { + match String::from_utf8(v) { + Ok(header_value) => { + self.res_headers.insert(k, header_value); + } + Err(_) => { + log( + LogLevel::Warn, + format!("response http header contains non-ASCII characters header: {}", k ).as_str(), + ).unwrap(); + } + } } + let ret = self .http_content .on_http_response_headers(num_headers, end_of_stream); From 4b3a28a042636017c4e30c9e6c637239ed6089d4 Mon Sep 17 00:00:00 2001 From: jaymie9019 Date: Tue, 10 Sep 2024 17:44:58 +0800 Subject: [PATCH 2/2] style(wasm-rust-sdk): cargo fmt and cargo clippy --- plugins/wasm-rust/src/plugin_wrapper.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/plugins/wasm-rust/src/plugin_wrapper.rs b/plugins/wasm-rust/src/plugin_wrapper.rs index 76e8d1a05b..ba4e0a2580 100644 --- a/plugins/wasm-rust/src/plugin_wrapper.rs +++ b/plugins/wasm-rust/src/plugin_wrapper.rs @@ -14,12 +14,11 @@ use crate::rule_matcher::SharedRuleMatcher; use multimap::MultiMap; +use proxy_wasm::hostcalls::log; use proxy_wasm::traits::{Context, HttpContext, RootContext}; +use proxy_wasm::types::LogLevel; use proxy_wasm::types::{Action, Bytes, DataAction, HeaderAction}; use serde::de::DeserializeOwned; -use proxy_wasm::hostcalls::log; -use proxy_wasm::types::LogLevel; - pub trait RootContextWrapper: RootContext where @@ -155,8 +154,13 @@ where Err(_) => { log( LogLevel::Warn, - format!("request http header contains non-ASCII characters header: {}", k ).as_str(), - ).unwrap(); + format!( + "request http header contains non-ASCII characters header: {}", + k + ) + .as_str(), + ) + .unwrap(); } } } @@ -210,8 +214,13 @@ where Err(_) => { log( LogLevel::Warn, - format!("response http header contains non-ASCII characters header: {}", k ).as_str(), - ).unwrap(); + format!( + "response http header contains non-ASCII characters header: {}", + k + ) + .as_str(), + ) + .unwrap(); } } }