Skip to content

Commit

Permalink
fix(wasmtime-cli): wrong linker in wasi http (#7167) (#7175)
Browse files Browse the repository at this point in the history
* chore: convert wasi http cli test to component

* fix(wasmtime-cli): wrong linker in wasi http

* fix(wasmtime-cli): fix multiple functions added to linker

We will allow shadowing in linker when using
WASI HTTP in order to prevent the following
error:
> import of `wasi:clocks/wall-clock` defined
twice

* chore: update to latest io streams wit definition

* chore: rename main function of core module

prtest:full

* fix(wasmtime-cli): add only http functions to linker

* chore: add test building http request and response

* chore: disable temporarily wasi http test

Co-authored-by: Eduardo de Moura Rodrigues <16357187+eduardomourar@users.noreply.github.com>
  • Loading branch information
alexcrichton and eduardomourar authored Oct 6, 2023
1 parent 1750566 commit a14004b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 103 deletions.
6 changes: 6 additions & 0 deletions crates/test-programs/tests/wasi-http-components-sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,9 @@ fn outbound_request_invalid_dnsname() -> Result<()> {
let server = Server::http1()?;
run("outbound_request_invalid_dnsname", &server)
}

#[test_log::test]
fn outbound_request_response_build() -> Result<()> {
let server = Server::http1()?;
run("outbound_request_response_build", &server)
}
6 changes: 6 additions & 0 deletions crates/test-programs/tests/wasi-http-components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,9 @@ async fn outbound_request_invalid_dnsname() -> Result<()> {
let server = Server::http1()?;
run("outbound_request_invalid_dnsname", &server).await
}

#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn outbound_request_response_build() -> Result<()> {
let server = Server::http1()?;
run("outbound_request_response_build", &server).await
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use wasi_http_tests::bindings::wasi::{cli::stdout::get_stdout, http::types as http_types};

fn print(msg: &[u8]) {
let _ = get_stdout().blocking_write_and_flush(&msg);
}

fn main() {
print("Called _start\n".as_bytes());
{
let headers = http_types::Headers::new(&[(
"Content-Type".to_string(),
"application/json".to_string().into_bytes(),
)]);
let request = http_types::OutgoingRequest::new(
&http_types::Method::Get,
None,
Some(&http_types::Scheme::Https),
Some("www.example.com"),
&headers,
);
let outgoing_body = request.write().unwrap();
let request_body = outgoing_body.write().unwrap();
request_body
.blocking_write_and_flush("request-body".as_bytes())
.unwrap();
}
{
let headers = http_types::Headers::new(&[(
"Content-Type".to_string(),
"application/text".to_string().into_bytes(),
)]);
let response = http_types::OutgoingResponse::new(200, &headers);
let outgoing_body = response.write().unwrap();
let response_body = outgoing_body.write().unwrap();
response_body
.blocking_write_and_flush("response-body".as_bytes())
.unwrap();
}
print("Done\n".as_bytes());
}
11 changes: 11 additions & 0 deletions crates/wasi-http/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ pub mod sync {
// dependencies.
preview2::command::sync::add_to_linker(l)?;

add_only_http_to_linker(l)?;

Ok(())
}

#[doc(hidden)]
// TODO: This is temporary solution until the preview2 command functions can be removed
pub fn add_only_http_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + preview2::WasiView + bindings::http::types::Host,
{
bindings::http::outgoing_handler::add_to_linker(l, |t| t)?;
bindings::http::types::add_to_linker(l, |t| t)?;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl RunCommand {
bail!("Cannot enable wasi-http for core wasm modules");
}
CliLinker::Component(linker) => {
wasmtime_wasi_http::proxy::add_to_linker(linker)?;
wasmtime_wasi_http::proxy::sync::add_only_http_to_linker(linker)?;
}
}

Expand Down
20 changes: 11 additions & 9 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,23 +790,25 @@ fn run_basic_component() -> Result<()> {
Ok(())
}

#[cfg(feature = "wasi-http")]
#[ignore = "needs to be ported to components"]
#[test]
fn run_wasi_http_module() -> Result<()> {
// #[cfg_attr(not(feature = "wasi-http"), ignore)]
#[ignore = "re-enable when we can reference tests from test-programs"]
fn run_wasi_http_component() -> Result<()> {
let output = run_wasmtime_for_output(
&[
"-Shttp,preview2",
"-Ccache=no",
"tests/all/cli_tests/wasi-http.wat",
"-Wcomponent-model",
"-Scommon,http,preview2",
"tests/all/cli_tests/wasi-http-component.wat",
],
None,
)?;
println!("{}", String::from_utf8_lossy(&output.stderr));
assert!(String::from_utf8(output.stdout)
.unwrap()
.starts_with("Called _start\n"));
assert!(!output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
println!("{}", stdout);
assert!(stdout.starts_with("Called _start\n"));
assert!(stdout.ends_with("Done\n"));
assert!(output.status.success());
Ok(())
}

Expand Down
93 changes: 0 additions & 93 deletions tests/all/cli_tests/wasi-http.wat

This file was deleted.

0 comments on commit a14004b

Please sign in to comment.