1
- wit_bindgen:: generate!( {
2
- path: "../../wit" ,
3
- world: "wasi:http/proxy@0.2.0" ,
4
- } ) ;
5
-
6
- use exports:: wasi:: http0_2_0:: incoming_handler;
7
- use url:: Url ;
8
- use wasi:: {
9
- http0_2_0:: {
10
- outgoing_handler,
11
- types:: {
12
- Headers , IncomingRequest , Method , OutgoingBody , OutgoingRequest , OutgoingResponse ,
13
- ResponseOutparam , Scheme ,
14
- } ,
15
- } ,
16
- io0_2_0:: streams:: StreamError ,
1
+ use helper:: bindings:: wasi:: http0_2_0:: outgoing_handler;
2
+ use helper:: bindings:: wasi:: http0_2_0:: types:: {
3
+ Headers , IncomingRequest , Method , OutgoingBody , OutgoingRequest , OutgoingResponse ,
4
+ ResponseOutparam , Scheme ,
17
5
} ;
6
+ use helper:: bindings:: wasi:: io0_2_0:: streams:: StreamError ;
7
+ use url:: Url ;
18
8
19
9
struct Component ;
20
10
21
- export ! ( Component ) ;
11
+ helper :: gen_http_trigger_bindings !( Component ) ;
22
12
23
- impl incoming_handler :: Guest for Component {
13
+ impl bindings :: Guest for Component {
24
14
fn handle ( request : IncomingRequest , outparam : ResponseOutparam ) {
25
15
// The request must have a "url" header.
26
16
let Some ( url) = request. headers ( ) . entries ( ) . iter ( ) . find_map ( |( k, v) | {
@@ -30,7 +20,7 @@ impl incoming_handler::Guest for Component {
30
20
. and_then ( |v| Url :: parse ( v) . ok ( ) )
31
21
} ) else {
32
22
// Otherwise, return a 400 Bad Request response.
33
- return_response ( outparam, 400 , b"Bad Request " ) ;
23
+ return_response ( outparam, 400 , b"missing header: url " ) ;
34
24
return ;
35
25
} ;
36
26
@@ -55,7 +45,7 @@ impl incoming_handler::Guest for Component {
55
45
. unwrap ( ) ;
56
46
57
47
// Write the request body.
58
- write_outgoing_body ( outgoing_request. body ( ) . unwrap ( ) , b"Hello, world!" ) ;
48
+ helper :: write_outgoing_body ( outgoing_request. body ( ) . unwrap ( ) , b"Hello, world!" ) ;
59
49
60
50
// Get the incoming response.
61
51
let response = match outgoing_handler:: handle ( outgoing_request, None ) {
@@ -103,20 +93,7 @@ impl incoming_handler::Guest for Component {
103
93
}
104
94
}
105
95
106
- fn write_outgoing_body ( outgoing_body : OutgoingBody , message : & [ u8 ] ) {
107
- assert ! ( message. len( ) <= 4096 ) ;
108
- {
109
- let outgoing_stream = outgoing_body. write ( ) . unwrap ( ) ;
110
- outgoing_stream. blocking_write_and_flush ( message) . unwrap ( ) ;
111
- // The outgoing stream must be dropped before the outgoing body is finished.
112
- }
113
- OutgoingBody :: finish ( outgoing_body, None ) . unwrap ( ) ;
114
- }
115
-
116
96
fn return_response ( outparam : ResponseOutparam , status : u16 , body : & [ u8 ] ) {
117
- let response = OutgoingResponse :: new ( Headers :: new ( ) ) ;
118
- response. set_status_code ( status) . unwrap ( ) ;
119
- write_outgoing_body ( response. body ( ) . unwrap ( ) , body) ;
120
-
97
+ let response = helper:: response ( status, body) ;
121
98
ResponseOutparam :: set ( outparam, Ok ( response) ) ;
122
99
}
0 commit comments