2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
40 static int parsefile(struct cfstate *s, FILE *in);
42 static int doinclude(struct cfstate *s, char *spec)
47 char *fbk, *dir, *fspec;
55 fspec = sprintf3("%s/%s", dirname(dir), spec);
58 if(glob(fspec, 0, NULL, &globm))
60 for(i = 0; i < globm.gl_pathc; i++) {
61 if((inc = fopen(globm.gl_pathv[i], "r")) != NULL) {
62 s->file = globm.gl_pathv[i];
63 if(parsefile(s, inc)) {
79 static int parsefile(struct cfstate *s, FILE *in)
84 int ind, indst[80], indl;
91 if(fgets(line, sizeof(line), in) == NULL) {
97 for(p = line + strlen(line) - 1; p >= line; p--) {
104 for(ind = 0, p = line; *p; p++) {
107 } else if(*p == '\t') {
108 ind = ind - (ind % 8) + 8;
113 if(!eof && (!*p || (*p == '#')))
117 if(ind > indst[indl]) {
120 s->res = tokenize("start");
128 s->res = tokenize("end");
133 while(ind < indst[indl]) {
135 s->res = tokenize("end");
139 if(ind > indst[indl]) {
140 flog(LOG_WARNING, "%s:%i: unexpected indentation level", s->file, s->lno);
148 argc = calen(w = tokenize(line));
150 /* Shouldn't happen, but... */
156 if(!strcmp(w[0], "include")) {
157 for(i = 1; i < argc; i++) {
158 if(doinclude(s, w[i])) {
168 if(!strcmp(w[0], "start") ||
169 !strcmp(w[0], "end") ||
170 !strcmp(w[0], "eof")) {
171 flog(LOG_WARNING, "%s:%i: illegal directive: %s", s->file, s->lno, w[0]);
180 static void parsefn(struct muth *mt, va_list args)
182 vavar(struct cfstate *, s);
186 s->file = sstrdup(file);
190 s->res = tokenize("eof");
197 char **getcfline(struct cfstate *s)
202 s->argc = calen(s->argv = s->res);
207 struct cfstate *mkcfparser(FILE *in, char *name)
212 s->pf = mustart(parsefn, s, in, name);
216 void freecfparser(struct cfstate *s)
224 static struct child *newchild(char *name, int type)
229 ch->name = sstrdup(name);
235 void freechild(struct child *ch)
246 void skipcfblock(struct cfstate *s)
252 if(!strcmp(w[0], "end") || !strcmp(w[0], "eof"))
257 struct child *parsechild(struct cfstate *s)
264 if(!strcmp(s->argv[0], "child")) {
267 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
271 ch = newchild(s->argv[1], CH_SOCKET);
272 } else if(!strcmp(s->argv[0], "fchild")) {
275 flog(LOG_WARNING, "%s:%i: missing name in child declaration", s->file, s->lno);
279 ch = newchild(s->argv[1], CH_FORK);
286 if(!strcmp(s->argv[0], "exec")) {
288 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", s->file, s->lno);
291 ch->argv = szmalloc(sizeof(*ch->argv) * s->argc);
292 for(i = 0; i < s->argc - 1; i++)
293 ch->argv[i] = sstrdup(s->argv[i + 1]);
294 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
297 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in child declaration", s->file, s->lno, s->argv[0]);
300 if(ch->argv == NULL) {
301 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", s->file, sl, ch->name);
308 int childhandle(struct child *ch, struct hthead *req, int fd)
310 if(ch->type == CH_SOCKET) {
312 ch->fd = stdmkchild(ch->argv);
313 if(sendreq(ch->fd, req, fd)) {
315 /* Assume that the child has crashed and restart it. */
317 ch->fd = stdmkchild(ch->argv);
318 if(!sendreq(ch->fd, req, fd))
321 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
326 } else if(ch->type == CH_FORK) {
327 if(stdforkserve(ch->argv, req, fd) < 0)