11#[ cfg( feature = "define-component" ) ]
22pub mod bindings {
33 wit_bindgen:: generate!( {
4- world: "platform-rc20231018 " ,
4+ world: "fermyon:spin/ platform@3.0.0 " ,
55 path: "../../../wit" ,
66 runtime_path: "::wit_bindgen::rt"
77 } ) ;
88}
99
1010#[ 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 ,
1313} ;
1414#[ 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 ;
1616
1717#[ cfg( feature = "define-component" ) ]
1818#[ macro_export]
@@ -23,15 +23,15 @@ macro_rules! define_component {
2323 // For now, this assumes the crate using this macro has `wit-bindgen` as a dependency
2424 mod bindings {
2525 $crate:: wit_bindgen:: generate!( {
26- world: "http-trigger-rc20231018 " ,
26+ world: "fermyon:spin/ http-trigger@3.0.0 " ,
2727 path: "../../../../wit" ,
2828 exports: {
29- "wasi:http/[email protected] -rc-2023-10-18 " : super :: Component 29+ "wasi:http/[email protected] " : super :: Component 3030 } ,
3131 } ) ;
3232 }
3333
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 } ;
3535 struct $name;
3636
3737 impl Guest for $name {
@@ -40,7 +40,7 @@ macro_rules! define_component {
4040 }
4141 }
4242
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 {
4444 fn from( value: ResponseOutparam ) -> Self {
4545 unsafe { Self :: from_handle( value. into_handle( ) ) }
4646 }
@@ -50,27 +50,31 @@ macro_rules! define_component {
5050
5151#[ cfg( feature = "define-component" ) ]
5252pub 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+ } ;
5458 match result {
5559 Ok ( ( ) ) => ResponseOutparam :: set ( response_out, Ok ( response ( 200 ) ) ) ,
5660 Err ( err) => {
5761 let resp = response ( 500 ) ;
58- let body = resp. write ( ) . expect ( "response body was already taken" ) ;
62+ let body = resp. body ( ) . unwrap ( ) ;
5963 ResponseOutparam :: set ( response_out, Ok ( resp) ) ;
6064 outgoing_body ( body, err. into_bytes ( ) ) . unwrap ( ) ;
6165 }
6266 }
6367}
6468
6569#[ 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 > {
6771 struct Outgoing ( Option < ( OutputStream , OutgoingBody ) > ) ;
6872
6973 impl Drop for Outgoing {
7074 fn drop ( & mut self ) {
7175 if let Some ( ( stream, body) ) = self . 0 . take ( ) {
7276 drop ( stream) ;
73- OutgoingBody :: finish ( body, None ) ;
77+ OutgoingBody :: finish ( body, None ) . expect ( "should have finished OutgoingBody" ) ;
7478 }
7579 }
7680 }
@@ -101,11 +105,11 @@ pub fn outgoing_body(body: OutgoingBody, buffer: Vec<u8>) -> Result<(), Error> {
101105 Ok ( ( ) ) => {
102106 offset += count;
103107 }
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}" ) ) ) ) ,
105109 }
106110 }
107111 }
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}" ) ) ) ) ,
109113 }
110114 }
111115}
0 commit comments