wasmtime_wasi_http/p3/mod.rs
1//! Experimental, unstable and incomplete implementation of wasip3 version of `wasi:http`.
2//!
3//! This module is under heavy development.
4//! It is not compliant with semver and is not ready
5//! for production use.
6//!
7//! Bug and security fixes limited to wasip3 will not be given patch releases.
8//!
9//! Documentation of this module may be incorrect or out-of-sync with the implementation.
10
11pub mod bindings;
12mod body;
13mod conv;
14mod helpers;
15mod host;
16mod proxy;
17mod request;
18mod response;
19
20pub use request::Request;
21pub use response::Response;
22
23use crate::{FieldMapError, WasiHttp, WasiHttpView};
24use bindings::http::{client, types};
25use core::ops::Deref;
26use std::sync::Arc;
27use wasmtime::component::Linker;
28use wasmtime_wasi::TrappableError;
29
30pub(crate) type HttpResult<T> = Result<T, HttpError>;
31pub(crate) type HttpError = TrappableError<types::ErrorCode>;
32
33pub(crate) type HeaderResult<T> = Result<T, HeaderError>;
34pub(crate) type HeaderError = TrappableError<types::HeaderError>;
35
36impl From<FieldMapError> for HeaderError {
37 fn from(e: FieldMapError) -> Self {
38 match e {
39 FieldMapError::Immutable => types::HeaderError::Immutable.into(),
40 FieldMapError::InvalidHeaderName | FieldMapError::InvalidHeaderValue => {
41 types::HeaderError::InvalidSyntax.into()
42 }
43 FieldMapError::TooManyFields | FieldMapError::TotalSizeTooBig => {
44 types::HeaderError::SizeExceeded.into()
45 }
46 FieldMapError::Forbidden => types::HeaderError::Forbidden.into(),
47 }
48 }
49}
50
51pub(crate) type RequestOptionsResult<T> = Result<T, RequestOptionsError>;
52pub(crate) type RequestOptionsError = TrappableError<types::RequestOptionsError>;
53
54/// Add all interfaces from this module into the `linker` provided.
55///
56/// This function will add all interfaces implemented by this module to the
57/// [`Linker`], which corresponds to the `wasi:http/imports` world supported by
58/// this module.
59///
60/// # Example
61///
62/// ```
63/// use wasmtime::{Engine, Result, Store, Config};
64/// use wasmtime::component::{Linker, ResourceTable};
65/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpCtxView, WasiHttpView};
66///
67/// fn main() -> Result<()> {
68/// let mut config = Config::new();
69/// config.wasm_component_model_async(true);
70/// let engine = Engine::new(&config)?;
71///
72/// let mut linker = Linker::<MyState>::new(&engine);
73/// wasmtime_wasi_http::p3::add_to_linker(&mut linker)?;
74/// // ... add any further functionality to `linker` if desired ...
75///
76/// let mut store = Store::new(
77/// &engine,
78/// MyState::default(),
79/// );
80///
81/// // ... use `linker` to instantiate within `store` ...
82///
83/// Ok(())
84/// }
85///
86/// #[derive(Default)]
87/// struct MyState {
88/// http: WasiHttpCtx,
89/// table: ResourceTable,
90/// }
91///
92/// impl WasiHttpView for MyState {
93/// fn http(&mut self) -> WasiHttpCtxView<'_> {
94/// WasiHttpCtxView {
95/// ctx: &mut self.http,
96/// table: &mut self.table,
97/// hooks: Default::default(),
98/// }
99/// }
100/// }
101/// ```
102pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
103where
104 T: WasiHttpView + 'static,
105{
106 client::add_to_linker::<_, WasiHttp>(linker, T::http)?;
107 types::add_to_linker::<_, WasiHttp>(linker, T::http)?;
108 Ok(())
109}
110
111/// An [Arc], which may be immutable.
112///
113/// In `wasi:http` resources like `fields` or `request-options` may be
114/// mutable or immutable. This construct is used to model them efficiently.
115pub enum MaybeMutable<T> {
116 /// Clone-on-write, mutable [Arc]
117 Mutable(Arc<T>),
118 /// Immutable [Arc]
119 Immutable(Arc<T>),
120}
121
122impl<T> From<MaybeMutable<T>> for Arc<T> {
123 fn from(v: MaybeMutable<T>) -> Self {
124 v.into_arc()
125 }
126}
127
128impl<T> Deref for MaybeMutable<T> {
129 type Target = Arc<T>;
130
131 fn deref(&self) -> &Self::Target {
132 match self {
133 Self::Mutable(v) | Self::Immutable(v) => v,
134 }
135 }
136}
137
138impl<T> MaybeMutable<T> {
139 /// Construct a mutable [`MaybeMutable`].
140 pub fn new_mutable(v: impl Into<Arc<T>>) -> Self {
141 Self::Mutable(v.into())
142 }
143
144 /// Construct a mutable [`MaybeMutable`] filling it with default `T`.
145 pub fn new_mutable_default() -> Self
146 where
147 T: Default,
148 {
149 Self::new_mutable(T::default())
150 }
151
152 /// Construct an immutable [`MaybeMutable`].
153 pub fn new_immutable(v: impl Into<Arc<T>>) -> Self {
154 Self::Immutable(v.into())
155 }
156
157 /// Unwrap [`MaybeMutable`] into [`Arc`].
158 pub fn into_arc(self) -> Arc<T> {
159 match self {
160 Self::Mutable(v) | Self::Immutable(v) => v,
161 }
162 }
163
164 /// If this [`MaybeMutable`] is [`Mutable`](MaybeMutable::Mutable),
165 /// return a mutable reference to it, otherwise return `None`.
166 ///
167 /// Internally, this will use [`Arc::make_mut`] and will clone the underlying
168 /// value, if multiple strong references to the inner [`Arc`] exist.
169 pub fn get_mut(&mut self) -> Option<&mut T>
170 where
171 T: Clone,
172 {
173 match self {
174 Self::Mutable(v) => Some(Arc::make_mut(v)),
175 Self::Immutable(..) => None,
176 }
177 }
178}