diff options
| author | Andrew Clayton <a.clayton@nginx.com> | 2023-09-08 21:51:25 +0100 |
|---|---|---|
| committer | Andrew Clayton <a.clayton@nginx.com> | 2023-09-25 13:49:36 +0100 |
| commit | 76086d6d7a027ddf42d86897200a53d724fb4bb7 (patch) | |
| tree | ac1b5f1a4c64641d0510e9ab289ffd1a99b47beb /src/wasm/nxt_rt_wasmtime.c | |
| parent | c9961610ed5651bda63191465e70e8227c3ef711 (diff) | |
| download | unit-76086d6d7a027ddf42d86897200a53d724fb4bb7.tar.gz unit-76086d6d7a027ddf42d86897200a53d724fb4bb7.tar.bz2 | |
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 <a.clayton@nginx.com>
Diffstat (limited to 'src/wasm/nxt_rt_wasmtime.c')
| -rw-r--r-- | src/wasm/nxt_rt_wasmtime.c | 25 |
1 files changed, 23 insertions, 2 deletions
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 }, { } |
