Skip to content

Commit 5305999

Browse files
feat(p3-http): implement consume-body changes (#11653)
* feat(p3-http): implement `consume-body` changes Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net> * Update WITs to the latest snapshot * Use the 2025-09-16 tag instead of 2025-08-15. * Pulls in `wasi:http` updates "officially". * Pulls in minor `wasi:cli` updates, and that's implemented here as well. --------- Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net> Co-authored-by: Alex Crichton <alex@alexcrichton.com>
1 parent 3cc0023 commit 5305999

Some content is hidden

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

64 files changed

+749
-596
lines changed

ci/vendor-wit.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ make_vendor "wasi-config" "config@f4d699b"
7070
make_vendor "wasi-keyvalue" "keyvalue@219ea36"
7171

7272
make_vendor "wasi/src/p3" "
73-
cli@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
74-
clocks@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
75-
filesystem@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
76-
random@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
77-
sockets@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
73+
cli@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
74+
clocks@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
75+
filesystem@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
76+
random@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
77+
sockets@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
7878
"
7979

8080
make_vendor "wasi-http/src/p3" "
81-
cli@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
82-
clocks@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
83-
filesystem@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
84-
http@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
85-
random@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
86-
sockets@v0.3.0-rc-2025-08-15@wit-0.3.0-draft
81+
cli@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
82+
clocks@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
83+
filesystem@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
84+
http@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
85+
random@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
86+
sockets@v0.3.0-rc-2025-09-16@wit-0.3.0-draft
8787
"
8888

8989
rm -rf $cache_dir

crates/test-programs/src/bin/cli_p3_hello_stdout.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ export!(Component);
77
impl exports::wasi::cli::run::Guest for Component {
88
async fn run() -> Result<(), ()> {
99
let (mut tx, rx) = wit_stream::new();
10-
wasi::cli::stdout::set_stdout(rx);
11-
tx.write(b"hello, world\n".to_vec()).await;
10+
futures::join!(
11+
async {
12+
wasi::cli::stdout::write_via_stream(rx).await.unwrap();
13+
},
14+
async {
15+
tx.write(b"hello, world\n".to_vec()).await;
16+
drop(tx);
17+
},
18+
);
1219
Ok(())
1320
}
1421
}

crates/test-programs/src/bin/cli_p3_much_stdout.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
1212

1313
let bytes = string_to_write.as_bytes();
1414
let (mut tx, rx) = wit_stream::new();
15-
wasi::cli::stdout::set_stdout(rx);
16-
for _ in 0..times_to_write {
17-
let result = tx.write_all(bytes.to_vec()).await;
18-
assert!(result.is_empty());
19-
}
15+
futures::join!(
16+
async { wasi::cli::stdout::write_via_stream(rx).await.unwrap() },
17+
async {
18+
for _ in 0..times_to_write {
19+
let result = tx.write_all(bytes.to_vec()).await;
20+
assert!(result.is_empty());
21+
}
22+
drop(tx);
23+
}
24+
);
2025
Ok(())
2126
}
2227
}

crates/test-programs/src/bin/p3_cli.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,37 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
1818
assert!(terminal_stdout::get_terminal_stdout().is_none());
1919
assert!(terminal_stderr::get_terminal_stderr().is_none());
2020

21-
let mut stdin = stdin::get_stdin();
21+
let (mut stdin, result) = stdin::read_via_stream();
2222
assert!(stdin.next().await.is_none());
2323

2424
let (mut stdout_tx, stdout_rx) = wit_stream::new();
25-
stdout::set_stdout(stdout_rx);
26-
let (res, buf) = stdout_tx.write(b"hello stdout\n".into()).await;
27-
assert_eq!(res, StreamResult::Complete(13));
28-
assert_eq!(buf.into_vec(), []);
25+
futures::join!(
26+
async {
27+
stdout::write_via_stream(stdout_rx).await.unwrap();
28+
},
29+
async {
30+
let (res, buf) = stdout_tx.write(b"hello stdout\n".into()).await;
31+
assert_eq!(res, StreamResult::Complete(13));
32+
assert_eq!(buf.into_vec(), []);
33+
drop(stdout_tx);
34+
}
35+
);
2936

3037
let (mut stderr_tx, stderr_rx) = wit_stream::new();
31-
stderr::set_stderr(stderr_rx);
32-
let (res, buf) = stderr_tx.write(b"hello stderr\n".into()).await;
33-
assert_eq!(res, StreamResult::Complete(13));
34-
assert_eq!(buf.into_vec(), []);
38+
futures::join!(
39+
async {
40+
stderr::write_via_stream(stderr_rx).await.unwrap();
41+
},
42+
async {
43+
let (res, buf) = stderr_tx.write(b"hello stderr\n".into()).await;
44+
assert_eq!(res, StreamResult::Complete(13));
45+
assert_eq!(buf.into_vec(), []);
46+
drop(stderr_tx);
47+
}
48+
);
49+
50+
drop(stdin);
51+
result.await.unwrap();
3552

3653
Ok(())
3754
}

crates/test-programs/src/bin/p3_http_echo.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ impl Handler for Component {
1515
/// Return a response which echoes the request headers, body, and trailers.
1616
async fn handle(request: Request) -> Result<Response, ErrorCode> {
1717
let headers = request.get_headers();
18-
let (body, trailers) = request.consume_body().unwrap();
19-
20-
// let (headers, body) = Request::into_parts(request);
18+
let (_, result_rx) = wit_future::new(|| Ok(()));
19+
let (body, trailers) = Request::consume_body(request, result_rx);
2120

2221
let (response, _result) = if false {
2322
// This is the easy and efficient way to do it...
@@ -47,7 +46,6 @@ impl Handler for Component {
4746
drop(pipe_tx);
4847

4948
trailers_tx.write(trailers.await).await.unwrap();
50-
drop(request);
5149
});
5250

5351
Response::new(headers, Some(pipe_rx), trailers_rx)

crates/test-programs/src/bin/p3_http_middleware.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ impl Handler for Component {
4848
}
4949
_ => true,
5050
});
51-
let (mut body, trailers) = request.consume_body().unwrap();
51+
let (_, result_rx) = wit_future::new(|| Ok(()));
52+
let (mut body, trailers) = Request::consume_body(request, result_rx);
5253

5354
let (body, trailers) = if content_deflated {
5455
// Next, spawn a task to pipe and decode the original request body and trailers into a new request
@@ -77,8 +78,6 @@ impl Handler for Component {
7778
}
7879

7980
trailers_tx.write(trailers.await).await.unwrap();
80-
81-
drop(request);
8281
});
8382

8483
(pipe_rx, trailers_rx)
@@ -110,7 +109,8 @@ impl Handler for Component {
110109
headers.push(("content-encoding".into(), b"deflate".into()));
111110
}
112111

113-
let (mut body, trailers) = response.consume_body().unwrap();
112+
let (_, result_rx) = wit_future::new(|| Ok(()));
113+
let (mut body, trailers) = Response::consume_body(response, result_rx);
114114
let (body, trailers) = if accept_deflated {
115115
headers.retain(|(name, _value)| name != "content-length");
116116

@@ -141,7 +141,6 @@ impl Handler for Component {
141141
}
142142

143143
trailers_tx.write(trailers.await).await.unwrap();
144-
drop(response);
145144
});
146145

147146
(pipe_rx, trailers_rx)

crates/test-programs/src/bin/p3_http_middleware_with_chain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ mod bindings {
66
package local:local;
77
88
world middleware-with-chain {
9-
include wasi:http/proxy@0.3.0-rc-2025-08-15;
9+
include wasi:http/proxy@0.3.0-rc-2025-09-16;
1010
1111
import chain-http;
1212
}
1313
1414
interface chain-http {
15-
use wasi:http/types@0.3.0-rc-2025-08-15.{request, response, error-code};
15+
use wasi:http/types@0.3.0-rc-2025-09-16.{request, response, error-code};
1616
1717
handle: async func(request: request) -> result<response, error-code>;
1818
}

crates/test-programs/src/p3/http.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ pub async fn request(
9494
let response = handler::handle(request).await?;
9595
let status = response.get_status_code();
9696
let headers = response.get_headers().copy_all();
97-
let (body_rx, trailers_rx) = response
98-
.consume_body()
99-
.expect("failed to get response body");
97+
let (_, result_rx) = wit_future::new(|| Ok(()));
98+
let (body_rx, trailers_rx) = types::Response::consume_body(response, result_rx);
10099
let ((), rx) = join!(
101100
async {
102101
if let Some(buf) = body {

crates/test-programs/src/p3/mod.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ wit_bindgen::generate!({
66
package wasmtime:test;
77
88
world testp3 {
9-
include wasi:cli/imports@0.3.0-rc-2025-08-15;
10-
include wasi:http/imports@0.3.0-rc-2025-08-15;
9+
include wasi:cli/imports@0.3.0-rc-2025-09-16;
10+
include wasi:http/imports@0.3.0-rc-2025-09-16;
1111
12-
export wasi:cli/run@0.3.0-rc-2025-08-15;
12+
export wasi:cli/run@0.3.0-rc-2025-09-16;
1313
}
1414
",
1515
path: "../wasi-http/src/p3/wit",
1616
world: "wasmtime:test/testp3",
1717
default_bindings_module: "test_programs::p3",
1818
pub_export_macro: true,
19-
async: [
20-
"wasi:cli/run@0.3.0-rc-2025-08-15#run",
21-
],
2219
generate_all,
2320
});
2421

@@ -28,22 +25,24 @@ pub mod proxy {
2825
package wasmtime:test;
2926
3027
world proxyp3 {
31-
include wasi:http/proxy@0.3.0-rc-2025-08-15;
28+
include wasi:http/proxy@0.3.0-rc-2025-09-16;
3229
}
3330
",
3431
path: "../wasi-http/src/p3/wit",
3532
world: "wasmtime:test/proxyp3",
3633
default_bindings_module: "test_programs::p3::proxy",
3734
pub_export_macro: true,
3835
with: {
39-
"wasi:http/handler@0.3.0-rc-2025-08-15": generate,
40-
"wasi:http/types@0.3.0-rc-2025-08-15": crate::p3::wasi::http::types,
41-
"wasi:random/random@0.3.0-rc-2025-08-15": crate::p3::wasi::random::random,
42-
"wasi:cli/stdout@0.3.0-rc-2025-08-15": crate::p3::wasi::cli::stdout,
43-
"wasi:cli/stderr@0.3.0-rc-2025-08-15": crate::p3::wasi::cli::stderr,
44-
"wasi:cli/stdin@0.3.0-rc-2025-08-15": crate::p3::wasi::cli::stdin,
45-
"wasi:clocks/monotonic-clock@0.3.0-rc-2025-08-15": crate::p3::wasi::clocks::monotonic_clock,
46-
"wasi:clocks/wall-clock@0.3.0-rc-2025-08-15": crate::p3::wasi::clocks::wall_clock,
36+
"wasi:http/handler@0.3.0-rc-2025-09-16": generate,
37+
"wasi:http/types@0.3.0-rc-2025-09-16": crate::p3::wasi::http::types,
38+
"wasi:random/random@0.3.0-rc-2025-09-16": crate::p3::wasi::random::random,
39+
"wasi:cli/stdout@0.3.0-rc-2025-09-16": crate::p3::wasi::cli::stdout,
40+
"wasi:cli/stderr@0.3.0-rc-2025-09-16": crate::p3::wasi::cli::stderr,
41+
"wasi:cli/stdin@0.3.0-rc-2025-09-16": crate::p3::wasi::cli::stdin,
42+
"wasi:cli/types@0.3.0-rc-2025-09-16": crate::p3::wasi::cli::types,
43+
"wasi:clocks/monotonic-clock@0.3.0-rc-2025-09-16": crate::p3::wasi::clocks::monotonic_clock,
44+
"wasi:clocks/wall-clock@0.3.0-rc-2025-09-16": crate::p3::wasi::clocks::wall_clock,
45+
"wasi:clocks/types@0.3.0-rc-2025-09-16": crate::p3::wasi::clocks::types,
4746
},
4847
});
4948
}

crates/wasi-http/src/p3/bindings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ mod generated {
99
"wasi:http/handler/[async]handle": async | store | trappable | tracing,
1010
"wasi:http/types/[drop]request": store | trappable | tracing,
1111
"wasi:http/types/[drop]response": store | trappable | tracing,
12-
"wasi:http/types/[method]request.consume-body": async | store | trappable | tracing,
13-
"wasi:http/types/[method]response.consume-body": async | store | trappable | tracing,
12+
"wasi:http/types/[static]request.consume-body": async | store | trappable | tracing,
1413
"wasi:http/types/[static]request.new": async | store | trappable | tracing,
14+
"wasi:http/types/[static]response.consume-body": async | store | trappable | tracing,
1515
"wasi:http/types/[static]response.new": async | store | trappable | tracing,
1616
default: trappable | tracing,
1717
},

0 commit comments

Comments
 (0)