ksgi

Форк
0
/
template.c 
251 строка · 5.9 Кб
1
/*	$Id$ */
2
/*
3
 * Copyright (c) 2017--2018 Kristaps Dzonsons <kristaps@bsd.lv>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
#include "config.h"
18

19
#include <sys/mman.h>
20
#include <sys/stat.h>
21

22
#include <fcntl.h>
23
#include <limits.h>
24
#include <stdarg.h>
25
#include <stdint.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <unistd.h>
29

30
#include "kcgi.h"
31
#include "extern.h"
32

33
static enum kcgi_err
34
khttp_templatex_write(const char *dat, size_t sz, void *arg)
35
{
36

37
	return khttp_write(arg, dat, sz);
38
}
39

40
enum kcgi_err
41
khttp_template_buf(struct kreq *req, 
42
	const struct ktemplate *t, const char *buf, size_t sz)
43
{
44
	struct ktemplatex x;
45

46
	memset(&x, 0, sizeof(struct ktemplatex));
47
	x.writer = khttp_templatex_write;
48

49
	return khttp_templatex_buf(t, buf, sz, &x, req);
50
}
51

52
/*
53
 * There are all sorts of ways to make this faster and more efficient.
54
 * For now, do it the easily-auditable way.
55
 * Memory-map the given file and look through it character by character
56
 * til we get to the key delimiter "@@".
57
 * Once there, scan to the matching "@@".
58
 * Look for the matching key within these pairs.
59
 * If found, invoke the callback function with the given key.
60
 */
61
enum kcgi_err
62
khttp_templatex_buf(const struct ktemplate *t, 
63
	const char *buf, size_t sz, 
64
	const struct ktemplatex *opt, void *arg)
65
{
66
	size_t		 i, j, len, start, end;
67
	ktemplate_writef fp;
68
	enum kcgi_err	 er;
69

70
	if (sz == 0)
71
		return KCGI_OK;
72

73
	fp = opt->writer;
74

75
	/*
76
	 * If we have no callback mechanism, then we're going to push
77
	 * the unmodified text out.
78
	 * Check both the per-key template system and the fallback, if
79
	 * provided.
80
	 */
81

82
	if (t == NULL && opt->fbk == NULL)
83
		return fp(buf, sz, arg);
84

85
	for (i = 0; i < sz - 1; i++) {
86
		/* 
87
		 * Read ahead til one of our significant characters.
88
		 * Then emit all characters between then and now.
89
		 */
90

91
		for (j = i; j < sz - 1; j++)
92
			if (buf[j] == '\\' || buf[j] == '@')
93
				break;
94
		if (j > i && (er = fp(&buf[i], j - i, arg)) != KCGI_OK)
95
			return er;
96
		i = j;
97

98
		/* 
99
		 * See if we're at an escaped @@, i.e., it's preceded by
100
		 * the backslash.
101
		 * If we are, then emit the standalone @@.
102
		 */
103

104
		if (i < sz - 2 && buf[i] == '\\' &&
105
		    buf[i + 1] == '@' && buf[i + 2] == '@') {
106
			if ((er = fp(&buf[i + 1], 2, arg)) != KCGI_OK)
107
				return er;
108
			i += 2;
109
			continue;
110
		}
111

112
		/* Look for the starting @@ marker. */
113

114
		if (!(buf[i] == '@' && buf[i + 1] == '@')) {
115
			if ((er = fp(&buf[i], 1, arg)) != KCGI_OK)
116
				return er;
117
			continue;
118
		} 
119

120
		/* Seek to find the end "@@" marker. */
121

122
		start = i + 2;
123
		for (end = start; end < sz - 1; end++)
124
			if (buf[end] == '@' && buf[end + 1] == '@')
125
				break;
126

127
		/* Continue printing if not found. */
128

129
		if (end >= sz - 1) {
130
			if ((er = fp(&buf[i], 1, arg)) != KCGI_OK)
131
				return er;
132
			continue;
133
		}
134

135
		/* 
136
		 * Look for a matching key.
137
		 * If we find no matching key, use the fallback (if
138
		 * found), otherwise just continue as if the key were
139
		 * opaque text.
140
		 */
141

142
		for (j = 0; j < t->keysz; j++) {
143
			len = strlen(t->key[j]);
144
			if (len != end - start)
145
				continue;
146
			else if (memcmp(&buf[start], t->key[j], len))
147
				continue;
148
			if (!(*t->cb)(j, t->arg)) {
149
				kutil_warnx(NULL, NULL, 
150
					"template callback error");
151
				return KCGI_FORM;
152
			}
153
			break;
154
		}
155

156
		if (j == t->keysz && opt->fbk != NULL) {
157
			len = end - start;
158
			if (!(*opt->fbk)(&buf[start], len, t->arg)) {
159
				kutil_warnx(NULL, NULL, "template "
160
					"default callback error");
161
				return KCGI_FORM;
162
			}
163
			i = end + 1;
164
		} else if (j == t->keysz) {
165
			if ((er = fp(&buf[i], 1, arg)) != KCGI_OK)
166
				return er;
167
		} else
168
			i = end + 1;
169
	}
170

171
	if (i < sz && (er = fp(&buf[i], 1, arg)) != KCGI_OK)
172
		return er;
173

174
	return KCGI_OK;
175
}
176

177
enum kcgi_err
178
khttp_template(struct kreq *req, 
179
	const struct ktemplate *t, const char *fname)
180
{
181
	struct ktemplatex x;
182

183
	memset(&x, 0, sizeof(struct ktemplatex));
184
	x.writer = khttp_templatex_write;
185
	return khttp_templatex(t, fname, &x, req);
186
}
187

188
enum kcgi_err
189
khttp_templatex(const struct ktemplate *t, 
190
	const char *fname, const struct ktemplatex *opt, void *arg)
191
{
192
	int		 fd;
193
	enum kcgi_err	 rc;
194

195
	if ((fd = open(fname, O_RDONLY, 0)) == -1) {
196
		kutil_warn(NULL, NULL, "%s", fname);
197
		return KCGI_SYSTEM;
198
	}
199

200
	rc = khttp_templatex_fd(t, fd, fname, opt, arg);
201
	close(fd);
202
	return rc;
203
}
204

205
enum kcgi_err
206
khttp_template_fd(struct kreq *req, 
207
	const struct ktemplate *t, int fd, const char *fname)
208
{
209
	struct ktemplatex x;
210

211
	memset(&x, 0, sizeof(struct ktemplatex));
212
	x.writer = khttp_templatex_write;
213
	return khttp_templatex_fd(t, fd, fname, &x, req);
214
}
215

216
enum kcgi_err
217
khttp_templatex_fd(const struct ktemplate *t, 
218
	int fd, const char *fname,
219
	const struct ktemplatex *opt, void *arg)
220
{
221
	struct stat 	 st;
222
	char		*buf;
223
	size_t		 sz;
224
	enum kcgi_err	 rc;
225

226
	if (fname == NULL)
227
		fname = "<unknown descriptor>";
228

229
	if (fstat(fd, &st) == -1) {
230
		kutil_warn(NULL, NULL, "%s", fname);
231
		return KCGI_SYSTEM;
232
	} else if (st.st_size > SSIZE_MAX) {
233
		kutil_warnx(NULL, NULL, "%s: too large", fname);
234
		return KCGI_SYSTEM;
235
	} else if (st.st_size <= 0) {
236
		kutil_warnx(NULL, NULL, "%s: zero-length", fname);
237
		return KCGI_OK;
238
	}
239

240
	sz = (size_t)st.st_size;
241
	buf = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
242

243
	if (buf == MAP_FAILED) {
244
		kutil_warn(NULL, NULL, "%s", fname);
245
		return KCGI_SYSTEM;
246
	}
247

248
	rc = khttp_templatex_buf(t, buf, sz, opt, arg);
249
	munmap(buf, sz);
250
	return rc;
251
}
252

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.