@@ -20,7 +20,7 @@ use crossbeam_channel::Sender;
2020use tracing:: instrument;
2121use wasmtime:: { InstanceAllocationStrategy , PoolingAllocationConfig } ;
2222use wasmtime_wasi:: preview2:: Table ;
23- use wasmtime_wasi_http:: types:: { WasiHttpCtx , WasiHttpView } ;
23+ use wasmtime_wasi_http:: types:: { default_send_request , WasiHttpCtx , WasiHttpView } ;
2424
2525use self :: host_component:: { HostComponents , HostComponentsBuilder } ;
2626
@@ -191,7 +191,7 @@ impl<T: Send> wasmtime_wasi::preview2::WasiView for Data<T> {
191191 }
192192}
193193
194- impl < T : Send > WasiHttpView for Data < T > {
194+ impl < T : Send + OutboundWasiHttpHandler > WasiHttpView for Data < T > {
195195 fn ctx ( & mut self ) -> & mut WasiHttpCtx {
196196 match & mut self . wasi {
197197 Wasi :: Preview1 ( _) => panic ! ( "using WASI Preview 1 functions with Preview 2 store" ) ,
@@ -202,6 +202,45 @@ impl<T: Send> WasiHttpView for Data<T> {
202202 fn table ( & mut self ) -> & mut Table {
203203 & mut self . table
204204 }
205+
206+ fn send_request (
207+ & mut self ,
208+ request : wasmtime_wasi_http:: types:: OutgoingRequest ,
209+ ) -> wasmtime:: Result <
210+ wasmtime:: component:: Resource < wasmtime_wasi_http:: types:: HostFutureIncomingResponse > ,
211+ >
212+ where
213+ Self : Sized ,
214+ {
215+ T :: send_request ( self , request)
216+ }
217+ }
218+
219+ /// Handler for wasi-http based requests
220+ pub trait OutboundWasiHttpHandler {
221+ /// Send the request
222+ fn send_request (
223+ data : & mut Data < Self > ,
224+ request : wasmtime_wasi_http:: types:: OutgoingRequest ,
225+ ) -> wasmtime:: Result <
226+ wasmtime:: component:: Resource < wasmtime_wasi_http:: types:: HostFutureIncomingResponse > ,
227+ >
228+ where
229+ Self : Sized ;
230+ }
231+
232+ impl OutboundWasiHttpHandler for ( ) {
233+ fn send_request (
234+ data : & mut Data < Self > ,
235+ request : wasmtime_wasi_http:: types:: OutgoingRequest ,
236+ ) -> wasmtime:: Result <
237+ wasmtime:: component:: Resource < wasmtime_wasi_http:: types:: HostFutureIncomingResponse > ,
238+ >
239+ where
240+ Self : Sized ,
241+ {
242+ default_send_request ( data, request)
243+ }
205244}
206245
207246/// An alias for [`wasmtime::Linker`] specialized to [`Data`].
@@ -222,13 +261,10 @@ pub struct EngineBuilder<T> {
222261 epoch_ticker_thread : bool ,
223262}
224263
225- impl < T : Send + Sync > EngineBuilder < T > {
264+ impl < T : Send + Sync + OutboundWasiHttpHandler > EngineBuilder < T > {
226265 fn new ( config : & Config ) -> Result < Self > {
227266 let engine = wasmtime:: Engine :: new ( & config. inner ) ?;
228-
229- let mut linker: Linker < T > = Linker :: new ( & engine) ;
230- wasmtime_wasi_http:: proxy:: add_to_linker ( & mut linker) ?;
231-
267+ let linker: Linker < T > = Linker :: new ( & engine) ;
232268 let mut module_linker = ModuleLinker :: new ( & engine) ;
233269 wasmtime_wasi:: tokio:: add_to_linker ( & mut module_linker, |data| match & mut data. wasi {
234270 Wasi :: Preview1 ( ctx) => ctx,
@@ -244,7 +280,9 @@ impl<T: Send + Sync> EngineBuilder<T> {
244280 epoch_ticker_thread : true ,
245281 } )
246282 }
283+ }
247284
285+ impl < T : Send + Sync > EngineBuilder < T > {
248286 /// Adds definition(s) to the built [`Engine`].
249287 ///
250288 /// This method's signature is meant to be used with
@@ -345,7 +383,7 @@ pub struct Engine<T> {
345383 _epoch_ticker_signal : Option < Sender < ( ) > > ,
346384}
347385
348- impl < T : Send + Sync > Engine < T > {
386+ impl < T : OutboundWasiHttpHandler + Send + Sync > Engine < T > {
349387 /// Creates a new [`EngineBuilder`] with the given [`Config`].
350388 pub fn builder ( config : & Config ) -> Result < EngineBuilder < T > > {
351389 EngineBuilder :: new ( config)
0 commit comments