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;
67 static struct config *cflist;
68 static struct config *gconfig, *lconfig;
71 static void freerule(struct rule *rule)
73 freeca(rule->patterns);
77 static void freepattern(struct pattern *pat)
81 for(rule = pat->rules; *rule; rule++)
83 if(pat->childnm != NULL)
89 static void freeconfig(struct config *cf)
91 struct child *ch, *nch;
92 struct pattern *pat, *npat;
95 cf->prev->next = cf->next;
97 cf->next->prev = cf->prev;
102 for(ch = cf->children; ch != NULL; ch = nch) {
106 for(pat = cf->patterns; pat != NULL; pat = npat) {
114 static struct child *getchild(struct config *cf, char *name)
118 for(ch = cf->children; ch; ch = ch->next) {
119 if(!strcmp(ch->name, name))
125 static struct rule *newrule(struct pattern *pat)
130 for(i = 0; pat->rules[i]; i++);
131 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
132 rule = pat->rules[i] = szmalloc(sizeof(*rule));
133 pat->rules[i + 1] = NULL;
137 static struct pattern *newpattern(void)
142 pat->rules = szmalloc(sizeof(*pat->rules));
146 static char **cadup(char **w)
152 ret = smalloc(sizeof(*ret) * (l + 1));
153 for(i = 0; i < l; i++)
154 ret[i] = sstrdup(w[i]);
159 static struct pattern *parsepattern(struct cfstate *s)
165 if(!strcmp(s->argv[0], "match")) {
175 if(!strcmp(s->argv[0], "filename")) {
177 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
181 rule->type = PAT_BASENAME;
182 rule->patterns = cadup(s->argv + 1);
183 } else if(!strcmp(s->argv[0], "pathname")) {
185 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
189 rule->type = PAT_PATHNAME;
190 rule->patterns = cadup(s->argv + 1);
191 } else if(!strcmp(s->argv[0], "all")) {
192 newrule(pat)->type = PAT_ALL;
193 } else if(!strcmp(s->argv[0], "default")) {
194 newrule(pat)->type = PAT_DEFAULT;
195 } else if(!strcmp(s->argv[0], "handler")) {
197 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
200 if(pat->childnm != NULL)
202 pat->childnm = sstrdup(s->argv[1]);
203 } else if(!strcmp(s->argv[0], "fork")) {
204 pat->fchild = cadup(s->argv + 1);
205 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
208 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
212 if(pat->rules[0] == NULL) {
213 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
217 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
218 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
225 static struct config *emptyconfig(void)
233 static struct config *readconfig(char *file)
241 if((in = fopen(file, "r")) == NULL) {
242 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
245 s = mkcfparser(in, file);
250 if((child = parsechild(s)) != NULL) {
251 child->next = cf->children;
252 cf->children = child;
253 } else if((pat = parsepattern(s)) != NULL) {
254 pat->next = cf->patterns;
256 } else if(!strcmp(s->argv[0], "index-file")) {
260 cf->index = cadup(s->argv + 1);
261 } else if(!strcmp(s->argv[0], "eof")) {
264 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
273 static struct config *getconfig(char *path)
280 fn = sprintf3("%s/.htrc", path);
281 for(cf = cflist; cf != NULL; cf = cf->next) {
282 if(!strcmp(cf->path, path)) {
283 if(now - cf->lastck > 5) {
284 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
293 if(access(fn, R_OK) || stat(fn, &sb)) {
297 if((cf = readconfig(fn)) == NULL)
301 cf->path = sstrdup(path);
312 static struct config **getconfigs(char *file)
314 static struct config **ret = NULL;
327 if((p = strrchr(tmp, '/')) == NULL)
330 if((cf = getconfig(tmp)) != NULL)
334 if((cf = getconfig(".")) != NULL)
337 bufadd(buf, lconfig);
339 bufadd(buf, gconfig);
344 static struct child *findchild(char *file, char *name)
350 cfs = getconfigs(file);
351 for(i = 0; cfs[i] != NULL; i++) {
352 if((ch = getchild(cfs[i], name)) != NULL)
358 static struct pattern *findmatch(char *file, int trydefault)
366 if((bn = strrchr(file, '/')) != NULL)
370 cfs = getconfigs(file);
371 for(c = 0; cfs[c] != NULL; c++) {
372 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
373 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
374 if(rule->type == PAT_BASENAME) {
375 for(o = 0; rule->patterns[o] != NULL; o++) {
376 if(!fnmatch(rule->patterns[o], bn, 0))
379 if(rule->patterns[o] == NULL)
381 } else if(rule->type == PAT_PATHNAME) {
382 for(o = 0; rule->patterns[o] != NULL; o++) {
383 if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
386 if(rule->patterns[o] == NULL)
388 } else if(rule->type == PAT_ALL) {
389 } else if(rule->type == PAT_DEFAULT) {
401 static void handlefile(struct hthead *req, int fd, char *path)
406 headappheader(req, "X-Ash-File", path);
407 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
408 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
412 stdforkserve(pat->fchild, req, fd);
414 if((ch = findchild(path, pat->childnm)) == NULL) {
415 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
416 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
419 if(childhandle(ch, req, fd))
420 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
424 static void handledir(struct hthead *req, int fd, char *path)
429 char *inm, *ipath, *p;
433 cfs = getconfigs(sprintf3("%s/", path));
434 for(i = 0; cfs[i] != NULL; i++) {
435 if(cfs[i]->index != NULL) {
436 for(o = 0; cfs[i]->index[o] != NULL; o++) {
437 inm = cfs[i]->index[o];
438 ipath = sprintf2("%s/%s", path, inm);
439 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
440 handlefile(req, fd, ipath);
447 if(!strchr(inm, '.') && ((dir = opendir(path)) != NULL)) {
448 while((dent = readdir(dir)) != NULL) {
449 if((p = strchr(dent->d_name, '.')) == NULL)
451 if(strncmp(dent->d_name, inm, strlen(inm)))
453 ipath = sprintf2("%s/%s", path, dent->d_name);
454 if(stat(ipath, &sb) || !S_ISREG(sb.st_mode)) {
464 handlefile(req, fd, ipath);
472 /* XXX: Directory listings */
473 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
476 static int checkdir(struct hthead *req, int fd, char *path)
481 static void serve(struct hthead *req, int fd)
483 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
493 if((p2 = strchr(p, '/')) == NULL) {
497 if((tmp = unquoteurl(p)) == NULL) {
498 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
506 if(stat(path, &sb)) {
507 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
508 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
513 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
518 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
525 * First, check the name verbatimely:
527 buf = sprintf3("%s/%s", path, p);
528 if(!stat(buf, &sb)) {
529 if(S_ISDIR(sb.st_mode)) {
531 if(!strcmp(path, "."))
534 path = sprintf2("%s/%s", path, p);
537 stdredir(req, fd, 301, sprintf3("%s/", p));
540 if(checkdir(req, fd, path))
544 if(S_ISREG(sb.st_mode)) {
546 path = sprintf2("%s/%s", path, p);
550 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
555 * Check the file extensionlessly:
557 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
558 while((dent = readdir(dir)) != NULL) {
559 buf = sprintf3("%s/%s", path, dent->d_name);
560 if((p3 = strchr(dent->d_name, '.')) != NULL)
562 if(strcmp(dent->d_name, p))
566 if(!S_ISREG(sb.st_mode))
578 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
590 if(!strncmp(path, "./", 2))
591 memmove(path, path + 2, strlen(path + 2) + 1);
592 if(S_ISDIR(sb.st_mode)) {
593 handledir(req, fd, path);
594 } else if(S_ISREG(sb.st_mode)) {
595 handlefile(req, fd, path);
597 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
603 /* No special handling, for now at least. */
608 static void usage(FILE *out)
610 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
613 int main(int argc, char **argv)
623 while((c = getopt(argc, argv, "hNc:")) >= 0) {
639 if(argc - optind < 1) {
644 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
645 gconfig = readconfig(gcf);
650 if((lconfig = readconfig(lcf)) == NULL)
653 if(chdir(argv[optind])) {
654 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
657 signal(SIGCHLD, SIG_IGN);
659 if((fd = recvreq(0, &req)) < 0) {
661 flog(LOG_ERR, "recvreq: %s", strerror(errno));