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/>.
49 struct child *children;
50 struct pattern *patterns;
67 static struct config *gconfig, *lconfig;
68 static volatile int reload = 0;
70 static void freepattern(struct pattern *pat)
74 for(rule = pat->rules; *rule; rule++) {
75 if((*rule)->header != NULL)
76 free((*rule)->header);
77 if((*rule)->pattern != NULL) {
78 regfree((*rule)->pattern);
79 free((*rule)->pattern);
83 if(pat->childnm != NULL)
88 static void freeconfig(struct config *cf)
90 struct child *ch, *nch;
91 struct pattern *pat, *npat;
93 for(ch = cf->children; ch != NULL; ch = nch) {
97 for(pat = cf->patterns; pat != NULL; pat = npat) {
104 static struct child *getchild(struct config *cf, char *name)
108 for(ch = cf->children; ch; ch = ch->next) {
109 if(!strcmp(ch->name, name))
115 static struct rule *newrule(struct pattern *pat)
120 for(i = 0; pat->rules[i]; i++);
121 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
122 rule = pat->rules[i] = szmalloc(sizeof(*rule));
123 pat->rules[i + 1] = NULL;
127 static struct pattern *newpattern(void)
132 pat->rules = szmalloc(sizeof(*pat->rules));
136 static regex_t *regalloc(char *regex, int flags)
143 if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
144 regerror(res, ret, errbuf, sizeof(errbuf));
145 flog(LOG_WARNING, "%s: %s", regex, errbuf);
152 static struct pattern *parsepattern(struct cfstate *s)
160 if(!strcmp(s->argv[0], "match")) {
170 if(!strcmp(s->argv[0], "point") ||
171 !strcmp(s->argv[0], "url") ||
172 !strcmp(s->argv[0], "method")) {
174 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
178 if(strchr(s->argv[2], 'i'))
181 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
182 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
186 if(!strcmp(s->argv[0], "point"))
187 rule->type = PAT_REST;
188 else if(!strcmp(s->argv[0], "url"))
189 rule->type = PAT_URL;
190 else if(!strcmp(s->argv[0], "method"))
191 rule->type = PAT_METHOD;
192 rule->pattern = regex;
194 if(strchr(s->argv[2], 's'))
195 rule->fl |= PATFL_MSS;
197 } else if(!strcmp(s->argv[0], "header")) {
199 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
203 if(strchr(s->argv[3], 'i'))
206 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
207 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
211 rule->type = PAT_HEADER;
212 rule->header = sstrdup(s->argv[1]);
213 rule->pattern = regex;
215 if(strchr(s->argv[3], 's'))
216 rule->fl |= PATFL_MSS;
218 } else if(!strcmp(s->argv[0], "all")) {
219 newrule(pat)->type = PAT_ALL;
220 } else if(!strcmp(s->argv[0], "default")) {
221 newrule(pat)->type = PAT_DEFAULT;
222 } else if(!strcmp(s->argv[0], "handler")) {
224 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
227 if(pat->childnm != NULL)
229 pat->childnm = sstrdup(s->argv[1]);
230 } else if(!strcmp(s->argv[0], "restpat")) {
232 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
235 if(pat->restpat != NULL)
237 pat->restpat = sstrdup(s->argv[1]);
238 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
241 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
245 if(pat->rules[0] == NULL) {
246 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
250 if(pat->childnm == NULL) {
251 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
258 static struct config *readconfig(char *filename)
266 if((in = fopen(filename, "r")) == NULL) {
267 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
270 s = mkcfparser(in, filename);
275 if((ch = parsechild(s)) != NULL) {
276 ch->next = cf->children;
278 } else if((pat = parsepattern(s)) != NULL) {
279 pat->next = cf->patterns;
281 } else if(!strcmp(s->argv[0], "eof")) {
284 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
293 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
302 for(mc = 0; mstr[mc]; mc++);
304 for(p = pat->restpat; *p; ) {
307 if((*p >= '0') && (*p <= '9')) {
309 bufcatstr(buf, mstr[*p - '0']);
311 } else if(*p == '_') {
312 bufcatstr(buf, req->rest);
314 } else if(*p == '$') {
317 } else if(*p == '{') {
318 if((p2 = strchr(p, '}')) == NULL) {
321 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
332 replrest(req, buf.b);
336 static char *findmatch(struct config *cf, struct hthead *req, int trydefault)
347 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
349 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
351 if(rule->type == PAT_REST) {
352 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
356 } else if(rule->type == PAT_URL) {
357 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
359 } else if(rule->type == PAT_METHOD) {
360 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
362 } else if(rule->type == PAT_HEADER) {
363 if(!(pstr = getheader(req, rule->header)))
365 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
367 } else if(rule->type == PAT_ALL) {
368 } else if(rule->type == PAT_DEFAULT) {
372 if(matched && (rule->fl & PATFL_MSS)) {
374 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
377 for(o = 0; o < 10; o++) {
381 mstr = szmalloc((o + 1) * sizeof(*mstr));
382 for(o = 0; o < 10; o++) {
385 mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
386 memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
387 mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
393 exprestpat(req, pat, mstr);
394 } else if(rmo != -1) {
395 replrest(req, req->rest + rmo);
399 return(pat->childnm);
409 static void serve(struct hthead *req, int fd)
416 chnm = findmatch(lconfig, req, 0);
418 chnm = findmatch(lconfig, req, 1);
419 if(gconfig != NULL) {
421 chnm = findmatch(gconfig, req, 0);
423 chnm = findmatch(gconfig, req, 1);
426 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
431 ch = getchild(lconfig, chnm);
432 if(gconfig != NULL) {
434 ch = getchild(gconfig, chnm);
437 flog(LOG_ERR, "child %s requested, but was not declared", chnm);
438 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", chnm);
442 if(childhandle(ch, req, fd, NULL, NULL))
443 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
446 static void reloadconf(char *nm)
450 if((cf = readconfig(nm)) == NULL) {
451 flog(LOG_WARNING, "could not reload configuration file `%s'", nm);
454 mergechildren(cf->children, lconfig->children);
459 static void chldhandler(int sig)
464 pid = waitpid(-1, NULL, WNOHANG);
468 static void sighandler(int sig)
474 static void usage(FILE *out)
476 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
479 int main(int argc, char **argv)
488 while((c = getopt(argc, argv, "hN")) >= 0) {
501 if(argc - optind < 1) {
506 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
507 gconfig = readconfig(gcf);
511 if((lconfig = readconfig(argv[optind])) == NULL) {
512 flog(LOG_ERR, "could not read `%s'", argv[optind]);
515 signal(SIGCHLD, chldhandler);
516 signal(SIGHUP, sighandler);
517 signal(SIGPIPE, sighandler);
520 reloadconf(argv[optind]);
523 if((fd = recvreq(0, &req)) < 0) {
527 flog(LOG_ERR, "recvreq: %s", strerror(errno));