Skip to main content

wasm_oidc_plugin/
pause.rs

1// log
2use log::{info, warn};
3
4// proxy-wasm
5use proxy_wasm::{
6    traits::{Context, HttpContext},
7    types::Action,
8};
9
10/// The PauseRequests Context is the filter struct which is used when the filter is not configured.
11/// All requests are paused and queued by the RootContext. Once the filter is configured, the
12/// request is resumed by the RootContext.
13pub struct PauseRequests {
14    /// Original path of the request
15    pub original_path: Option<String>,
16}
17
18/// The context is used to process incoming HTTP requests when the filter is not configured.
19impl HttpContext for PauseRequests {
20    /// This function is called when the request headers are received. As the filter is not
21    /// configured, the request is paused and queued by the RootContext. Once the filter is
22    /// configured, the request is resumed by the RootContext.
23    fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
24        warn!("plugin not ready, pausing request");
25
26        // Get the original path from the request headers
27        self.original_path = Some(
28            self.get_http_request_header(":path")
29                .unwrap_or("/".to_string()),
30        );
31
32        Action::Pause
33    }
34
35    /// When the filter is configured, this function is called once the root context resumes the
36    /// request. This function sends a redirect to create a new context for the configured filter.
37    fn on_http_response_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
38        info!("filter now ready, sending redirect");
39
40        // Send a redirect to the original path
41        let location = self.original_path.as_deref().unwrap_or("/");
42
43        self.send_http_response(
44            307,
45            vec![
46                // Redirect to the requested path
47                ("location", location),
48                // Disable caching
49                ("Cache-Control", "no-cache"),
50            ],
51            Some(b"Filter is ready now."),
52        );
53        Action::Continue
54    }
55}
56
57impl Context for PauseRequests {}