From b030baab59f4fc0429ddc6a03c24c085aacc49b8 Mon Sep 17 00:00:00 2001 From: Chay Nabors Date: Thu, 2 Apr 2026 15:31:40 -0400 Subject: [PATCH] Add wasi:http support --- tests/component/test_linker.py | 30 +++++++++++++++++++++++++++++- wasmtime/_bindings.py | 12 ++++++++++++ wasmtime/_store.py | 9 +++++++++ wasmtime/component/_linker.py | 12 ++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/tests/component/test_linker.py b/tests/component/test_linker.py index df0ac093..a424bc18 100644 --- a/tests/component/test_linker.py +++ b/tests/component/test_linker.py @@ -1,6 +1,6 @@ import unittest -from wasmtime import Engine, WasmtimeError, Module, Store +from wasmtime import Engine, WasiConfig, WasmtimeError, Module, Store from wasmtime.component import Linker, Component, LinkerInstance @@ -143,6 +143,34 @@ def test_shadow_func(self): with linker.root() as l: l.add_func('x', lambda: None) + def test_add_wasi_http(self): + engine = Engine() + linker = Linker(engine) + linker.add_wasip2() + linker.add_wasi_http() + + with linker.root(): + with self.assertRaises(WasmtimeError): + linker.add_wasi_http() + + linker.close() + + with Linker(engine) as l2: + l2.add_wasip2() + l2.add_wasi_http() + with self.assertRaises(WasmtimeError): + l2.add_wasi_http() + l2.allow_shadowing = True + l2.add_wasi_http() + + def test_set_wasi_http(self): + engine = Engine() + store = Store(engine) + wasi = WasiConfig() + wasi.inherit_stdout() + store.set_wasi(wasi) + store.set_wasi_http() + def test_define_unknown_imports_as_traps(self): engine = Engine() store = Store(engine) diff --git a/wasmtime/_bindings.py b/wasmtime/_bindings.py index f74b8a01..8b896303 100644 --- a/wasmtime/_bindings.py +++ b/wasmtime/_bindings.py @@ -2694,6 +2694,12 @@ def wasmtime_context_get_fuel(context: Any, fuel: Any) -> ctypes._Pointer: def wasmtime_context_set_wasi(context: Any, wasi: Any) -> ctypes._Pointer: return _wasmtime_context_set_wasi(context, wasi) # type: ignore +_wasmtime_context_set_wasi_http = dll.wasmtime_context_set_wasi_http +_wasmtime_context_set_wasi_http.restype = None +_wasmtime_context_set_wasi_http.argtypes = [ctypes.POINTER(wasmtime_context_t)] +def wasmtime_context_set_wasi_http(context: Any) -> None: + return _wasmtime_context_set_wasi_http(context) # type: ignore + _wasmtime_context_set_epoch_deadline = dll.wasmtime_context_set_epoch_deadline _wasmtime_context_set_epoch_deadline.restype = None _wasmtime_context_set_epoch_deadline.argtypes = [ctypes.POINTER(wasmtime_context_t), ctypes.c_uint64] @@ -4751,6 +4757,12 @@ def wasmtime_component_linker_instance_add_func(linker_instance: Any, name: Any, def wasmtime_component_linker_add_wasip2(linker: Any) -> ctypes._Pointer: return _wasmtime_component_linker_add_wasip2(linker) # type: ignore +_wasmtime_component_linker_add_wasi_http = dll.wasmtime_component_linker_add_wasi_http +_wasmtime_component_linker_add_wasi_http.restype = ctypes.POINTER(wasmtime_error_t) +_wasmtime_component_linker_add_wasi_http.argtypes = [ctypes.POINTER(wasmtime_component_linker_t)] +def wasmtime_component_linker_add_wasi_http(linker: Any) -> ctypes._Pointer: + return _wasmtime_component_linker_add_wasi_http(linker) # type: ignore + wasmtime_component_resource_destructor_t = ctypes.CFUNCTYPE(ctypes.c_size_t, ctypes.c_void_p, ctypes.POINTER(wasmtime_context_t), ctypes.c_uint32) _wasmtime_component_linker_instance_add_resource = dll.wasmtime_component_linker_instance_add_resource diff --git a/wasmtime/_store.py b/wasmtime/_store.py index 9467f062..bd3a6c2a 100644 --- a/wasmtime/_store.py +++ b/wasmtime/_store.py @@ -97,6 +97,15 @@ def set_wasi(self, wasi: "WasiConfig") -> None: if error: raise WasmtimeError._from_ptr(error) + def set_wasi_http(self) -> None: + """ + Initializes the WASI HTTP context for this store. + + Must be called before instantiating a component that uses `wasi:http`. + Requires WASI to be configured first via `set_wasi()`. + """ + ffi.wasmtime_context_set_wasi_http(self._context()) + def set_epoch_deadline(self, ticks_after_current: int) -> None: """ Configures the relative epoch deadline, after the current engine's diff --git a/wasmtime/component/_linker.py b/wasmtime/component/_linker.py index fa1acd6d..6ce56ddc 100644 --- a/wasmtime/component/_linker.py +++ b/wasmtime/component/_linker.py @@ -73,6 +73,18 @@ def add_wasip2(self) -> None: if err: raise WasmtimeError._from_ptr(err) + def add_wasi_http(self) -> None: + """ + Adds the WASI HTTP API definitions to this linker. + + This adds `wasi:http/types` and `wasi:http/outgoing-handler`. + Requires WASIp2 to be added first via `add_wasip2()`. + """ + self._assert_not_locked() + err = ffi.wasmtime_component_linker_add_wasi_http(self.ptr()) + if err: + raise WasmtimeError._from_ptr(err) + def instantiate(self, store: Storelike, component: Component) -> Instance: """ Instantiates the given component using this linker within the provided