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;
74 static struct config *gconfig, *lconfig;
75 static volatile int reload = 0;
77 static void freepattern(struct pattern *pat)
82 for(rule = pat->rules; *rule; rule++) {
83 if((*rule)->header != NULL)
84 free((*rule)->header);
85 if((*rule)->pattern != NULL) {
86 regfree((*rule)->pattern);
87 free((*rule)->pattern);
91 while((head = pat->headers) != NULL) {
92 pat->headers = head->next;
97 if(pat->childnm != NULL)
102 static void freeconfig(struct config *cf)
104 struct child *ch, *nch;
105 struct pattern *pat, *npat;
107 for(ch = cf->children; ch != NULL; ch = nch) {
111 for(pat = cf->patterns; pat != NULL; pat = npat) {
118 static struct child *getchild(struct config *cf, char *name)
122 for(ch = cf->children; ch; ch = ch->next) {
123 if(!strcmp(ch->name, name))
129 static struct rule *newrule(struct pattern *pat)
134 for(i = 0; pat->rules[i]; i++);
135 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
136 rule = pat->rules[i] = szmalloc(sizeof(*rule));
137 pat->rules[i + 1] = NULL;
141 static struct pattern *newpattern(void)
146 pat->rules = szmalloc(sizeof(*pat->rules));
150 static regex_t *regalloc(char *regex, int flags)
157 if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
158 regerror(res, ret, errbuf, sizeof(errbuf));
159 flog(LOG_WARNING, "%s: %s", regex, errbuf);
166 static struct pattern *parsepattern(struct cfstate *s)
171 struct headmod *head;
175 if(!strcmp(s->argv[0], "match")) {
185 if(!strcmp(s->argv[0], "point") ||
186 !strcmp(s->argv[0], "url") ||
187 !strcmp(s->argv[0], "method")) {
189 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
193 if(strchr(s->argv[2], 'i'))
196 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
197 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
201 if(!strcmp(s->argv[0], "point"))
202 rule->type = PAT_REST;
203 else if(!strcmp(s->argv[0], "url"))
204 rule->type = PAT_URL;
205 else if(!strcmp(s->argv[0], "method"))
206 rule->type = PAT_METHOD;
207 rule->pattern = regex;
209 if(strchr(s->argv[2], 's'))
210 rule->fl |= PATFL_MSS;
211 if(strchr(s->argv[2], 'q'))
212 rule->fl |= PATFL_UNQ;
214 } else if(!strcmp(s->argv[0], "header")) {
216 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
220 if(strchr(s->argv[3], 'i'))
223 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
224 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
228 rule->type = PAT_HEADER;
229 rule->header = sstrdup(s->argv[1]);
230 rule->pattern = regex;
232 if(strchr(s->argv[3], 's'))
233 rule->fl |= PATFL_MSS;
235 } else if(!strcmp(s->argv[0], "all")) {
236 newrule(pat)->type = PAT_ALL;
237 } else if(!strcmp(s->argv[0], "default")) {
238 newrule(pat)->type = PAT_DEFAULT;
239 } else if(!strcmp(s->argv[0], "handler")) {
241 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
244 if(pat->childnm != NULL)
246 pat->childnm = sstrdup(s->argv[1]);
247 } else if(!strcmp(s->argv[0], "restpat")) {
249 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
252 if(pat->restpat != NULL)
254 pat->restpat = sstrdup(s->argv[1]);
255 } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
257 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
261 if(!strcmp(s->argv[0], "xset"))
262 head->name = sprintf2("X-Ash-%s", s->argv[1]);
264 head->name = sstrdup(s->argv[1]);
265 head->value = sstrdup(s->argv[2]);
266 head->next = pat->headers;
268 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
271 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
275 if(pat->rules[0] == NULL) {
276 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
280 if(pat->childnm == NULL) {
281 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
288 static struct config *readconfig(char *filename)
296 if((in = fopen(filename, "r")) == NULL) {
297 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
300 s = mkcfparser(in, filename);
305 if((ch = parsechild(s)) != NULL) {
306 ch->next = cf->children;
308 } else if((pat = parsepattern(s)) != NULL) {
309 pat->next = cf->patterns;
311 } else if(!strcmp(s->argv[0], "eof")) {
314 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
323 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
332 for(mc = 0; mstr[mc]; mc++);
334 for(p = pat->restpat; *p; ) {
337 if((*p >= '0') && (*p <= '9')) {
339 bufcatstr(buf, mstr[*p - '0']);
341 } else if(*p == '_') {
342 bufcatstr(buf, req->rest);
344 } else if(*p == '$') {
347 } else if(*p == '{') {
348 if((p2 = strchr(p, '}')) == NULL) {
351 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
362 replrest(req, buf.b);
366 static void qoffsets(char *buf, int *obuf, char *pstr, int unquote)
374 if((pstr[i] == '%') && ((d1 = hexdigit(pstr[i + 1])) >= 0) && ((d2 = hexdigit(pstr[i + 2])) >= 0)) {
375 buf[o] = (d1 << 4) | d2;
385 for(i = 0; pstr[i]; i++) {
393 static struct pattern *findmatch(struct config *cf, struct hthead *req, int trydefault)
405 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
407 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
409 if(rule->type == PAT_REST) {
412 } else if(rule->type == PAT_URL) {
415 } else if(rule->type == PAT_METHOD) {
418 } else if(rule->type == PAT_HEADER) {
420 if(!(pstr = getheader(req, rule->header)))
424 char pbuf[strlen(pstr) + 1];
425 int obuf[strlen(pstr) + 1];
426 qoffsets(pbuf, obuf, pstr, !!(rule->fl & PATFL_UNQ));
427 if(regexec(rx, pbuf, 10, gr, 0))
429 else if(rule->type == PAT_REST)
430 rmo = obuf[gr[0].rm_eo];
431 if(rule->fl & PATFL_MSS) {
433 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
436 for(o = 0; o < 10; o++) {
440 mstr = szmalloc((o + 1) * sizeof(*mstr));
441 for(o = 0; o < 10; o++) {
444 mstr[o] = smalloc(obuf[gr[o].rm_eo] - obuf[gr[o].rm_so] + 1);
445 memcpy(mstr[o], pstr + obuf[gr[o].rm_so], obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]);
446 mstr[o][obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]] = 0;
449 } else if(rule->type == PAT_ALL) {
450 } else if(rule->type == PAT_DEFAULT) {
457 exprestpat(req, pat, mstr);
458 } else if(rmo != -1) {
459 replrest(req, req->rest + rmo);
473 static void serve(struct hthead *req, int fd)
476 struct headmod *head;
481 pat = findmatch(lconfig, req, 0);
483 pat = findmatch(lconfig, req, 1);
484 if(gconfig != NULL) {
486 pat = findmatch(gconfig, req, 0);
488 pat = findmatch(gconfig, req, 1);
491 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
496 ch = getchild(lconfig, pat->childnm);
497 if(gconfig != NULL) {
499 ch = getchild(gconfig, pat->childnm);
502 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
503 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
507 for(head = pat->headers; head != NULL; head = head->next) {
508 headrmheader(req, head->name);
509 headappheader(req, head->name, head->value);
511 if(childhandle(ch, req, fd, NULL, NULL))
512 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
515 static void reloadconf(char *nm)
519 if((cf = readconfig(nm)) == NULL) {
520 flog(LOG_WARNING, "could not reload configuration file `%s'", nm);
523 mergechildren(cf->children, lconfig->children);
528 static void chldhandler(int sig)
533 while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
535 flog(LOG_WARNING, "child process %i dumped core", pid);
539 static void sighandler(int sig)
545 static void usage(FILE *out)
547 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
550 int main(int argc, char **argv)
559 while((c = getopt(argc, argv, "hN")) >= 0) {
572 if(argc - optind < 1) {
577 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
578 gconfig = readconfig(gcf);
582 if((strchr(lcf = argv[optind], '/')) == NULL) {
583 if((lcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
584 flog(LOG_ERR, "could not find requested configuration file `%s'", argv[optind]);
588 if((lconfig = readconfig(lcf)) == NULL) {
589 flog(LOG_ERR, "could not read `%s'", lcf);
592 signal(SIGCHLD, chldhandler);
593 signal(SIGHUP, sighandler);
594 signal(SIGPIPE, sighandler);
600 if((fd = recvreq(0, &req)) < 0) {
604 flog(LOG_ERR, "recvreq: %s", strerror(errno));