Skip to content

Commit

Permalink
fix(wasm): 优化 req header 获取
Browse files Browse the repository at this point in the history
优化 PluginHttpWrapper req_header 获取方方式,避免因为不规范的 http header 导致 plugin panic,并且打印不规范的 header 日志
  • Loading branch information
xiaoma committed Sep 10, 2024
1 parent d682e05 commit ebf482c
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions plugins/wasm-rust/src/plugin_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginConfig>: RootContext
where
Expand Down Expand Up @@ -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!("http header contains non-ASCII characters header: {}", k ).as_str(),
).unwrap();
}
}
}

if let Some(config) = &self.config {
self.http_content.on_config(config);
}
Expand Down

0 comments on commit ebf482c

Please sign in to comment.