http-wasm-guest 0.9.3

A Rust library for implementing HTTP-Wasm guest handlers.
Documentation

http-wasm Guest Library

crate Test codecov

This library provides a Rust implementation for the Wasm Guest ABI and interfaces with http-wasm.

It is designed for writing Traefik plugins in Rust, and works with any http-wasm compatible runtime.

Design Goals

  • Not opinionated, the focus is to provide a very thin wrapper around the host functions.
  • Minimal dependency footprint: only the log crate is required at runtime.
  • Low-level Byte abstraction to enable all use-cases.
  • Memory-efficient data handling to suit constrained Wasm environments.

Credits

Usage

Add the dependency to your project:

cargo add http-wasm-guest

Implement the Guest trait and register the plugin. See the examples for complete code.

use http_wasm_guest::{
    Guest,
    host::{Request, Response},
    register,
};
struct Plugin {}

impl Guest for Plugin {
    fn handle_request(&self, request: &Request, _response: &Response) -> (bool, i32) {
        request.header().add(b"X-Custom-Header", b"FooBar");
        (true, 0)
    }
}

fn main() {
    let plugin = Plugin {};
    register(plugin);
}

Build

Add the WASM target for building the plugin:

rustup target add wasm32-wasip1

Build the library with

cargo build --target wasm32-wasip1 --release

Test

You can run the examples via the provided run.sh script. This creates a running container for the traefik-server with the plugin configured and the whois-service wired into the router.

$ ./run.sh header
[lots of logging output]
$ curl  http://whoami.localhost:8080
Hostname: pensive_curran
IP: 127.0.0.1
IP: ::1
RemoteAddr: [::1]:53364
GET / HTTP/1.1
Host: whoami.localhost:8080
User-Agent: curl/8.18.0
Accept: */*
Accept-Encoding: gzip
X-Custom-Header: FooBar
[more output]