-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wasi-http: support inbound requests (proxy world) #7091
Merged
Merged
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
65fc5c7
Move the incoming_handler impl into http_impl
elliottt 1b817ea
Remove the incoming handler -- we need to use it as a guest export
elliottt df83b75
Start adding a test-programs test for the server side of wasi-http
elliottt 4d36120
Progress towards running a server test
elliottt 54c2a5c
Implement incoming-request-method
elliottt 1697b77
Validate outparam value
elliottt 25432ea
Initial incoming handler test
elliottt d021210
Implement more of the incoming api
elliottt b194929
Finish the incoming api implementations
elliottt 894fdca
Initial cut at `wasmtime serve`
elliottt 0b4a07c
fix warning
f5f20f7
wasmtime-cli: invoke ServeCommand, and add enough stuff to the linker…
06e120a
fix warnings
3f1c7d7
fix warnings
61d9093
argument parsing: allow --addr to specify sockaddr
622875b
rustfmt
0880e04
sync wit definitions between wasmtime-wasi and wasmtime-wasi-http
3585d2d
cargo vet: add an import config and wildcard audit for wasmtime-wmemc…
e9f77e1
cargo vet: audit signal-hook-registry
a6115d8
Remove duplicate add_to_linker calls for preview2 interfaces
elliottt 19a574b
Add a method to finish outgoing responses
elliottt 2a2f33d
Mark the result of the incoming_{request,response}_consume methods as…
elliottt 698f0ff
Explicit versions for http-body and http-body-util
elliottt 147713f
Explicit `serve` feature for the `wasmtime serve` command
elliottt cc24768
Move the spawn outside of the future returned by `ProxyHandler::call`
elliottt 2c17fe7
Review feedback
elliottt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#![cfg(all(feature = "test_programs", not(skip_wasi_http_tests)))] | ||
use anyhow::Context; | ||
use wasmtime::{ | ||
component::{Component, Linker}, | ||
Config, Engine, Store, | ||
}; | ||
use wasmtime_wasi::preview2::{ | ||
self, pipe::MemoryOutputPipe, IsATTY, Table, WasiCtx, WasiCtxBuilder, WasiView, | ||
}; | ||
use wasmtime_wasi_http::{proxy::Proxy, WasiHttpCtx, WasiHttpView}; | ||
|
||
lazy_static::lazy_static! { | ||
static ref ENGINE: Engine = { | ||
let mut config = Config::new(); | ||
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); | ||
config.wasm_component_model(true); | ||
config.async_support(true); | ||
let engine = Engine::new(&config).unwrap(); | ||
engine | ||
}; | ||
} | ||
// uses ENGINE, creates a fn get_module(&str) -> Module | ||
include!(concat!( | ||
env!("OUT_DIR"), | ||
"/wasi_http_proxy_tests_components.rs" | ||
)); | ||
|
||
struct Ctx { | ||
table: Table, | ||
wasi: WasiCtx, | ||
http: WasiHttpCtx, | ||
} | ||
|
||
impl WasiView for Ctx { | ||
fn table(&self) -> &Table { | ||
&self.table | ||
} | ||
fn table_mut(&mut self) -> &mut Table { | ||
&mut self.table | ||
} | ||
fn ctx(&self) -> &WasiCtx { | ||
&self.wasi | ||
} | ||
fn ctx_mut(&mut self) -> &mut WasiCtx { | ||
&mut self.wasi | ||
} | ||
} | ||
|
||
impl WasiHttpView for Ctx { | ||
fn table(&mut self) -> &mut Table { | ||
&mut self.table | ||
} | ||
fn ctx(&mut self) -> &mut WasiHttpCtx { | ||
&mut self.http | ||
} | ||
} | ||
|
||
async fn instantiate(component: Component, ctx: Ctx) -> Result<(Store<Ctx>, Proxy), anyhow::Error> { | ||
let mut linker = Linker::new(&ENGINE); | ||
wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?; | ||
|
||
let mut store = Store::new(&ENGINE, ctx); | ||
|
||
let (proxy, _instance) = Proxy::instantiate_async(&mut store, &component, &linker).await?; | ||
Ok((store, proxy)) | ||
} | ||
|
||
#[test_log::test(tokio::test)] | ||
async fn wasi_http_proxy_tests() -> anyhow::Result<()> { | ||
let stdout = MemoryOutputPipe::new(4096); | ||
let stderr = MemoryOutputPipe::new(4096); | ||
|
||
let mut table = Table::new(); | ||
let component = get_component("wasi_http_proxy_tests"); | ||
|
||
// Create our wasi context. | ||
let mut builder = WasiCtxBuilder::new(); | ||
builder.stdout(stdout.clone(), IsATTY::No); | ||
builder.stderr(stderr.clone(), IsATTY::No); | ||
for (var, val) in test_programs::wasi_tests_environment() { | ||
builder.env(var, val); | ||
} | ||
let wasi = builder.build(&mut table)?; | ||
let http = WasiHttpCtx; | ||
|
||
let ctx = Ctx { table, wasi, http }; | ||
|
||
let (mut store, proxy) = instantiate(component, ctx).await?; | ||
|
||
let req = { | ||
use http_body_util::{BodyExt, Empty}; | ||
|
||
let req = hyper::Request::builder().method(http::Method::GET).body( | ||
Empty::<bytes::Bytes>::new() | ||
.map_err(|e| anyhow::anyhow!(e)) | ||
.boxed(), | ||
)?; | ||
store.data_mut().new_incoming_request(req)? | ||
}; | ||
|
||
let (sender, receiver) = tokio::sync::oneshot::channel(); | ||
let out = store.data_mut().new_response_outparam(sender)?; | ||
|
||
let handle = preview2::spawn(async move { | ||
proxy | ||
.wasi_http_incoming_handler() | ||
.call_handle(&mut store, req, out) | ||
.await?; | ||
|
||
Ok::<_, anyhow::Error>(()) | ||
}); | ||
|
||
let resp = match receiver.await { | ||
Ok(Ok(resp)) => { | ||
use http_body_util::BodyExt; | ||
let (parts, body) = resp.into_parts(); | ||
let collected = BodyExt::collect(body).await?; | ||
Ok(hyper::Response::from_parts(parts, collected)) | ||
} | ||
|
||
Ok(Err(e)) => Err(e), | ||
|
||
// This happens if the wasm never calls `set-response-outparam` | ||
Err(e) => panic!("Failed to receive a response: {e:?}"), | ||
}; | ||
|
||
// Now that the response has been processed, we can wait on the wasm to finish without | ||
// deadlocking. | ||
handle.await.context("Component execution")?; | ||
|
||
let stdout = stdout.contents(); | ||
if !stdout.is_empty() { | ||
println!("[guest] stdout:\n{}\n===", String::from_utf8_lossy(&stdout)); | ||
} | ||
let stderr = stderr.contents(); | ||
if !stderr.is_empty() { | ||
println!("[guest] stderr:\n{}\n===", String::from_utf8_lossy(&stderr)); | ||
} | ||
|
||
match resp { | ||
Ok(resp) => println!("response: {resp:?}"), | ||
Err(e) => panic!("Error given in response: {e:?}"), | ||
}; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "wasi-http-proxy-tests" | ||
version = "0.0.0" | ||
readme = "README.md" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
crate-type=["cdylib"] | ||
|
||
[dependencies] | ||
wit-bindgen = { workspace = true, features = ["macros", "realloc"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
pub mod bindings { | ||
use super::T; | ||
|
||
wit_bindgen::generate!({ | ||
path: "../../wasi-http/wit", | ||
world: "wasi:http/proxy", | ||
exports: { | ||
"wasi:http/incoming-handler": T, | ||
}, | ||
}); | ||
} | ||
|
||
use bindings::wasi::http::types::{IncomingRequest, ResponseOutparam}; | ||
|
||
struct T; | ||
|
||
impl bindings::exports::wasi::http::incoming_handler::Guest for T { | ||
fn handle(_request: IncomingRequest, outparam: ResponseOutparam) { | ||
let hdrs = bindings::wasi::http::types::new_fields(&[]); | ||
let resp = bindings::wasi::http::types::new_outgoing_response(200, hdrs); | ||
let body = | ||
bindings::wasi::http::types::outgoing_response_write(resp).expect("outgoing response"); | ||
|
||
bindings::wasi::http::types::set_response_outparam(outparam, Ok(resp)); | ||
|
||
let out = bindings::wasi::http::types::outgoing_body_write(body).expect("outgoing stream"); | ||
bindings::wasi::io::streams::blocking_write_and_flush(out, b"hello, world!") | ||
.expect("writing response"); | ||
|
||
bindings::wasi::io::streams::drop_output_stream(out); | ||
bindings::wasi::http::types::outgoing_body_finish(body, None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're at it mind putting
=
constraints on these as well? (works around issues with Cargo's handling of pre-releases)