Skip to content

Commit 5e23189

Browse files
committed
wasi-http: Add default-send-request feature flag to disable default handler and TLS deps
1 parent 88079b4 commit 5e23189

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

crates/wasi-http/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ description = "Experimental HTTP library for WebAssembly in Wasmtime"
1111
[lints]
1212
workspace = true
1313

14+
[features]
15+
default = ["default-send-request"]
16+
default-send-request = ["tokio-rustls", "rustls", "webpki-roots"]
17+
1418
[dependencies]
1519
anyhow = { workspace = true }
1620
async-trait = { workspace = true }
@@ -29,9 +33,9 @@ tracing = { workspace = true }
2933
wasmtime-wasi = { workspace = true }
3034
wasmtime-wasi-io = { workspace = true }
3135
wasmtime = { workspace = true, features = ['component-model'] }
32-
tokio-rustls = { workspace = true }
33-
rustls = { workspace = true }
34-
webpki-roots = { workspace = true }
36+
tokio-rustls = { workspace = true, optional = true }
37+
rustls = { workspace = true, optional = true }
38+
webpki-roots = { workspace = true, optional = true }
3539

3640
[dev-dependencies]
3741
test-programs-artifacts = { workspace = true }

crates/wasi-http/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl fmt::Display for HttpError {
5858

5959
impl Error for HttpError {}
6060

61+
#[cfg(feature = "default-send-request")]
6162
pub(crate) fn dns_error(rcode: String, info_code: u16) -> ErrorCode {
6263
ErrorCode::DnsError(crate::bindings::http::types::DnsErrorPayload {
6364
rcode: Some(rcode),

crates/wasi-http/src/types.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
//! Implements the base structure (i.e. [WasiHttpCtx]) that will provide the
22
//! implementation of the wasi-http API.
33
4-
use crate::io::TokioIo;
54
use crate::{
65
bindings::http::types::{self, Method, Scheme},
76
body::{HostIncomingBody, HyperIncomingBody, HyperOutgoingBody},
8-
error::dns_error,
9-
hyper_request_error,
107
};
118
use anyhow::bail;
129
use bytes::Bytes;
@@ -15,12 +12,18 @@ use hyper::body::Body;
1512
use hyper::header::HeaderName;
1613
use std::any::Any;
1714
use std::time::Duration;
18-
use tokio::net::TcpStream;
19-
use tokio::time::timeout;
2015
use wasmtime::component::{Resource, ResourceTable};
2116
use wasmtime_wasi::p2::{IoImpl, IoView, Pollable};
2217
use wasmtime_wasi::runtime::AbortOnDropJoinHandle;
2318

19+
#[cfg(feature = "default-send-request")]
20+
use {
21+
crate::io::TokioIo,
22+
crate::{error::dns_error, hyper_request_error},
23+
tokio::net::TcpStream,
24+
tokio::time::timeout,
25+
};
26+
2427
/// Capture the state necessary for use in the wasi-http API implementation.
2528
#[derive(Debug)]
2629
pub struct WasiHttpCtx {
@@ -117,7 +120,18 @@ pub trait WasiHttpView: IoView {
117120
request: hyper::Request<HyperOutgoingBody>,
118121
config: OutgoingRequestConfig,
119122
) -> crate::HttpResult<HostFutureIncomingResponse> {
120-
Ok(default_send_request(request, config))
123+
#[cfg(feature = "default-send-request")]
124+
{
125+
Ok(default_send_request(request, config))
126+
}
127+
#[cfg(not(feature = "default-send-request"))]
128+
{
129+
let _ = (request, config);
130+
Err(crate::bindings::http::types::ErrorCode::InternalError(Some(
131+
"default-send-request feature disabled".to_string(),
132+
))
133+
.into())
134+
}
121135
}
122136

123137
/// Whether a given header should be considered forbidden and not allowed.
@@ -317,6 +331,7 @@ pub struct OutgoingRequestConfig {
317331
///
318332
/// This implementation is used by the `wasi:http/outgoing-handler` interface
319333
/// default implementation.
334+
#[cfg(feature = "default-send-request")]
320335
pub fn default_send_request(
321336
request: hyper::Request<HyperOutgoingBody>,
322337
config: OutgoingRequestConfig,
@@ -331,6 +346,7 @@ pub fn default_send_request(
331346
/// in a task.
332347
///
333348
/// This is called from [default_send_request] to actually send the request.
349+
#[cfg(feature = "default-send-request")]
334350
pub async fn default_send_request_handler(
335351
mut request: hyper::Request<HyperOutgoingBody>,
336352
OutgoingRequestConfig {

crates/wasi-http/tests/all/p2.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ impl WasiHttpView for Ctx {
6767
if let Some(send_request) = self.send_request.clone() {
6868
Ok(send_request(request, config))
6969
} else {
70-
Ok(types::default_send_request(request, config))
70+
#[cfg(feature = "default-send-request")]
71+
{
72+
Ok(types::default_send_request(request, config))
73+
}
74+
#[cfg(not(feature = "default-send-request"))]
75+
{
76+
Err(ErrorCode::InternalError(Some(
77+
"default-send-request feature disabled".to_string(),
78+
))
79+
.into())
80+
}
7181
}
7282
}
7383

0 commit comments

Comments
 (0)