fn on_http_request_complete_body()

in plugins/wasm-rust/extensions/ai-data-masking/src/lib.rs [584:655]


    fn on_http_request_complete_body(&mut self, req_body: &Bytes) -> DataAction {
        if self.config.is_none() {
            return DataAction::Continue;
        }
        let config = self.config.as_ref().unwrap();
        let mut req_body = match String::from_utf8(req_body.clone()) {
            Ok(r) => r,
            Err(_) => return DataAction::Continue,
        };
        if config.deny_openai {
            if let Ok(r) = serde_json::from_str(req_body.as_str()) {
                let req: Req = r;
                self.is_openai = true;
                self.stream = req.stream;
                for msg in req.messages {
                    if self.check_message(&msg.content) {
                        return self.deny(false);
                    }
                    let new_content = self.replace_request_msg(&msg.content);
                    if new_content != msg.content {
                        if let (Ok(from), Ok(to)) = (
                            serde_json::to_string(&msg.content),
                            serde_json::to_string(&new_content),
                        ) {
                            req_body = req_body.replace(&from, &to);
                        }
                    }
                }
                self.replace_http_request_body(req_body.as_bytes());
                return DataAction::Continue;
            }
        }
        if !config.deny_jsonpath.is_empty() {
            if let Ok(r) = serde_json::from_str(req_body.as_str()) {
                let json: Value = r;
                for jsonpath in config.deny_jsonpath.clone() {
                    for v in jsonpath.find_slice(&json) {
                        if let JsonPathValue::Slice(d, _) = v {
                            if let Some(s) = d.as_str() {
                                if self.check_message(s) {
                                    return self.deny(false);
                                }
                                let content = s.to_string();
                                let new_content = self.replace_request_msg(&content);
                                if new_content != content {
                                    if let (Ok(from), Ok(to)) = (
                                        serde_json::to_string(&content),
                                        serde_json::to_string(&new_content),
                                    ) {
                                        req_body = req_body.replace(&from, &to);
                                    }
                                }
                            }
                        }
                    }
                }
                self.replace_http_request_body(req_body.as_bytes());
                return DataAction::Continue;
            }
        }
        if config.deny_raw {
            if self.check_message(&req_body) {
                return self.deny(false);
            }
            let new_body = self.replace_request_msg(&req_body);
            if new_body != req_body {
                self.replace_http_request_body(new_body.as_bytes())
            }
            return DataAction::Continue;
        }
        DataAction::Continue
    }