diff --git a/src/traits.rs b/src/traits.rs index 005e2e38..cc8c6494 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -80,40 +80,48 @@ pub trait Context { /// # Example /// /// ```rust + /// /// use proxy_wasm::traits::*; /// use proxy_wasm::types::*; - /// use std::time::Duration; + /// + /// use log::warn; /// /// struct MyContext; /// /// impl HttpContext for MyContext { /// - /// fn on_http_request_headers(&mut self, _num_headers: usize) -> Action { + /// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { /// /// match self.dispatch_http_call( - /// "upstream", - /// vec![(":method", "GET"), (":path", "/"), (":authority", "www.example.com")], - /// None, - /// vec![] - /// Duration::from_secs(5), - /// ) { + /// "cluster_name", + /// vec![(":method", "GET"), (":path", "/"), (":authority", "google.com")], + /// None, + /// vec![], + /// Duration::from_secs(5), + /// ) { /// Ok(_) => Action::Pause, - /// Err(_) => Action::Continue, - /// } - /// } + /// Err(e) => { + /// warn!("Failed to dispatch_http_call: {:?}", e); + /// Action::Pause + /// } + /// } + /// } /// } /// /// impl Context for MyContext { /// - /// fn on_http_call_response(&mut self, token_id: u32, _: usize, body_size: usize, _: usize) { + /// fn on_http_call_response(&mut self, _token_id: u32, _: usize, body_size: usize, _: usize) { /// - /// let headers = self.get_http_call_response_headers(); - /// let body = self.get_http_call_response_body(0, body_size); + /// let headers = self.get_http_call_response_headers(); + /// let body = self.get_http_call_response_body(0, body_size); /// - /// // Do something with the response + /// info!("Received response headers: {:?}", headers); /// - /// } + /// // Do something with the response + /// + /// } /// } + /// ``` fn dispatch_http_call( &self, upstream: &str, @@ -147,35 +155,42 @@ pub trait Context { /// use proxy_wasm::traits::*; /// use proxy_wasm::types::*; /// + /// use log::warn; + /// /// struct MyContext; /// /// impl HttpContext for MyContext { /// - /// fn on_http_request_headers(&mut self, _num_headers: usize) -> Action { + /// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { /// /// match self.dispatch_http_call( - /// "upstream", - /// vec![(":method", "GET"), (":path", "/"), (":authority", "www.example.com")], - /// None, - /// vec![] - /// Duration::from_secs(5), - /// ) { + /// "cluster_name", + /// vec![(":method", "GET"), (":path", "/"), (":authority", "google.com")], + /// None, + /// vec![], + /// Duration::from_secs(5), + /// ) { /// Ok(_) => Action::Pause, - /// Err(_) => Action::Continue, - /// } - /// } + /// Err(e) => { + /// warn!("Failed to dispatch_http_call: {:?}", e); + /// Action::Pause + /// } + /// } + /// } /// } /// /// impl Context for MyContext { /// - /// fn on_http_call_response(&mut self, token_id: u32, _: usize, body_size: usize, _: usize) { + /// fn on_http_call_response(&mut self, _token_id: u32, _: usize, body_size: usize, _: usize) { /// - /// let headers = self.get_http_call_response_headers(); - /// let body = self.get_http_call_response_body(0, body_size); + /// let headers = self.get_http_call_response_headers(); + /// let body = self.get_http_call_response_body(0, body_size); /// - /// // Do something with the response + /// info!("Received response headers: {:?}", headers); /// - /// } + /// // Do something with the response + /// + /// } /// } /// ``` fn on_http_call_response( @@ -353,14 +368,14 @@ pub trait RootContext: Context { /// /// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool { /// - /// let vm_confuguration = self.get_vm_configuration().unwrap(); + /// let vm_confuguration = self.get_vm_configuration().unwrap(); /// - /// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::(&vm_confuguration).unwrap(); + /// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::(&vm_confuguration).unwrap(); /// - /// // Do something with the parsed vm configuration + /// // Do something with the parsed vm configuration /// - /// true - /// } + /// true + /// } /// } fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool { true @@ -389,14 +404,14 @@ pub trait RootContext: Context { /// /// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool { /// - /// let vm_confuguration = self.get_vm_configuration().unwrap(); + /// let vm_confuguration = self.get_vm_configuration().unwrap(); /// - /// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::(&vm_confuguration).unwrap(); + /// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::(&vm_confuguration).unwrap(); /// - /// // Do something with the parsed vm configuration + /// // Do something with the parsed vm configuration /// - /// true - /// } + /// true + /// } /// } fn get_vm_configuration(&self) -> Option { hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap() @@ -430,14 +445,14 @@ pub trait RootContext: Context { /// /// fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool { /// - /// let plugin_configuration = self.get_plugin_configuration().unwrap(); + /// let plugin_configuration = self.get_plugin_configuration().unwrap(); /// - /// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::(&plugin_configuration).unwrap(); + /// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::(&plugin_configuration).unwrap(); /// - /// // Do something with the parsed plugin configuration + /// // Do something with the parsed plugin configuration /// - /// true - /// } + /// true + /// } /// } fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool { true @@ -466,14 +481,14 @@ pub trait RootContext: Context { /// /// fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool { /// - /// let plugin_configuration = self.get_plugin_configuration().unwrap(); + /// let plugin_configuration = self.get_plugin_configuration().unwrap(); /// - /// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::(&plugin_configuration).unwrap(); + /// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::(&plugin_configuration).unwrap(); /// - /// // Do something with the parsed plugin configuration + /// // Do something with the parsed plugin configuration /// - /// true - /// } + /// true + /// } /// } fn get_plugin_configuration(&self) -> Option { hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap() @@ -499,16 +514,16 @@ pub trait RootContext: Context { /// /// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool { /// - /// self.set_tick_period(Duration::from_millis(5000)); + /// self.set_tick_period(Duration::from_millis(5000)); /// - /// true - /// } + /// true + /// } /// - /// fn on_tick(&mut self) { + /// fn on_tick(&mut self) { /// /// // Do something every 5 seconds /// info!("tick!") - /// } + /// } /// } fn set_tick_period(&self, period: Duration) { hostcalls::set_tick_period(period).unwrap() @@ -532,16 +547,16 @@ pub trait RootContext: Context { /// /// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool { /// - /// self.set_tick_period(Duration::from_millis(5000)); + /// self.set_tick_period(Duration::from_millis(5000)); /// - /// true - /// } + /// true + /// } /// - /// fn on_tick(&mut self) { + /// fn on_tick(&mut self) { /// - /// // Do something every 5 seconds - /// info!("tick!") - /// } + /// // Do something every 5 seconds + /// info!("tick!") + /// } /// } fn on_tick(&mut self) {} @@ -615,7 +630,6 @@ pub trait StreamContext: Context { } pub trait HttpContext: Context { - /// Called when HTTP request headers are received from downstream. /// /// All `num_headers` headers can be retrieved and/or modified using `self.get_http_request_headers()`. @@ -675,7 +689,7 @@ pub trait HttpContext: Context { /// use proxy_wasm::traits::HttpContext; /// /// impl HttpContext for MyPlugin { - /// fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { + /// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { /// /// let headers = self.get_http_request_headers(); /// @@ -721,7 +735,7 @@ pub trait HttpContext: Context { /// use proxy_wasm::types::Action; /// /// impl HttpContext for MyPlugin { - /// fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { + /// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { /// /// let header = self.get_http_request_header(":path"); /// @@ -763,7 +777,7 @@ pub trait HttpContext: Context { /// use proxy_wasm::types::Action; /// /// impl HttpContext for MyPlugin { - /// fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { + /// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { /// /// self.add_http_request_header("x-my-header", "my-value"); /// @@ -971,18 +985,16 @@ pub trait HttpContext: Context { /// /// impl HttpContext for MyHttpContext { /// - /// fn on_http_request_headers(&mut self, _num_headers: usize) -> Action { + /// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { /// - /// let auth = self.get_http_request_header("Authorization").unwrap_or_else( - /// || self.send_http_response(401, vec![("WWW-Authenticate", "Basic")], Some(b"Unauthorized")) - /// ); + /// let auth = self.get_http_request_header("Authorization").unwrap_or_defauklt(); /// /// if auth == "I am authorized!" { /// // Send an HTTP response with a status code of 200 and a body of "Hello, World!" /// self.send_http_response(200, vec![("A header", "Some Value")], Some(b"Hello, World!")); /// } else { /// // Send an HTTP response with a status code of 403 and a body of "Forbidden" - /// self.send_http_response(403, vec![("location", "authenticate-here.com")], Some(b"Forbidden")); + /// self.send_http_response(307, vec![("location", "https://authenticate-here.com")], Some(b"Forbidden")); /// } /// /// Action::Pause