Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ make components
### Components

- [`http-client`](./components/http-client/)
- [`readonly`](./components/readonly/)

## Community

Expand Down
2 changes: 1 addition & 1 deletion components/http-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ A higher-level HTTP client that delegates to wasi:http/client.
## The `http-client` World

- exports `componentized:http/client`
- imports `wasi:http/client@0.3.1`
- imports `wasi:http/client@0.3`
11 changes: 11 additions & 0 deletions components/readonly/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "readonly"
version = "0.1.0"
edition = "2024"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib"]

[dependencies]
wit-bindgen = { workspace = true, features = ["async-spawn"] }
8 changes: 8 additions & 0 deletions components/readonly/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# `readonly`

Restricts http methods to `get`, `head`, `query` and `options`, otherwise returns `http-request-method-invalid` error code.

## The `readonly` World

- exports `wasi:http/client@0.3`
- imports `wasi:http/client@0.3`
49 changes: 49 additions & 0 deletions components/readonly/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_std]

use crate::{
exports::wasi::http::client::{ErrorCode, Guest, Request, Response},
wasi::http::{client, types::Method},
};

struct ReadOnlyClient;

impl ReadOnlyClient {
fn allowed_method(method: Method) -> bool {
let method = match method {
Method::Get => "get",
Method::Head => "head",
Method::Post => "post",
Method::Put => "put",
Method::Delete => "delete",
Method::Connect => "connect",
Method::Options => "options",
Method::Trace => "trace",
Method::Patch => "patch",
Method::Other(method) => &method.to_lowercase(),
};
match method {
"get" | "head" | "query" | "options" => true,
_ => false,
}
}
}

impl Guest for ReadOnlyClient {
#[doc = "/ This function may be used to either send an outgoing request over the"]
#[doc = "/ network or to forward it to another component."]
#[allow(async_fn_in_trait)]
async fn send(request: Request) -> Result<Response, ErrorCode> {
match Self::allowed_method(request.get_method()) {
true => client::send(request).await,
false => Err(ErrorCode::HttpRequestMethodInvalid),
}
}
}

export!(ReadOnlyClient);

wit_bindgen::generate!({
path: "../wit",
world: "client",
generate_all,
});
5 changes: 5 additions & 0 deletions components/wit/worlds.wit
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ world http-client {
import wasi:http/client@0.3.0;
export componentized:http/client@0.1.0-dev;
}

world client {
import wasi:http/client@0.3.0;
export wasi:http/client@0.3.0;
}