From d6ed6a219b31a58526721f96195c80061d41ce54 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Wed, 2 Aug 2023 17:03:48 +0100 Subject: Initial commit libunit-wasm and example C and Rust WebAssembly modules for NGINX Unit. Co-developed-by: Timo Stark Co-developed-by: Liam Crilly Signed-off-by: Andrew Clayton --- examples/c/unit-wasm-raw.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/c/unit-wasm-raw.c (limited to 'examples/c/unit-wasm-raw.c') diff --git a/examples/c/unit-wasm-raw.c b/examples/c/unit-wasm-raw.c new file mode 100644 index 0000000..42ebcbf --- /dev/null +++ b/examples/c/unit-wasm-raw.c @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +/* + * Copyright (C) Andrew Clayton + * Copyright (C) F5, Inc. + */ + +#define _GNU_SOURCE + +#include +#include + +#include "unit-wasm-raw.h" + +__attribute__((import_module("env"), import_name("nxt_wasm_send_headers"))) +void nxt_wasm_send_headers(u32 offset); + +void send_headers(u8 *addr, const char *ct, size_t len) +{ + struct resp_hdr *rh; + char clen[32]; + u8 *p; + static const u32 hdr_offs = 0; + + rh = (struct resp_hdr *)addr; + +#define SET_HDR_FIELD(idx, name, val) \ + do { \ + rh->fields[idx].name_offs = p - addr; \ + rh->fields[idx].name_len = strlen(name); \ + p = mempcpy(p, name, rh->fields[idx].name_len); \ + rh->fields[idx].value_offs = p - addr; \ + rh->fields[idx].value_len = strlen(val); \ + p = mempcpy(p, val, rh->fields[idx].value_len); \ + } while (0) + + rh->nr_fields = 2; + p = addr + sizeof(struct resp_hdr) + + (rh->nr_fields * sizeof(struct hdr_field)); + + SET_HDR_FIELD(0, "Content-Type", ct); + snprintf(clen, sizeof(clen), "%lu", len); + SET_HDR_FIELD(1, "Content-Length", clen); + + nxt_wasm_send_headers(hdr_offs); +} -- cgit