From 76086d6d7a027ddf42d86897200a53d724fb4bb7 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Fri, 8 Sep 2023 21:51:25 +0100 Subject: Wasm: Allow to set the HTTP response status. This commit enables WebAssembly modules to set the HTTP response status to something other than the previously hard coded '200 OK'. To do this they can make a call to nxt_wasm_set_resp_status() providing the required status code. If this function isn't called the status code defaults to '200 OK'. The WebAssembly module can also return -1 from the request_handler function as a short cut to signal a '500 Internal Server Error'. Signed-off-by: Andrew Clayton --- src/wasm/nxt_rt_wasmtime.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src/wasm/nxt_rt_wasmtime.c') diff --git a/src/wasm/nxt_rt_wasmtime.c b/src/wasm/nxt_rt_wasmtime.c index 99786b89..7f05fae0 100644 --- a/src/wasm/nxt_rt_wasmtime.c +++ b/src/wasm/nxt_rt_wasmtime.c @@ -102,6 +102,19 @@ nxt_wasm_send_headers(void *env, wasmtime_caller_t *caller, } +static wasm_trap_t * +nxt_wasm_set_resp_status(void *env, wasmtime_caller_t *caller, + const wasmtime_val_t *args, size_t nargs, + wasmtime_val_t *results, size_t nresults) +{ + nxt_wasm_ctx_t *ctx = env; + + ctx->status = args[0].of.i32; + + return NULL; +} + + static void nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) { @@ -123,8 +136,8 @@ nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) } -static void -nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx) +static int +nxt_wasmtime_execute_request(nxt_wasm_ctx_t *ctx) { int i = 0; wasm_trap_t *trap = NULL; @@ -142,7 +155,10 @@ nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx) nxt_wasmtime_err_msg(error, trap, "failed to call function [->wasm_request_handler]" ); + return -1; } + + return results[0].of.i32; } @@ -183,6 +199,11 @@ nxt_wasmtime_set_function_imports(nxt_wasm_ctx_t *ctx) .func = nxt_wasm_send_headers, .params = { WASM_I32 }, .ft = NXT_WASM_FT_1_0 + }, { + .func_name = "nxt_wasm_set_resp_status", + .func = nxt_wasm_set_resp_status, + .params = { WASM_I32 }, + .ft = NXT_WASM_FT_1_0 }, { } -- cgit