Skip to content

Commit

Permalink
chore(wasm): add wasm32-wasip2 example
Browse files Browse the repository at this point in the history
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
brooksmtownsend committed Oct 21, 2024
1 parent 2cad7e9 commit 1379d90
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/wasm_component/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-wasip2"
25 changes: 25 additions & 0 deletions examples/wasm_component/Cargo.toml
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
34 changes: 34 additions & 0 deletions examples/wasm_component/README.md
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>
....
```
34 changes: 34 additions & 0 deletions examples/wasm_component/src/lib.rs
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);

0 comments on commit 1379d90

Please sign in to comment.