1 mod bindings { 2 wit_bindgen::generate!({ 3 path: "../wasi-http/src/p3/wit", 4 world: "local:local/middleware-with-chain", 5 inline: " 6 package local:local; 7 8 world middleware-with-chain { 9 include wasi:http/proxy@0.3.0-draft; 10 11 import chain-http; 12 } 13 14 interface chain-http { 15 use wasi:http/types@0.3.0-draft.{request, response, error-code}; 16 17 handle: async func(request: request) -> result<response, error-code>; 18 } 19 ", 20 generate_all, 21 }); 22 23 use super::Component; 24 export!(Component); 25 } 26 27 use bindings::{ 28 exports::wasi::http::handler::Guest as Handler, 29 local::local::chain_http, 30 wasi::clocks::monotonic_clock, 31 wasi::http::types::{ErrorCode, Request, Response}, 32 }; 33 use std::time::Duration; 34 35 struct Component; 36 37 impl Handler for Component { 38 async fn handle(request: Request) -> Result<Response, ErrorCode> { 39 // First, sleep briefly. This will ensure the next call happens via a 40 // host->guest call to the `wit_bindgen_rt::async_support::callback` 41 // function, which exercises different code paths in both the host and 42 // the guest, which we want to test here. 43 let duration = Duration::from_millis(10); 44 monotonic_clock::wait_for(duration.as_nanos().try_into().unwrap()).await; 45 46 chain_http::handle(request).await 47 } 48 } 49 50 // Unused function; required since this file is built as a `bin`: 51 fn main() {} 52