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/>.
50 struct child *children;
51 struct pattern *patterns;
68 struct headmod *headers;
75 static struct config *gconfig, *lconfig;
76 static volatile int reload = 0;
78 static void freepattern(struct pattern *pat)
83 for(rule = pat->rules; *rule; rule++) {
84 if((*rule)->header != NULL)
85 free((*rule)->header);
86 if((*rule)->pattern != NULL) {
87 regfree((*rule)->pattern);
88 free((*rule)->pattern);
92 while((head = pat->headers) != NULL) {
93 pat->headers = head->next;
98 if(pat->childnm != NULL)
103 static void freeconfig(struct config *cf)
105 struct child *ch, *nch;
106 struct pattern *pat, *npat;
108 for(ch = cf->children; ch != NULL; ch = nch) {
112 for(pat = cf->patterns; pat != NULL; pat = npat) {
119 static struct child *getchild(struct config *cf, char *name)
123 for(ch = cf->children; ch; ch = ch->next) {
124 if(!strcmp(ch->name, name))
130 static struct rule *newrule(struct pattern *pat)
135 for(i = 0; pat->rules[i]; i++);
136 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
137 rule = pat->rules[i] = szmalloc(sizeof(*rule));
138 pat->rules[i + 1] = NULL;
142 static struct pattern *newpattern(void)
147 pat->rules = szmalloc(sizeof(*pat->rules));
151 static regex_t *regalloc(char *regex, int flags)
158 if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
159 regerror(res, ret, errbuf, sizeof(errbuf));
160 flog(LOG_WARNING, "%s: %s", regex, errbuf);
167 static struct pattern *parsepattern(struct cfstate *s)
172 struct headmod *head;
176 if(!strcmp(s->argv[0], "match")) {
186 if(!strcmp(s->argv[0], "point") ||
187 !strcmp(s->argv[0], "url") ||
188 !strcmp(s->argv[0], "method")) {
190 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
195 if(strchr(s->argv[2], 'i'))
198 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
199 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
203 if(!strcmp(s->argv[0], "point"))
204 rule->type = PAT_REST;
205 else if(!strcmp(s->argv[0], "url"))
206 rule->type = PAT_URL;
207 else if(!strcmp(s->argv[0], "method"))
208 rule->type = PAT_METHOD;
209 rule->pattern = regex;
211 if(strchr(s->argv[2], 's'))
212 rule->fl |= PATFL_MSS;
213 if(strchr(s->argv[2], 'q'))
214 rule->fl |= PATFL_UNQ;
216 } else if(!strcmp(s->argv[0], "header")) {
218 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
223 if(strchr(s->argv[3], 'i'))
226 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
227 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
231 rule->type = PAT_HEADER;
232 rule->header = sstrdup(s->argv[1]);
233 rule->pattern = regex;
235 if(strchr(s->argv[3], 's'))
236 rule->fl |= PATFL_MSS;
238 } else if(!strcmp(s->argv[0], "all")) {
239 newrule(pat)->type = PAT_ALL;
240 } else if(!strcmp(s->argv[0], "default")) {
241 newrule(pat)->type = PAT_ALL;
243 } else if(!strcmp(s->argv[0], "order") || !strcmp(s->argv[0], "priority")) {
245 flog(LOG_WARNING, "%s:%i: missing specification for `%s' directive", s->file, s->lno, s->argv[0]);
248 pat->prio = atoi(s->argv[1]);
249 } else if(!strcmp(s->argv[0], "handler")) {
251 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
254 if(pat->childnm != NULL)
256 pat->childnm = sstrdup(s->argv[1]);
257 } else if(!strcmp(s->argv[0], "restpat")) {
259 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
262 if(pat->restpat != NULL)
264 pat->restpat = sstrdup(s->argv[1]);
265 } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
267 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
271 if(!strcmp(s->argv[0], "xset"))
272 head->name = sprintf2("X-Ash-%s", s->argv[1]);
274 head->name = sstrdup(s->argv[1]);
275 head->value = sstrdup(s->argv[2]);
276 head->next = pat->headers;
278 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
281 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
285 if(pat->rules[0] == NULL) {
286 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
290 if(pat->childnm == NULL) {
291 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
298 static struct config *readconfig(char *filename)
306 if((in = fopen(filename, "r")) == NULL) {
307 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
310 s = mkcfparser(in, filename);
315 if((ch = parsechild(s)) != NULL) {
316 ch->next = cf->children;
318 } else if((pat = parsepattern(s)) != NULL) {
319 pat->next = cf->patterns;
321 } else if(!strcmp(s->argv[0], "eof")) {
324 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
333 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
342 for(mc = 0; mstr[mc]; mc++);
344 for(p = pat->restpat; *p; ) {
347 if((*p >= '0') && (*p <= '9')) {
349 bufcatstr(buf, mstr[*p - '0']);
351 } else if(*p == '_') {
352 bufcatstr(buf, req->rest);
354 } else if(*p == '$') {
357 } else if(*p == '{') {
358 if((p2 = strchr(p, '}')) == NULL) {
361 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
372 replrest(req, buf.b);
376 static void qoffsets(char *buf, int *obuf, char *pstr, int unquote)
384 if((pstr[i] == '%') && ((d1 = hexdigit(pstr[i + 1])) >= 0) && ((d2 = hexdigit(pstr[i + 2])) >= 0)) {
385 buf[o] = (d1 << 4) | d2;
396 for(i = 0; pstr[i]; i++) {
411 static void freematch(struct match *match)
417 static struct match *findmatch(struct config *cf, struct hthead *req, struct match *match)
429 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
430 if(match && (pat->prio <= match->pat->prio))
433 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
435 if(rule->type == PAT_REST) {
438 } else if(rule->type == PAT_URL) {
441 } else if(rule->type == PAT_METHOD) {
444 } else if(rule->type == PAT_HEADER) {
446 if(!(pstr = getheader(req, rule->header)))
450 char pbuf[strlen(pstr) + 1];
451 int obuf[strlen(pstr) + 1];
452 qoffsets(pbuf, obuf, pstr, !!(rule->fl & PATFL_UNQ));
453 if(regexec(rx, pbuf, 10, gr, 0))
455 else if(rule->type == PAT_REST)
456 rmo = obuf[gr[0].rm_eo];
457 if(rule->fl & PATFL_MSS) {
459 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
462 for(o = 0; o < 10; o++) {
466 mstr = szmalloc((o + 1) * sizeof(*mstr));
467 for(o = 0; o < 10; o++) {
470 mstr[o] = smalloc(obuf[gr[o].rm_eo] - obuf[gr[o].rm_so] + 1);
471 memcpy(mstr[o], pstr + obuf[gr[o].rm_so], obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]);
472 mstr[o][obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]] = 0;
475 } else if(rule->type == PAT_ALL) {
490 static void execmatch(struct hthead *req, struct match *match)
492 struct headmod *head;
494 if(match->pat->restpat)
495 exprestpat(req, match->pat, match->mstr);
496 else if(match->rmo != -1)
497 replrest(req, req->rest + match->rmo);
498 for(head = match->pat->headers; head != NULL; head = head->next) {
499 headrmheader(req, head->name);
500 headappheader(req, head->name, head->value);
504 static void childerror(struct hthead *req, int fd)
507 simpleerror(fd, 500, "Server Error", "The request handler is overloaded.");
509 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
512 static void serve(struct hthead *req, int fd)
518 match = findmatch(lconfig, req, match);
520 match = findmatch(gconfig, req, match);
522 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
527 ch = getchild(lconfig, match->pat->childnm);
528 if((ch == NULL) && (gconfig != NULL))
529 ch = getchild(gconfig, match->pat->childnm);
531 flog(LOG_ERR, "child %s requested, but was not declared", match->pat->childnm);
532 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", match->pat->childnm);
537 execmatch(req, match);
538 if(childhandle(ch, req, fd, NULL, NULL))
543 static void reloadconf(char *nm)
547 if((cf = readconfig(nm)) == NULL) {
548 flog(LOG_WARNING, "could not reload configuration file `%s'", nm);
551 mergechildren(cf->children, lconfig->children);
556 static void chldhandler(int sig)
561 while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
563 flog(LOG_WARNING, "child process %i dumped core", pid);
567 static void sighandler(int sig)
573 static void usage(FILE *out)
575 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
578 int main(int argc, char **argv)
587 while((c = getopt(argc, argv, "hN")) >= 0) {
600 if(argc - optind < 1) {
605 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
606 gconfig = readconfig(gcf);
610 if((strchr(lcf = argv[optind], '/')) == NULL) {
611 if((lcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
612 flog(LOG_ERR, "could not find requested configuration file `%s'", argv[optind]);
616 if((lconfig = readconfig(lcf)) == NULL) {
617 flog(LOG_ERR, "could not read `%s'", lcf);
620 signal(SIGCHLD, chldhandler);
621 signal(SIGHUP, sighandler);
622 signal(SIGPIPE, sighandler);
628 if((fd = recvreq(0, &req)) < 0) {
632 flog(LOG_ERR, "recvreq: %s", strerror(errno));