Skip to content

Commit e430575

Browse files
authored
Add wasi:http support to the C API (#12950)
1 parent 7e43211 commit e430575

11 files changed

Lines changed: 84 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/c-api/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ wat = { workspace = true, optional = true }
3333
cap-std = { workspace = true, optional = true }
3434
tokio = { workspace = true, optional = true, features = ["fs"] }
3535
wasmtime-wasi = { workspace = true, optional = true, features = ["p1"] }
36+
wasmtime-wasi-http = { workspace = true, optional = true, features = ["p2", "default-send-request"] }
3637
wasmtime-wasi-io = { workspace = true, optional = true, features = ["std"] }
3738
async-trait = { workspace = true, optional = true }
3839
bytes = { workspace = true, optional = true }
@@ -47,6 +48,7 @@ profiling = ["wasmtime/profiling"]
4748
cache = ["wasmtime/cache"]
4849
parallel-compilation = ['wasmtime/parallel-compilation']
4950
wasi = ['cap-std', 'wasmtime-wasi', 'wasmtime-wasi-io', 'tokio', 'async-trait', 'bytes']
51+
wasi-http = ['wasi', 'wasmtime-wasi-http']
5052
logging = ['dep:env_logger']
5153
disable-logging = ["log/max_level_off", "tracing/max_level_off"]
5254
coredump = ["wasmtime/coredump"]

crates/c-api/artifact/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ default = [
2626
'profiling',
2727
'wat',
2828
'wasi',
29+
'wasi-http',
2930
'cache',
3031
'parallel-compilation',
3132
'async',
@@ -51,6 +52,7 @@ profiling = ["wasmtime-c-api/profiling"]
5152
cache = ["wasmtime-c-api/cache"]
5253
parallel-compilation = ['wasmtime-c-api/parallel-compilation']
5354
wasi = ['wasmtime-c-api/wasi']
55+
wasi-http = ['wasmtime-c-api/wasi-http']
5456
logging = ['wasmtime-c-api/logging']
5557
disable-logging = ["wasmtime-c-api/disable-logging"]
5658
coredump = ["wasmtime-c-api/coredump"]

crates/c-api/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const FEATURES: &[&str] = &[
88
"CACHE",
99
"PARALLEL_COMPILATION",
1010
"WASI",
11+
"WASI_HTTP",
1112
"LOGGING",
1213
"DISABLE_LOGGING",
1314
"COREDUMP",

crates/c-api/cmake/features.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ feature(wat ON)
3131
feature(cache ON)
3232
feature(parallel-compilation ON)
3333
feature(wasi ON)
34+
feature(wasi-http ON)
3435
feature(logging ON)
3536
feature(disable-logging OFF)
3637
feature(coredump ON)

crates/c-api/include/wasmtime/component/linker.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ wasmtime_component_linker_add_wasip2(wasmtime_component_linker_t *linker);
165165

166166
#endif // WASMTIME_FEATURE_WASI
167167

168+
#ifdef WASMTIME_FEATURE_WASI_HTTP
169+
170+
/**
171+
* \brief Add WASI HTTP interfaces into the \p linker provided.
172+
*
173+
* This adds `wasi:http/types` and `wasi:http/outgoing-handler` to the linker.
174+
* Requires WASI to be added first via #wasmtime_component_linker_add_wasip2.
175+
*/
176+
WASM_API_EXTERN wasmtime_error_t *
177+
wasmtime_component_linker_add_wasi_http(wasmtime_component_linker_t *linker);
178+
179+
#endif // WASMTIME_FEATURE_WASI_HTTP
180+
168181
/// Type of the callback used in
169182
/// #wasmtime_component_linker_instance_add_resource
170183
typedef wasmtime_error_t *(*wasmtime_component_resource_destructor_t)(

crates/c-api/include/wasmtime/component/linker.hh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ class Linker {
215215
return std::monostate();
216216
}
217217
#endif // WASMTIME_FEATURE_WASI
218+
219+
#ifdef WASMTIME_FEATURE_WASI_HTTP
220+
/**
221+
* \brief Adds WASI HTTP definitions to this linker.
222+
*
223+
* This adds `wasi:http/types` and `wasi:http/outgoing-handler` to the linker.
224+
* Requires WASIp2 to be added first via \ref add_wasip2.
225+
*/
226+
Result<std::monostate> add_wasi_http() {
227+
wasmtime_error_t *error =
228+
wasmtime_component_linker_add_wasi_http(ptr.get());
229+
if (error != nullptr) {
230+
return Error(error);
231+
}
232+
return std::monostate();
233+
}
234+
#endif // WASMTIME_FEATURE_WASI_HTTP
218235
};
219236

220237
} // namespace component

crates/c-api/include/wasmtime/conf.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#cmakedefine WASMTIME_FEATURE_CACHE
1414
#cmakedefine WASMTIME_FEATURE_PARALLEL_COMPILATION
1515
#cmakedefine WASMTIME_FEATURE_WASI
16+
#cmakedefine WASMTIME_FEATURE_WASI_HTTP
1617
#cmakedefine WASMTIME_FEATURE_LOGGING
1718
#cmakedefine WASMTIME_FEATURE_DISABLE_LOGGING
1819
#cmakedefine WASMTIME_FEATURE_COREDUMP

crates/c-api/include/wasmtime/store.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ wasmtime_context_set_wasi(wasmtime_context_t *context, wasi_config_t *wasi);
195195

196196
#endif // WASMTIME_FEATURE_WASI
197197

198+
#ifdef WASMTIME_FEATURE_WASI_HTTP
199+
200+
/**
201+
* \brief Initializes the WASI HTTP context for this store.
202+
*
203+
* Must be called before instantiating a component that uses `wasi:http`.
204+
* Requires WASI to be configured first via #wasmtime_context_set_wasi.
205+
*/
206+
WASM_API_EXTERN void
207+
wasmtime_context_set_wasi_http(wasmtime_context_t *context);
208+
209+
#endif // WASMTIME_FEATURE_WASI_HTTP
210+
198211
/**
199212
* \brief Configures the relative deadline at which point WebAssembly code will
200213
* trap or invoke the callback function.

crates/c-api/src/component/linker.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ pub unsafe extern "C" fn wasmtime_component_linker_add_wasip2(
167167
crate::handle_result(result, |_| ())
168168
}
169169

170+
#[unsafe(no_mangle)]
171+
#[cfg(feature = "wasi-http")]
172+
pub unsafe extern "C" fn wasmtime_component_linker_add_wasi_http(
173+
linker: &mut wasmtime_component_linker_t,
174+
) -> Option<Box<wasmtime_error_t>> {
175+
let result = wasmtime_wasi_http::p2::add_only_http_to_linker_sync(&mut linker.linker);
176+
crate::handle_result(result, |_| ())
177+
}
178+
170179
#[unsafe(no_mangle)]
171180
pub unsafe extern "C" fn wasmtime_component_linker_define_unknown_imports_as_traps(
172181
linker: &mut wasmtime_component_linker_t,

0 commit comments

Comments
 (0)