X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fpatplex.c;h=b03b626614c9c79f3440e5da00042ec885d45bd2;hb=acc2d159e6f946ed6abc7c0e843a483d6478bee3;hp=26c6bd3fd22d2461344e1702549e83867733c5b8;hpb=06c1a7183754349e29a6f4656d88d3f89e4f448a;p=ashd.git diff --git a/src/patplex.c b/src/patplex.c index 26c6bd3..b03b626 100644 --- a/src/patplex.c +++ b/src/patplex.c @@ -24,6 +24,7 @@ #include #include #include +#include #ifdef HAVE_CONFIG_H #include @@ -56,18 +57,26 @@ struct rule { regex_t *pattern; }; +struct headmod { + struct headmod *next; + char *name, *value; +}; + struct pattern { struct pattern *next; + struct headmod *headers; char *childnm; struct rule **rules; char *restpat; }; -static struct config *config; +static struct config *gconfig, *lconfig; +static volatile int reload = 0; static void freepattern(struct pattern *pat) { struct rule **rule; + struct headmod *head; for(rule = pat->rules; *rule; rule++) { if((*rule)->header != NULL) @@ -78,11 +87,33 @@ static void freepattern(struct pattern *pat) } free(*rule); } + while((head = pat->headers) != NULL) { + pat->headers = head->next; + free(head->name); + free(head->value); + free(head); + } if(pat->childnm != NULL) free(pat->childnm); free(pat); } +static void freeconfig(struct config *cf) +{ + struct child *ch, *nch; + struct pattern *pat, *npat; + + for(ch = cf->children; ch != NULL; ch = nch) { + nch = ch->next; + freechild(ch); + } + for(pat = cf->patterns; pat != NULL; pat = npat) { + npat = pat->next; + freepattern(pat); + } + free(cf); +} + static struct child *getchild(struct config *cf, char *name) { struct child *ch; @@ -136,6 +167,7 @@ static struct pattern *parsepattern(struct cfstate *s) struct pattern *pat; int sl; struct rule *rule; + struct headmod *head; regex_t *regex; int rxfl; @@ -217,6 +249,16 @@ static struct pattern *parsepattern(struct cfstate *s) if(pat->restpat != NULL) free(pat->restpat); pat->restpat = sstrdup(s->argv[1]); + } else if(!strcmp(s->argv[0], "set")) { + if(s->argc < 3) { + flog(LOG_WARNING, "%s:%i: missing header name or pattern for `set' directive", s->file, s->lno); + continue; + } + omalloc(head); + head->name = sstrdup(s->argv[1]); + head->value = sstrdup(s->argv[2]); + head->next = pat->headers; + pat->headers = head; } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) { break; } else { @@ -315,7 +357,7 @@ static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr) buffree(buf); } -static char *findmatch(struct config *cf, struct hthead *req, int trydefault) +static struct pattern *findmatch(struct config *cf, struct hthead *req, int trydefault) { int i, o; struct pattern *pat; @@ -378,7 +420,7 @@ static char *findmatch(struct config *cf, struct hthead *req, int trydefault) } if(mstr) freeca(mstr); - return(pat->childnm); + return(pat); } if(mstr) { freeca(mstr); @@ -390,36 +432,126 @@ static char *findmatch(struct config *cf, struct hthead *req, int trydefault) static void serve(struct hthead *req, int fd) { - char *chnm; + struct pattern *pat; + struct headmod *head; struct child *ch; - if(((chnm = findmatch(config, req, 0)) == NULL) && ((chnm = findmatch(config, req, 1)) == NULL)) { + pat = NULL; + if(pat == NULL) + pat = findmatch(lconfig, req, 0); + if(pat == NULL) + pat = findmatch(lconfig, req, 1); + if(gconfig != NULL) { + if(pat == NULL) + pat = findmatch(gconfig, req, 0); + if(pat == NULL) + pat = findmatch(gconfig, req, 1); + } + if(pat == NULL) { simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server."); return; } - if((ch = getchild(config, chnm)) == NULL) { - flog(LOG_ERR, "child %s requested, but was not declared", chnm); - simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", chnm); + ch = NULL; + if(ch == NULL) + ch = getchild(lconfig, pat->childnm); + if(gconfig != NULL) { + if(ch == NULL) + ch = getchild(gconfig, pat->childnm); + } + if(ch == 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)) + for(head = pat->headers; head != NULL; head = head->next) { + headrmheader(req, head->name); + headappheader(req, head->name, head->value); + } + if(childhandle(ch, req, fd, NULL, NULL)) simpleerror(fd, 500, "Server Error", "The request handler crashed."); } +static void reloadconf(char *nm) +{ + struct config *cf; + + if((cf = readconfig(nm)) == NULL) { + flog(LOG_WARNING, "could not reload configuration file `%s'", nm); + return; + } + mergechildren(cf->children, lconfig->children); + freeconfig(lconfig); + lconfig = cf; +} + +static void chldhandler(int sig) +{ + pid_t pid; + + do { + pid = waitpid(-1, NULL, WNOHANG); + } while(pid > 0); +} + +static void sighandler(int sig) +{ + if(sig == SIGHUP) + reload = 1; +} + +static void usage(FILE *out) +{ + fprintf(out, "usage: patplex [-hN] CONFIGFILE\n"); +} + int main(int argc, char **argv) { + int c; + int nodef; + char *gcf; struct hthead *req; int fd; - - if(argc < 2) { - flog(LOG_ERR, "usage: patplex CONFIGFILE"); + + nodef = 0; + while((c = getopt(argc, argv, "hN")) >= 0) { + switch(c) { + case 'h': + usage(stdout); + exit(0); + case 'N': + nodef = 1; + break; + default: + usage(stderr); + exit(1); + } + } + if(argc - optind < 1) { + usage(stderr); exit(1); } - config = readconfig(argv[1]); - signal(SIGCHLD, SIG_IGN); + if(!nodef) { + if((gcf = findstdconf("ashd/patplex.rc")) != NULL) { + gconfig = readconfig(gcf); + free(gcf); + } + } + if((lconfig = readconfig(argv[optind])) == NULL) { + flog(LOG_ERR, "could not read `%s'", argv[optind]); + exit(1); + } + signal(SIGCHLD, chldhandler); + signal(SIGHUP, sighandler); + signal(SIGPIPE, sighandler); while(1) { + if(reload) { + reloadconf(argv[optind]); + reload = 0; + } if((fd = recvreq(0, &req)) < 0) { + if(errno == EINTR) + continue; if(errno != 0) flog(LOG_ERR, "recvreq: %s", strerror(errno)); break;