1
1
#[ cfg( feature = "define-component" ) ]
2
2
pub mod bindings {
3
3
wit_bindgen:: generate!( {
4
- world: "platform-rc20231018 " ,
4
+ world: "fermyon:spin/ platform@3.0.0 " ,
5
5
path: "../../../wit" ,
6
6
runtime_path: "::wit_bindgen::rt"
7
7
} ) ;
8
8
}
9
9
10
10
#[ cfg( feature = "define-component" ) ]
11
- use bindings:: wasi:: http0_2_0_rc_2023_10_18 :: types:: {
12
- Error , Headers , OutgoingBody , OutgoingResponse , ResponseOutparam ,
11
+ use bindings:: wasi:: http0_2_0 :: types:: {
12
+ ErrorCode , Fields , OutgoingBody , OutgoingResponse , ResponseOutparam ,
13
13
} ;
14
14
#[ cfg( feature = "define-component" ) ]
15
- use bindings:: wasi:: io0_2_0_rc_2023_10_18 :: streams:: OutputStream ;
15
+ use bindings:: wasi:: io0_2_0 :: streams:: OutputStream ;
16
16
17
17
#[ cfg( feature = "define-component" ) ]
18
18
#[ macro_export]
@@ -23,15 +23,15 @@ macro_rules! define_component {
23
23
// For now, this assumes the crate using this macro has `wit-bindgen` as a dependency
24
24
mod bindings {
25
25
$crate:: wit_bindgen:: generate!( {
26
- world: "http-trigger-rc20231018 " ,
26
+ world: "fermyon:spin/ http-trigger@3.0.0 " ,
27
27
path: "../../../../wit" ,
28
28
exports: {
29
- "wasi:http/[email protected] -rc-2023-10-18 " : super :: Component
29
+ "wasi:http/[email protected] " : super :: Component
30
30
} ,
31
31
} ) ;
32
32
}
33
33
34
- use bindings:: exports:: wasi:: http0_2_0_rc_2023_10_18 :: incoming_handler:: { Guest , IncomingRequest , ResponseOutparam } ;
34
+ use bindings:: exports:: wasi:: http0_2_0 :: incoming_handler:: { Guest , IncomingRequest , ResponseOutparam } ;
35
35
struct $name;
36
36
37
37
impl Guest for $name {
@@ -40,7 +40,7 @@ macro_rules! define_component {
40
40
}
41
41
}
42
42
43
- impl From <ResponseOutparam > for $crate:: bindings:: wasi:: http0_2_0_rc_2023_10_18 :: types:: ResponseOutparam {
43
+ impl From <ResponseOutparam > for $crate:: bindings:: wasi:: http0_2_0 :: types:: ResponseOutparam {
44
44
fn from( value: ResponseOutparam ) -> Self {
45
45
unsafe { Self :: from_handle( value. into_handle( ) ) }
46
46
}
@@ -50,27 +50,31 @@ macro_rules! define_component {
50
50
51
51
#[ cfg( feature = "define-component" ) ]
52
52
pub fn handle ( response_out : ResponseOutparam , result : Result < ( ) , String > ) {
53
- let response = |status| OutgoingResponse :: new ( status, & Headers :: new ( & [ ] ) ) ;
53
+ let response = |status| {
54
+ let resp = OutgoingResponse :: new ( Fields :: new ( ) ) ;
55
+ resp. set_status_code ( status) . expect ( "should have set status code" ) ;
56
+ resp
57
+ } ;
54
58
match result {
55
59
Ok ( ( ) ) => ResponseOutparam :: set ( response_out, Ok ( response ( 200 ) ) ) ,
56
60
Err ( err) => {
57
61
let resp = response ( 500 ) ;
58
- let body = resp. write ( ) . expect ( "response body was already taken" ) ;
62
+ let body = resp. body ( ) . unwrap ( ) ;
59
63
ResponseOutparam :: set ( response_out, Ok ( resp) ) ;
60
64
outgoing_body ( body, err. into_bytes ( ) ) . unwrap ( ) ;
61
65
}
62
66
}
63
67
}
64
68
65
69
#[ cfg( feature = "define-component" ) ]
66
- pub fn outgoing_body ( body : OutgoingBody , buffer : Vec < u8 > ) -> Result < ( ) , Error > {
70
+ pub fn outgoing_body ( body : OutgoingBody , buffer : Vec < u8 > ) -> Result < ( ) , ErrorCode > {
67
71
struct Outgoing ( Option < ( OutputStream , OutgoingBody ) > ) ;
68
72
69
73
impl Drop for Outgoing {
70
74
fn drop ( & mut self ) {
71
75
if let Some ( ( stream, body) ) = self . 0 . take ( ) {
72
76
drop ( stream) ;
73
- OutgoingBody :: finish ( body, None ) ;
77
+ OutgoingBody :: finish ( body, None ) . expect ( "should have finished OutgoingBody" ) ;
74
78
}
75
79
}
76
80
}
@@ -101,11 +105,11 @@ pub fn outgoing_body(body: OutgoingBody, buffer: Vec<u8>) -> Result<(), Error> {
101
105
Ok ( ( ) ) => {
102
106
offset += count;
103
107
}
104
- Err ( e) => return Err ( Error :: ProtocolError ( format ! ( "I/O error: {e}" ) ) ) ,
108
+ Err ( e) => return Err ( ErrorCode :: InternalError ( Some ( format ! ( "I/O error: {e}" ) ) ) ) ,
105
109
}
106
110
}
107
111
}
108
- Err ( e) => return Err ( Error :: ProtocolError ( format ! ( "I/O error: {e}" ) ) ) ,
112
+ Err ( e) => return Err ( ErrorCode :: InternalError ( Some ( format ! ( "I/O error: {e}" ) ) ) ) ,
109
113
}
110
114
}
111
115
}
0 commit comments