14
14
15
15
use proxy_wasm:: callout:: http:: HttpClient ;
16
16
use proxy_wasm:: callout:: promise:: Promise ;
17
- use proxy_wasm:: hostcalls;
18
17
use proxy_wasm:: traits:: * ;
19
18
use proxy_wasm:: types:: * ;
19
+ use std:: cell:: RefCell ;
20
+ use std:: rc:: Rc ;
20
21
use std:: time:: Duration ;
21
22
22
23
proxy_wasm:: main! { {
23
24
proxy_wasm:: set_log_level( LogLevel :: Trace ) ;
24
25
proxy_wasm:: set_http_context( |_, _| -> Box <dyn HttpContext > { Box :: new( HttpParallelCall :: default ( ) ) } ) ;
25
26
} }
26
27
27
- #[ derive( Default ) ]
28
+ #[ derive( Default , Clone ) ]
28
29
struct HttpParallelCall {
29
- client : HttpClient ,
30
+ client : Rc < RefCell < HttpClient > > ,
30
31
}
31
32
32
33
impl HttpContext for HttpParallelCall {
33
34
fn on_http_request_headers ( & mut self , _: usize , _: bool ) -> Action {
35
+ let self_clone_for_promise1 = self . clone ( ) ;
36
+ let self_clone_for_promise2 = self . clone ( ) ;
37
+ let self_clone_for_join = self . clone ( ) ;
38
+
34
39
// "Hello, "
35
40
let promise1 = self
36
41
. client
42
+ . borrow_mut ( )
37
43
. dispatch (
38
44
"httpbin" ,
39
45
vec ! [
@@ -45,12 +51,17 @@ impl HttpContext for HttpParallelCall {
45
51
vec ! [ ] ,
46
52
Duration :: from_secs ( 1 ) ,
47
53
)
48
- . then ( |( _, _, body_size, _) | get_http_call_response_body_string ( 0 , body_size) )
49
- . then ( |body| body. unwrap_or_default ( ) ) ;
54
+ . then ( move |( _, _, body_size, _) | {
55
+ match self_clone_for_promise1. get_http_call_response_body ( 0 , body_size) {
56
+ None => "" . to_owned ( ) ,
57
+ Some ( bytes) => String :: from_utf8 ( bytes. to_vec ( ) ) . unwrap ( ) ,
58
+ }
59
+ } ) ;
50
60
51
61
// "World!"
52
62
let promise2 = self
53
63
. client
64
+ . borrow_mut ( )
54
65
. dispatch (
55
66
"httpbin" ,
56
67
vec ! [
@@ -62,11 +73,15 @@ impl HttpContext for HttpParallelCall {
62
73
vec ! [ ] ,
63
74
Duration :: from_secs ( 1 ) ,
64
75
)
65
- . then ( |( _, _, body_size, _) | get_http_call_response_body_string ( 0 , body_size) )
66
- . then ( |body| body. unwrap_or_default ( ) ) ;
76
+ . then ( move |( _, _, body_size, _) | {
77
+ match self_clone_for_promise2. get_http_call_response_body ( 0 , body_size) {
78
+ None => "" . to_owned ( ) ,
79
+ Some ( bytes) => String :: from_utf8 ( bytes. to_vec ( ) ) . unwrap ( ) ,
80
+ }
81
+ } ) ;
67
82
68
- Promise :: all_of ( vec ! [ promise1, promise2] ) . then ( |results| {
69
- send_http_response (
83
+ Promise :: all_of ( vec ! [ promise1, promise2] ) . then ( move |results| {
84
+ self_clone_for_join . send_http_response (
70
85
200 ,
71
86
vec ! [ ] ,
72
87
Some ( format ! ( "{}{}\n " , results[ 0 ] , results[ 1 ] ) . as_bytes ( ) ) ,
@@ -91,20 +106,7 @@ impl Context for HttpParallelCall {
91
106
num_trailers : usize ,
92
107
) {
93
108
self . client
109
+ . borrow_mut ( )
94
110
. callback ( token_id, num_headers, body_size, num_trailers)
95
111
}
96
112
}
97
-
98
- fn get_http_call_response_body_string ( start : usize , max_size : usize ) -> Option < String > {
99
- match hostcalls:: get_buffer ( BufferType :: HttpCallResponseBody , start, max_size) . unwrap ( ) {
100
- None => None ,
101
- Some ( bytes) => {
102
- let body_string = String :: from_utf8 ( bytes. to_vec ( ) ) . unwrap ( ) ;
103
- Some ( body_string)
104
- }
105
- }
106
- }
107
-
108
- fn send_http_response ( status_code : u32 , headers : Vec < ( & str , & str ) > , body : Option < & [ u8 ] > ) {
109
- hostcalls:: send_http_response ( status_code, headers, body) . unwrap ( )
110
- }
0 commit comments