@@ -80,40 +80,48 @@ pub trait Context {
80
80
/// # Example
81
81
///
82
82
/// ```rust
83
+ ///
83
84
/// use proxy_wasm::traits::*;
84
85
/// use proxy_wasm::types::*;
85
- /// use std::time::Duration;
86
+ ///
87
+ /// use log::warn;
86
88
///
87
89
/// struct MyContext;
88
90
///
89
91
/// impl HttpContext for MyContext {
90
92
///
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 {
92
94
///
93
95
/// 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
+ /// ) {
100
102
/// 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
+ /// }
104
109
/// }
105
110
///
106
111
/// impl Context for MyContext {
107
112
///
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) {
109
114
///
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);
112
117
///
113
- /// // Do something with the response
118
+ /// info!("Received response headers: {:?}", headers);
114
119
///
115
- /// }
120
+ /// // Do something with the response
121
+ ///
122
+ /// }
116
123
/// }
124
+ /// ```
117
125
fn dispatch_http_call (
118
126
& self ,
119
127
upstream : & str ,
@@ -147,35 +155,42 @@ pub trait Context {
147
155
/// use proxy_wasm::traits::*;
148
156
/// use proxy_wasm::types::*;
149
157
///
158
+ /// use log::warn;
159
+ ///
150
160
/// struct MyContext;
151
161
///
152
162
/// impl HttpContext for MyContext {
153
163
///
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 {
155
165
///
156
166
/// 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
+ /// ) {
163
173
/// 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
+ /// }
167
180
/// }
168
181
///
169
182
/// impl Context for MyContext {
170
183
///
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) {
172
185
///
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);
175
188
///
176
- /// // Do something with the response
189
+ /// info!("Received response headers: {:?}", headers);
177
190
///
178
- /// }
191
+ /// // Do something with the response
192
+ ///
193
+ /// }
179
194
/// }
180
195
/// ```
181
196
fn on_http_call_response (
@@ -353,14 +368,14 @@ pub trait RootContext: Context {
353
368
///
354
369
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
355
370
///
356
- /// let vm_confuguration = self.get_vm_configuration().unwrap();
371
+ /// let vm_confuguration = self.get_vm_configuration().unwrap();
357
372
///
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();
359
374
///
360
- /// // Do something with the parsed vm configuration
375
+ /// // Do something with the parsed vm configuration
361
376
///
362
- /// true
363
- /// }
377
+ /// true
378
+ /// }
364
379
/// }
365
380
fn on_vm_start ( & mut self , _vm_configuration_size : usize ) -> bool {
366
381
true
@@ -389,14 +404,14 @@ pub trait RootContext: Context {
389
404
///
390
405
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
391
406
///
392
- /// let vm_confuguration = self.get_vm_configuration().unwrap();
407
+ /// let vm_confuguration = self.get_vm_configuration().unwrap();
393
408
///
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();
395
410
///
396
- /// // Do something with the parsed vm configuration
411
+ /// // Do something with the parsed vm configuration
397
412
///
398
- /// true
399
- /// }
413
+ /// true
414
+ /// }
400
415
/// }
401
416
fn get_vm_configuration ( & self ) -> Option < Bytes > {
402
417
hostcalls:: get_buffer ( BufferType :: VmConfiguration , 0 , usize:: MAX ) . unwrap ( )
@@ -430,14 +445,14 @@ pub trait RootContext: Context {
430
445
///
431
446
/// fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
432
447
///
433
- /// let plugin_configuration = self.get_plugin_configuration().unwrap();
448
+ /// let plugin_configuration = self.get_plugin_configuration().unwrap();
434
449
///
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();
436
451
///
437
- /// // Do something with the parsed plugin configuration
452
+ /// // Do something with the parsed plugin configuration
438
453
///
439
- /// true
440
- /// }
454
+ /// true
455
+ /// }
441
456
/// }
442
457
fn on_configure ( & mut self , _plugin_configuration_size : usize ) -> bool {
443
458
true
@@ -466,14 +481,14 @@ pub trait RootContext: Context {
466
481
///
467
482
/// fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
468
483
///
469
- /// let plugin_configuration = self.get_plugin_configuration().unwrap();
484
+ /// let plugin_configuration = self.get_plugin_configuration().unwrap();
470
485
///
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();
472
487
///
473
- /// // Do something with the parsed plugin configuration
488
+ /// // Do something with the parsed plugin configuration
474
489
///
475
- /// true
476
- /// }
490
+ /// true
491
+ /// }
477
492
/// }
478
493
fn get_plugin_configuration ( & self ) -> Option < Bytes > {
479
494
hostcalls:: get_buffer ( BufferType :: PluginConfiguration , 0 , usize:: MAX ) . unwrap ( )
@@ -499,16 +514,16 @@ pub trait RootContext: Context {
499
514
///
500
515
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
501
516
///
502
- /// self.set_tick_period(Duration::from_millis(5000));
517
+ /// self.set_tick_period(Duration::from_millis(5000));
503
518
///
504
- /// true
505
- /// }
519
+ /// true
520
+ /// }
506
521
///
507
- /// fn on_tick(&mut self) {
522
+ /// fn on_tick(&mut self) {
508
523
///
509
524
/// // Do something every 5 seconds
510
525
/// info!("tick!")
511
- /// }
526
+ /// }
512
527
/// }
513
528
fn set_tick_period ( & self , period : Duration ) {
514
529
hostcalls:: set_tick_period ( period) . unwrap ( )
@@ -532,16 +547,16 @@ pub trait RootContext: Context {
532
547
///
533
548
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
534
549
///
535
- /// self.set_tick_period(Duration::from_millis(5000));
550
+ /// self.set_tick_period(Duration::from_millis(5000));
536
551
///
537
- /// true
538
- /// }
552
+ /// true
553
+ /// }
539
554
///
540
- /// fn on_tick(&mut self) {
555
+ /// fn on_tick(&mut self) {
541
556
///
542
- /// // Do something every 5 seconds
543
- /// info!("tick!")
544
- /// }
557
+ /// // Do something every 5 seconds
558
+ /// info!("tick!")
559
+ /// }
545
560
/// }
546
561
fn on_tick ( & mut self ) { }
547
562
@@ -615,7 +630,6 @@ pub trait StreamContext: Context {
615
630
}
616
631
617
632
pub trait HttpContext : Context {
618
-
619
633
/// Called when HTTP request headers are received from downstream.
620
634
///
621
635
/// All `num_headers` headers can be retrieved and/or modified using `self.get_http_request_headers()`.
@@ -675,7 +689,7 @@ pub trait HttpContext: Context {
675
689
/// use proxy_wasm::traits::HttpContext;
676
690
///
677
691
/// 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 {
679
693
///
680
694
/// let headers = self.get_http_request_headers();
681
695
///
@@ -721,7 +735,7 @@ pub trait HttpContext: Context {
721
735
/// use proxy_wasm::types::Action;
722
736
///
723
737
/// 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 {
725
739
///
726
740
/// let header = self.get_http_request_header(":path");
727
741
///
@@ -763,7 +777,7 @@ pub trait HttpContext: Context {
763
777
/// use proxy_wasm::types::Action;
764
778
///
765
779
/// 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 {
767
781
///
768
782
/// self.add_http_request_header("x-my-header", "my-value");
769
783
///
@@ -971,18 +985,16 @@ pub trait HttpContext: Context {
971
985
///
972
986
/// impl HttpContext for MyHttpContext {
973
987
///
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 {
975
989
///
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();
979
991
///
980
992
/// if auth == "I am authorized!" {
981
993
/// // Send an HTTP response with a status code of 200 and a body of "Hello, World!"
982
994
/// self.send_http_response(200, vec![("A header", "Some Value")], Some(b"Hello, World!"));
983
995
/// } else {
984
996
/// // 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"));
986
998
/// }
987
999
///
988
1000
/// Action::Pause
0 commit comments