A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add wasi:http support to the C API (#12950)
Chay Naborse43057557251
diff --git a/Cargo.lock b/Cargo.lockindex 798d057479..ad1b97177a 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4689,6 +4689,7 @@ dependencies = ["wasmtime","wasmtime-internal-c-api-macros","wasmtime-wasi",+ "wasmtime-wasi-http","wasmtime-wasi-io","wat",]diff --git a/crates/c-api/Cargo.toml b/crates/c-api/Cargo.tomlindex 4ec29b4830..eccea9cc76 100644--- a/crates/c-api/Cargo.toml+++ b/crates/c-api/Cargo.toml@@ -33,6 +33,7 @@ wat = { workspace = true, optional = true }cap-std = { workspace = true, optional = true }tokio = { workspace = true, optional = true, features = ["fs"] }wasmtime-wasi = { workspace = true, optional = true, features = ["p1"] }+wasmtime-wasi-http = { workspace = true, optional = true, features = ["p2", "default-send-request"] }wasmtime-wasi-io = { workspace = true, optional = true, features = ["std"] }async-trait = { workspace = true, optional = true }bytes = { workspace = true, optional = true }@@ -47,6 +48,7 @@ profiling = ["wasmtime/profiling"]cache = ["wasmtime/cache"]parallel-compilation = ['wasmtime/parallel-compilation']wasi = ['cap-std', 'wasmtime-wasi', 'wasmtime-wasi-io', 'tokio', 'async-trait', 'bytes']+wasi-http = ['wasi', 'wasmtime-wasi-http']logging = ['dep:env_logger']disable-logging = ["log/max_level_off", "tracing/max_level_off"]coredump = ["wasmtime/coredump"]diff --git a/crates/c-api/artifact/Cargo.toml b/crates/c-api/artifact/Cargo.tomlindex d02aaf2f62..bf23c85472 100644--- a/crates/c-api/artifact/Cargo.toml+++ b/crates/c-api/artifact/Cargo.toml@@ -26,6 +26,7 @@ default = ['profiling','wat','wasi',+ 'wasi-http','cache','parallel-compilation','async',@@ -51,6 +52,7 @@ profiling = ["wasmtime-c-api/profiling"]cache = ["wasmtime-c-api/cache"]parallel-compilation = ['wasmtime-c-api/parallel-compilation']wasi = ['wasmtime-c-api/wasi']+wasi-http = ['wasmtime-c-api/wasi-http']logging = ['wasmtime-c-api/logging']disable-logging = ["wasmtime-c-api/disable-logging"]coredump = ["wasmtime-c-api/coredump"]diff --git a/crates/c-api/build.rs b/crates/c-api/build.rsindex 016c6188b7..211f2c4d0e 100644--- a/crates/c-api/build.rs+++ b/crates/c-api/build.rs@@ -8,6 +8,7 @@ const FEATURES: &[&str] = &["CACHE","PARALLEL_COMPILATION","WASI",+ "WASI_HTTP","LOGGING","DISABLE_LOGGING","COREDUMP",diff --git a/crates/c-api/cmake/features.cmake b/crates/c-api/cmake/features.cmakeindex 2ea8bad995..c487ea1eb6 100644--- a/crates/c-api/cmake/features.cmake+++ b/crates/c-api/cmake/features.cmake@@ -31,6 +31,7 @@ feature(wat ON)feature(cache ON)feature(parallel-compilation ON)feature(wasi ON)+feature(wasi-http ON)feature(logging ON)feature(disable-logging OFF)feature(coredump ON)diff --git a/crates/c-api/include/wasmtime/component/linker.h b/crates/c-api/include/wasmtime/component/linker.hindex 550620bc9f..117acdcc2a 100644--- a/crates/c-api/include/wasmtime/component/linker.h+++ b/crates/c-api/include/wasmtime/component/linker.h@@ -165,6 +165,19 @@ wasmtime_component_linker_add_wasip2(wasmtime_component_linker_t *linker);#endif // WASMTIME_FEATURE_WASI+#ifdef WASMTIME_FEATURE_WASI_HTTP++/**+ * \brief Add WASI HTTP interfaces into the \p linker provided.+ *+ * This adds `wasi:http/types` and `wasi:http/outgoing-handler` to the linker.+ * Requires WASI to be added first via #wasmtime_component_linker_add_wasip2.+ */+WASM_API_EXTERN wasmtime_error_t *+wasmtime_component_linker_add_wasi_http(wasmtime_component_linker_t *linker);++#endif // WASMTIME_FEATURE_WASI_HTTP+/// Type of the callback used in/// #wasmtime_component_linker_instance_add_resourcetypedef wasmtime_error_t *(*wasmtime_component_resource_destructor_t)(diff --git a/crates/c-api/include/wasmtime/component/linker.hh b/crates/c-api/include/wasmtime/component/linker.hhindex 5606e9cb34..74d896f65e 100644--- a/crates/c-api/include/wasmtime/component/linker.hh+++ b/crates/c-api/include/wasmtime/component/linker.hh@@ -215,6 +215,23 @@ class Linker {return std::monostate();}#endif // WASMTIME_FEATURE_WASI++#ifdef WASMTIME_FEATURE_WASI_HTTP+ /**+ * \brief Adds WASI HTTP definitions to this linker.+ *+ * This adds `wasi:http/types` and `wasi:http/outgoing-handler` to the linker.+ * Requires WASIp2 to be added first via \ref add_wasip2.+ */+ Result<std::monostate> add_wasi_http() {+ wasmtime_error_t *error =+ wasmtime_component_linker_add_wasi_http(ptr.get());+ if (error != nullptr) {+ return Error(error);+ }+ return std::monostate();+ }+#endif // WASMTIME_FEATURE_WASI_HTTP};} // namespace componentdiff --git a/crates/c-api/include/wasmtime/conf.h.in b/crates/c-api/include/wasmtime/conf.h.inindex f308dd3e5c..56d6c2514a 100644--- a/crates/c-api/include/wasmtime/conf.h.in+++ b/crates/c-api/include/wasmtime/conf.h.in@@ -13,6 +13,7 @@#cmakedefine WASMTIME_FEATURE_CACHE#cmakedefine WASMTIME_FEATURE_PARALLEL_COMPILATION#cmakedefine WASMTIME_FEATURE_WASI+#cmakedefine WASMTIME_FEATURE_WASI_HTTP#cmakedefine WASMTIME_FEATURE_LOGGING#cmakedefine WASMTIME_FEATURE_DISABLE_LOGGING#cmakedefine WASMTIME_FEATURE_COREDUMPdiff --git a/crates/c-api/include/wasmtime/store.h b/crates/c-api/include/wasmtime/store.hindex aefe31c959..f440db1a3f 100644--- a/crates/c-api/include/wasmtime/store.h+++ b/crates/c-api/include/wasmtime/store.h@@ -195,6 +195,19 @@ wasmtime_context_set_wasi(wasmtime_context_t *context, wasi_config_t *wasi);#endif // WASMTIME_FEATURE_WASI+#ifdef WASMTIME_FEATURE_WASI_HTTP++/**+ * \brief 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 #wasmtime_context_set_wasi.+ */+WASM_API_EXTERN void+wasmtime_context_set_wasi_http(wasmtime_context_t *context);++#endif // WASMTIME_FEATURE_WASI_HTTP+/*** \brief Configures the relative deadline at which point WebAssembly code will* trap or invoke the callback function.diff --git a/crates/c-api/src/component/linker.rs b/crates/c-api/src/component/linker.rsindex b17045a081..9af8c82c9c 100644--- a/crates/c-api/src/component/linker.rs+++ b/crates/c-api/src/component/linker.rs@@ -167,6 +167,15 @@ pub unsafe extern "C" fn wasmtime_component_linker_add_wasip2(crate::handle_result(result, |_| ())}+#[unsafe(no_mangle)]+#[cfg(feature = "wasi-http")]+pub unsafe extern "C" fn wasmtime_component_linker_add_wasi_http(+ linker: &mut wasmtime_component_linker_t,+) -> Option<Box<wasmtime_error_t>> {+ let result = wasmtime_wasi_http::p2::add_only_http_to_linker_sync(&mut linker.linker);+ crate::handle_result(result, |_| ())+}+#[unsafe(no_mangle)]pub unsafe extern "C" fn wasmtime_component_linker_define_unknown_imports_as_traps(linker: &mut wasmtime_component_linker_t,diff --git a/crates/c-api/src/store.rs b/crates/c-api/src/store.rsindex 6e5d2d8e74..6ea7fdce72 100644--- a/crates/c-api/src/store.rs+++ b/crates/c-api/src/store.rs@@ -84,6 +84,8 @@ pub struct WasmtimeStoreData {foreign: crate::ForeignData,#[cfg(feature = "wasi")]pub(crate) wasi: Option<wasmtime_wasi::p1::WasiP1Ctx>,+ #[cfg(feature = "wasi-http")]+ pub(crate) wasi_http: Option<wasmtime_wasi_http::WasiHttpCtx>,/// Temporary storage for usage during a wasm->host call to store values/// in a slice we pass to the C API.@@ -104,6 +106,20 @@ impl wasmtime_wasi::WasiView for WasmtimeStoreData {}}+#[cfg(all(feature = "component-model", feature = "wasi-http"))]+impl wasmtime_wasi_http::p2::WasiHttpView for WasmtimeStoreData {+ fn http(&mut self) -> wasmtime_wasi_http::p2::WasiHttpCtxView<'_> {+ use wasmtime_wasi::WasiView;+ let ctx = self.wasi_http.as_mut().unwrap();+ let table = self.wasi.as_mut().unwrap().ctx().table;+ wasmtime_wasi_http::p2::WasiHttpCtxView {+ ctx,+ table,+ hooks: wasmtime_wasi_http::p2::default_hooks(),+ }+ }+}+#[unsafe(no_mangle)]pub extern "C" fn wasmtime_store_new(engine: &wasm_engine_t,@@ -117,6 +133,8 @@ pub extern "C" fn wasmtime_store_new(foreign: ForeignData { data, finalizer },#[cfg(feature = "wasi")]wasi: None,+ #[cfg(feature = "wasi-http")]+ wasi_http: None,hostcall_val_storage: Vec::new(),wasm_val_storage: Vec::new(),store_limits: StoreLimits::default(),@@ -224,6 +242,12 @@ pub extern "C" fn wasmtime_context_set_wasi(})}+#[cfg(feature = "wasi-http")]+#[unsafe(no_mangle)]+pub extern "C" fn wasmtime_context_set_wasi_http(mut context: WasmtimeStoreContextMut<'_>) {+ context.data_mut().wasi_http = Some(wasmtime_wasi_http::WasiHttpCtx::new());+}+#[unsafe(no_mangle)]pub extern "C" fn wasmtime_context_gc(mut context: WasmtimeStoreContextMut<'_>,