wasmcloud-component 0.2.0

wasmCloud component library giving access to interfaces provided by wasmCloud host runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! This module provides utilities for writing HTTP servers and clients using the WASI HTTP API.
//!
//! It's inspired by the WASI 0.3 proposal for <https://github.com/WebAssembly/wasi-http> and will
//! be supported until the release of wasi:http@0.3.0. After that, this module will likely be deprecated.
//!
//! ```rust
//! use wasmcloud_component::http;
//!
//! struct Component;
//!
//! http::export!(Component);
//!
//! // Implementing the [`Server`] trait for a component
//! impl http::Server for Component {
//!     fn handle(_request: http::IncomingRequest) -> http::Result<http::Response<impl http::OutgoingBody>> {
//!         Ok(http::Response::new("Hello from Rust!"))
//!     }
//! }
//! ```
use core::fmt::Display;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

use std::io::{Read, Write};

use anyhow::{anyhow, Context as _};
use wasi::http::types::{OutgoingResponse, ResponseOutparam};
use wasi::io::streams::{InputStream, OutputStream, StreamError};

pub use http::{
    header, method, response, uri, HeaderMap, HeaderName, HeaderValue, Method, Request, Response,
    StatusCode, Uri,
};
pub use wasi::http::types::ErrorCode;

pub type Result<T, E = ErrorCode> = core::result::Result<T, E>;

pub type IncomingRequest = Request<IncomingBody>;

impl crate::From<Method> for wasi::http::types::Method {
    fn from(method: Method) -> Self {
        match method.as_str() {
            "GET" => Self::Get,
            "HEAD" => Self::Head,
            "POST" => Self::Post,
            "PUT" => Self::Put,
            "DELETE" => Self::Delete,
            "CONNECT" => Self::Connect,
            "OPTIONS" => Self::Options,
            "TRACE" => Self::Trace,
            "PATCH" => Self::Patch,
            _ => Self::Other(method.to_string()),
        }
    }
}

impl crate::TryFrom<wasi::http::types::Method> for Method {
    type Error = method::InvalidMethod;

    fn try_from(method: wasi::http::types::Method) -> Result<Self, Self::Error> {
        match method {
            wasi::http::types::Method::Get => Ok(Self::GET),
            wasi::http::types::Method::Head => Ok(Self::HEAD),
            wasi::http::types::Method::Post => Ok(Self::POST),
            wasi::http::types::Method::Put => Ok(Self::PUT),
            wasi::http::types::Method::Delete => Ok(Self::DELETE),
            wasi::http::types::Method::Connect => Ok(Self::CONNECT),
            wasi::http::types::Method::Options => Ok(Self::OPTIONS),
            wasi::http::types::Method::Trace => Ok(Self::TRACE),
            wasi::http::types::Method::Patch => Ok(Self::PATCH),
            wasi::http::types::Method::Other(method) => method.parse(),
        }
    }
}

impl crate::From<uri::Scheme> for wasi::http::types::Scheme {
    fn from(scheme: uri::Scheme) -> Self {
        match scheme.as_str() {
            "http" => Self::Http,
            "https" => Self::Https,
            _ => Self::Other(scheme.to_string()),
        }
    }
}

impl crate::TryFrom<wasi::http::types::Scheme> for http::uri::Scheme {
    type Error = uri::InvalidUri;

    fn try_from(scheme: wasi::http::types::Scheme) -> Result<Self, Self::Error> {
        match scheme {
            wasi::http::types::Scheme::Http => Ok(Self::HTTP),
            wasi::http::types::Scheme::Https => Ok(Self::HTTPS),
            wasi::http::types::Scheme::Other(scheme) => scheme.parse(),
        }
    }
}

#[derive(Debug)]
pub enum FieldsToHeaderMapError {
    InvalidHeaderName(header::InvalidHeaderName),
    InvalidHeaderValue(header::InvalidHeaderValue),
}

impl Display for FieldsToHeaderMapError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            FieldsToHeaderMapError::InvalidHeaderName(e) => write!(f, "invalid header name: {e}"),
            FieldsToHeaderMapError::InvalidHeaderValue(e) => write!(f, "invalid header value: {e}"),
        }
    }
}

impl std::error::Error for FieldsToHeaderMapError {}

impl crate::TryFrom<wasi::http::types::Fields> for HeaderMap {
    type Error = FieldsToHeaderMapError;

    fn try_from(fields: wasi::http::types::Fields) -> Result<Self, Self::Error> {
        let mut headers = HeaderMap::new();
        for (name, value) in fields.entries() {
            let name =
                HeaderName::try_from(name).map_err(FieldsToHeaderMapError::InvalidHeaderName)?;
            let value =
                HeaderValue::try_from(value).map_err(FieldsToHeaderMapError::InvalidHeaderValue)?;
            match headers.entry(name) {
                header::Entry::Vacant(entry) => {
                    entry.insert(value);
                }
                header::Entry::Occupied(mut entry) => {
                    entry.append(value);
                }
            };
        }
        Ok(headers)
    }
}

impl crate::TryFrom<HeaderMap> for wasi::http::types::Fields {
    type Error = wasi::http::types::HeaderError;

    fn try_from(headers: HeaderMap) -> Result<Self, Self::Error> {
        let fields = wasi::http::types::Fields::new();
        for (name, value) in &headers {
            fields.append(&name.to_string(), &value.as_bytes().to_vec())?;
        }
        Ok(fields)
    }
}

/// Trait for implementing a type that can be written to an outgoing HTTP response body.
///
/// When implementing this trait, you should write your type to the provided `OutputStream` and then
/// drop the stream. Finally, you should call `wasi::http::types::OutgoingBody::finish` to finish
/// the body and return the result.
///
/// This trait is already implemented for common Rust types, and it's implemented for
/// `wasi::http::types::IncomingBody` and `wasi::io::streams::InputStream` as well. This enables
/// using any stream from a Wasm interface as an outgoing body.
///
/// ```ignore
/// use std::io::Write;
///
/// impl wasmcloud_component::http::OutgoingBody for Vec<u8> {
///    fn write(
///        self,
///        body: wasi::http::types::OutgoingBody,
///        mut stream: wasi::io::streams::OutputStream,
///    ) -> std::io::Result<()> {
///        stream.write_all(&self)?;
///        drop(stream);
///        wasi::http::types::OutgoingBody::finish(body, None)
///            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
///     }
/// }
/// ```
pub trait OutgoingBody {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()>;
}

impl OutgoingBody for () {
    fn write(
        self,
        _body: wasi::http::types::OutgoingBody,
        _stream: OutputStream,
    ) -> std::io::Result<()> {
        Ok(())
    }
}

impl OutgoingBody for &[u8] {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        mut stream: OutputStream,
    ) -> std::io::Result<()> {
        stream.write_all(self)?;
        drop(stream);
        wasi::http::types::OutgoingBody::finish(body, None)
            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
    }
}

impl OutgoingBody for Box<[u8]> {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        self.as_ref().write(body, stream)
    }
}

impl OutgoingBody for Vec<u8> {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        self.as_slice().write(body, stream)
    }
}

impl OutgoingBody for &str {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        self.as_bytes().write(body, stream)
    }
}

impl OutgoingBody for Box<str> {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        self.as_ref().write(body, stream)
    }
}

impl OutgoingBody for String {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        self.as_str().write(body, stream)
    }
}

impl OutgoingBody for InputStream {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        loop {
            match stream.blocking_splice(&self, u64::MAX) {
                Ok(0) | Err(StreamError::Closed) => break,
                Ok(_) => continue,
                Err(StreamError::LastOperationFailed(err)) => {
                    return Err(std::io::Error::new(std::io::ErrorKind::Other, err));
                }
            }
        }
        drop(stream);
        wasi::http::types::OutgoingBody::finish(body, None)
            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
    }
}

impl OutgoingBody for wasi::http::types::IncomingBody {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        let input = self
            .stream()
            .map_err(|()| std::io::Error::from(std::io::ErrorKind::Other))?;
        loop {
            match stream.blocking_splice(&input, u64::MAX) {
                Ok(0) | Err(StreamError::Closed) => break,
                Ok(_) => continue,
                Err(StreamError::LastOperationFailed(err)) => {
                    return Err(std::io::Error::new(std::io::ErrorKind::Other, err));
                }
            }
        }
        drop(stream);
        let _trailers = wasi::http::types::IncomingBody::finish(self);
        // NOTE: getting trailers crashes Wasmtime 25, so avoid doing so
        //let trailers = if let Some(trailers) = trailers.get() {
        //    trailers
        //} else {
        //    trailers.subscribe().block();
        //    trailers
        //        .get()
        //        .ok_or_else(|| std::io::Error::from(std::io::ErrorKind::Other))?
        //};
        //let trailers = trailers.map_err(|()| std::io::Error::from(std::io::ErrorKind::Other))?;
        //let trailers =
        //    trailers.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
        wasi::http::types::OutgoingBody::finish(body, None)
            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
    }
}

impl OutgoingBody for IncomingBody {
    fn write(
        self,
        body: wasi::http::types::OutgoingBody,
        stream: OutputStream,
    ) -> std::io::Result<()> {
        loop {
            match stream.blocking_splice(&self.stream, u64::MAX) {
                Ok(0) | Err(StreamError::Closed) => break,
                Ok(_) => continue,
                Err(StreamError::LastOperationFailed(err)) => {
                    return Err(std::io::Error::new(std::io::ErrorKind::Other, err));
                }
            }
        }
        drop(stream);
        let trailers = self
            .into_trailers_wasi()
            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
        wasi::http::types::OutgoingBody::finish(body, trailers)
            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
    }
}

/// Wraps a body, which implements [Read]
#[derive(Clone, Copy, Debug)]
pub struct ReadBody<T>(T);

impl<T: Read> OutgoingBody for ReadBody<T> {
    fn write(
        mut self,
        body: wasi::http::types::OutgoingBody,
        mut stream: OutputStream,
    ) -> std::io::Result<()> {
        std::io::copy(&mut self.0, &mut stream)?;
        drop(stream);
        wasi::http::types::OutgoingBody::finish(body, None)
            .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
    }
}

/// Wraps the incoming body of a request which just contains
/// the stream and the body of the request itself. The bytes of the body
/// are only read into memory explicitly. The implementation of [`OutgoingBody`]
/// for this type will read the bytes from the stream and write them to the
/// output stream.
pub struct IncomingBody {
    stream: InputStream,
    body: wasi::http::types::IncomingBody,
}

impl TryFrom<wasi::http::types::IncomingBody> for IncomingBody {
    type Error = anyhow::Error;

    fn try_from(body: wasi::http::types::IncomingBody) -> Result<Self, Self::Error> {
        let stream = body
            .stream()
            .map_err(|()| anyhow!("failed to get incoming request body"))?;
        Ok(Self { body, stream })
    }
}

impl TryFrom<wasi::http::types::IncomingRequest> for IncomingBody {
    type Error = anyhow::Error;

    fn try_from(request: wasi::http::types::IncomingRequest) -> Result<Self, Self::Error> {
        let body = request
            .consume()
            .map_err(|()| anyhow!("failed to consume incoming request"))?;
        body.try_into()
    }
}

impl Deref for IncomingBody {
    type Target = InputStream;

    fn deref(&self) -> &Self::Target {
        &self.stream
    }
}

impl DerefMut for IncomingBody {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.stream
    }
}

impl Read for IncomingBody {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        Read::read(&mut self.stream, buf)
    }
}

impl IncomingBody {
    pub fn into_trailers(self) -> anyhow::Result<Option<HeaderMap>> {
        let trailers = self.into_trailers_wasi()?;
        let trailers = trailers.map(crate::TryInto::try_into).transpose()?;
        Ok(trailers)
    }

    pub fn into_trailers_wasi(self) -> anyhow::Result<Option<wasi::http::types::Fields>> {
        let IncomingBody { body, stream } = self;
        drop(stream);
        let _trailers = wasi::http::types::IncomingBody::finish(body);
        // NOTE: getting trailers crashes Wasmtime 25, so avoid doing so
        //let trailers = if let Some(trailers) = trailers.get() {
        //    trailers
        //} else {
        //    trailers.subscribe().block();
        //    trailers.get().context("trailers missing")?
        //};
        //let trailers = trailers.map_err(|()| anyhow!("trailers already consumed"))?;
        //trailers.context("failed to receive trailers")
        Ok(None)
    }
}

impl crate::TryFrom<wasi::http::types::IncomingRequest> for Request<IncomingBody> {
    type Error = anyhow::Error;

    fn try_from(request: wasi::http::types::IncomingRequest) -> Result<Self, Self::Error> {
        let uri = Uri::builder();
        let uri = if let Some(path_with_query) = request.path_with_query() {
            uri.path_and_query(path_with_query)
        } else {
            uri.path_and_query("/")
        };
        let uri = if let Some(scheme) = request.scheme() {
            let scheme = <uri::Scheme as crate::TryFrom<_>>::try_from(scheme)
                .context("failed to convert scheme")?;
            uri.scheme(scheme)
        } else {
            uri
        };
        let uri = if let Some(authority) = request.authority() {
            uri.authority(authority)
        } else {
            uri
        };
        let uri = uri.build().context("failed to build URI")?;
        let method = <Method as crate::TryFrom<_>>::try_from(request.method())
            .context("failed to convert method")?;
        let mut req = Request::builder().method(method).uri(uri);
        let req_headers = req
            .headers_mut()
            .context("failed to construct header map")?;
        *req_headers = crate::TryInto::try_into(request.headers())
            .context("failed to convert header fields to header map")?;
        let body = IncomingBody::try_from(request)?;
        req.body(body).context("failed to construct request")
    }
}

#[doc(hidden)]
#[derive(Default, Debug, Copy, Clone)]
pub struct IncomingHandler<T: ?Sized>(PhantomData<T>);

pub enum ResponseError {
    /// Status code is not valid
    StatusCode(StatusCode),
    /// Failed to set headers
    Headers(wasi::http::types::HeaderError),
    /// Failed to get outgoing response body
    Body,
    /// Failed to get outgoing response body stream
    BodyStream,
}

/// Trait for implementing an HTTP server WebAssembly component that receives a
/// [`IncomingRequest`] and returns a [`Response`].
pub trait Server {
    fn handle(request: IncomingRequest) -> Result<Response<impl OutgoingBody>, ErrorCode>;

    fn request_error(err: anyhow::Error) {
        eprintln!("failed to convert `wasi:http/types.incoming-request` to `http::Request`: {err}");
    }

    fn response_error(out: ResponseOutparam, err: ResponseError) {
        match err {
            ResponseError::StatusCode(code) => {
                ResponseOutparam::set(
                    out,
                    Err(ErrorCode::InternalError(Some(format!(
                        "code `{code}` is not a valid HTTP status code",
                    )))),
                );
            }
            ResponseError::Headers(err) => {
                ResponseOutparam::set(
                    out,
                    Err(ErrorCode::InternalError(Some(format!(
                        "{:#}",
                        anyhow!(err).context("failed to set headers"),
                    )))),
                );
            }
            ResponseError::Body => {
                ResponseOutparam::set(
                    out,
                    Err(ErrorCode::InternalError(Some(
                        "failed to get response body".into(),
                    ))),
                );
            }
            ResponseError::BodyStream => {
                ResponseOutparam::set(
                    out,
                    Err(ErrorCode::InternalError(Some(
                        "failed to get response body stream".into(),
                    ))),
                );
            }
        }
    }

    fn body_error(err: std::io::Error) {
        eprintln!("failed to write response body: {err}");
    }
}

impl<T: Server + ?Sized> wasi::exports::http::incoming_handler::Guest for IncomingHandler<T> {
    fn handle(
        request: wasi::http::types::IncomingRequest,
        response_out: wasi::http::types::ResponseOutparam,
    ) {
        match crate::TryInto::try_into(request) {
            Ok(request) => match T::handle(request) {
                Ok(response) => {
                    let (
                        response::Parts {
                            status, headers, ..
                        },
                        body,
                    ) = response.into_parts();

                    let headers = match crate::TryInto::try_into(headers) {
                        Ok(headers) => headers,
                        Err(err) => {
                            T::response_error(response_out, ResponseError::Headers(err));
                            return;
                        }
                    };
                    let resp_tx = OutgoingResponse::new(headers);
                    if let Err(()) = resp_tx.set_status_code(status.as_u16()) {
                        T::response_error(response_out, ResponseError::StatusCode(status));
                        return;
                    }

                    let Ok(resp_body) = resp_tx.body() else {
                        T::response_error(response_out, ResponseError::Body);
                        return;
                    };

                    let Ok(stream) = resp_body.write() else {
                        T::response_error(response_out, ResponseError::BodyStream);
                        return;
                    };

                    ResponseOutparam::set(response_out, Ok(resp_tx));
                    if let Err(err) = body.write(resp_body, stream) {
                        T::body_error(err);
                    }
                }
                Err(err) => ResponseOutparam::set(response_out, Err(err)),
            },
            Err(err) => T::request_error(err),
        }
    }
}

// Macro wrapper for wasi:http/incoming-handler

/// Macro to export [`wasi::exports::http::incoming_handler::Guest`] implementation for a type that
/// implements [`Server`]. This aims to be as similar as possible to [`wasi::http::proxy::export!`].
#[macro_export]
macro_rules! export {
    ($t:ty) => {
        type __IncomingHandlerExport = ::wasmcloud_component::http::IncomingHandler<$t>;
        ::wasmcloud_component::wasi::http::proxy::export!(__IncomingHandlerExport with_types_in ::wasmcloud_component::wasi);
    };
}
pub use export;