proxy-wasm-rust-sdk icon indicating copy to clipboard operation
proxy-wasm-rust-sdk copied to clipboard

When I changed request body,the program enters an endless loop

Open Fengxq2014 opened this issue 1 year ago • 3 comments

impl HttpContext for HttpHeadersBody {
    fn on_http_request_body(&mut self, body_size: usize, end_of_stream: bool) -> Action {
        if !end_of_stream {
            return Action::Pause;
        }
        info!("on_http_request_body");
        match self.get_shared_data("changed") {
            (change, _) => {
                match change {
                    None => {}
                    Some(c) => {
                        info!("context-{}, changed:{}",self.context_id, String::from_utf8(c).unwrap());
                        return Action::Continue;
                    }
                }
            }
        }
        self.dispatch_http_call(
            "httpbin",
            vec![
                (":method", "GET"),
                (":path", "/bytes/1"),
                (":authority", "httpbin.org"),
            ],
            None,
            vec![],
            Duration::from_secs(5),
        )
            .unwrap();
        Action::Pause
    }
}

impl Context for HttpHeadersBody {
    fn on_http_call_response(&mut self, _: u32, _: usize, body_size: usize, _: usize) {
        info!("on_http_call_response");
        if let Some(body) = self.get_http_call_response_body(0, body_size) {
            if !body.is_empty() {
                let mod_body = "modify body";
                self.set_http_request_body(0, mod_body.as_bytes().len(), mod_body.as_bytes());
                self.set_shared_data("changed", Some("true".as_bytes()), None);
                self.resume_http_request();
                return;
            }
        }
        info!("Access forbidden.");
        self.send_http_response(
            403,
            vec![("Powered-By", "proxy-wasm")],
            Some(b"Access forbidden.\n"),
        );
    }
}

image

Fengxq2014 avatar Sep 20 '23 03:09 Fengxq2014

I don't see any loop in your logs, the context_id is clearly different across those log entries.

The problem with your code is that the key-value store is global, not local to a request, so "changed" persists across requests, which probably breaks your logic.

PiotrSikora avatar Sep 25 '23 22:09 PiotrSikora

I only sent one request, and the log began to print indefinitely

Fengxq2014 avatar Sep 27 '23 00:09 Fengxq2014

I have a very similar issue with the (unchanged) http_auth_random example - sending requests to localhost:10000/headers will always return "Access forbidden.". When I examine the logs I can see that multiple passes are made through on_http_request_headers until it hits send_http_response. The below were for a single request from the client:

image

paulgreenfield1983 avatar Nov 23 '23 15:11 paulgreenfield1983