summaryrefslogtreecommitdiffhomepage
path: root/examples/c/unit-wasm-raw.c
blob: 42ebcbfc55af095e82a5fe47a85e7268f99750fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
}