Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I changed request body,he program enters an endless loop #209

Open
Fengxq2014 opened this issue Sep 20, 2023 · 3 comments
Open

When I changed request body,he program enters an endless loop #209

Fengxq2014 opened this issue Sep 20, 2023 · 3 comments

Comments

@Fengxq2014
Copy link

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

@PiotrSikora
Copy link
Contributor

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.

@Fengxq2014
Copy link
Author

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

@paulgreenfield1983
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants