-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(wasm): add wasm32-wasip2 example
Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com> example cleanup extra blocking feature Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com> use body example Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>
- Loading branch information
1 parent
2cad7e9
commit 1379d90
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
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,2 @@ | ||
[build] | ||
target = "wasm32-wasip2" |
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,25 @@ | ||
[package] | ||
name = "http-reqwest" | ||
edition = "2021" | ||
version = "0.1.0" | ||
|
||
[workspace] | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
futures = "0.3.30" | ||
reqwest = { version = "0.12.5", path = "../../", features = ["stream"] } | ||
wasi = "=0.13.3" # For compatibility, pin to wasi@0.2.2 bindings | ||
|
||
# https://github.com/servo/rust-url/pull/983 | ||
[patch.crates-io] | ||
url = { git = "https://github.com/servo/rust-url", rev = "fc447cce1d2c06ef0bec3dbadce56b83e46ca1ff"} | ||
form_urlencoded = { git = "https://github.com/servo/rust-url", rev = "fc447cce1d2c06ef0bec3dbadce56b83e46ca1ff"} | ||
|
||
[profile.release] | ||
# Optimize for small code size | ||
lto = true | ||
opt-level = "s" | ||
strip = true |
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,34 @@ | ||
# HTTP Reqwest | ||
|
||
This is a simple Rust Wasm example that sends an outgoing http request using the `reqwest` library to [https://hyper.rs](https://hyper.rs). | ||
|
||
## Prerequisites | ||
|
||
- `cargo` 1.82+ | ||
- `rustup target add wasm32-wasip2` | ||
- [wasmtime 23.0.0+](https://github.com/bytecodealliance/wasmtime) | ||
|
||
## Building | ||
|
||
```bash | ||
# Build Wasm component | ||
cargo build --target wasm32-wasip2 | ||
``` | ||
|
||
## Running with wasmtime | ||
|
||
```bash | ||
wasmtime serve -Scommon ./target/wasm32-wasip2/debug/http_reqwest.wasm | ||
``` | ||
|
||
Then send a request to `localhost:8080` | ||
|
||
```bash | ||
> curl localhost:8080 | ||
|
||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Example Domain</title> | ||
.... | ||
``` |
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,34 @@ | ||
use wasi::http::types::{ | ||
Fields, IncomingBody, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam, | ||
}; | ||
|
||
#[allow(unused)] | ||
struct ReqwestComponent; | ||
|
||
impl wasi::exports::http::incoming_handler::Guest for ReqwestComponent { | ||
fn handle(_request: IncomingRequest, response_out: ResponseOutparam) { | ||
let response = OutgoingResponse::new(Fields::new()); | ||
response.set_status_code(200).unwrap(); | ||
let response_body = response | ||
.body() | ||
.expect("should be able to get response body"); | ||
ResponseOutparam::set(response_out, Ok(response)); | ||
|
||
let mut response = reqwest::get("https://hyper.rs").expect("should get response bytes"); | ||
let (mut body_stream, incoming_body) = response | ||
.bytes_stream() | ||
.expect("should be able to get response body stream"); | ||
std::io::copy( | ||
&mut body_stream, | ||
&mut response_body | ||
.write() | ||
.expect("should be able to write to response body"), | ||
) | ||
.expect("should be able to stream input to output"); | ||
drop(body_stream); | ||
IncomingBody::finish(incoming_body); | ||
OutgoingBody::finish(response_body, None).expect("failed to finish response body"); | ||
} | ||
} | ||
|
||
wasi::http::proxy::export!(ReqwestComponent); |