1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//! A Rust library for implementing HTTP WebAssembly guest plugins.
//!
//! This library provides a complete implementation of the [http-wasm Guest ABI](https://http-wasm.io/http-handler-abi/)
//! for building WebAssembly modules that can process HTTP requests and responses in compatible host environments
//! such as [Traefik](https://traefik.io/) and other http-wasm enabled proxies.
//!
//! # Quick Start
//!
//! Create a plugin by implementing the [`Guest`] trait and registering it:
//!
//! ```no_run
//! use http_wasm_guest::{Guest, host::{Request, Response}, register};
//!
//! struct MyPlugin;
//!
//! impl Guest for MyPlugin {
//! fn handle_request(&self, request: Request, _response: Response) -> (bool, i32) {
//! // Add a custom header to all requests
//! request.header().add(b"X-Plugin", b"MyPlugin-v1.0");
//! (true, 0) // Continue to next handler
//! }
//!
//! fn handle_response(&self, _request: Request, response: Response) {
//! // Add security headers to all responses
//! response.header().add(b"X-Content-Type-Options", b"nosniff");
//! }
//! }
//!
//! fn main() {
//! register(MyPlugin);
//! }
//! ```
//!
//! # Building
//!
//! Compile your plugin to WebAssembly using:
//!
//! ```bash
//! cargo build --target wasm32-wasip1 --release
//! ```
//!
//! # Core Concepts
//!
//! ## Guest Trait
//!
//! The [`Guest`] trait is the main interface for your plugin. It provides two methods:
//! - [`Guest::handle_request`] - Called for each incoming request
//! - [`Guest::handle_response`] - Called for each outgoing response
//!
//! ## Request/Response Processing
//!
//! Your plugin can:
//! - Inspect and modify HTTP headers
//! - Read and write request/response bodies
//! - Change request methods and URIs
//! - Set response status codes
//! - Access client information (IP address, etc.)
//!
//! ## Features
//!
//! Enable optional host features for advanced functionality:
//!
//! ```no_run
//! use http_wasm_guest::host::feature::{enable, BufferRequest, BufferResponse};
//! use http_wasm_guest::{Guest, register};
//!
//! struct MyPlugin;
//! impl Guest for MyPlugin {}
//!
//! fn main() {
//! // Enable body buffering for modification
//! enable(BufferRequest | BufferResponse);
//!
//! register(MyPlugin);
//! }
//! ```
//!
//! ## Logging
//!
//! Set up logging to debug your plugin:
//!
//! ```no_run
//! use http_wasm_guest::{Guest, host, register};
//! use log::info;
//!
//! struct MyPlugin;
//! impl Guest for MyPlugin {}
//!
//! fn main() {
//! host::log::init().expect("Failed to initialize logger");
//! info!("Plugin starting up");
//!
//! register(MyPlugin);
//! }
//! ```
//!
//! # Modules
//!
//! - [`host`] - Interface to the host environment (requests, responses, logging, etc.)
//!
//! # Examples
//!
//! See the `examples/` directory for complete plugin implementations.
use OnceLock;
use ;
unsafe
unsafe
static GUEST: = new;
/// The main trait for implementing HTTP WebAssembly guest plugins.
///
/// This trait defines the interface between the WebAssembly guest module and the host
/// environment (such as Traefik). Implementations of this trait can intercept and modify
/// HTTP requests and responses as they flow through the host's request processing pipeline.
///
/// The trait follows the [http-wasm Guest ABI](https://http-wasm.io/http-handler-abi/) specification.
///
/// # Lifecycle
///
/// 1. **Request Phase**: The host calls [`handle_request`] when an HTTP request arrives
/// 2. **Response Phase**: The host calls [`handle_response`] when an HTTP response is ready
///
/// # Example
///
/// ```no_run
/// use http_wasm_guest::{Guest, host::{Request, Response}, register};
///
/// struct MyPlugin;
///
/// impl Guest for MyPlugin {
/// fn handle_request(&self, request: Request, _response: Response) -> (bool, i32) {
/// // Add a custom header to the request
/// request.header().add(b"X-Custom-Header", b"MyValue");
///
/// // Continue to next handler with context 0
/// (true, 0)
/// }
///
/// fn handle_response(&self, _request: Request, response: Response) {
/// // Modify the response status
/// response.set_status(200);
/// }
/// }
///
/// fn main() {
/// register(MyPlugin);
/// }
/// ```
///
/// [`handle_request`]: Guest::handle_request
/// [`handle_response`]: Guest::handle_response
/// Registers a guest plugin implementation with the http-wasm runtime.
///
/// This function must be called exactly once in the `main` function of your WebAssembly
/// module to register your [`Guest`] implementation with the host environment.
///
/// # Parameters
///
/// - `guest`: An instance of your plugin that implements the [`Guest`] trait
///
/// # Panics
///
/// This function uses [`OnceLock::get_or_init`] internally, so calling it multiple times
/// will not panic, but only the first registration will take effect.
///
/// # Example
///
/// ```rust
/// use http_wasm_guest::{Guest, host::{Request, Response}, register};
///
/// struct MyPlugin {
/// config: String,
/// }
///
/// impl Guest for MyPlugin {
/// fn handle_request(&self, request: Request, _response: Response) -> (bool, i32) {
/// // Plugin logic here
/// (true, 0)
/// }
/// }
///
/// fn main() {
/// let plugin = MyPlugin {
/// config: "example".to_string(),
/// };
/// register(plugin);
/// }
/// ```
///
/// # Notes
///
/// - The registered plugin will be called by the host for each HTTP request/response
/// - The plugin instance must be `'static` and will live for the entire duration of the module
/// - This function should be called in `main()` before the module completes initialization
///
/// [`Guest`]: Guest
/// [`OnceLock::get_or_init`]: std::sync::OnceLock::get_or_init