Skip to main content

wasmtime_wasi_http/p2/
types.rs

1//! Implements the base structure that will provide the implementation of the
2//! wasi-http API.
3
4use crate::p2::{
5    bindings::http::types::{self, Method, Scheme},
6    body::{HostIncomingBody, HyperIncomingBody, HyperOutgoingBody},
7};
8use crate::{Error, FieldMap, WasiHttpCtxView};
9use bytes::Bytes;
10use http_body_util::BodyExt;
11use hyper::body::Body;
12use wasmtime::component::Resource;
13use wasmtime::{Result, bail};
14use wasmtime_wasi::p2::Pollable;
15use wasmtime_wasi::runtime::AbortOnDropJoinHandle;
16
17impl From<http::Method> for types::Method {
18    fn from(method: http::Method) -> Self {
19        if method == http::Method::GET {
20            types::Method::Get
21        } else if method == hyper::Method::HEAD {
22            types::Method::Head
23        } else if method == hyper::Method::POST {
24            types::Method::Post
25        } else if method == hyper::Method::PUT {
26            types::Method::Put
27        } else if method == hyper::Method::DELETE {
28            types::Method::Delete
29        } else if method == hyper::Method::CONNECT {
30            types::Method::Connect
31        } else if method == hyper::Method::OPTIONS {
32            types::Method::Options
33        } else if method == hyper::Method::TRACE {
34            types::Method::Trace
35        } else if method == hyper::Method::PATCH {
36            types::Method::Patch
37        } else {
38            types::Method::Other(method.to_string())
39        }
40    }
41}
42
43impl TryInto<http::Method> for types::Method {
44    type Error = http::method::InvalidMethod;
45
46    fn try_into(self) -> Result<http::Method, Self::Error> {
47        match self {
48            Method::Get => Ok(http::Method::GET),
49            Method::Head => Ok(http::Method::HEAD),
50            Method::Post => Ok(http::Method::POST),
51            Method::Put => Ok(http::Method::PUT),
52            Method::Delete => Ok(http::Method::DELETE),
53            Method::Connect => Ok(http::Method::CONNECT),
54            Method::Options => Ok(http::Method::OPTIONS),
55            Method::Trace => Ok(http::Method::TRACE),
56            Method::Patch => Ok(http::Method::PATCH),
57            Method::Other(s) => http::Method::from_bytes(s.as_bytes()),
58        }
59    }
60}
61
62/// The concrete type behind a `wasi:http/types.incoming-request` resource.
63#[derive(Debug)]
64pub struct HostIncomingRequest {
65    pub(crate) method: http::method::Method,
66    pub(crate) uri: http::uri::Uri,
67    pub(crate) headers: FieldMap,
68    pub(crate) scheme: Scheme,
69    pub(crate) authority: String,
70    /// The body of the incoming request.
71    pub body: Option<HostIncomingBody>,
72}
73
74impl WasiHttpCtxView<'_> {
75    /// Create a new incoming request resource.
76    pub fn new_incoming_request<B>(
77        &mut self,
78        scheme: Scheme,
79        req: hyper::Request<B>,
80    ) -> wasmtime::Result<Resource<HostIncomingRequest>>
81    where
82        B: Body<Data = Bytes> + Send + 'static,
83        B::Error: Into<Error>,
84    {
85        let (parts, body) = req.into_parts();
86        let body = body.map_err(Into::into).boxed_unsync();
87        let body = HostIncomingBody::new(body);
88        let authority = match parts.uri.authority() {
89            Some(authority) => authority.to_string(),
90            None => match parts.headers.get(http::header::HOST) {
91                Some(host) => host.to_str()?.to_string(),
92                None => bail!("invalid HTTP request missing authority in URI and host header"),
93            },
94        };
95
96        let headers = FieldMap::new_immutable(self.hooks, parts.headers);
97
98        let req = HostIncomingRequest {
99            method: parts.method,
100            uri: parts.uri,
101            headers,
102            authority,
103            scheme,
104            body: Some(body),
105        };
106        Ok(self.table.push(req)?)
107    }
108}
109
110/// The concrete type behind a `wasi:http/types.response-outparam` resource.
111pub struct HostResponseOutparam {
112    /// The callback sending a response.
113    pub send:
114        Box<dyn FnOnce(Result<hyper::Response<HyperOutgoingBody>, types::ErrorCode>) + Send + Sync>,
115}
116
117impl WasiHttpCtxView<'_> {
118    /// Create a new outgoing response resource.
119    pub fn new_response_outparam(
120        &mut self,
121        result: tokio::sync::oneshot::Sender<
122            Result<hyper::Response<HyperOutgoingBody>, types::ErrorCode>,
123        >,
124    ) -> wasmtime::Result<Resource<HostResponseOutparam>> {
125        let id = self.table.push(HostResponseOutparam {
126            send: Box::new(move |value| {
127                // Giving the API doesn't return any error, it's probably
128                // better to ignore the error than trap the guest, in case of
129                // host timeout and dropped the receiver side of the channel.
130                // See also: #10784
131                _ = result.send(value)
132            }),
133        })?;
134        Ok(id)
135    }
136
137    /// Create a new outgoing response from an `FnOnce`.
138    pub fn new_response_outparam_from_callback(
139        &mut self,
140        callback: impl FnOnce(Result<hyper::Response<HyperOutgoingBody>, types::ErrorCode>)
141        + Send
142        + Sync
143        + 'static,
144    ) -> wasmtime::Result<Resource<HostResponseOutparam>> {
145        let id = self.table.push(HostResponseOutparam {
146            send: Box::new(callback),
147        })?;
148        Ok(id)
149    }
150}
151
152/// The concrete type behind a `wasi:http/types.outgoing-response` resource.
153pub struct HostOutgoingResponse {
154    /// The status of the response.
155    pub status: http::StatusCode,
156    /// The headers of the response.
157    pub headers: FieldMap,
158    /// The body of the response.
159    pub body: Option<HyperOutgoingBody>,
160}
161
162impl TryFrom<HostOutgoingResponse> for hyper::Response<HyperOutgoingBody> {
163    type Error = http::Error;
164
165    fn try_from(
166        resp: HostOutgoingResponse,
167    ) -> Result<hyper::Response<HyperOutgoingBody>, Self::Error> {
168        use http_body_util::Empty;
169
170        let mut builder = hyper::Response::builder().status(resp.status);
171
172        *builder.headers_mut().unwrap() = resp.headers.into();
173
174        match resp.body {
175            Some(body) => builder.body(body),
176            None => builder.body(
177                Empty::<bytes::Bytes>::new()
178                    .map_err(|_| unreachable!("Infallible error"))
179                    .boxed_unsync(),
180            ),
181        }
182    }
183}
184
185/// The concrete type behind a `wasi:http/types.outgoing-request` resource.
186#[derive(Debug)]
187pub struct HostOutgoingRequest {
188    /// The method of the request.
189    pub method: Method,
190    /// The scheme of the request.
191    pub scheme: Option<Scheme>,
192    /// The authority of the request.
193    pub authority: Option<String>,
194    /// The path and query of the request.
195    pub path_with_query: Option<String>,
196    /// The request headers.
197    pub headers: FieldMap,
198    /// The request body.
199    pub body: Option<HyperOutgoingBody>,
200}
201
202/// The concrete type behind a `wasi:http/types.incoming-response` resource.
203#[derive(Debug)]
204pub struct HostIncomingResponse {
205    /// The response status
206    pub status: u16,
207    /// The response headers
208    pub headers: FieldMap,
209    /// The response body
210    pub body: Option<HostIncomingBody>,
211}
212
213/// A handle to a future incoming response.
214pub type FutureIncomingResponseHandle = AbortOnDropJoinHandle<SendRequestResult>;
215
216/// A response that is in the process of being received.
217#[derive(Debug)]
218pub struct IncomingResponse {
219    /// The response itself.
220    pub resp: hyper::Response<HyperIncomingBody>,
221    /// Optional worker task that continues to process the response.
222    pub worker: Option<AbortOnDropJoinHandle<()>>,
223}
224
225type SendRequestResult =
226    crate::Result<(http::Response<HyperIncomingBody>, AbortOnDropJoinHandle<()>)>;
227
228/// The concrete type behind a `wasi:http/types.future-incoming-response` resource.
229pub enum HostFutureIncomingResponse {
230    /// A pending response
231    Pending(FutureIncomingResponseHandle),
232    /// The response is ready.
233    ///
234    /// An outer error will trap while the inner error gets returned to the guest.
235    Ready(SendRequestResult),
236    /// The response has been consumed.
237    Consumed,
238}
239
240impl HostFutureIncomingResponse {
241    /// Unwrap the response, panicking if it is not ready.
242    pub(crate) fn unwrap_ready(self) -> SendRequestResult {
243        match self {
244            Self::Ready(res) => res,
245            Self::Pending(_) | Self::Consumed => {
246                panic!("unwrap_ready called on a pending HostFutureIncomingResponse")
247            }
248        }
249    }
250}
251
252#[async_trait::async_trait]
253impl Pollable for HostFutureIncomingResponse {
254    async fn ready(&mut self) {
255        if let Self::Pending(handle) = self {
256            *self = Self::Ready(handle.await);
257        }
258    }
259}