Skip to main content

header/
header.rs

1//! This example shows how to add a new header to the request
2//!
3//! # Example
4//! This plugin demonstrates how to use the http-wasm-guest API to
5//! add a custom header (`X-Bar: bar`) to incoming HTTP requests.
6//! The plugin implements the `Guest` trait and registers itself
7//! in the `main` function.
8
9use http_wasm_guest::{
10    Guest,
11    host::{Request, Response, log},
12    register,
13};
14
15/// A simple plugin that adds a custom header to each request.
16struct Plugin {}
17
18impl Guest for Plugin {
19    /// Handles incoming requests by adding the `X-Bar: bar` header.
20    ///
21    /// # Arguments
22    /// * `request` - The incoming HTTP request.
23    /// * `_response` - The HTTP response (unused in this example).
24    ///
25    /// # Returns
26    /// Returns a tuple `(true, 0)` to indicate the request should continue.
27    fn handle_request(&self, request: &Request, _response: &Response) -> (bool, i32) {
28        request.header.add(b"X-Custom-Header", b"FooBar");
29        (true, 0)
30    }
31}
32
33/// Registers the plugin with the http-wasm runtime.
34fn main() {
35    let plugin = Plugin {};
36    log::write(0, b"Registering plugin to add custom header");
37    register(plugin);
38}