wasmtime-wasi-http 42.0.2

Experimental HTTP library for WebAssembly in Wasmtime
Documentation
use crate::p3::WasiHttpView;
use crate::p3::bindings::Service;
use crate::p3::bindings::http::types::{ErrorCode, Request, Response};
use wasmtime::component::{Accessor, TaskExit};
use wasmtime::error::Context as _;

impl Service {
    /// Call `wasi:http/handler#handle` on [Service] getting a [Response] back.
    pub async fn handle(
        &self,
        store: &Accessor<impl WasiHttpView>,
        req: impl Into<Request>,
    ) -> wasmtime::Result<Result<(Response, TaskExit), ErrorCode>> {
        let req = store.with(|mut store| {
            store
                .data_mut()
                .http()
                .table
                .push(req.into())
                .context("failed to push request to table")
        })?;
        match self.wasi_http_handler().call_handle(store, req).await? {
            (Ok(res), task) => {
                let res = store.with(|mut store| {
                    store
                        .data_mut()
                        .http()
                        .table
                        .delete(res)
                        .context("failed to delete response from table")
                })?;
                Ok(Ok((res, task)))
            }
            (Err(err), _) => Ok(Err(err)),
        }
    }
}