1
1
//! # Wasmtime's WASI HTTP Implementation
2
2
//!
3
- //! This crate is Wasmtime's host implementation of the `wasi:http` package as
4
- //! part of WASIp2. This crate's implementation is primarily built on top of
3
+ //! This crate is Wasmtime's host implementation of the `wasi:http` package.
4
+ //! This crate's implementation is primarily built on top of
5
5
//! [`hyper`] and [`tokio`].
6
6
//!
7
- //! # WASI HTTP Interfaces
8
- //!
9
- //! This crate contains implementations of the following interfaces:
10
- //!
11
- //! * [`wasi:http/incoming-handler`]
12
- //! * [`wasi:http/outgoing-handler`]
13
- //! * [`wasi:http/types`]
14
- //!
15
- //! The crate also contains an implementation of the [`wasi:http/proxy`] world.
16
- //!
17
- //! [`wasi:http/proxy`]: crate::bindings::Proxy
18
- //! [`wasi:http/outgoing-handler`]: crate::bindings::http::outgoing_handler::Host
19
- //! [`wasi:http/types`]: crate::bindings::http::types::Host
20
- //! [`wasi:http/incoming-handler`]: crate::bindings::exports::wasi::http::incoming_handler::Guest
21
- //!
22
- //! This crate is very similar to [`wasmtime-wasi`] in the it uses the
23
- //! `bindgen!` macro in Wasmtime to generate bindings to interfaces. Bindings
24
- //! are located in the [`bindings`] module.
25
- //!
26
7
//! # The `WasiHttpView` trait
27
8
//!
28
9
//! All `bindgen!`-generated `Host` traits are implemented in terms of a
55
36
//! no others. This is useful when working with
56
37
//! [`wasmtime_wasi::add_to_linker_async`] for example.
57
38
//! * Add individual interfaces such as with the
58
- //! [`bindings::http::outgoing_handler::add_to_linker_get_host`] function.
39
+ //! [`p2:: bindings::http::outgoing_handler::add_to_linker_get_host`] function.
59
40
//! 3. Use [`ProxyPre`](bindings::ProxyPre) to pre-instantiate a component
60
41
//! before serving requests.
61
42
//! 4. When serving requests use
217
198
#![ expect( clippy:: allow_attributes_without_reason, reason = "crate not migrated" ) ]
218
199
219
200
mod error;
220
- mod http_impl;
221
- mod types_impl;
222
201
223
202
pub mod body;
224
203
pub mod io;
204
+ pub mod p2;
225
205
pub mod types;
226
206
227
- pub mod bindings;
228
-
229
207
pub use crate :: error:: {
230
208
http_request_error, hyper_request_error, hyper_response_error, HttpError , HttpResult ,
231
209
} ;
@@ -234,70 +212,8 @@ pub use crate::types::{
234
212
WasiHttpCtx , WasiHttpImpl , WasiHttpView , DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS ,
235
213
DEFAULT_OUTGOING_BODY_CHUNK_SIZE ,
236
214
} ;
237
- use wasmtime_wasi:: IoImpl ;
238
- /// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
239
- ///
240
- /// This function will add the `async` variant of all interfaces into the
241
- /// `Linker` provided. By `async` this means that this function is only
242
- /// compatible with [`Config::async_support(true)`][async]. For embeddings with
243
- /// async support disabled see [`add_to_linker_sync`] instead.
244
- ///
245
- /// [async]: wasmtime::Config::async_support
246
- ///
247
- /// # Example
248
- ///
249
- /// ```
250
- /// use wasmtime::{Engine, Result, Config};
251
- /// use wasmtime::component::{ResourceTable, Linker};
252
- /// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
253
- /// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
254
- ///
255
- /// fn main() -> Result<()> {
256
- /// let mut config = Config::new();
257
- /// config.async_support(true);
258
- /// let engine = Engine::new(&config)?;
259
- ///
260
- /// let mut linker = Linker::<MyState>::new(&engine);
261
- /// wasmtime_wasi_http::add_to_linker_async(&mut linker)?;
262
- /// // ... add any further functionality to `linker` if desired ...
263
- ///
264
- /// Ok(())
265
- /// }
266
- ///
267
- /// struct MyState {
268
- /// ctx: WasiCtx,
269
- /// http_ctx: WasiHttpCtx,
270
- /// table: ResourceTable,
271
- /// }
272
- ///
273
- /// impl IoView for MyState {
274
- /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
275
- /// }
276
- /// impl WasiHttpView for MyState {
277
- /// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
278
- /// }
279
- /// impl WasiView for MyState {
280
- /// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
281
- /// }
282
- /// ```
283
- pub fn add_to_linker_async < T > ( l : & mut wasmtime:: component:: Linker < T > ) -> anyhow:: Result < ( ) >
284
- where
285
- T : WasiHttpView + wasmtime_wasi:: WasiView ,
286
- {
287
- let io_closure = type_annotate_io :: < T , _ > ( |t| wasmtime_wasi:: IoImpl ( t) ) ;
288
- let closure = type_annotate_wasi :: < T , _ > ( |t| wasmtime_wasi:: WasiImpl ( wasmtime_wasi:: IoImpl ( t) ) ) ;
289
- wasmtime_wasi:: bindings:: clocks:: wall_clock:: add_to_linker_get_host ( l, closure) ?;
290
- wasmtime_wasi:: bindings:: clocks:: monotonic_clock:: add_to_linker_get_host ( l, closure) ?;
291
- wasmtime_wasi:: bindings:: io:: poll:: add_to_linker_get_host ( l, io_closure) ?;
292
- wasmtime_wasi:: bindings:: io:: error:: add_to_linker_get_host ( l, io_closure) ?;
293
- wasmtime_wasi:: bindings:: io:: streams:: add_to_linker_get_host ( l, io_closure) ?;
294
- wasmtime_wasi:: bindings:: cli:: stdin:: add_to_linker_get_host ( l, closure) ?;
295
- wasmtime_wasi:: bindings:: cli:: stdout:: add_to_linker_get_host ( l, closure) ?;
296
- wasmtime_wasi:: bindings:: cli:: stderr:: add_to_linker_get_host ( l, closure) ?;
297
- wasmtime_wasi:: bindings:: random:: random:: add_to_linker_get_host ( l, closure) ?;
298
-
299
- add_only_http_to_linker_async ( l)
300
- }
215
+ #[ doc( inline) ]
216
+ pub use p2:: * ;
301
217
302
218
// NB: workaround some rustc inference - a future refactoring may make this
303
219
// obsolete.
@@ -319,100 +235,3 @@ where
319
235
{
320
236
val
321
237
}
322
-
323
- /// A slimmed down version of [`add_to_linker_async`] which only adds
324
- /// `wasi:http` interfaces to the linker.
325
- ///
326
- /// This is useful when using [`wasmtime_wasi::add_to_linker_async`] for
327
- /// example to avoid re-adding the same interfaces twice.
328
- pub fn add_only_http_to_linker_async < T > (
329
- l : & mut wasmtime:: component:: Linker < T > ,
330
- ) -> anyhow:: Result < ( ) >
331
- where
332
- T : WasiHttpView ,
333
- {
334
- let closure = type_annotate_http :: < T , _ > ( |t| WasiHttpImpl ( IoImpl ( t) ) ) ;
335
- crate :: bindings:: http:: outgoing_handler:: add_to_linker_get_host ( l, closure) ?;
336
- crate :: bindings:: http:: types:: add_to_linker_get_host ( l, closure) ?;
337
-
338
- Ok ( ( ) )
339
- }
340
-
341
- /// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
342
- ///
343
- /// This function will add the `sync` variant of all interfaces into the
344
- /// `Linker` provided. For embeddings with async support see
345
- /// [`add_to_linker_async`] instead.
346
- ///
347
- /// # Example
348
- ///
349
- /// ```
350
- /// use wasmtime::{Engine, Result, Config};
351
- /// use wasmtime::component::{ResourceTable, Linker};
352
- /// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
353
- /// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
354
- ///
355
- /// fn main() -> Result<()> {
356
- /// let config = Config::default();
357
- /// let engine = Engine::new(&config)?;
358
- ///
359
- /// let mut linker = Linker::<MyState>::new(&engine);
360
- /// wasmtime_wasi_http::add_to_linker_sync(&mut linker)?;
361
- /// // ... add any further functionality to `linker` if desired ...
362
- ///
363
- /// Ok(())
364
- /// }
365
- ///
366
- /// struct MyState {
367
- /// ctx: WasiCtx,
368
- /// http_ctx: WasiHttpCtx,
369
- /// table: ResourceTable,
370
- /// }
371
- /// impl IoView for MyState {
372
- /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
373
- /// }
374
- /// impl WasiHttpView for MyState {
375
- /// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
376
- /// }
377
- /// impl WasiView for MyState {
378
- /// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
379
- /// }
380
- /// ```
381
- pub fn add_to_linker_sync < T > ( l : & mut wasmtime:: component:: Linker < T > ) -> anyhow:: Result < ( ) >
382
- where
383
- T : WasiHttpView + wasmtime_wasi:: WasiView ,
384
- {
385
- let io_closure = type_annotate_io :: < T , _ > ( |t| wasmtime_wasi:: IoImpl ( t) ) ;
386
- let closure = type_annotate_wasi :: < T , _ > ( |t| wasmtime_wasi:: WasiImpl ( wasmtime_wasi:: IoImpl ( t) ) ) ;
387
-
388
- wasmtime_wasi:: bindings:: clocks:: wall_clock:: add_to_linker_get_host ( l, closure) ?;
389
- wasmtime_wasi:: bindings:: clocks:: monotonic_clock:: add_to_linker_get_host ( l, closure) ?;
390
- wasmtime_wasi:: bindings:: sync:: io:: poll:: add_to_linker_get_host ( l, io_closure) ?;
391
- wasmtime_wasi:: bindings:: sync:: io:: streams:: add_to_linker_get_host ( l, io_closure) ?;
392
- wasmtime_wasi:: bindings:: io:: error:: add_to_linker_get_host ( l, io_closure) ?;
393
- wasmtime_wasi:: bindings:: cli:: stdin:: add_to_linker_get_host ( l, closure) ?;
394
- wasmtime_wasi:: bindings:: cli:: stdout:: add_to_linker_get_host ( l, closure) ?;
395
- wasmtime_wasi:: bindings:: cli:: stderr:: add_to_linker_get_host ( l, closure) ?;
396
- wasmtime_wasi:: bindings:: random:: random:: add_to_linker_get_host ( l, closure) ?;
397
-
398
- add_only_http_to_linker_sync ( l) ?;
399
-
400
- Ok ( ( ) )
401
- }
402
-
403
- /// A slimmed down version of [`add_to_linker_sync`] which only adds
404
- /// `wasi:http` interfaces to the linker.
405
- ///
406
- /// This is useful when using [`wasmtime_wasi::add_to_linker_sync`] for
407
- /// example to avoid re-adding the same interfaces twice.
408
- pub fn add_only_http_to_linker_sync < T > ( l : & mut wasmtime:: component:: Linker < T > ) -> anyhow:: Result < ( ) >
409
- where
410
- T : WasiHttpView ,
411
- {
412
- let closure = type_annotate_http :: < T , _ > ( |t| WasiHttpImpl ( IoImpl ( t) ) ) ;
413
-
414
- crate :: bindings:: http:: outgoing_handler:: add_to_linker_get_host ( l, closure) ?;
415
- crate :: bindings:: http:: types:: add_to_linker_get_host ( l, closure) ?;
416
-
417
- Ok ( ( ) )
418
- }
0 commit comments