-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_ssi.c
473 lines (400 loc) · 11.3 KB
/
io_ssi.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright (c) 2006,2007 Steven Johnson <[email protected]>
* Copyright (c) 2007 Sergey Lyubka <[email protected]>
* All rights reserved
*
* "THE BEER-WARE LICENSE" (Revision 42):
* Sergey Lyubka wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
*/
#define MY_DEBUGGING 1
#include "defs.h"
#if !defined(NO_SSI)
#define CMDBUFSIZ 512 /* SSI command buffer size */
#define NEST_MAX 6 /* Maximum nesting level */
struct ssi_func {
struct llhead link;
void *user_data;
char *name;
shttpd_callback_t func;
};
struct ssi_inc {
int state; /* Buffering state */
int cond; /* Conditional state */
FILE *fp; /* Icluded file stream */
char buf[CMDBUFSIZ]; /* SSI command buffer */
size_t nbuf; /* Bytes in a command buffer */
FILE *pipe; /* #exec stream */
struct ssi_func func; /* #call function */
};
struct ssi {
struct conn *conn; /* Connection we belong to */
int nest; /* Current nesting level */
struct ssi_inc incs[NEST_MAX]; /* Nested includes */
};
enum { SSI_PASS, SSI_BUF, SSI_EXEC, SSI_CALL };
enum { SSI_GO, SSI_STOP }; /* Conditional states */
static const struct vec st = {"<!--#", 5};
void shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name,
shttpd_callback_t func, void *user_data)
{
struct ssi_func *e;
if ((e = malloc(sizeof(*e))) != NULL) {
e->name = _shttpd_strdup(name);
e->func = func;
e->user_data = user_data;
LL_TAIL(&ctx->ssi_funcs, &e->link);
}
}
void _shttpd_ssi_func_destructor(struct llhead *lp)
{
struct ssi_func *e = LL_ENTRY(lp, struct ssi_func, link);
free(e->name);
free(e);
}
static const struct ssi_func *find_ssi_func(struct ssi *ssi, const char *name)
{
struct ssi_func *e;
struct llhead *lp;
LL_FOREACH(&ssi->conn->ctx->ssi_funcs, lp) {
e = LL_ENTRY(lp, struct ssi_func, link);
if (!strcmp(name, e->name))
return (e);
}
return (NULL);
}
static void call(struct ssi *ssi, const char *name,
struct shttpd_arg *arg, char *buf, int len)
{
const struct ssi_func *ssi_func;
(void) memset(arg, 0, sizeof(*arg));
MY_DEBUG("name=%s, buf=%p, len=%d.\n", name, buf, len);
/*
* SSI function may be called with parameters. These parameters
* are passed as arg->in.buf, arg->in.len vector.
*/
arg->in.buf = strchr(name, ' ');
if (arg->in.buf != NULL) {
*arg->in.buf++ = '\0';
arg->in.len = strlen(arg->in.buf);
}
if ((ssi_func = find_ssi_func(ssi, name)) != NULL) {
arg->priv = ssi->conn;
arg->user_data = ssi_func->user_data;
arg->out.buf = buf;
arg->out.len = len;
ssi_func->func(arg);
}
}
static int evaluate(struct ssi *ssi, const char *name)
{
struct shttpd_arg arg;
// wkliang:20110527 - a bit tricky & risky, eval logic cannot output
call(ssi, name, &arg, NULL, 0);
return (arg.flags & SHTTPD_SSI_EVAL_TRUE);
}
static void pass(struct ssi_inc *inc, void *buf, int *n)
{
if (inc->cond == SSI_GO) {
(void) memcpy(buf, inc->buf, inc->nbuf);
(*n) += inc->nbuf;
}
inc->nbuf = 0;
inc->state = SSI_PASS;
}
static int get_path(struct conn *conn, const char *src,
int src_len, char *dst, int dst_len)
{
static struct vec accepted[] = {
{"\"", 1}, /* Relative to webserver CWD */
{"file=\"", 6}, /* Relative to current URI */
{"virtual=\"", 9}, /* Relative to document root */
{NULL, 0},
};
struct vec *vec;
const char *p, *root = conn->ctx->options[OPT_ROOT];
int len;
for (vec = accepted; vec->len > 0; vec++)
if (src_len > vec->len && !memcmp(src, vec->ptr, vec->len)) {
src += vec->len;
src_len -= vec->len;
if ((p = memchr(src, '"', src_len)) == NULL)
break;
if (vec->len == 6) {
len = _shttpd_snprintf(dst, dst_len, "%s%c%s",
root, DIRSEP, conn->uri);
while (len > 0 && dst[len] != '/')
len--;
dst += len;
dst_len -= len;
} else if (vec->len == 9) {
len = _shttpd_snprintf(dst, dst_len, "%s%c",
root, DIRSEP);
dst += len;
dst_len -= len;
}
_shttpd_url_decode(src, p - src, dst, dst_len);
return (1);
}
return (0);
}
static void do_include(struct ssi *ssi)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
char buf[FILENAME_MAX];
FILE *fp;
assert(inc->nbuf >= 13);
if (inc->cond == SSI_STOP) {
/* Do nothing - conditional FALSE */
} else if (ssi->nest >= (int) NELEMS(ssi->incs) - 1) {
_shttpd_elog(E_LOG, ssi->conn,
"ssi: #include: maximum nested level reached");
} else if (!get_path(ssi->conn,
inc->buf + 13, inc->nbuf - 13, buf, sizeof(buf))) {
_shttpd_elog(E_LOG, ssi->conn, "ssi: bad #include: [%.*s]",
inc->nbuf, inc->buf);
} else if ((fp = fopen(buf, "r")) == NULL) {
_shttpd_elog(E_LOG, ssi->conn,
"ssi: fopen(%s): %s", buf, strerror(errno));
} else {
ssi->nest++;
ssi->incs[ssi->nest].fp = fp;
ssi->incs[ssi->nest].nbuf = 0;
ssi->incs[ssi->nest].cond = SSI_GO;
}
}
static char *trim_spaces(struct ssi_inc *inc)
{
char *p = inc->buf + inc->nbuf - 2;
/* Trim spaces from the right */
*p-- = '\0';
while (isspace(* (unsigned char *) p))
*p-- = '\0';
/* Shift pointer to the start of attributes */
for (p = inc->buf; !isspace(* (unsigned char *) p); p++);
while (*p && isspace(* (unsigned char *) p)) p++;
return (p);
}
static void do_if(struct ssi *ssi)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
char *name = trim_spaces(inc);
inc->cond = evaluate(ssi, name) ? SSI_GO : SSI_STOP;
}
static void do_elif(struct ssi *ssi)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
char *name = trim_spaces(inc);
if (inc->cond == SSI_STOP && evaluate(ssi, name))
inc->cond = SSI_GO;
else
inc->cond = SSI_STOP;
}
static void do_endif(struct ssi *ssi)
{
ssi->incs[ssi->nest].cond = SSI_GO;
}
static void do_else(struct ssi *ssi)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
inc->cond = inc->cond == SSI_GO ? SSI_STOP : SSI_GO;
}
static void do_call2(struct ssi *ssi, char *buf, int len, int *n)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
struct shttpd_arg arg;
call(ssi, inc->buf, &arg, buf, len);
(*n) += arg.out.num_bytes;
if (arg.flags & SHTTPD_END_OF_OUTPUT)
inc->state = SSI_PASS;
}
static void do_call(struct ssi *ssi, char *buf, int len, int *n)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
char *name = trim_spaces(inc);
if (inc->cond == SSI_GO) {
(void) memmove(inc->buf, name, strlen(name) + 1);
inc->state = SSI_CALL;
do_call2(ssi, buf, len, n);
}
}
static void do_exec2(struct ssi *ssi, char *buf, int len, int *n)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
int i, ch;
for (i = 0; i < len; i++) {
if ((ch = fgetc(inc->pipe)) == EOF) {
inc->state = SSI_PASS;
(void) pclose(inc->pipe);
inc->pipe = NULL;
break;
}
*buf++ = ch;
(*n)++;
}
}
static void do_exec(struct ssi *ssi, char *buf, int len, int *n)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
char cmd[sizeof(inc->buf)], *e, *p;
p = trim_spaces(inc);
if (inc->cond == SSI_STOP) {
/* Do nothing - conditional FALSE */
} else if (*p != '"' || (e = strchr(p + 1, '"')) == NULL) {
_shttpd_elog(E_LOG, ssi->conn, "ssi: bad exec(%s)", p);
} else if (!_shttpd_url_decode(p + 1, e - p - 1, cmd, sizeof(cmd))) {
_shttpd_elog(E_LOG, ssi->conn,
"ssi: cannot url_decode: exec(%s)", p);
} else if ((inc->pipe = popen(cmd, "r")) == NULL) {
_shttpd_elog(E_LOG, ssi->conn, "ssi: popen(%s)", cmd);
} else {
inc->state = SSI_EXEC;
do_exec2(ssi, buf, len, n);
}
}
static const struct ssi_cmd {
struct vec vec;
void (*func)();
} known_ssi_commands [] = {
{{"include ", 8}, do_include },
{{"if ", 3}, do_if },
{{"elif ", 5}, do_elif },
{{"else", 4}, do_else },
{{"endif", 5}, do_endif },
{{"call ", 5}, do_call },
{{"exec ", 5}, do_exec },
{{NULL, 0}, NULL }
};
static void do_command(struct ssi *ssi, char *buf, size_t len, int *n)
{
struct ssi_inc *inc = ssi->incs + ssi->nest;
const struct ssi_cmd *cmd;
assert(len > 0);
assert(inc->nbuf <= len);
inc->state = SSI_PASS;
for (cmd = known_ssi_commands; cmd->func != NULL; cmd++)
if (inc->nbuf > (size_t) st.len + cmd->vec.len &&
!memcmp(inc->buf + st.len, cmd->vec.ptr, cmd->vec.len)) {
cmd->func(ssi, buf, len, n);
break;
}
if (cmd->func == NULL)
pass(inc, buf, n);
inc->nbuf = 0;
}
static int read_ssi(struct stream *stream, void *vbuf, size_t len)
{
struct ssi *ssi = stream->conn->ssi;
struct ssi_inc *inc = ssi->incs + ssi->nest;
char *buf = vbuf;
int ch = EOF, n = 0;
again:
if (inc->state == SSI_CALL)
do_call2(ssi, buf, len, &n);
else if (inc->state == SSI_EXEC)
do_exec2(ssi, buf, len, &n);
while (n + inc->nbuf < len && (ch = fgetc(inc->fp)) != EOF) {
switch (inc->state) {
case SSI_PASS:
if (ch == '<') {
inc->nbuf = 0;
inc->buf[inc->nbuf++] = ch;
inc->state = SSI_BUF;
} else if (inc->cond == SSI_GO) {
buf[n++] = ch;
}
break;
/*
* We are buffering whole SSI command, until closing "-->".
* That means that when do_command() is called, we can rely
* on that full command with arguments is buffered in and
* there is no need for streaming.
* Restrictions:
* 1. The command must fit in CMDBUFSIZ
* 2. HTML comments inside the command ? Not sure about this.
*/
case SSI_BUF:
if (inc->nbuf >= sizeof(inc->buf) - 1) {
pass(inc, buf + n, &n);
} else if (ch == '>' &&
!memcmp(inc->buf + inc->nbuf - 2, "--", 2)) {
do_command(ssi, buf + n, len - n, &n);
inc = ssi->incs + ssi->nest;
} else {
inc->buf[inc->nbuf++] = ch;
/* If not SSI tag, pass it */
if (inc->nbuf <= (size_t) st.len &&
memcmp(inc->buf, st.ptr, inc->nbuf) != 0)
pass(inc, buf + n, &n);
}
break;
case SSI_EXEC:
case SSI_CALL:
break;
default:
/* Never happens */
abort();
break;
}
}
if (ssi->nest > 0 && n + inc->nbuf < len && ch == EOF) {
(void) fclose(inc->fp);
inc->fp = NULL;
ssi->nest--;
inc--;
goto again;
}
return (n); // wkliang:20110530 read_stream() get 0 stop stream
}
static void close_ssi(struct stream *stream)
{
struct ssi *ssi = stream->conn->ssi;
size_t i;
MY_DEBUG("stream:%p\n", stream);
MY_DEBUG("stream->conn:%p\n", stream->conn);
MY_DEBUG("stream->conn->ssi:%p\n", stream->conn->ssi);
if (ssi) { // wkliang:20110526 - check nil condition
for (i = 0; i < NELEMS(ssi->incs); i++) {
if (ssi->incs[i].fp != NULL)
(void) fclose(ssi->incs[i].fp);
if (ssi->incs[i].pipe != NULL)
(void) pclose(ssi->incs[i].pipe);
}
free(ssi);
}
}
void _shttpd_do_ssi(struct conn *c)
{
char date[64];
struct ssi *ssi;
(void) strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S GMT",
localtime(&_shttpd_current_time));
c->loc.io.head = c->loc.headers_len = _shttpd_snprintf(c->loc.io.buf,
c->loc.io.size,
"HTTP/1.1 200 OK\r\n"
"Date: %s\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n",
date);
c->status = 200;
c->loc.io_class = &_shttpd_io_ssi;
c->loc.flags |= FLAG_R | FLAG_ALWAYS_READY; // wkliang:20110530: only GET?
if (c->method != METHOD_GET) { // wkliang:20110530 org: == METHOD_HEAD
_shttpd_stop_stream(&c->loc);
} else if ((ssi = calloc(1, sizeof(struct ssi))) == NULL) {
_shttpd_send_server_error(c, 500,
"Cannot allocate SSI descriptor");
} else {
ssi->incs[0].fp = fdopen(c->loc.chan.fd, "r");
ssi->conn = c;
c->ssi = ssi;
}
}
const struct io_class _shttpd_io_ssi = {
"ssi",
read_ssi,
NULL,
close_ssi
};
#endif /* !NO_SSI */