Skip to content

Commit 1379d90

Browse files
chore(wasm): add wasm32-wasip2 example
Signed-off-by: Brooks Townsend <[email protected]> example cleanup extra blocking feature Signed-off-by: Brooks Townsend <[email protected]> use body example Signed-off-by: Brooks Townsend <[email protected]>
1 parent 2cad7e9 commit 1379d90

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "wasm32-wasip2"

examples/wasm_component/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "http-reqwest"
3+
edition = "2021"
4+
version = "0.1.0"
5+
6+
[workspace]
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
futures = "0.3.30"
13+
reqwest = { version = "0.12.5", path = "../../", features = ["stream"] }
14+
wasi = "=0.13.3" # For compatibility, pin to [email protected] bindings
15+
16+
# https://github.com/servo/rust-url/pull/983
17+
[patch.crates-io]
18+
url = { git = "https://github.com/servo/rust-url", rev = "fc447cce1d2c06ef0bec3dbadce56b83e46ca1ff"}
19+
form_urlencoded = { git = "https://github.com/servo/rust-url", rev = "fc447cce1d2c06ef0bec3dbadce56b83e46ca1ff"}
20+
21+
[profile.release]
22+
# Optimize for small code size
23+
lto = true
24+
opt-level = "s"
25+
strip = true

examples/wasm_component/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# HTTP Reqwest
2+
3+
This is a simple Rust Wasm example that sends an outgoing http request using the `reqwest` library to [https://hyper.rs](https://hyper.rs).
4+
5+
## Prerequisites
6+
7+
- `cargo` 1.82+
8+
- `rustup target add wasm32-wasip2`
9+
- [wasmtime 23.0.0+](https://github.com/bytecodealliance/wasmtime)
10+
11+
## Building
12+
13+
```bash
14+
# Build Wasm component
15+
cargo build --target wasm32-wasip2
16+
```
17+
18+
## Running with wasmtime
19+
20+
```bash
21+
wasmtime serve -Scommon ./target/wasm32-wasip2/debug/http_reqwest.wasm
22+
```
23+
24+
Then send a request to `localhost:8080`
25+
26+
```bash
27+
> curl localhost:8080
28+
29+
<!doctype html>
30+
<html>
31+
<head>
32+
<title>Example Domain</title>
33+
....
34+
```

examples/wasm_component/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use wasi::http::types::{
2+
Fields, IncomingBody, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
3+
};
4+
5+
#[allow(unused)]
6+
struct ReqwestComponent;
7+
8+
impl wasi::exports::http::incoming_handler::Guest for ReqwestComponent {
9+
fn handle(_request: IncomingRequest, response_out: ResponseOutparam) {
10+
let response = OutgoingResponse::new(Fields::new());
11+
response.set_status_code(200).unwrap();
12+
let response_body = response
13+
.body()
14+
.expect("should be able to get response body");
15+
ResponseOutparam::set(response_out, Ok(response));
16+
17+
let mut response = reqwest::get("https://hyper.rs").expect("should get response bytes");
18+
let (mut body_stream, incoming_body) = response
19+
.bytes_stream()
20+
.expect("should be able to get response body stream");
21+
std::io::copy(
22+
&mut body_stream,
23+
&mut response_body
24+
.write()
25+
.expect("should be able to write to response body"),
26+
)
27+
.expect("should be able to stream input to output");
28+
drop(body_stream);
29+
IncomingBody::finish(incoming_body);
30+
OutgoingBody::finish(response_body, None).expect("failed to finish response body");
31+
}
32+
}
33+
34+
wasi::http::proxy::export!(ReqwestComponent);

0 commit comments

Comments
 (0)