use proxy_wasm::traits::*;
use proxy_wasm::types::*;
#[no_mangle]
pub fn _start() {
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpBodyRoot) });
}
struct HttpBodyRoot;
impl Context for HttpBodyRoot {}
impl RootContext for HttpBodyRoot {
fn get_type(&self) -> Option<ContextType> {
Some(ContextType::HttpContext)
}
fn create_http_context(&self, _context_id: u32) -> Option<Box<dyn HttpContext>> {
Some(Box::new(HttpBody))
}
}
struct HttpBody;
impl Context for HttpBody {}
impl HttpContext for HttpBody {
fn on_http_response_headers(&mut self, _: usize) -> Action {
self.set_http_response_header("content-length", None);
Action::Continue
}
fn on_http_response_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
if !end_of_stream {
return Action::Pause;
}
if let Some(body_bytes) = self.get_http_response_body(0, body_size) {
let body_str = String::from_utf8(body_bytes).unwrap();
if body_str.contains("secret") {
let new_body = format!("Original message body ({} bytes) redacted.", body_size);
self.set_http_response_body(0, body_size, &new_body.into_bytes());
}
}
Action::Continue
}
}