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/>.
48 struct child *children;
49 struct pattern *patterns;
66 static struct config *gconfig, *lconfig;
67 static volatile int reload = 0;
69 static void freepattern(struct pattern *pat)
73 for(rule = pat->rules; *rule; rule++) {
74 if((*rule)->header != NULL)
75 free((*rule)->header);
76 if((*rule)->pattern != NULL) {
77 regfree((*rule)->pattern);
78 free((*rule)->pattern);
82 if(pat->childnm != NULL)
87 static void freeconfig(struct config *cf)
89 struct child *ch, *nch;
90 struct pattern *pat, *npat;
92 for(ch = cf->children; ch != NULL; ch = nch) {
96 for(pat = cf->patterns; pat != NULL; pat = npat) {
103 static struct child *getchild(struct config *cf, char *name)
107 for(ch = cf->children; ch; ch = ch->next) {
108 if(!strcmp(ch->name, name))
114 static struct rule *newrule(struct pattern *pat)
119 for(i = 0; pat->rules[i]; i++);
120 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
121 rule = pat->rules[i] = szmalloc(sizeof(*rule));
122 pat->rules[i + 1] = NULL;
126 static struct pattern *newpattern(void)
131 pat->rules = szmalloc(sizeof(*pat->rules));
135 static regex_t *regalloc(char *regex, int flags)
142 if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
143 regerror(res, ret, errbuf, sizeof(errbuf));
144 flog(LOG_WARNING, "%s: %s", regex, errbuf);
151 static struct pattern *parsepattern(struct cfstate *s)
159 if(!strcmp(s->argv[0], "match")) {
169 if(!strcmp(s->argv[0], "point") ||
170 !strcmp(s->argv[0], "url") ||
171 !strcmp(s->argv[0], "method")) {
173 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
177 if(strchr(s->argv[2], 'i'))
180 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
181 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
185 if(!strcmp(s->argv[0], "point"))
186 rule->type = PAT_REST;
187 else if(!strcmp(s->argv[0], "url"))
188 rule->type = PAT_URL;
189 else if(!strcmp(s->argv[0], "method"))
190 rule->type = PAT_METHOD;
191 rule->pattern = regex;
193 if(strchr(s->argv[2], 's'))
194 rule->fl |= PATFL_MSS;
196 } else if(!strcmp(s->argv[0], "header")) {
198 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
202 if(strchr(s->argv[3], 'i'))
205 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
206 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
210 rule->type = PAT_HEADER;
211 rule->header = sstrdup(s->argv[1]);
212 rule->pattern = regex;
214 if(strchr(s->argv[3], 's'))
215 rule->fl |= PATFL_MSS;
217 } else if(!strcmp(s->argv[0], "all")) {
218 newrule(pat)->type = PAT_ALL;
219 } else if(!strcmp(s->argv[0], "default")) {
220 newrule(pat)->type = PAT_DEFAULT;
221 } else if(!strcmp(s->argv[0], "handler")) {
223 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
226 if(pat->childnm != NULL)
228 pat->childnm = sstrdup(s->argv[1]);
229 } else if(!strcmp(s->argv[0], "restpat")) {
231 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
234 if(pat->restpat != NULL)
236 pat->restpat = sstrdup(s->argv[1]);
237 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
240 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
244 if(pat->rules[0] == NULL) {
245 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
249 if(pat->childnm == NULL) {
250 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
257 static struct config *readconfig(char *filename)
265 if((in = fopen(filename, "r")) == NULL) {
266 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
269 s = mkcfparser(in, filename);
274 if((ch = parsechild(s)) != NULL) {
275 ch->next = cf->children;
277 } else if((pat = parsepattern(s)) != NULL) {
278 pat->next = cf->patterns;
280 } else if(!strcmp(s->argv[0], "eof")) {
283 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
292 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
301 for(mc = 0; mstr[mc]; mc++);
303 for(p = pat->restpat; *p; ) {
306 if((*p >= '0') && (*p <= '9')) {
308 bufcatstr(buf, mstr[*p - '0']);
310 } else if(*p == '_') {
311 bufcatstr(buf, req->rest);
313 } else if(*p == '$') {
316 } else if(*p == '{') {
317 if((p2 = strchr(p, '}')) == NULL) {
320 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
331 replrest(req, buf.b);
335 static char *findmatch(struct config *cf, struct hthead *req, int trydefault)
346 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
348 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
350 if(rule->type == PAT_REST) {
351 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
355 } else if(rule->type == PAT_URL) {
356 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
358 } else if(rule->type == PAT_METHOD) {
359 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
361 } else if(rule->type == PAT_HEADER) {
362 if(!(pstr = getheader(req, rule->header)))
364 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
366 } else if(rule->type == PAT_ALL) {
367 } else if(rule->type == PAT_DEFAULT) {
371 if(matched && (rule->fl & PATFL_MSS)) {
373 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
376 for(o = 0; o < 10; o++) {
380 mstr = szmalloc((o + 1) * sizeof(*mstr));
381 for(o = 0; o < 10; o++) {
384 mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
385 memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
386 mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
392 exprestpat(req, pat, mstr);
393 } else if(rmo != -1) {
394 replrest(req, req->rest + rmo);
398 return(pat->childnm);
408 static void serve(struct hthead *req, int fd)
415 chnm = findmatch(lconfig, req, 0);
417 chnm = findmatch(lconfig, req, 1);
418 if(gconfig != NULL) {
420 chnm = findmatch(gconfig, req, 0);
422 chnm = findmatch(gconfig, req, 1);
425 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
430 ch = getchild(lconfig, chnm);
431 if(gconfig != NULL) {
433 ch = getchild(gconfig, chnm);
436 flog(LOG_ERR, "child %s requested, but was not declared", chnm);
437 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", chnm);
441 if(childhandle(ch, req, fd))
442 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
445 static void reloadconf(char *nm)
448 struct child *ch1, *ch2;
450 if((cf = readconfig(nm)) == NULL) {
451 flog(LOG_WARNING, "could not reload configuration file `%s'", nm);
454 for(ch1 = cf->children; ch1 != NULL; ch1 = ch1->next) {
455 for(ch2 = lconfig->children; ch2 != NULL; ch2 = ch2->next) {
456 if(!strcmp(ch1->name, ch2->name)) {
467 static void sighandler(int sig)
473 static void usage(FILE *out)
475 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
478 int main(int argc, char **argv)
487 while((c = getopt(argc, argv, "hN")) >= 0) {
500 if(argc - optind < 1) {
505 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
506 gconfig = readconfig(gcf);
510 if((lconfig = readconfig(argv[optind])) == NULL) {
511 flog(LOG_ERR, "could not read `%s'", argv[optind]);
514 signal(SIGCHLD, SIG_IGN);
515 signal(SIGHUP, sighandler);
518 reloadconf(argv[optind]);
521 if((fd = recvreq(0, &req)) < 0) {
525 flog(LOG_ERR, "recvreq: %s", strerror(errno));