Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

25.3 KB
Raw Blame
#[cfg(feature = "p2")]
use crate::p2::bindings::http::types as p2;
#[cfg(feature = "p3")]
use crate::p3::bindings::http::types as p3;
use crate::{DEFAULT_FORBIDDEN_HEADERS, Error, RequestOptions, Result};
use bytes::Bytes;
use http::{HeaderName, uri::Scheme};
use http_body_util::combinators::UnsyncBoxBody;
use std::marker;
use std::sync::{Arc, LazyLock};
use wasmtime::component::{HasData, ResourceTable};
use wasmtime_wasi::{NamedId, WasiCtxNamedView};

/// A helper struct which implements [`HasData`] for the `wasi:http` APIs.
///
/// This can be useful when directly calling `add_to_linker` functions directly,
/// such as [`wasmtime_wasi_http::p3::bindings::http::types::add_to_linker`] as
/// the `D` type parameter. See [`HasData`] for more information about the type
/// parameter's purpose.
///
/// When using this type you can skip the [`WasiHttpView`] trait, for example.
///
/// [`wasmtime_wasi_http::p3::bindings::http::types::add_to_linker`]: crate::p3::bindings::http::types::add_to_linker
///
/// # Examples
///
/// ```
/// use wasmtime::component::Linker;
/// use wasmtime::{Engine, Result};
/// use wasmtime_wasi_http::{WasiHttp, WasiHttpCtxView};
///
/// struct MyStoreState {
///     // ...
/// }
///
/// impl MyStoreState {
///     fn http(&mut self) -> WasiHttpCtxView<'_> {
///         // ...
/// #       todo!()
///     }
/// }
///
/// fn main() -> Result<()> {
///     let engine = Engine::default();
///     let mut linker = Linker::new(&engine);
///
///     wasmtime_wasi_http::p3::bindings::http::types::add_to_linker::<MyStoreState, WasiHttp>(
///         &mut linker,
///         |state| state.http(),
///     )?;
///     Ok(())
/// }
/// ```
pub struct WasiHttp;

impl HasData for WasiHttp {
    type Data<'a> = WasiHttpCtxView<'a>;
}

/// A trait which provides internal WASI HTTP state.
///
/// This trait is used by the [`add_to_linker`] convenience functions of this
/// crate. This trait can be implemented for the `T` in `Store<T>` to provide
/// access to wasi:http information at runtime.
///
/// [`add_to_linker`]: crate::p3::add_to_linker
///
/// # Example
///
/// ```
/// use wasmtime::component::ResourceTable;
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView, WasiHttpCtxView};
///
/// struct MyState {
///     http_ctx: WasiHttpCtx,
///     table: ResourceTable,
/// }
///
/// impl WasiHttpView for MyState {
///     fn http(&mut self) -> WasiHttpCtxView<'_> {
///         WasiHttpCtxView {
///             ctx: &mut self.http_ctx,
///             table: &mut self.table,
///             hooks: Default::default(),
///         }
///     }
/// }
/// ```
pub trait WasiHttpView: Send {
    /// Return a [WasiHttpCtxView] from mutable reference to self.
    fn http(&mut self) -> WasiHttpCtxView<'_>;
}

/// Basis of implementation of all `wasi:http` APIs in this crate.
///
/// This type provides a temporary view into information such as a WASI
/// resource table, HTTP context information, and embedder-provided hooks if
/// so desired. THe fields in this structure are typically stored within the
/// `T` of `Store<T>` and this struct borrows from there. All `Host` traits
/// generated by `bindgen!` are implemented for this type.
pub struct WasiHttpCtxView<'a> {
    /// Mutable reference to the WASI HTTP hooks.
    ///
    /// Note that [`default_hooks`] or [`Default::default()`] can be used if
    /// you don't want or need to customize this.
    pub hooks: &'a mut dyn WasiHttpHooks,

    /// Mutable reference to table used to manage resources.
    pub table: &'a mut ResourceTable,

    /// Mutable reference to the WASI HTTP context.
    pub ctx: &'a mut WasiHttpCtx,
}

/// Default maximum size for the contents of a fields resource.
///
/// Typically, HTTP proxies limit headers to 8k. This number is higher than that
/// because it not only includes the wire-size of headers but it additionally
/// includes factors for the in-memory representation of `HeaderMap`. This is in
/// theory high enough that no one runs into it but low enough such that a
/// completely full `HeaderMap` doesn't break the bank in terms of memory
/// consumption.
const DEFAULT_FIELD_SIZE_LIMIT: usize = 128 * 1024;

/// Capture the state necessary for use in the wasi-http API implementation.
#[derive(Debug, Clone)]
pub struct WasiHttpCtx {
    pub(crate) field_size_limit: usize,
}

impl WasiHttpCtx {
    /// Create a new context.
    pub fn new() -> Self {
        Self {
            field_size_limit: DEFAULT_FIELD_SIZE_LIMIT,
        }
    }

    /// Set the maximum size for any fields resources created by this context.
    ///
    /// The limit specified here is roughly a byte limit for the size of the
    /// in-memory representation of headers. This means that the limit needs to
    /// be larger than the literal representation of headers on the wire to
    /// account for in-memory Rust-side data structures representing the header
    /// names/values/etc.
    pub fn set_field_size_limit(&mut self, limit: usize) {
        self.field_size_limit = limit;
    }
}

impl Default for WasiHttpCtx {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience type definition for the bodies used in this crate.
pub type WasiBody = UnsyncBoxBody<Bytes, Error>;

/// A cloneable predicate deciding whether a header name is forbidden. This
/// predicate needs to be executed in tasks which do not have access to the
/// store.
pub type ForbiddenHeaderFilter = Arc<dyn Fn(&HeaderName) -> bool + Send + Sync>;

/// A trait which provides hooks into internal WASI HTTP operations.
///
/// Note that when using this type if state is needed to implement the methods
/// the state will need to be stored separately in a distinct structure to
/// implement this trait as the same type can't implement both [`WasiHttpView`]
/// and [`WasiHttpHooks`] and be usable.
///
/// # Example
///
/// ```
/// use wasmtime::component::ResourceTable;
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView, WasiHttpCtxView, WasiHttpHooks};
///
/// struct MyState {
///     http_ctx: WasiHttpCtx,
///     table: ResourceTable,
///     hooks: MyHooks,
/// }
///
/// impl MyState {
///     fn new() -> MyState {
///         MyState {
///             table: ResourceTable::new(),
///             http_ctx: WasiHttpCtx::new(),
///             hooks: MyHooks,
///         }
///     }
/// }
///
/// impl WasiHttpView for MyState {
///     fn http(&mut self) -> WasiHttpCtxView<'_> {
///         WasiHttpCtxView {
///             ctx: &mut self.http_ctx,
///             table: &mut self.table,
///             hooks: &mut self.hooks,
///         }
///     }
/// }
///
/// struct MyHooks;
///
/// impl WasiHttpHooks for MyHooks {
///     fn forbidden_header_filter(&mut self) -> wasmtime_wasi_http::ForbiddenHeaderFilter {
///         std::sync::Arc::new(|name| {
///             *name == http::header::AUTHORIZATION ||
///                 wasmtime_wasi_http::DEFAULT_FORBIDDEN_HEADERS.contains(name)
///         })
///     }
/// }
/// ```
pub trait WasiHttpHooks: Send {
    /// Returns a predicate deciding whether a given header should be
    /// considered forbidden and not allowed.
    ///
    /// The predicate applies to header and trailer fields crossing the guest
    /// boundary in either direction. It may be retained and invoked after the
    /// store has been dropped, e.g. for trailers of a host body forwarded by a
    /// guest, so it is obtained as a value rather than called on `self`.
    ///
    /// The default forbids [`DEFAULT_FORBIDDEN_HEADERS`].
    fn forbidden_header_filter(&mut self) -> ForbiddenHeaderFilter {
        static DEFAULT: LazyLock<ForbiddenHeaderFilter> =
            LazyLock::new(|| Arc::new(|name| DEFAULT_FORBIDDEN_HEADERS.contains(name)));
        DEFAULT.clone()
    }

    /// Whether a given scheme should be considered supported.
    ///
    /// `handle` will return [Error::HttpProtocolError] for unsupported schemes.
    fn is_supported_scheme(&mut self, scheme: &Scheme) -> bool {
        *scheme == Scheme::HTTP || *scheme == Scheme::HTTPS
    }

    /// Whether to set `host` header in the request passed to `send_request`.
    fn set_host_header(&mut self) -> bool {
        true
    }

    /// Scheme to default to, when not set by the guest.
    ///
    /// If [None], `handle` will return [Error::HttpProtocolError]
    /// for requests missing a scheme.
    fn default_scheme(&mut self) -> Option<Scheme> {
        Some(Scheme::HTTPS)
    }

    /// Send an outgoing request.
    ///
    /// This function will be used by the `wasi:http/handler#handle` implementation.
    ///
    /// The specified [Future] `fut` will be used to communicate
    /// a response processing error, if any.
    /// For example, if the response body is consumed via `wasi:http/types.response#consume-body`,
    /// a result will be sent on `fut`.
    ///
    /// The returned [Future] can be used to communicate
    /// a request processing error, if any, to the constructor of the request.
    /// For example, if the request was constructed via `wasi:http/types.request#new`,
    /// a result resolved from it will be forwarded to the guest on the future handle returned.
    ///
    /// `Content-Length` of the request passed to this function will be validated, however no
    /// `Content-Length` validation will be performed for the received response.
    #[cfg(feature = "default-send-request")]
    fn send_request(
        &mut self,
        request: http::Request<WasiBody>,
        options: Option<RequestOptions>,
        fut: Box<dyn Future<Output = Result<(), Error>> + Send>,
    ) -> Box<
        dyn Future<
                Output = Result<(
                    http::Response<WasiBody>,
                    Box<dyn Future<Output = Result<(), Error>> + Send>,
                )>,
            > + Send,
    > {
        _ = fut;
        Box::new(async move {
            use http_body_util::BodyExt;

            let (res, io) = crate::default_send_request(request, options).await?;
            Ok((
                res.map(BodyExt::boxed_unsync),
                Box::new(io) as Box<dyn Future<Output = _> + Send>,
            ))
        })
    }

    /// Send an outgoing request.
    ///
    /// This function will be used by the `wasi:http/handler#handle` implementation.
    ///
    /// The specified [Future] `fut` will be used to communicate
    /// a response processing error, if any.
    /// For example, if the response body is consumed via `wasi:http/types.response#consume-body`,
    /// a result will be sent on `fut`.
    ///
    /// The returned [Future] can be used to communicate
    /// a request processing error, if any, to the constructor of the request.
    /// For example, if the request was constructed via `wasi:http/types.request#new`,
    /// a result resolved from it will be forwarded to the guest on the future handle returned.
    ///
    /// `Content-Length` of the request passed to this function will be validated, however no
    /// `Content-Length` validation will be performed for the received response.
    #[cfg(not(feature = "default-send-request"))]
    fn send_request(
        &mut self,
        request: http::Request<WasiBody>,
        options: Option<RequestOptions>,
        fut: Box<dyn Future<Output = Result<(), Error>> + Send>,
    ) -> Box<
        dyn Future<
                Output = Result<(
                    http::Response<WasiBody>,
                    Box<dyn Future<Output = Result<(), Error>> + Send>,
                )>,
            > + Send,
    >;

    /// Number of distinct write calls to the outgoing body's output-stream
    /// that the implementation will buffer.
    /// Default: 1.
    #[cfg(feature = "p2")]
    fn p2_outgoing_body_buffer_chunks(&mut self) -> usize {
        crate::p2::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS
    }

    /// Maximum size allowed in a write call to the outgoing body's
    /// output-stream.  Default: 1024 * 1024.
    #[cfg(feature = "p2")]
    fn p2_outgoing_body_chunk_size(&mut self) -> usize {
        crate::p2::DEFAULT_OUTGOING_BODY_CHUNK_SIZE
    }

    /// Optional hook to configure the error code for hyper errors.
    #[cfg(feature = "p2")]
    fn p2_error_from_hyper(&mut self, err: &hyper::Error) -> p2::ErrorCode {
        tracing::warn!("hyper error: {err:?}");
        p2::ErrorCode::HttpProtocolError
    }

    /// Optional hook to configure the error code for connect I/O errors.
    ///
    /// The default distinguishes refused, unreachable, timed out, and terminated
    /// connections. Unrecognized errors become `connection-refused`.
    #[cfg(feature = "p2")]
    fn p2_error_from_connect(&mut self, err: &std::io::Error) -> p2::ErrorCode {
        tracing::warn!("connect error: {err:?}");
        use std::io::ErrorKind;
        match err.kind() {
            ErrorKind::ConnectionRefused => p2::ErrorCode::ConnectionRefused,
            ErrorKind::HostUnreachable | ErrorKind::NetworkUnreachable | ErrorKind::NetworkDown => {
                p2::ErrorCode::DestinationUnavailable
            }
            ErrorKind::TimedOut => p2::ErrorCode::ConnectionTimeout,
            ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted => {
                p2::ErrorCode::ConnectionTerminated
            }
            _ if is_address_family_unsupported(err) => p2::ErrorCode::DestinationUnavailable,
            _ => p2::ErrorCode::ConnectionRefused,
        }
    }

    /// Optional hook to configure the error code for TLS I/O errors.
    ///
    /// The default distinguishes terminated connections and, with the
    /// `default-send-request` feature, rustls certificate errors and received
    /// alerts. Other errors become `TLS-protocol-error`.
    #[cfg(feature = "p2")]
    fn p2_error_from_tls(&mut self, err: &std::io::Error) -> p2::ErrorCode {
        tracing::warn!("tls error: {err:?}");
        #[cfg(feature = "default-send-request")]
        if let Some(err) = err
            .get_ref()
            .and_then(|err| err.downcast_ref::<rustls::Error>())
        {
            return match err {
                rustls::Error::InvalidCertificate(_) => p2::ErrorCode::TlsCertificateError,
                rustls::Error::AlertReceived(alert) => {
                    p2::ErrorCode::TlsAlertReceived(p2::TlsAlertReceivedPayload {
                        alert_id: Some(u8::from(*alert)),
                        alert_message: None,
                    })
                }
                _ => p2::ErrorCode::TlsProtocolError,
            };
        }
        match err.kind() {
            std::io::ErrorKind::ConnectionReset
            | std::io::ErrorKind::ConnectionAborted
            | std::io::ErrorKind::BrokenPipe
            | std::io::ErrorKind::UnexpectedEof => p2::ErrorCode::ConnectionTerminated,
            _ => p2::ErrorCode::TlsProtocolError,
        }
    }

    /// Optional hook to configure the error code for DNS errors.
    #[cfg(all(feature = "p2", feature = "default-send-request"))]
    fn p2_error_from_dns(&mut self, err: &rustls::pki_types::InvalidDnsNameError) -> p2::ErrorCode {
        tracing::warn!("dns lookup error: {err:?}");
        p2::ErrorCode::DnsError(p2::DnsErrorPayload {
            rcode: Some("invalid dns name".to_string()),
            info_code: None,
        })
    }

    /// Number of distinct write calls to the outgoing body's output-stream
    /// that the implementation will buffer.
    /// Default: 1.
    #[cfg(feature = "p3")]
    fn p3_outgoing_body_buffer_chunks(&mut self) -> usize {
        crate::p3::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS
    }

    /// Maximum number of bytes the implementation will copy out of the guest in
    /// a single write to an outgoing body's stream.
    #[cfg(feature = "p3")]
    fn p3_outgoing_body_chunk_size(&mut self) -> usize {
        crate::p3::DEFAULT_OUTGOING_BODY_CHUNK_SIZE
    }

    /// Optional hook to configure the error code for hyper errors.
    #[cfg(feature = "p3")]
    fn p3_error_from_hyper(&mut self, err: &hyper::Error) -> p3::ErrorCode {
        tracing::warn!("hyper error: {err:?}");
        p3::ErrorCode::HttpProtocolError
    }

    /// Optional hook to configure the error code for connect I/O errors.
    ///
    /// The default distinguishes refused, unreachable, timed out, and terminated
    /// connections. Unrecognized errors become `connection-refused`.
    #[cfg(feature = "p3")]
    fn p3_error_from_connect(&mut self, err: &std::io::Error) -> p3::ErrorCode {
        tracing::warn!("connect error: {err:?}");
        use std::io::ErrorKind;
        match err.kind() {
            ErrorKind::ConnectionRefused => p3::ErrorCode::ConnectionRefused,
            ErrorKind::HostUnreachable | ErrorKind::NetworkUnreachable | ErrorKind::NetworkDown => {
                p3::ErrorCode::DestinationUnavailable
            }
            ErrorKind::TimedOut => p3::ErrorCode::ConnectionTimeout,
            ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted => {
                p3::ErrorCode::ConnectionTerminated
            }
            _ if is_address_family_unsupported(err) => p3::ErrorCode::DestinationUnavailable,
            _ => p3::ErrorCode::ConnectionRefused,
        }
    }

    /// Optional hook to configure the error code for TLS I/O errors.
    ///
    /// The default distinguishes terminated connections and, with the
    /// `default-send-request` feature, rustls certificate errors and received
    /// alerts. Other errors become `TLS-protocol-error`.
    #[cfg(feature = "p3")]
    fn p3_error_from_tls(&mut self, err: &std::io::Error) -> p3::ErrorCode {
        tracing::warn!("tls error: {err:?}");
        #[cfg(feature = "default-send-request")]
        if let Some(err) = err
            .get_ref()
            .and_then(|err| err.downcast_ref::<rustls::Error>())
        {
            return match err {
                rustls::Error::InvalidCertificate(_) => p3::ErrorCode::TlsCertificateError,
                rustls::Error::AlertReceived(alert) => {
                    p3::ErrorCode::TlsAlertReceived(p3::TlsAlertReceivedPayload {
                        alert_id: Some(u8::from(*alert)),
                        alert_message: None,
                    })
                }
                _ => p3::ErrorCode::TlsProtocolError,
            };
        }
        match err.kind() {
            std::io::ErrorKind::ConnectionReset
            | std::io::ErrorKind::ConnectionAborted
            | std::io::ErrorKind::BrokenPipe
            | std::io::ErrorKind::UnexpectedEof => p3::ErrorCode::ConnectionTerminated,
            _ => p3::ErrorCode::TlsProtocolError,
        }
    }

    /// Optional hook to configure the error code for DNS errors.
    #[cfg(all(feature = "p3", feature = "default-send-request"))]
    fn p3_error_from_dns(&mut self, err: &rustls::pki_types::InvalidDnsNameError) -> p3::ErrorCode {
        tracing::warn!("dns lookup error: {err:?}");
        p3::ErrorCode::DnsError(p3::DnsErrorPayload {
            rcode: Some("invalid dns name".to_string()),
            info_code: None,
        })
    }
}

// EAFNOSUPPORT has no corresponding stable ErrorKind.
#[cfg(any(feature = "p2", feature = "p3"))]
fn is_address_family_unsupported(err: &std::io::Error) -> bool {
    use rustix::io::Errno;
    Errno::from_io_error(err) == Some(Errno::AFNOSUPPORT)
}

/// Returns a value suitable for the `WasiHttpCtxView::hooks` field which has
/// the default behavior for `wasi:http`.
#[cfg(feature = "default-send-request")]
pub fn default_hooks() -> &'static mut dyn WasiHttpHooks {
    Default::default()
}

#[cfg(feature = "default-send-request")]
impl<'a> Default for &'a mut dyn WasiHttpHooks {
    fn default() -> Self {
        let x: &mut [(); 0] = &mut [];
        x
    }
}

#[doc(hidden)]
#[cfg(feature = "default-send-request")]
impl WasiHttpHooks for [(); 0] {}

/// A helper struct which implements [`HasData`] for the `wasi:http` APIs when
/// used in combination with named imports.
///
/// This structure is similar in purpose to [`WasiHttp`] and is used
/// when using the [`named_imports`] module for `wasi:http`. This structure
/// serves as the `D` type parameter for `add_to_linker` functions.
///
/// [`named_imports`]: crate::p3::bindings::named_imports::wasi::http
///
/// # Meaning of the `T` parameter
///
/// Here the `T` must be something that implements [`WasiHttpNamedView`]. The
/// corresponding `Data` for this type is [`WasiCtxNamedView`] which internally
/// will contain `&mut T`.
///
/// Effectively you're going to implement [`WasiHttpNamedView`] for something in
/// your embedding, and that's the `T` you'll fill in here.
///
/// # Examples
///
/// ```
/// use wasmtime::component::{Linker, Component, ResourceTable};
/// use wasmtime::{Engine, Result};
/// use wasmtime_wasi::{NamedId, WasiCtxNamedView};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpCtxView, WasiHttpNamed, WasiHttpNamedView};
/// use wasmtime_wasi_http::p2::bindings::named_imports;
/// use std::collections::HashMap;
///
/// struct MyStoreState {
///     table: ResourceTable,
///     states: HashMap<NamedId, WasiHttpCtx>,
/// }
///
/// fn main() -> Result<()> {
///     let engine = Engine::default();
///     let mut linker = Linker::new(&engine);
///     let component = Component::new(&engine, "(component)")?;
///     let mut name_map = HashMap::new();
///
///     named_imports::wasi::http::outgoing_handler::add_to_linker::<MyStoreState, WasiHttpNamed<MyStoreState>>(
///         &mut linker,
///         &component,
///         |name| {
///             let len = name_map.len();
///             Ok(NamedId(*name_map.entry(name.to_string()).or_insert(len)))
///         },
///         |state| WasiCtxNamedView(state),
///     )?;
///     Ok(())
/// }
///
/// impl WasiHttpNamedView for MyStoreState {
///     fn http(&mut self, id: NamedId) -> WasiHttpCtxView<'_> {
///         let ctx = self.states.get_mut(&id).expect("state for id");
///         WasiHttpCtxView {
///             ctx,
///             table: &mut self.table,
///             hooks: Default::default(),
///         }
///     }
/// }
/// ```
pub struct WasiHttpNamed<T>(marker::PhantomData<fn() -> T>);

impl<T> HasData for WasiHttpNamed<T>
where
    T: WasiHttpNamedView,
{
    type Data<'a> = WasiCtxNamedView<'a, T>;
}

/// A trait used to look up a specific `wasi:http` context for a named import.
///
/// This trait is used in conjunction with the [`named_imports`] bindings
/// generated for all `wasi:http` interfaces. The purpose of this trait is for
/// embedders to define how a [`NamedId`] maps to a particular `wasi:http`
/// context, here returned as [`WasiHttpCtxView`]. Embedders are responsible
/// for assigning meaning to [`NamedId`] values themselves. These IDs are
/// assigned when [`add_named_to_linker`] is called, for example, as the
/// `lookup` argument to that function.
///
/// When using [`add_named_to_linker`] it's sufficient to implement this trait
/// for the `T` in `Store<T>`.
///
/// When using `add_to_linker` in the generated `bindings::named_imports`
/// module then values implementing this live within the `T` of `Store<T>`, and
/// be temporarily referenced in [`WasiCtxNamedView`] where internally that'll
/// hold `WasiCtxNamedView(&mut your_type)`.
///
/// [`named_imports`]: crate::p3::bindings::named_imports
/// [`add_named_to_linker`]: crate::p3::add_named_to_linker
///
/// # Examples
///
/// ```
/// use wasmtime::component::{Linker, Component, ResourceTable};
/// use wasmtime::{Engine, Result};
/// use wasmtime_wasi::NamedId;
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpCtxView, WasiHttpNamedView};
/// use std::collections::HashMap;
///
/// struct MyStoreState {
///     table: ResourceTable,
///     states: HashMap<NamedId, WasiHttpCtx>,
/// }
///
/// fn main() -> Result<()> {
///     let engine = Engine::default();
///     let mut linker = Linker::new(&engine);
///     let component = Component::new(&engine, "(component)")?;
///     let mut name_map = HashMap::new();
///
///     wasmtime_wasi_http::p3::add_named_to_linker::<MyStoreState>(
///         &mut linker,
///         &component,
///         |_, name| {
///             let len = name_map.len();
///             Ok(NamedId(*name_map.entry(name.to_string()).or_insert(len)))
///         },
///     )?;
///     Ok(())
/// }
///
/// impl WasiHttpNamedView for MyStoreState {
///     fn http(&mut self, id: NamedId) -> WasiHttpCtxView<'_> {
///         let ctx = self.states.get_mut(&id).expect("state for id");
///         WasiHttpCtxView {
///             ctx,
///             table: &mut self.table,
///             hooks: Default::default(),
///         }
///     }
/// }
/// ```
pub trait WasiHttpNamedView: Send + 'static {
    /// Looks up the [`WasiHttpCtxView`] for the given [`NamedId`].
    ///
    /// This method will resolve the `id` specified to a specific HTTP context
    /// that is available to be used. Note that this method is specifically
    /// infallible meaning that an HTTP context must be returned and this cannot
    /// generate a trap or panic or similar.
    ///
    /// Embedders are responsible for allocating [`NamedId`] and assigning
    /// meaning to ids. When a `Linker` is populated embedders will have the
    /// ability to generate a `NamedId` for all imports found, and then that
    /// embedder-allocated id is then passed back here when the corresponding
    /// imported function is invoked.
    ///
    /// It's strongly recommended, though not always required, that the
    /// [`ResourceTable`] in the returned [`WasiHttpCtxView`] is the same table
    /// that the default context, [`WasiHttpView::http`], uses. Resources
    /// created through a named import can be used through interfaces bound to
    /// the default context (and vice versa), and with separate tables those
    /// lookups fail or find another context's resources.
    fn http(&mut self, id: NamedId) -> WasiHttpCtxView<'_>;
}