summaryrefslogtreecommitdiffhomepage
path: root/examples/c/unit-wasm-raw.c
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2023-08-02 17:03:48 +0100
committerAndrew Clayton <a.clayton@nginx.com>2023-08-21 23:24:12 +0100
commitd6ed6a219b31a58526721f96195c80061d41ce54 (patch)
tree17a1fd6ecf72a327916ff0f8bc7aaf85b981ceff /examples/c/unit-wasm-raw.c
downloadunit-wasm-d6ed6a219b31a58526721f96195c80061d41ce54.tar.gz
unit-wasm-d6ed6a219b31a58526721f96195c80061d41ce54.tar.bz2
Initial commitv0.1.0
libunit-wasm and example C and Rust WebAssembly modules for NGINX Unit. Co-developed-by: Timo Stark <t.stark@nginx.com> Co-developed-by: Liam Crilly <liam@nginx.com> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'examples/c/unit-wasm-raw.c')
-rw-r--r--examples/c/unit-wasm-raw.c46
1 files changed, 46 insertions, 0 deletions
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 <stdio.h>
+#include <string.h>
+
+#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);
+}