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 struct child *findchild(char *file, char *name)
359 if(!strcmp(buf, "."))
361 if((p = strrchr(buf, '/')) != NULL)
368 if((ch = getchild(cf, name)) != NULL)
375 static struct pattern *findmatch(char *file, int trydefault)
383 if((bn = strrchr(file, '/')) != NULL)
390 if(!strcmp(buf, "."))
392 if((p = strrchr(buf, '/')) != NULL)
399 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
400 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
401 if(rule->type == PAT_BASENAME) {
402 if(fnmatch(rule->pattern, bn, 0))
404 } else if(rule->type == PAT_PATHNAME) {
405 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
407 } else if(rule->type == PAT_ALL) {
408 } else if(rule->type == PAT_DEFAULT) {
423 static void forkchild(struct child *ch)
425 ch->fd = stdmkchild(ch->argv);
428 static void passreq(struct child *ch, struct hthead *req, int fd)
432 if(sendreq(ch->fd, req, fd)) {
434 /* Assume that the child has crashed and restart it. */
436 if(!sendreq(ch->fd, req, fd))
439 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
445 static void handlefile(struct hthead *req, int fd, char *path)
450 headappheader(req, "X-Ash-File", path);
451 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
452 /* XXX: Send a 500 error? 404? */
455 if((ch = findchild(path, pat->childnm)) == NULL) {
456 /* XXX: Send a 500 error. */
457 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
461 if(ch->type == CH_SOCKET) {
462 passreq(ch, req, fd);
463 } else if(ch->type == CH_FORK) {
464 stdforkserve(ch->argv, req, fd);
468 static void handledir(struct hthead *req, int fd, char *path)
472 static int checkdir(struct hthead *req, int fd, char *path)
477 static void serve(struct hthead *req, int fd)
479 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
488 if((p2 = strchr(p, '/')) == NULL) {
495 if(stat(path, &sb)) {
496 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
510 * First, check the name verbatimely:
512 buf = sprintf3("%s/%s", path, p);
513 if(!stat(buf, &sb)) {
514 if(S_ISDIR(sb.st_mode)) {
516 if(!strcmp(path, "."))
519 path = sprintf2("%s/%s", path, p);
521 if(checkdir(req, fd, path))
525 if(S_ISREG(sb.st_mode)) {
527 path = sprintf2("%s/%s", path, p);
535 * Check the file extensionlessly:
537 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
538 while((dent = readdir(dir)) != NULL) {
539 buf = sprintf3("%s/%s", path, dent->d_name);
540 if((p3 = strchr(dent->d_name, '.')) != NULL)
542 if(strcmp(dent->d_name, p))
546 if(!S_ISREG(sb.st_mode))
569 if(!strncmp(path, "./", 2))
570 memmove(path, path + 2, strlen(path + 2) + 1);
571 if(S_ISDIR(sb.st_mode)) {
572 handledir(req, fd, path);
573 } else if(S_ISREG(sb.st_mode)) {
574 handlefile(req, fd, path);
581 /* XXX: Send error report. */
586 int main(int argc, char **argv)
592 flog(LOG_ERR, "usage: dirplex DIR");
596 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
599 signal(SIGCHLD, SIG_IGN);
601 if((fd = recvreq(0, &req)) < 0) {
603 flog(LOG_ERR, "recvreq: %s", strerror(errno));