Skip to content

Commit 42d3c44

Browse files
authored
Merge pull request #1877 from fermyon/wasi-http-wasmtime-rebase
Add `wasi-http` trigger support based on `wasmtime-wasi-http` take 2
2 parents e9b5049 + 11b3f96 commit 42d3c44

File tree

49 files changed

+3093
-213
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3093
-213
lines changed

Cargo.lock

Lines changed: 89 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ subprocess = "0.2.9"
7878
openssl = { version = "0.10" }
7979

8080
[dev-dependencies]
81-
hyper = { version = "0.14", features = ["full"] }
81+
hex = "0.4.3"
82+
hyper = { workspace = true }
8283
sha2 = "0.10.1"
8384
which = "4.2.5"
8485
e2e-testing = { path = "crates/e2e-testing" }
@@ -114,7 +115,10 @@ wasi-common-preview1 = { git = "https://github.com/bytecodealliance/wasmtime", r
114115
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", rev = "c796ce7376a57a40605f03e74bd78cefcc9acf3a", features = [
115116
"component-model",
116117
] }
118+
wasmtime-wasi-http = { git = "https://github.com/bytecodealliance/wasmtime", rev = "c796ce7376a57a40605f03e74bd78cefcc9acf3a" }
117119
spin-componentize = { git = "https://github.com/fermyon/spin-componentize", rev = "84286054d632ccb8ae06c6419940b5d328229e59" }
120+
hyper = { version = "=1.0.0-rc.3", features = ["full"] }
121+
http-body-util = "=0.1.0-rc.2"
118122

119123
[[bin]]
120124
name = "spin"

build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const RUST_HTTP_INTEGRATION_ENV_TEST: &str = "tests/http/headers-env-routes-test
1212
const RUST_HTTP_VAULT_CONFIG_TEST: &str = "tests/http/vault-config-test";
1313
const RUST_OUTBOUND_REDIS_INTEGRATION_TEST: &str = "tests/outbound-redis/http-rust-outbound-redis";
1414
const TIMER_TRIGGER_INTEGRATION_TEST: &str = "examples/spin-timer/app-example";
15+
const WASI_HTTP_INTEGRATION_TEST: &str = "examples/wasi-http-rust-async";
1516

1617
fn main() {
1718
// Extract environment information to be passed to plugins.
@@ -90,6 +91,7 @@ error: the `wasm32-wasi` target is not installed
9091
cargo_build(RUST_HTTP_VAULT_CONFIG_TEST);
9192
cargo_build(RUST_OUTBOUND_REDIS_INTEGRATION_TEST);
9293
cargo_build(TIMER_TRIGGER_INTEGRATION_TEST);
94+
cargo_build(WASI_HTTP_INTEGRATION_TEST);
9395
}
9496

9597
fn build_wasm_test_program(name: &'static str, root: &'static str) {
@@ -99,6 +101,7 @@ fn build_wasm_test_program(name: &'static str, root: &'static str) {
99101
.build();
100102
println!("cargo:rerun-if-changed={root}/Cargo.toml");
101103
println!("cargo:rerun-if-changed={root}/Cargo.lock");
104+
println!("cargo:rerun-if-changed={root}/src");
102105
}
103106

104107
fn has_wasm32_wasi_target() -> bool {
@@ -129,6 +132,8 @@ fn cargo_build(dir: &str) {
129132
Some(dir),
130133
None,
131134
);
135+
println!("cargo:rerun-if-changed={dir}/Cargo.toml");
136+
println!("cargo:rerun-if-changed={dir}/src");
132137
}
133138

134139
fn run<S: Into<String> + AsRef<std::ffi::OsStr>>(

crates/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crossbeam-channel = "0.5"
1111
tracing = { workspace = true }
1212
wasmtime = { workspace = true }
1313
wasmtime-wasi = { workspace = true }
14+
wasmtime-wasi-http = { workspace = true }
1415
wasi-common-preview1 = { workspace = true }
1516
system-interface = { version = "0.26.0", features = ["cap_std_impls"] }
1617
cap-std = "2.0.0"

crates/core/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crossbeam_channel::Sender;
2020
use tracing::instrument;
2121
use wasmtime::{InstanceAllocationStrategy, PoolingAllocationConfig};
2222
use wasmtime_wasi::preview2::Table;
23+
use wasmtime_wasi_http::types::{WasiHttpCtx, WasiHttpView};
2324

2425
use self::host_component::{HostComponents, HostComponentsBuilder};
2526

@@ -178,18 +179,31 @@ impl<T: Send> wasmtime_wasi::preview2::WasiView for Data<T> {
178179
fn ctx(&self) -> &wasmtime_wasi::preview2::WasiCtx {
179180
match &self.wasi {
180181
Wasi::Preview1(_) => panic!("using WASI Preview 1 functions with Preview 2 store"),
181-
Wasi::Preview2(ctx) => ctx,
182+
Wasi::Preview2 { wasi_ctx, .. } => wasi_ctx,
182183
}
183184
}
184185

185186
fn ctx_mut(&mut self) -> &mut wasmtime_wasi::preview2::WasiCtx {
186187
match &mut self.wasi {
187188
Wasi::Preview1(_) => panic!("using WASI Preview 1 functions with Preview 2 store"),
188-
Wasi::Preview2(ctx) => ctx,
189+
Wasi::Preview2 { wasi_ctx, .. } => wasi_ctx,
189190
}
190191
}
191192
}
192193

194+
impl<T: Send> WasiHttpView for Data<T> {
195+
fn ctx(&mut self) -> &mut WasiHttpCtx {
196+
match &mut self.wasi {
197+
Wasi::Preview1(_) => panic!("using WASI Preview 1 functions with Preview 2 store"),
198+
Wasi::Preview2 { wasi_http_ctx, .. } => wasi_http_ctx,
199+
}
200+
}
201+
202+
fn table(&mut self) -> &mut Table {
203+
&mut self.table
204+
}
205+
}
206+
193207
/// An alias for [`wasmtime::Linker`] specialized to [`Data`].
194208
pub type ModuleLinker<T> = wasmtime::Linker<Data<T>>;
195209

@@ -213,12 +227,12 @@ impl<T: Send + Sync> EngineBuilder<T> {
213227
let engine = wasmtime::Engine::new(&config.inner)?;
214228

215229
let mut linker: Linker<T> = Linker::new(&engine);
216-
wasmtime_wasi::preview2::command::add_to_linker(&mut linker)?;
230+
wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?;
217231

218232
let mut module_linker = ModuleLinker::new(&engine);
219233
wasmtime_wasi::tokio::add_to_linker(&mut module_linker, |data| match &mut data.wasi {
220234
Wasi::Preview1(ctx) => ctx,
221-
Wasi::Preview2(_) => panic!("using WASI Preview 2 functions with Preview 1 store"),
235+
Wasi::Preview2 { .. } => panic!("using WASI Preview 2 functions with Preview 1 store"),
222236
})?;
223237

224238
Ok(Self {

crates/core/src/store.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use system_interface::io::ReadReady;
99
use tokio::io::{AsyncRead, AsyncWrite};
1010
use wasi_common_preview1 as wasi_preview1;
1111
use wasmtime_wasi as wasmtime_wasi_preview1;
12-
use wasmtime_wasi::preview2::{self as wasi_preview2, StdinStream, StdoutStream};
13-
use wasmtime_wasi_preview1::preview2::{HostInputStream, HostOutputStream};
12+
use wasmtime_wasi::preview2::{
13+
self as wasi_preview2, HostInputStream, HostOutputStream, StdinStream, StdoutStream,
14+
};
15+
use wasmtime_wasi_http::types::WasiHttpCtx;
1416

1517
use crate::{
1618
host_component::{HostComponents, HostComponentsData},
@@ -41,7 +43,13 @@ pub enum Wasi {
4143
/// Preview 1 `WasiCtx`
4244
Preview1(wasi_preview1::WasiCtx),
4345
/// Preview 2 `WasiCtx`
44-
Preview2(wasi_preview2::WasiCtx),
46+
Preview2 {
47+
/// `wasi-cli` context
48+
wasi_ctx: wasi_preview2::WasiCtx,
49+
50+
/// `wasi-http` context
51+
wasi_http_ctx: WasiHttpCtx,
52+
},
4553
}
4654

4755
/// The version of Wasi being used
@@ -472,7 +480,10 @@ impl WasiCtxBuilder {
472480
fn build(self) -> Wasi {
473481
match self {
474482
WasiCtxBuilder::Preview1(ctx) => Wasi::Preview1(ctx),
475-
WasiCtxBuilder::Preview2(mut b) => Wasi::Preview2(b.build()),
483+
WasiCtxBuilder::Preview2(mut b) => Wasi::Preview2 {
484+
wasi_ctx: b.build(),
485+
wasi_http_ctx: WasiHttpCtx,
486+
},
476487
}
477488
}
478489
}

0 commit comments

Comments
 (0)