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
12 use http::{HeaderName, header};
13
14 mod ctx;
15 mod field_map;
16 #[cfg(feature = "component-model-async")]
17 pub mod handler;
18 pub mod io;
19 #[cfg(feature = "p2")]
20 pub mod p2;
21 #[cfg(feature = "p3")]
22 pub mod p3;
23
24 pub use ctx::*;
25 pub 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"))]
get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>>31 fn 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.
42 pub 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 ];
53