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
14#[cfg(any(feature = "p2", feature = "p3"))]
15mod authority;
16#[cfg(any(feature = "p2", feature = "p3"))]
17pub(crate) use authority::parse_authority;
18
19mod ctx;
20#[cfg(feature = "default-send-request")]
21mod default_send_request;
22mod error;
23mod field_map;
24#[cfg(feature = "component-model-async")]
25pub mod handler;
26pub mod io;
27#[cfg(feature = "p2")]
28pub mod p2;
29#[cfg(feature = "p3")]
30pub mod p3;
31mod request_options;
32
33pub use ctx::*;
34#[cfg(feature = "default-send-request")]
35pub use default_send_request::*;
36pub use error::*;
37pub use field_map::*;
38pub use request_options::*;
39
40/// Extract the `Content-Length` header value from a [`http::HeaderMap`], returning `None` if it's not
41/// present. This function will return `Err` if it's not possible to parse the `Content-Length`
42/// header.
43#[cfg(any(feature = "p2", feature = "p3"))]
44fn get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>> {
45    let Some(v) = headers.get(header::CONTENT_LENGTH) else {
46        return Ok(None);
47    };
48    let v = v.to_str()?;
49    // RFC 9110 defines `Content-Length` as `1*DIGIT`. `u64`'s `FromStr` is more
50    // lenient and also accepts a leading `+`, so reject anything that isn't a
51    // non-empty run of decimal digits before parsing.
52    if v.is_empty() || !v.bytes().all(|b| b.is_ascii_digit()) {
53        wasmtime::bail!("invalid `content-length` header value: {v:?}");
54    }
55    let v = v.parse()?;
56    Ok(Some(v))
57}
58
59#[cfg(all(test, any(feature = "p2", feature = "p3")))]
60mod content_length_tests {
61    use super::get_content_length;
62    use http::{HeaderMap, HeaderValue, header};
63
64    fn headers(value: &str) -> HeaderMap {
65        let mut map = HeaderMap::new();
66        map.insert(
67            header::CONTENT_LENGTH,
68            HeaderValue::from_str(value).unwrap(),
69        );
70        map
71    }
72
73    #[test]
74    fn content_length_must_be_decimal_digits() {
75        assert_eq!(get_content_length(&HeaderMap::new()).unwrap(), None);
76        assert_eq!(get_content_length(&headers("0")).unwrap(), Some(0));
77        assert_eq!(get_content_length(&headers("1234")).unwrap(), Some(1234));
78
79        // `u64::from_str` accepts these but they are not `1*DIGIT` per RFC 9110.
80        assert!(get_content_length(&headers("+5")).is_err());
81        assert!(get_content_length(&headers("-5")).is_err());
82        assert!(get_content_length(&headers(" 5")).is_err());
83        assert!(get_content_length(&headers("")).is_err());
84    }
85}
86
87/// Set of [http::header::HeaderName], that are forbidden by default
88/// for requests and responses originating in the guest.
89pub const DEFAULT_FORBIDDEN_HEADERS: [HeaderName; 9] = [
90    header::CONNECTION,
91    HeaderName::from_static("keep-alive"),
92    header::PROXY_AUTHENTICATE,
93    header::PROXY_AUTHORIZATION,
94    HeaderName::from_static("proxy-connection"),
95    header::TRANSFER_ENCODING,
96    header::UPGRADE,
97    header::HOST,
98    HeaderName::from_static("http2-settings"),
99];