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;
66 static struct config *cflist;
67 static struct config *gconfig, *lconfig;
70 static void freerule(struct rule *rule)
72 freeca(rule->patterns);
76 static void freepattern(struct pattern *pat)
80 for(rule = pat->rules; *rule; rule++)
82 if(pat->childnm != NULL)
88 static void freeconfig(struct config *cf)
90 struct child *ch, *nch;
91 struct pattern *pat, *npat;
94 cf->prev->next = cf->next;
96 cf->next->prev = cf->prev;
101 for(ch = cf->children; ch != NULL; ch = nch) {
105 for(pat = cf->patterns; pat != NULL; pat = npat) {
112 static struct child *getchild(struct config *cf, char *name)
116 for(ch = cf->children; ch; ch = ch->next) {
117 if(!strcmp(ch->name, name))
123 static struct rule *newrule(struct pattern *pat)
128 for(i = 0; pat->rules[i]; i++);
129 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
130 rule = pat->rules[i] = szmalloc(sizeof(*rule));
131 pat->rules[i + 1] = NULL;
135 static struct pattern *newpattern(void)
140 pat->rules = szmalloc(sizeof(*pat->rules));
144 static char **cadup(char **w)
150 ret = smalloc(sizeof(*ret) * (l + 1));
151 for(i = 0; i < l; i++)
152 ret[i] = sstrdup(w[i]);
157 static struct pattern *parsepattern(struct cfstate *s)
163 if(!strcmp(s->argv[0], "match")) {
173 if(!strcmp(s->argv[0], "filename")) {
175 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
179 rule->type = PAT_BASENAME;
180 rule->patterns = cadup(s->argv + 1);
181 } else if(!strcmp(s->argv[0], "pathname")) {
183 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
187 rule->type = PAT_PATHNAME;
188 rule->patterns = cadup(s->argv + 1);
189 } else if(!strcmp(s->argv[0], "all")) {
190 newrule(pat)->type = PAT_ALL;
191 } else if(!strcmp(s->argv[0], "default")) {
192 newrule(pat)->type = PAT_DEFAULT;
193 } else if(!strcmp(s->argv[0], "handler")) {
195 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
198 if(pat->childnm != NULL)
200 pat->childnm = sstrdup(s->argv[1]);
201 } else if(!strcmp(s->argv[0], "fork")) {
202 pat->fchild = cadup(s->argv + 1);
203 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
206 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
210 if(pat->rules[0] == NULL) {
211 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
215 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
216 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
223 static struct config *emptyconfig(void)
231 static struct config *readconfig(char *file)
239 if((in = fopen(file, "r")) == NULL) {
240 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
243 s = mkcfparser(in, file);
248 if((child = parsechild(s)) != NULL) {
249 child->next = cf->children;
250 cf->children = child;
251 } else if((pat = parsepattern(s)) != NULL) {
252 pat->next = cf->patterns;
254 } else if(!strcmp(s->argv[0], "eof")) {
257 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
266 static struct config *getconfig(char *path)
273 fn = sprintf3("%s/.htrc", path);
274 for(cf = cflist; cf != NULL; cf = cf->next) {
275 if(!strcmp(cf->path, path)) {
276 if(now - cf->lastck > 5) {
277 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
286 if(access(fn, R_OK) || stat(fn, &sb)) {
290 if((cf = readconfig(fn)) == NULL)
294 cf->path = sstrdup(path);
302 static struct config **getconfigs(char *file)
304 static struct config **ret = NULL;
317 if((p = strrchr(tmp, '/')) == NULL)
320 if((cf = getconfig(tmp)) != NULL)
324 if((cf = getconfig(".")) != NULL)
327 bufadd(buf, lconfig);
329 bufadd(buf, gconfig);
334 static struct child *findchild(char *file, char *name)
340 cfs = getconfigs(file);
341 for(i = 0; cfs[i] != NULL; i++) {
342 if((ch = getchild(cfs[i], name)) != NULL)
348 static struct pattern *findmatch(char *file, int trydefault)
356 if((bn = strrchr(file, '/')) != NULL)
360 cfs = getconfigs(file);
361 for(c = 0; cfs[c] != NULL; c++) {
362 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
363 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
364 if(rule->type == PAT_BASENAME) {
365 for(o = 0; rule->patterns[o] != NULL; o++) {
366 if(!fnmatch(rule->patterns[o], bn, 0))
369 if(rule->patterns[o] == NULL)
371 } else if(rule->type == PAT_PATHNAME) {
372 for(o = 0; rule->patterns[o] != NULL; o++) {
373 if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
376 if(rule->patterns[o] == NULL)
378 } else if(rule->type == PAT_ALL) {
379 } else if(rule->type == PAT_DEFAULT) {
391 static void handlefile(struct hthead *req, int fd, char *path)
396 headappheader(req, "X-Ash-File", path);
397 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
398 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
402 stdforkserve(pat->fchild, req, fd);
404 if((ch = findchild(path, pat->childnm)) == NULL) {
405 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
406 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
409 if(childhandle(ch, req, fd))
410 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
414 static void handledir(struct hthead *req, int fd, char *path)
417 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet.");
420 static int checkdir(struct hthead *req, int fd, char *path)
425 static void serve(struct hthead *req, int fd)
427 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
437 if((p2 = strchr(p, '/')) == NULL) {
444 if(stat(path, &sb)) {
445 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
446 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
451 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
456 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
463 * First, check the name verbatimely:
465 buf = sprintf3("%s/%s", path, p);
466 if(!stat(buf, &sb)) {
467 if(S_ISDIR(sb.st_mode)) {
469 if(!strcmp(path, "."))
472 path = sprintf2("%s/%s", path, p);
475 stdredir(req, fd, 301, sprintf3("%s/", p));
478 if(checkdir(req, fd, path))
482 if(S_ISREG(sb.st_mode)) {
484 path = sprintf2("%s/%s", path, p);
488 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
493 * Check the file extensionlessly:
495 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
496 while((dent = readdir(dir)) != NULL) {
497 buf = sprintf3("%s/%s", path, dent->d_name);
498 if((p3 = strchr(dent->d_name, '.')) != NULL)
500 if(strcmp(dent->d_name, p))
504 if(!S_ISREG(sb.st_mode))
516 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
528 if(!strncmp(path, "./", 2))
529 memmove(path, path + 2, strlen(path + 2) + 1);
530 if(S_ISDIR(sb.st_mode)) {
531 handledir(req, fd, path);
532 } else if(S_ISREG(sb.st_mode)) {
533 handlefile(req, fd, path);
535 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
541 /* No special handling, for now at least. */
546 static void usage(FILE *out)
548 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
551 int main(int argc, char **argv)
561 while((c = getopt(argc, argv, "hNc:")) >= 0) {
577 if(argc - optind < 1) {
582 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
583 gconfig = readconfig(gcf);
588 if((lconfig = readconfig(lcf)) == NULL)
591 if(chdir(argv[optind])) {
592 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
595 signal(SIGCHLD, SIG_IGN);
597 if((fd = recvreq(0, &req)) < 0) {
599 flog(LOG_ERR, "recvreq: %s", strerror(errno));