3
.\" Copyright (c) 2014, 2017--2018, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
4
.\" Copyright (c) 2018 Ingo Schwarze <schwarze@openbsd.org>
6
.\" Permission to use, copy, modify, and distribute this software for any
7
.\" purpose with or without fee is hereby granted, provided that the above
8
.\" copyright notice and this permission notice appear in all copies.
10
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
.Nm khttp_template_buf ,
25
.Nd emit filled-in templates for kcgi
36
.Fa "const struct ktemplate *t"
37
.Fa "const char *filename"
42
.Fa "const struct ktemplate *t"
49
.Fa "const struct ktemplate *t"
51
.Fa "const char *filename"
54
Modify input by replacing keys in a template.
55
May only be called after
61
previously allocated with
64
.Xr khttp_fcgi_parse 3 .
67
family allows for alternative writers.
69
All functions accept a template
71
consisting of the following fields:
73
.It Fa "const char *const *key"
74
An array of keys to be replaced in the template.
79
An optional argument passed to
81
.It Fa "int (*cb)(size_t index, void *arg)"
82
The callback function invoked when a key at position
84
which is always less than
90
is passed the function.
91
The function must return zero on failure, non-zero on success.
98
the input is passed through to
100
without any processing.
102
Otherwise, the input is passed to
104
until a key sequence in encountered matching a key in
108
is then invoked instead of printing the key sequence.
109
If there are multiple matching keys in
111
only one is used (which is not yet fixed).
112
If the key sequence is not found in
114
it is passed unchanged to
117
The different input types are
119
which reads input from the file
121
.Fn khttp_template_buf ,
122
which reads from a binary buffer
127
.Fn khttp_template_fd ,
128
which reads from a file descriptor
130
with optional file-name
132
used only for logging purposes.
134
Each substring of the input beginning and ending with a pair
138
.Cm @@ Ns Ar key Ns Cm @@ ,
143
are allowed and match empty template keys.
146
pair is escaped with a single backslash,
148
the backslash is removed and it's emitted as
151
A key sequence may not contain an escaped pair: this is parsed as a
152
backslash followed by the trailing pair.
156
indicating the error state:
157
.Bl -tag -width KCGI_SYSTEM
161
Memory allocation failed.
164
For example, writing to the output stream failed, or
174
The following simple example takes a buffer
176
and applies the replacement template of two values, writing it to the
179
.Bd -literal -offset indent
180
static int writer(size_t idx, void *arg)
182
struct kreq *r = arg;
184
khttp_puts(r, "foo-value");
186
khttp_puts(r, "bar-value");
190
enum kcgi_err format(struct kreq *r)
192
const char *const keys[] = { "foo", "bar" };
193
struct ktemplate t = {
199
const char *buf = "foo=@@foo@@, bar=@@bar@@";
200
return khttp_template_buf(r, &t, buf, strlen(buf));
204
The function will produce
205
.Qq foo=foo-value, bar=bar-value .
210
.Xr khttp_templatex 3 ,
214
.An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .