2 * Copyright (c) 2006,2007 Steven Johnson <sjohnson@sakuraindustries.com>
3 * Copyright (c) 2007 Sergey Lyubka <valenok@gmail.com>
6 * "THE BEER-WARE LICENSE" (Revision 42):
7 * Sergey Lyubka wrote this file. As long as you retain this notice you
8 * can do whatever you want with this stuff. If we meet some day, and you think
9 * this stuff is worth it, you can buy me a beer in return.
16 #define CMDBUFSIZ 512 /* SSI command buffer size */
17 #define NEST_MAX 6 /* Maximum nesting level */
23 shttpd_callback_t func;
27 int state; /* Buffering state */
28 int cond; /* Conditional state */
29 FILE *fp; /* Icluded file stream */
30 char buf[CMDBUFSIZ]; /* SSI command buffer */
31 size_t nbuf; /* Bytes in a command buffer */
32 FILE *pipe; /* #exec stream */
33 struct ssi_func func; /* #call function */
37 struct conn *conn; /* Connection we belong to */
38 int nest; /* Current nesting level */
39 struct ssi_inc incs[NEST_MAX]; /* Nested includes */
42 enum { SSI_PASS, SSI_BUF, SSI_EXEC, SSI_CALL };
43 enum { SSI_GO, SSI_STOP }; /* Conditional states */
45 static const struct vec st = {"<!--#", 5};
48 shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name,
49 shttpd_callback_t func, void *user_data)
53 if ((e = malloc(sizeof(*e))) != NULL) {
54 e->name = _shttpd_strdup(name);
56 e->user_data = user_data;
57 LL_TAIL(&ctx->ssi_funcs, &e->link);
62 _shttpd_ssi_func_destructor(struct llhead *lp)
64 struct ssi_func *e = LL_ENTRY(lp, struct ssi_func, link);
70 static const struct ssi_func *
71 find_ssi_func(struct ssi *ssi, const char *name)
76 LL_FOREACH(&ssi->conn->ctx->ssi_funcs, lp) {
77 e = LL_ENTRY(lp, struct ssi_func, link);
78 if (!strcmp(name, e->name))
86 call(struct ssi *ssi, const char *name,
87 struct shttpd_arg *arg, char *buf, int len)
89 const struct ssi_func *ssi_func;
91 (void) memset(arg, 0, sizeof(*arg));
94 * SSI function may be called with parameters. These parameters
95 * are passed as arg->in.buf, arg->in.len vector.
97 arg->in.buf = strchr(name, ' ');
98 if (arg->in.buf != NULL) {
99 *arg->in.buf++ = '\0';
100 arg->in.len = strlen(arg->in.buf);
103 if ((ssi_func = find_ssi_func(ssi, name)) != NULL) {
104 arg->priv = ssi->conn;
105 arg->user_data = ssi_func->user_data;
113 evaluate(struct ssi *ssi, const char *name)
115 struct shttpd_arg arg;
117 call(ssi, name, &arg, NULL, 0);
119 return (arg.flags & SHTTPD_SSI_EVAL_TRUE);
123 pass(struct ssi_inc *inc, void *buf, int *n)
125 if (inc->cond == SSI_GO) {
126 (void) memcpy(buf, inc->buf, inc->nbuf);
130 inc->state = SSI_PASS;
134 get_path(struct conn *conn, const char *src,
135 int src_len, char *dst, int dst_len)
137 static struct vec accepted[] = {
138 {"\"", 1}, /* Relative to webserver CWD */
139 {"file=\"", 6}, /* Relative to current URI */
140 {"virtual=\"", 9}, /* Relative to document root */
144 const char *p, *root = conn->ctx->options[OPT_ROOT];
147 for (vec = accepted; vec->len > 0; vec++)
148 if (src_len > vec->len && !memcmp(src, vec->ptr, vec->len)) {
151 if ((p = memchr(src, '"', src_len)) == NULL)
154 len = _shttpd_snprintf(dst, dst_len, "%s%c%s",
155 root, DIRSEP, conn->uri);
156 while (len > 0 && dst[len] != '/')
160 } else if (vec->len == 9) {
161 len = _shttpd_snprintf(dst, dst_len, "%s%c",
166 _shttpd_url_decode(src, p - src, dst, dst_len);
174 do_include(struct ssi *ssi)
176 struct ssi_inc *inc = ssi->incs + ssi->nest;
177 char buf[FILENAME_MAX];
180 assert(inc->nbuf >= 13);
182 if (inc->cond == SSI_STOP) {
183 /* Do nothing - conditional FALSE */
184 } else if (ssi->nest >= (int) NELEMS(ssi->incs) - 1) {
185 _shttpd_elog(E_LOG, ssi->conn,
186 "ssi: #include: maximum nested level reached");
187 } else if (!get_path(ssi->conn,
188 inc->buf + 13, inc->nbuf - 13, buf, sizeof(buf))) {
189 _shttpd_elog(E_LOG, ssi->conn, "ssi: bad #include: [%.*s]",
190 inc->nbuf, inc->buf);
191 } else if ((fp = fopen(buf, "r")) == NULL) {
192 _shttpd_elog(E_LOG, ssi->conn,
193 "ssi: fopen(%s): %s", buf, strerror(errno));
196 ssi->incs[ssi->nest].fp = fp;
197 ssi->incs[ssi->nest].nbuf = 0;
198 ssi->incs[ssi->nest].cond = SSI_GO;
203 trim_spaces(struct ssi_inc *inc)
205 char *p = inc->buf + inc->nbuf - 2;
207 /* Trim spaces from the right */
209 while (isspace(* (unsigned char *) p))
212 /* Shift pointer to the start of attributes */
213 for (p = inc->buf; !isspace(* (unsigned char *) p); p++);
214 while (*p && isspace(* (unsigned char *) p)) p++;
220 do_if(struct ssi *ssi)
222 struct ssi_inc *inc = ssi->incs + ssi->nest;
223 char *name = trim_spaces(inc);
225 inc->cond = evaluate(ssi, name) ? SSI_GO : SSI_STOP;
229 do_elif(struct ssi *ssi)
231 struct ssi_inc *inc = ssi->incs + ssi->nest;
232 char *name = trim_spaces(inc);
234 if (inc->cond == SSI_STOP && evaluate(ssi, name))
237 inc->cond = SSI_STOP;
240 do_endif(struct ssi *ssi)
242 ssi->incs[ssi->nest].cond = SSI_GO;
246 do_else(struct ssi *ssi)
248 struct ssi_inc *inc = ssi->incs + ssi->nest;
250 inc->cond = inc->cond == SSI_GO ? SSI_STOP : SSI_GO;
254 do_call2(struct ssi *ssi, char *buf, int len, int *n)
256 struct ssi_inc *inc = ssi->incs + ssi->nest;
257 struct shttpd_arg arg;
259 call(ssi, inc->buf, &arg, buf, len);
260 (*n) += arg.out.num_bytes;
261 if (arg.flags & SHTTPD_END_OF_OUTPUT)
262 inc->state = SSI_PASS;
266 do_call(struct ssi *ssi, char *buf, int len, int *n)
268 struct ssi_inc *inc = ssi->incs + ssi->nest;
269 char *name = trim_spaces(inc);
271 if (inc->cond == SSI_GO) {
272 (void) memmove(inc->buf, name, strlen(name) + 1);
273 inc->state = SSI_CALL;
274 do_call2(ssi, buf, len, n);
279 do_exec2(struct ssi *ssi, char *buf, int len, int *n)
281 struct ssi_inc *inc = ssi->incs + ssi->nest;
284 for (i = 0; i < len; i++) {
285 if ((ch = fgetc(inc->pipe)) == EOF) {
286 inc->state = SSI_PASS;
287 (void) pclose(inc->pipe);
297 do_exec(struct ssi *ssi, char *buf, int len, int *n)
299 struct ssi_inc *inc = ssi->incs + ssi->nest;
300 char cmd[sizeof(inc->buf)], *e, *p;
302 p = trim_spaces(inc);
304 if (inc->cond == SSI_STOP) {
305 /* Do nothing - conditional FALSE */
306 } else if (*p != '"' || (e = strchr(p + 1, '"')) == NULL) {
307 _shttpd_elog(E_LOG, ssi->conn, "ssi: bad exec(%s)", p);
308 } else if (!_shttpd_url_decode(p + 1, e - p - 1, cmd, sizeof(cmd))) {
309 _shttpd_elog(E_LOG, ssi->conn,
310 "ssi: cannot url_decode: exec(%s)", p);
311 } else if ((inc->pipe = popen(cmd, "r")) == NULL) {
312 _shttpd_elog(E_LOG, ssi->conn, "ssi: popen(%s)", cmd);
314 inc->state = SSI_EXEC;
315 do_exec2(ssi, buf, len, n);
319 static const struct ssi_cmd {
322 } known_ssi_commands [] = {
323 {{"include ", 8}, do_include },
324 {{"if ", 3}, do_if },
325 {{"elif ", 5}, do_elif },
326 {{"else", 4}, do_else },
327 {{"endif", 5}, do_endif },
328 {{"call ", 5}, do_call },
329 {{"exec ", 5}, do_exec },
334 do_command(struct ssi *ssi, char *buf, size_t len, int *n)
336 struct ssi_inc *inc = ssi->incs + ssi->nest;
337 const struct ssi_cmd *cmd;
340 assert(inc->nbuf <= len);
341 inc->state = SSI_PASS;
343 for (cmd = known_ssi_commands; cmd->func != NULL; cmd++)
344 if (inc->nbuf > (size_t) st.len + cmd->vec.len &&
345 !memcmp(inc->buf + st.len, cmd->vec.ptr, cmd->vec.len)) {
346 cmd->func(ssi, buf, len, n);
350 if (cmd->func == NULL)
357 read_ssi(struct stream *stream, void *vbuf, size_t len)
359 struct ssi *ssi = stream->conn->ssi;
360 struct ssi_inc *inc = ssi->incs + ssi->nest;
366 if (inc->state == SSI_CALL)
367 do_call2(ssi, buf, len, &n);
368 else if (inc->state == SSI_EXEC)
369 do_exec2(ssi, buf, len, &n);
371 while (n + inc->nbuf < len && (ch = fgetc(inc->fp)) != EOF)
373 switch (inc->state) {
378 inc->buf[inc->nbuf++] = ch;
379 inc->state = SSI_BUF;
380 } else if (inc->cond == SSI_GO) {
386 * We are buffering whole SSI command, until closing "-->".
387 * That means that when do_command() is called, we can rely
388 * on that full command with arguments is buffered in and
389 * there is no need for streaming.
391 * 1. The command must fit in CMDBUFSIZ
392 * 2. HTML comments inside the command ? Not sure about this.
395 if (inc->nbuf >= sizeof(inc->buf) - 1) {
396 pass(inc, buf + n, &n);
397 } else if (ch == '>' &&
398 !memcmp(inc->buf + inc->nbuf - 2, "--", 2)) {
399 do_command(ssi, buf + n, len - n, &n);
400 inc = ssi->incs + ssi->nest;
402 inc->buf[inc->nbuf++] = ch;
404 /* If not SSI tag, pass it */
405 if (inc->nbuf <= (size_t) st.len &&
406 memcmp(inc->buf, st.ptr, inc->nbuf) != 0)
407 pass(inc, buf + n, &n);
421 if (ssi->nest > 0 && n + inc->nbuf < len && ch == EOF) {
422 (void) fclose(inc->fp);
433 close_ssi(struct stream *stream)
435 struct ssi *ssi = stream->conn->ssi;
438 for (i = 0; i < NELEMS(ssi->incs); i++) {
439 if (ssi->incs[i].fp != NULL)
440 (void) fclose(ssi->incs[i].fp);
441 if (ssi->incs[i].pipe != NULL)
442 (void) pclose(ssi->incs[i].pipe);
449 _shttpd_do_ssi(struct conn *c)
454 (void) strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S GMT",
455 localtime(&_shttpd_current_time));
457 c->loc.io.head = c->loc.headers_len = _shttpd_snprintf(c->loc.io.buf,
459 "HTTP/1.1 200 OK\r\n"
461 "Content-Type: text/html\r\n"
462 "Connection: close\r\n\r\n",
466 c->loc.io_class = &_shttpd_io_ssi;
467 c->loc.flags |= FLAG_R | FLAG_ALWAYS_READY;
469 if (c->method == METHOD_HEAD) {
470 _shttpd_stop_stream(&c->loc);
471 } else if ((ssi = calloc(1, sizeof(struct ssi))) == NULL) {
472 _shttpd_send_server_error(c, 500,
473 "Cannot allocate SSI descriptor");
475 ssi->incs[0].fp = fdopen(c->loc.chan.fd, "r");
481 const struct io_class _shttpd_io_ssi = {