Skip to content

Commit 4e79be6

Browse files
All Examples tested and corrections added
Signed-off-by: Anton Engelhardt <[email protected]>
1 parent d8b90e6 commit 4e79be6

File tree

1 file changed

+84
-72
lines changed

1 file changed

+84
-72
lines changed

src/traits.rs

+84-72
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,48 @@ pub trait Context {
8080
/// # Example
8181
///
8282
/// ```rust
83+
///
8384
/// use proxy_wasm::traits::*;
8485
/// use proxy_wasm::types::*;
85-
/// use std::time::Duration;
86+
///
87+
/// use log::warn;
8688
///
8789
/// struct MyContext;
8890
///
8991
/// impl HttpContext for MyContext {
9092
///
91-
/// fn on_http_request_headers(&mut self, _num_headers: usize) -> Action {
93+
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
9294
///
9395
/// match self.dispatch_http_call(
94-
/// "upstream",
95-
/// vec![(":method", "GET"), (":path", "/"), (":authority", "www.example.com")],
96-
/// None,
97-
/// vec![]
98-
/// Duration::from_secs(5),
99-
/// ) {
96+
/// "cluster_name",
97+
/// vec![(":method", "GET"), (":path", "/"), (":authority", "google.com")],
98+
/// None,
99+
/// vec![],
100+
/// Duration::from_secs(5),
101+
/// ) {
100102
/// Ok(_) => Action::Pause,
101-
/// Err(_) => Action::Continue,
102-
/// }
103-
/// }
103+
/// Err(e) => {
104+
/// warn!("Failed to dispatch_http_call: {:?}", e);
105+
/// Action::Pause
106+
/// }
107+
/// }
108+
/// }
104109
/// }
105110
///
106111
/// impl Context for MyContext {
107112
///
108-
/// fn on_http_call_response(&mut self, token_id: u32, _: usize, body_size: usize, _: usize) {
113+
/// fn on_http_call_response(&mut self, _token_id: u32, _: usize, body_size: usize, _: usize) {
109114
///
110-
/// let headers = self.get_http_call_response_headers();
111-
/// let body = self.get_http_call_response_body(0, body_size);
115+
/// let headers = self.get_http_call_response_headers();
116+
/// let body = self.get_http_call_response_body(0, body_size);
112117
///
113-
/// // Do something with the response
118+
/// info!("Received response headers: {:?}", headers);
114119
///
115-
/// }
120+
/// // Do something with the response
121+
///
122+
/// }
116123
/// }
124+
/// ```
117125
fn dispatch_http_call(
118126
&self,
119127
upstream: &str,
@@ -147,35 +155,42 @@ pub trait Context {
147155
/// use proxy_wasm::traits::*;
148156
/// use proxy_wasm::types::*;
149157
///
158+
/// use log::warn;
159+
///
150160
/// struct MyContext;
151161
///
152162
/// impl HttpContext for MyContext {
153163
///
154-
/// fn on_http_request_headers(&mut self, _num_headers: usize) -> Action {
164+
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
155165
///
156166
/// match self.dispatch_http_call(
157-
/// "upstream",
158-
/// vec![(":method", "GET"), (":path", "/"), (":authority", "www.example.com")],
159-
/// None,
160-
/// vec![]
161-
/// Duration::from_secs(5),
162-
/// ) {
167+
/// "cluster_name",
168+
/// vec![(":method", "GET"), (":path", "/"), (":authority", "google.com")],
169+
/// None,
170+
/// vec![],
171+
/// Duration::from_secs(5),
172+
/// ) {
163173
/// Ok(_) => Action::Pause,
164-
/// Err(_) => Action::Continue,
165-
/// }
166-
/// }
174+
/// Err(e) => {
175+
/// warn!("Failed to dispatch_http_call: {:?}", e);
176+
/// Action::Pause
177+
/// }
178+
/// }
179+
/// }
167180
/// }
168181
///
169182
/// impl Context for MyContext {
170183
///
171-
/// fn on_http_call_response(&mut self, token_id: u32, _: usize, body_size: usize, _: usize) {
184+
/// fn on_http_call_response(&mut self, _token_id: u32, _: usize, body_size: usize, _: usize) {
172185
///
173-
/// let headers = self.get_http_call_response_headers();
174-
/// let body = self.get_http_call_response_body(0, body_size);
186+
/// let headers = self.get_http_call_response_headers();
187+
/// let body = self.get_http_call_response_body(0, body_size);
175188
///
176-
/// // Do something with the response
189+
/// info!("Received response headers: {:?}", headers);
177190
///
178-
/// }
191+
/// // Do something with the response
192+
///
193+
/// }
179194
/// }
180195
/// ```
181196
fn on_http_call_response(
@@ -353,14 +368,14 @@ pub trait RootContext: Context {
353368
///
354369
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
355370
///
356-
/// let vm_confuguration = self.get_vm_configuration().unwrap();
371+
/// let vm_confuguration = self.get_vm_configuration().unwrap();
357372
///
358-
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_confuguration).unwrap();
373+
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_confuguration).unwrap();
359374
///
360-
/// // Do something with the parsed vm configuration
375+
/// // Do something with the parsed vm configuration
361376
///
362-
/// true
363-
/// }
377+
/// true
378+
/// }
364379
/// }
365380
fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
366381
true
@@ -389,14 +404,14 @@ pub trait RootContext: Context {
389404
///
390405
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
391406
///
392-
/// let vm_confuguration = self.get_vm_configuration().unwrap();
407+
/// let vm_confuguration = self.get_vm_configuration().unwrap();
393408
///
394-
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_confuguration).unwrap();
409+
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_confuguration).unwrap();
395410
///
396-
/// // Do something with the parsed vm configuration
411+
/// // Do something with the parsed vm configuration
397412
///
398-
/// true
399-
/// }
413+
/// true
414+
/// }
400415
/// }
401416
fn get_vm_configuration(&self) -> Option<Bytes> {
402417
hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap()
@@ -430,14 +445,14 @@ pub trait RootContext: Context {
430445
///
431446
/// fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
432447
///
433-
/// let plugin_configuration = self.get_plugin_configuration().unwrap();
448+
/// let plugin_configuration = self.get_plugin_configuration().unwrap();
434449
///
435-
/// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::<MyPluginConfiguration>(&plugin_configuration).unwrap();
450+
/// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::<MyPluginConfiguration>(&plugin_configuration).unwrap();
436451
///
437-
/// // Do something with the parsed plugin configuration
452+
/// // Do something with the parsed plugin configuration
438453
///
439-
/// true
440-
/// }
454+
/// true
455+
/// }
441456
/// }
442457
fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
443458
true
@@ -466,14 +481,14 @@ pub trait RootContext: Context {
466481
///
467482
/// fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
468483
///
469-
/// let plugin_configuration = self.get_plugin_configuration().unwrap();
484+
/// let plugin_configuration = self.get_plugin_configuration().unwrap();
470485
///
471-
/// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::<MyPluginConfiguration>(&plugin_configuration).unwrap();
486+
/// let parsed_plugin_configuration: MyPluginConfiguration = serde_json::from_slice::<MyPluginConfiguration>(&plugin_configuration).unwrap();
472487
///
473-
/// // Do something with the parsed plugin configuration
488+
/// // Do something with the parsed plugin configuration
474489
///
475-
/// true
476-
/// }
490+
/// true
491+
/// }
477492
/// }
478493
fn get_plugin_configuration(&self) -> Option<Bytes> {
479494
hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap()
@@ -499,16 +514,16 @@ pub trait RootContext: Context {
499514
///
500515
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
501516
///
502-
/// self.set_tick_period(Duration::from_millis(5000));
517+
/// self.set_tick_period(Duration::from_millis(5000));
503518
///
504-
/// true
505-
/// }
519+
/// true
520+
/// }
506521
///
507-
/// fn on_tick(&mut self) {
522+
/// fn on_tick(&mut self) {
508523
///
509524
/// // Do something every 5 seconds
510525
/// info!("tick!")
511-
/// }
526+
/// }
512527
/// }
513528
fn set_tick_period(&self, period: Duration) {
514529
hostcalls::set_tick_period(period).unwrap()
@@ -532,16 +547,16 @@ pub trait RootContext: Context {
532547
///
533548
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
534549
///
535-
/// self.set_tick_period(Duration::from_millis(5000));
550+
/// self.set_tick_period(Duration::from_millis(5000));
536551
///
537-
/// true
538-
/// }
552+
/// true
553+
/// }
539554
///
540-
/// fn on_tick(&mut self) {
555+
/// fn on_tick(&mut self) {
541556
///
542-
/// // Do something every 5 seconds
543-
/// info!("tick!")
544-
/// }
557+
/// // Do something every 5 seconds
558+
/// info!("tick!")
559+
/// }
545560
/// }
546561
fn on_tick(&mut self) {}
547562

@@ -615,7 +630,6 @@ pub trait StreamContext: Context {
615630
}
616631

617632
pub trait HttpContext: Context {
618-
619633
/// Called when HTTP request headers are received from downstream.
620634
///
621635
/// All `num_headers` headers can be retrieved and/or modified using `self.get_http_request_headers()`.
@@ -675,7 +689,7 @@ pub trait HttpContext: Context {
675689
/// use proxy_wasm::traits::HttpContext;
676690
///
677691
/// impl HttpContext for MyPlugin {
678-
/// fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
692+
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
679693
///
680694
/// let headers = self.get_http_request_headers();
681695
///
@@ -721,7 +735,7 @@ pub trait HttpContext: Context {
721735
/// use proxy_wasm::types::Action;
722736
///
723737
/// impl HttpContext for MyPlugin {
724-
/// fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
738+
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
725739
///
726740
/// let header = self.get_http_request_header(":path");
727741
///
@@ -763,7 +777,7 @@ pub trait HttpContext: Context {
763777
/// use proxy_wasm::types::Action;
764778
///
765779
/// impl HttpContext for MyPlugin {
766-
/// fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
780+
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
767781
///
768782
/// self.add_http_request_header("x-my-header", "my-value");
769783
///
@@ -971,18 +985,16 @@ pub trait HttpContext: Context {
971985
///
972986
/// impl HttpContext for MyHttpContext {
973987
///
974-
/// fn on_http_request_headers(&mut self, _num_headers: usize) -> Action {
988+
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
975989
///
976-
/// let auth = self.get_http_request_header("Authorization").unwrap_or_else(
977-
/// || self.send_http_response(401, vec![("WWW-Authenticate", "Basic")], Some(b"Unauthorized"))
978-
/// );
990+
/// let auth = self.get_http_request_header("Authorization").unwrap_or_defauklt();
979991
///
980992
/// if auth == "I am authorized!" {
981993
/// // Send an HTTP response with a status code of 200 and a body of "Hello, World!"
982994
/// self.send_http_response(200, vec![("A header", "Some Value")], Some(b"Hello, World!"));
983995
/// } else {
984996
/// // Send an HTTP response with a status code of 403 and a body of "Forbidden"
985-
/// self.send_http_response(403, vec![("location", "authenticate-here.com")], Some(b"Forbidden"));
997+
/// self.send_http_response(307, vec![("location", "https://authenticate-here.com")], Some(b"Forbidden"));
986998
/// }
987999
///
9881000
/// Action::Pause

0 commit comments

Comments
 (0)