Skip to content

Commit

Permalink
All Examples tested and corrections added
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Engelhardt <[email protected]>
  • Loading branch information
antonengelhardt committed Feb 27, 2024
1 parent d8b90e6 commit 4e79be6
Showing 1 changed file with 84 additions and 72 deletions.
156 changes: 84 additions & 72 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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::<MyVmConfiguration>(&vm_confuguration).unwrap();
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&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
Expand Down Expand Up @@ -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::<MyVmConfiguration>(&vm_confuguration).unwrap();
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&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<Bytes> {
hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap()
Expand Down Expand Up @@ -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::<MyPluginConfiguration>(&plugin_configuration).unwrap();
/// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::<MyPluginConfiguration>(&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
Expand Down Expand Up @@ -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::<MyPluginConfiguration>(&plugin_configuration).unwrap();
/// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::<MyPluginConfiguration>(&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<Bytes> {
hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap()
Expand All @@ -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()
Expand All @@ -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) {}

Expand Down Expand Up @@ -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()`.
Expand Down Expand Up @@ -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();
///
Expand Down Expand Up @@ -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");
///
Expand Down Expand Up @@ -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");
///
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4e79be6

Please sign in to comment.