Skip to content

Commit b617075

Browse files
committed
wasi-http: Add default-send-request feature flag to disable default send handler
1 parent 88079b4 commit b617075

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ jobs:
394394
-p wasmtime-c-api --no-default-features
395395
-p wasmtime-c-api --no-default-features --features wat
396396
-p wasmtime-c-api --no-default-features --features wasi
397+
398+
- name: wasmtime-wasi-http
399+
checks: |
400+
-p wasmtime-wasi-http --no-default-features
397401
runs-on: ubuntu-latest
398402
steps:
399403
- uses: actions/checkout@v4

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 = ["dep:tokio-rustls", "dep:rustls", "dep: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: 19 additions & 5 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 {
@@ -112,6 +115,7 @@ pub trait WasiHttpView: IoView {
112115
}
113116

114117
/// Send an outgoing request.
118+
#[cfg(feature = "default-send-request")]
115119
fn send_request(
116120
&mut self,
117121
request: hyper::Request<HyperOutgoingBody>,
@@ -120,6 +124,14 @@ pub trait WasiHttpView: IoView {
120124
Ok(default_send_request(request, config))
121125
}
122126

127+
/// Send an outgoing request.
128+
#[cfg(not(feature = "default-send-request"))]
129+
fn send_request(
130+
&mut self,
131+
request: hyper::Request<HyperOutgoingBody>,
132+
config: OutgoingRequestConfig,
133+
) -> crate::HttpResult<HostFutureIncomingResponse>;
134+
123135
/// Whether a given header should be considered forbidden and not allowed.
124136
fn is_forbidden_header(&mut self, name: &HeaderName) -> bool {
125137
DEFAULT_FORBIDDEN_HEADERS.contains(name)
@@ -317,6 +329,7 @@ pub struct OutgoingRequestConfig {
317329
///
318330
/// This implementation is used by the `wasi:http/outgoing-handler` interface
319331
/// default implementation.
332+
#[cfg(feature = "default-send-request")]
320333
pub fn default_send_request(
321334
request: hyper::Request<HyperOutgoingBody>,
322335
config: OutgoingRequestConfig,
@@ -331,6 +344,7 @@ pub fn default_send_request(
331344
/// in a task.
332345
///
333346
/// This is called from [default_send_request] to actually send the request.
347+
#[cfg(feature = "default-send-request")]
334348
pub async fn default_send_request_handler(
335349
mut request: hyper::Request<HyperOutgoingBody>,
336350
OutgoingRequestConfig {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ 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+
unimplemented!(
77+
"send_request is not implemented; either enable the `default-send-request` feature or provide a custom implementation"
78+
)
79+
}
7180
}
7281
}
7382

0 commit comments

Comments
 (0)