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/>.
41 #define PAT_BASENAME 0
42 #define PAT_PATHNAME 1
47 struct config *next, *prev;
50 struct child *children;
51 struct pattern *patterns;
73 struct config *cflist;
75 static void freechild(struct child *ch)
86 static void freepattern(struct pattern *pat)
90 for(rule = pat->rules; *rule; rule++) {
91 if((*rule)->pattern != NULL)
92 free((*rule)->pattern);
95 if(pat->childnm != NULL)
100 static void freeconfig(struct config *cf)
102 struct child *ch, *nch;
103 struct pattern *pat, *npat;
106 cf->prev->next = cf->next;
108 cf->next->prev = cf->prev;
112 for(ch = cf->children; ch != NULL; ch = nch) {
116 for(pat = cf->patterns; pat != NULL; pat = npat) {
123 static struct child *newchild(char *name, int type)
128 ch->name = sstrdup(name);
134 static struct child *getchild(struct config *cf, char *name)
138 for(ch = cf->children; ch; ch = ch->next) {
139 if(!strcmp(ch->name, name))
145 static struct rule *newrule(struct pattern *pat)
150 for(i = 0; pat->rules[i]; i++);
151 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
152 rule = pat->rules[i] = smalloc(sizeof(*rule));
153 pat->rules[i + 1] = NULL;
157 static struct pattern *newpattern(void)
162 pat->rules = szmalloc(sizeof(*pat->rules));
166 static struct config *readconfig(char *path)
185 if((s = fopen(sprintf3("%s/.htrc", path), "r")) == NULL)
188 cf->mtime = sb.st_mtime;
189 cf->path = sstrdup(path);
195 if(fgets(line, sizeof(line), s) == NULL) {
200 for(p = line; *p; p++) {
206 ind = isspace(line[0]);
213 flog(LOG_WARNING, "%s%i: unexpected line indentation in global scope", path, lno);
217 } else if(!strcmp(w[0], "child")) {
219 flog(LOG_WARNING, "%s:%i: missing name in child declaration", path, lno);
222 child = newchild(w[1], CH_SOCKET);
224 } else if(!strcmp(w[0], "fchild")) {
226 flog(LOG_WARNING, "%s:%i: missing name in child declaration", path, lno);
229 child = newchild(w[1], CH_FORK);
231 } else if(!strcmp(w[0], "match")) {
235 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
238 } else if(state == 1) {
241 } else if(!strcmp(w[0], "exec")) {
243 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", path, lno);
246 child->argv = szmalloc(sizeof(*child->argv) * argc);
247 for(i = 0; i < argc - 1; i++)
248 child->argv[i] = sstrdup(w[i + 1]);
250 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
254 if(child->argv == NULL) {
255 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", path, lno, child->name);
259 child->next = cf->children;
260 cf->children = child;
263 } else if(state == 2) {
266 } else if(!strcmp(w[0], "filename")) {
268 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", path, lno);
272 rule->type = PAT_BASENAME;
273 rule->pattern = sstrdup(w[1]);
274 } else if(!strcmp(w[0], "pathname")) {
276 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", path, lno);
280 rule->type = PAT_PATHNAME;
281 rule->pattern = sstrdup(w[1]);
282 } else if(!strcmp(w[0], "all")) {
283 newrule(pat)->type = PAT_ALL;
284 } else if(!strcmp(w[0], "default")) {
285 newrule(pat)->type = PAT_DEFAULT;
286 } else if(!strcmp(w[0], "handler")) {
288 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", path, lno);
291 if(pat->childnm != NULL)
293 pat->childnm = sstrdup(w[1]);
295 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
299 if(pat->rules[0] == NULL) {
300 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", path, lno);
304 if(pat->childnm == NULL) {
305 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", path, lno);
309 pat->next = cf->patterns;
327 static struct config *getconfig(char *path)
332 for(cf = cflist; cf != NULL; cf = cf->next) {
333 if(!strcmp(cf->path, path)) {
336 if(sb.st_mtime != cf->mtime) {
343 if((cf = readconfig(path)) != NULL) {
350 static char *findfile(struct hthead *req, char *nm)
352 char *p, *p2, *path, *ftmp, *buf, *rv, *p3;
361 if((p2 = strchr(p, '/')) == NULL) {
380 * First, check the name verbatimely:
382 buf = sprintf3("%s/%s", path, p);
383 if(!stat(buf, &sb)) {
384 if(S_ISDIR(sb.st_mode)) {
386 if(!strcmp(path, "."))
389 path = sprintf2("%s/%s", path, p);
393 if(S_ISREG(sb.st_mode)) {
401 * Check the file extensionlessly:
403 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
404 while((dent = readdir(dir)) != NULL) {
405 buf = sprintf3("%s/%s", path, dent->d_name);
406 if((p3 = strchr(dent->d_name, '.')) != NULL)
408 if(strcmp(dent->d_name, p))
412 if(!S_ISREG(sb.st_mode))
429 if(!strncmp(rv, "./", 2))
430 memmove(rv, rv + 2, strlen(rv + 2) + 1);
440 static struct child *findchild(char *file, char *name)
449 if(!strcmp(buf, "."))
451 if((p = strrchr(buf, '/')) != NULL)
458 if((ch = getchild(cf, name)) != NULL)
465 static struct pattern *findmatch(char *file, int trydefault)
473 if((bn = strrchr(file, '/')) != NULL)
480 if(!strcmp(buf, "."))
482 if((p = strrchr(buf, '/')) != NULL)
489 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
490 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
491 if(rule->type == PAT_BASENAME) {
492 if(fnmatch(rule->pattern, bn, 0))
494 } else if(rule->type == PAT_PATHNAME) {
495 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
497 } else if(rule->type == PAT_ALL) {
498 } else if(rule->type == PAT_DEFAULT) {
513 static void forkchild(struct child *ch)
515 ch->fd = stdmkchild(ch->argv);
518 static void passreq(struct child *ch, struct hthead *req, int fd)
522 if(sendreq(ch->fd, req, fd)) {
524 /* Assume that the child has crashed and restart it. */
526 if(!sendreq(ch->fd, req, fd))
529 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
535 static void serve(struct hthead *req, int fd)
542 if((file = findfile(req, req->rest)) == NULL) {
543 /* XXX: Do 404 handling */
547 if(stat(file, &sb)) {
548 flog(LOG_ERR, "could not stat previously found file %s: %s", file, strerror(errno));
552 if(!S_ISREG(sb.st_mode)) {
553 /* XXX: Handle default files or similar stuff. */
557 if(((pat = findmatch(file, 0)) == NULL) && ((pat = findmatch(file, 1)) == NULL)) {
558 /* XXX: Send a 500 error? 404? */
562 if((ch = findchild(file, pat->childnm)) == NULL) {
563 /* XXX: Send a 500 error. */
564 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
569 if(ch->type == CH_SOCKET) {
570 passreq(ch, req, fd);
571 } else if(ch->type == CH_FORK) {
572 stdforkserve(ch->argv, req, fd);
578 int main(int argc, char **argv)
584 flog(LOG_ERR, "usage: dirplex DIR");
588 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
591 signal(SIGCHLD, SIG_IGN);
593 if((fd = recvreq(0, &req)) < 0) {
595 flog(LOG_ERR, "recvreq: %s", strerror(errno));