X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdirplex.c;h=f4cc6121cae2670d3b23a4d07740cf39e7ccb934;hb=1eba4f97b3f976614da9d820b2130c0150c38910;hp=c612c152ba63de5c4ed1e1e3bb8c881b1e18febc;hpb=d1b065b5e557cafab691a5da47b503e45f86a314;p=ashd.git diff --git a/src/dirplex.c b/src/dirplex.c index c612c15..f4cc612 100644 --- a/src/dirplex.c +++ b/src/dirplex.c @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef HAVE_CONFIG_H #include @@ -45,35 +46,42 @@ struct config { struct config *next, *prev; char *path; - time_t mtime; + time_t mtime, lastck; struct child *children; struct pattern *patterns; }; struct rule { int type; - char *pattern; + char **patterns; }; struct pattern { struct pattern *next; char *childnm; + char **fchild; struct rule **rules; }; -struct config *cflist; +static struct config *cflist; +static struct config *gconfig, *lconfig; +static time_t now; + +static void freerule(struct rule *rule) +{ + freeca(rule->patterns); + free(rule); +} static void freepattern(struct pattern *pat) { struct rule **rule; - for(rule = pat->rules; *rule; rule++) { - if((*rule)->pattern != NULL) - free((*rule)->pattern); - free(*rule); - } + for(rule = pat->rules; *rule; rule++) + freerule(*rule); if(pat->childnm != NULL) free(pat->childnm); + freeca(pat->fchild); free(pat); } @@ -88,7 +96,8 @@ static void freeconfig(struct config *cf) cf->next->prev = cf->prev; if(cf == cflist) cflist = cf->next; - free(cf->path); + if(cf->path != NULL) + free(cf->path); for(ch = cf->children; ch != NULL; ch = nch) { nch = ch->next; freechild(ch); @@ -132,6 +141,19 @@ static struct pattern *newpattern(void) return(pat); } +static char **cadup(char **w) +{ + char **ret; + int i, l; + + l = calen(w); + ret = smalloc(sizeof(*ret) * (l + 1)); + for(i = 0; i < l; i++) + ret[i] = sstrdup(w[i]); + ret[i] = NULL; + return(ret); +} + static struct pattern *parsepattern(struct cfstate *s) { struct pattern *pat; @@ -155,7 +177,7 @@ static struct pattern *parsepattern(struct cfstate *s) } rule = newrule(pat); rule->type = PAT_BASENAME; - rule->pattern = sstrdup(s->argv[1]); + rule->patterns = cadup(s->argv + 1); } else if(!strcmp(s->argv[0], "pathname")) { if(s->argc < 2) { flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno); @@ -163,7 +185,7 @@ static struct pattern *parsepattern(struct cfstate *s) } rule = newrule(pat); rule->type = PAT_PATHNAME; - rule->pattern = sstrdup(s->argv[1]); + rule->patterns = cadup(s->argv + 1); } else if(!strcmp(s->argv[0], "all")) { newrule(pat)->type = PAT_ALL; } else if(!strcmp(s->argv[0], "default")) { @@ -176,6 +198,8 @@ static struct pattern *parsepattern(struct cfstate *s) if(pat->childnm != NULL) free(pat->childnm); pat->childnm = sstrdup(s->argv[1]); + } else if(!strcmp(s->argv[0], "fork")) { + pat->fchild = cadup(s->argv + 1); } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) { break; } else { @@ -188,7 +212,7 @@ static struct pattern *parsepattern(struct cfstate *s) freepattern(pat); return(NULL); } - if(pat->childnm == NULL) { + if((pat->childnm == NULL) && (pat->fchild == NULL)) { flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl); freepattern(pat); return(NULL); @@ -196,27 +220,28 @@ static struct pattern *parsepattern(struct cfstate *s) return(pat); } -static struct config *readconfig(char *path) +static struct config *emptyconfig(void) +{ + struct config *cf; + + omalloc(cf); + return(cf); +} + +static struct config *readconfig(char *file) { struct cfstate *s; FILE *in; struct config *cf; struct child *child; struct pattern *pat; - struct stat sb; - char *p; - p = sprintf3("%s/.htrc", path); - if(stat(p, &sb)) - return(NULL); - if((in = fopen(p, "r")) == NULL) { - flog(LOG_WARNING, "%s: %s", p, strerror(errno)); + if((in = fopen(file, "r")) == NULL) { + flog(LOG_WARNING, "%s: %s", file, strerror(errno)); return(NULL); } - s = mkcfparser(in, p); - omalloc(cf); - cf->mtime = sb.st_mtime; - cf->path = sstrdup(path); + s = mkcfparser(in, file); + cf = emptyconfig(); while(1) { getcfline(s); @@ -242,55 +267,89 @@ static struct config *getconfig(char *path) { struct config *cf; struct stat sb; + char *fn; + time_t mtime; + fn = sprintf3("%s/.htrc", path); for(cf = cflist; cf != NULL; cf = cf->next) { if(!strcmp(cf->path, path)) { - if(stat(sprintf3("%s/.htrc", path), &sb)) - return(NULL); - if(sb.st_mtime != cf->mtime) { - freeconfig(cf); - break; + if(now - cf->lastck > 5) { + if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) { + freeconfig(cf); + break; + } } + cf->lastck = now; return(cf); } } - if((cf = readconfig(path)) != NULL) { - cf->next = cflist; - cflist = cf; + if(access(fn, R_OK) || stat(fn, &sb)) { + cf = emptyconfig(); + mtime = 0; + } else { + if((cf = readconfig(fn)) == NULL) + return(NULL); + mtime = sb.st_mtime; } + cf->path = sstrdup(path); + cf->mtime = mtime; + cf->lastck = now; + cf->next = cflist; + cflist = cf; return(cf); } -static struct child *findchild(char *file, char *name) +static struct config **getconfigs(char *file) { - char *buf, *p; + static struct config **ret = NULL; + struct { + struct config **b; + size_t s, d; + } buf; struct config *cf; - struct child *ch; - - buf = sstrdup(file); + char *tmp, *p; + + if(ret != NULL) + free(ret); + bufinit(buf); + tmp = sstrdup(file); while(1) { - ch = NULL; - if(!strcmp(buf, ".")) + if((p = strrchr(tmp, '/')) == NULL) break; - if((p = strrchr(buf, '/')) != NULL) - *p = 0; - else - strcpy(buf, "."); - cf = getconfig(buf); - if(cf == NULL) - continue; - if((ch = getchild(cf, name)) != NULL) + *p = 0; + if((cf = getconfig(tmp)) != NULL) + bufadd(buf, cf); + } + free(tmp); + if((cf = getconfig(".")) != NULL) + bufadd(buf, cf); + if(lconfig != NULL) + bufadd(buf, lconfig); + if(gconfig != NULL) + bufadd(buf, gconfig); + bufadd(buf, NULL); + return(ret = buf.b); +} + +static struct child *findchild(char *file, char *name) +{ + int i; + struct config **cfs; + struct child *ch; + + cfs = getconfigs(file); + for(i = 0; cfs[i] != NULL; i++) { + if((ch = getchild(cfs[i], name)) != NULL) break; } - free(buf); return(ch); } static struct pattern *findmatch(char *file, int trydefault) { - int i; - char *buf, *p, *bn; - struct config *cf; + int i, o, c; + char *bn; + struct config **cfs; struct pattern *pat; struct rule *rule; @@ -298,25 +357,23 @@ static struct pattern *findmatch(char *file, int trydefault) bn++; else bn = file; - buf = sstrdup(file); - while(1) { - pat = NULL; - if(!strcmp(buf, ".")) - break; - if((p = strrchr(buf, '/')) != NULL) - *p = 0; - else - strcpy(buf, "."); - cf = getconfig(buf); - if(cf == NULL) - continue; - for(pat = cf->patterns; pat != NULL; pat = pat->next) { + cfs = getconfigs(file); + for(c = 0; cfs[c] != NULL; c++) { + for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) { for(i = 0; (rule = pat->rules[i]) != NULL; i++) { if(rule->type == PAT_BASENAME) { - if(fnmatch(rule->pattern, bn, 0)) + for(o = 0; rule->patterns[o] != NULL; o++) { + if(!fnmatch(rule->patterns[o], bn, 0)) + break; + } + if(rule->patterns[o] == NULL) break; } else if(rule->type == PAT_PATHNAME) { - if(fnmatch(rule->pattern, file, FNM_PATHNAME)) + for(o = 0; rule->patterns[o] != NULL; o++) { + if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME)) + break; + } + if(rule->patterns[o] == NULL) break; } else if(rule->type == PAT_ALL) { } else if(rule->type == PAT_DEFAULT) { @@ -325,13 +382,10 @@ static struct pattern *findmatch(char *file, int trydefault) } } if(!rule) - goto out; + return(pat); } } - -out: - free(buf); - return(pat); + return(NULL); } static void handlefile(struct hthead *req, int fd, char *path) @@ -341,17 +395,20 @@ static void handlefile(struct hthead *req, int fd, char *path) headappheader(req, "X-Ash-File", path); if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) { - /* XXX: Send a 500 error? 404? */ + simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); return; } - if((ch = findchild(path, pat->childnm)) == NULL) { - flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm); - simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm); - return; + if(pat->fchild) { + stdforkserve(pat->fchild, req, fd); + } else { + if((ch = findchild(path, pat->childnm)) == NULL) { + flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm); + simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm); + return; + } + if(childhandle(ch, req, fd)) + simpleerror(fd, 500, "Server Error", "The request handler crashed."); } - - if(childhandle(ch, req, fd)) - simpleerror(fd, 500, "Server Error", "The request handler crashed."); } static void handledir(struct hthead *req, int fd, char *path) @@ -372,6 +429,7 @@ static void serve(struct hthead *req, int fd) DIR *dir; struct dirent *dent; + now = time(NULL); nm = req->rest; path = sstrdup("."); p = nm; @@ -413,6 +471,10 @@ static void serve(struct hthead *req, int fd) else path = sprintf2("%s/%s", path, p); free(tmp); + if(p2 == NULL) { + stdredir(req, fd, 301, sprintf3("%s/", p)); + goto out; + } if(checkdir(req, fd, path)) break; goto next; @@ -481,17 +543,53 @@ out: free(path); } +static void usage(FILE *out) +{ + fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n"); +} + int main(int argc, char **argv) { + int c; + int nodef; + char *gcf, *lcf; struct hthead *req; int fd; - if(argc < 2) { - flog(LOG_ERR, "usage: dirplex DIR"); + nodef = 0; + lcf = NULL; + while((c = getopt(argc, argv, "hNc:")) >= 0) { + switch(c) { + case 'h': + usage(stdout); + exit(0); + case 'N': + nodef = 1; + break; + case 'c': + lcf = optarg; + break; + default: + usage(stderr); + exit(1); + } + } + if(argc - optind < 1) { + usage(stderr); exit(1); } - if(chdir(argv[1])) { - flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno)); + if(!nodef) { + if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) { + gconfig = readconfig(gcf); + free(gcf); + } + } + if(lcf != NULL) { + if((lconfig = readconfig(lcf)) == NULL) + exit(1); + } + if(chdir(argv[optind])) { + flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno)); exit(1); } signal(SIGCHLD, SIG_IGN);