Skip to main content

wasmtime_wasi_http/
lib.rs

1//! Wasmtime's implementation of `wasi:http`
2//!
3//! This crate is organized similarly to [`wasmtime_wasi`] where there is a
4//! top-level [`p2`] and [`p3`] module corresponding to the implementation for
5//! WASIp2 and WASIp3.
6
7#![deny(missing_docs)]
8#![doc(test(attr(deny(warnings))))]
9#![doc(test(attr(allow(dead_code, unused_variables, unused_mut))))]
10#![cfg_attr(docsrs, feature(doc_cfg))]
11
12use http::{HeaderName, header};
13
14mod ctx;
15mod field_map;
16#[cfg(feature = "component-model-async")]
17pub mod handler;
18pub mod io;
19#[cfg(feature = "p2")]
20pub mod p2;
21#[cfg(feature = "p3")]
22pub mod p3;
23
24pub use ctx::*;
25pub use field_map::*;
26
27/// Extract the `Content-Length` header value from a [`http::HeaderMap`], returning `None` if it's not
28/// present. This function will return `Err` if it's not possible to parse the `Content-Length`
29/// header.
30#[cfg(any(feature = "p2", feature = "p3"))]
31fn get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>> {
32    let Some(v) = headers.get(header::CONTENT_LENGTH) else {
33        return Ok(None);
34    };
35    let v = v.to_str()?;
36    let v = v.parse()?;
37    Ok(Some(v))
38}
39
40/// Set of [http::header::HeaderName], that are forbidden by default
41/// for requests and responses originating in the guest.
42pub const DEFAULT_FORBIDDEN_HEADERS: [HeaderName; 9] = [
43    header::CONNECTION,
44    HeaderName::from_static("keep-alive"),
45    header::PROXY_AUTHENTICATE,
46    header::PROXY_AUTHORIZATION,
47    HeaderName::from_static("proxy-connection"),
48    header::TRANSFER_ENCODING,
49    header::UPGRADE,
50    header::HOST,
51    HeaderName::from_static("http2-settings"),
52];