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]);
194 if(strchr(s->argv[2], 'i'))
197 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
198 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
202 if(!strcmp(s->argv[0], "point"))
203 rule->type = PAT_REST;
204 else if(!strcmp(s->argv[0], "url"))
205 rule->type = PAT_URL;
206 else if(!strcmp(s->argv[0], "method"))
207 rule->type = PAT_METHOD;
208 rule->pattern = regex;
210 if(strchr(s->argv[2], 's'))
211 rule->fl |= PATFL_MSS;
212 if(strchr(s->argv[2], 'q'))
213 rule->fl |= PATFL_UNQ;
215 } else if(!strcmp(s->argv[0], "header")) {
217 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
222 if(strchr(s->argv[3], 'i'))
225 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
226 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
230 rule->type = PAT_HEADER;
231 rule->header = sstrdup(s->argv[1]);
232 rule->pattern = regex;
234 if(strchr(s->argv[3], 's'))
235 rule->fl |= PATFL_MSS;
237 } else if(!strcmp(s->argv[0], "all")) {
238 newrule(pat)->type = PAT_ALL;
239 } else if(!strcmp(s->argv[0], "default")) {
240 newrule(pat)->type = PAT_DEFAULT;
241 } else if(!strcmp(s->argv[0], "handler")) {
243 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
246 if(pat->childnm != NULL)
248 pat->childnm = sstrdup(s->argv[1]);
249 } else if(!strcmp(s->argv[0], "restpat")) {
251 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
254 if(pat->restpat != NULL)
256 pat->restpat = sstrdup(s->argv[1]);
257 } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
259 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
263 if(!strcmp(s->argv[0], "xset"))
264 head->name = sprintf2("X-Ash-%s", s->argv[1]);
266 head->name = sstrdup(s->argv[1]);
267 head->value = sstrdup(s->argv[2]);
268 head->next = pat->headers;
270 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
273 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
277 if(pat->rules[0] == NULL) {
278 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
282 if(pat->childnm == NULL) {
283 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
290 static struct config *readconfig(char *filename)
298 if((in = fopen(filename, "r")) == NULL) {
299 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
302 s = mkcfparser(in, filename);
307 if((ch = parsechild(s)) != NULL) {
308 ch->next = cf->children;
310 } else if((pat = parsepattern(s)) != NULL) {
311 pat->next = cf->patterns;
313 } else if(!strcmp(s->argv[0], "eof")) {
316 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
325 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
334 for(mc = 0; mstr[mc]; mc++);
336 for(p = pat->restpat; *p; ) {
339 if((*p >= '0') && (*p <= '9')) {
341 bufcatstr(buf, mstr[*p - '0']);
343 } else if(*p == '_') {
344 bufcatstr(buf, req->rest);
346 } else if(*p == '$') {
349 } else if(*p == '{') {
350 if((p2 = strchr(p, '}')) == NULL) {
353 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
364 replrest(req, buf.b);
368 static void qoffsets(char *buf, int *obuf, char *pstr, int unquote)
376 if((pstr[i] == '%') && ((d1 = hexdigit(pstr[i + 1])) >= 0) && ((d2 = hexdigit(pstr[i + 2])) >= 0)) {
377 buf[o] = (d1 << 4) | d2;
387 for(i = 0; pstr[i]; i++) {
395 static struct pattern *findmatch(struct config *cf, struct hthead *req, int trydefault)
407 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
409 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
411 if(rule->type == PAT_REST) {
414 } else if(rule->type == PAT_URL) {
417 } else if(rule->type == PAT_METHOD) {
420 } else if(rule->type == PAT_HEADER) {
422 if(!(pstr = getheader(req, rule->header)))
426 char pbuf[strlen(pstr) + 1];
427 int obuf[strlen(pstr) + 1];
428 qoffsets(pbuf, obuf, pstr, !!(rule->fl & PATFL_UNQ));
429 if(regexec(rx, pbuf, 10, gr, 0))
431 else if(rule->type == PAT_REST)
432 rmo = obuf[gr[0].rm_eo];
433 if(rule->fl & PATFL_MSS) {
435 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
438 for(o = 0; o < 10; o++) {
442 mstr = szmalloc((o + 1) * sizeof(*mstr));
443 for(o = 0; o < 10; o++) {
446 mstr[o] = smalloc(obuf[gr[o].rm_eo] - obuf[gr[o].rm_so] + 1);
447 memcpy(mstr[o], pstr + obuf[gr[o].rm_so], obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]);
448 mstr[o][obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]] = 0;
451 } else if(rule->type == PAT_ALL) {
452 } else if(rule->type == PAT_DEFAULT) {
459 exprestpat(req, pat, mstr);
460 } else if(rmo != -1) {
461 replrest(req, req->rest + rmo);
475 static void childerror(struct hthead *req, int fd)
478 simpleerror(fd, 500, "Server Error", "The request handler is overloaded.");
480 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
483 static void serve(struct hthead *req, int fd)
486 struct headmod *head;
491 pat = findmatch(lconfig, req, 0);
493 pat = findmatch(lconfig, req, 1);
494 if(gconfig != NULL) {
496 pat = findmatch(gconfig, req, 0);
498 pat = findmatch(gconfig, req, 1);
501 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
506 ch = getchild(lconfig, pat->childnm);
507 if(gconfig != NULL) {
509 ch = getchild(gconfig, pat->childnm);
512 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
513 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
517 for(head = pat->headers; head != NULL; head = head->next) {
518 headrmheader(req, head->name);
519 headappheader(req, head->name, head->value);
521 if(childhandle(ch, req, fd, NULL, NULL))
525 static void reloadconf(char *nm)
529 if((cf = readconfig(nm)) == NULL) {
530 flog(LOG_WARNING, "could not reload configuration file `%s'", nm);
533 mergechildren(cf->children, lconfig->children);
538 static void chldhandler(int sig)
543 while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
545 flog(LOG_WARNING, "child process %i dumped core", pid);
549 static void sighandler(int sig)
555 static void usage(FILE *out)
557 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
560 int main(int argc, char **argv)
569 while((c = getopt(argc, argv, "hN")) >= 0) {
582 if(argc - optind < 1) {
587 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
588 gconfig = readconfig(gcf);
592 if((strchr(lcf = argv[optind], '/')) == NULL) {
593 if((lcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
594 flog(LOG_ERR, "could not find requested configuration file `%s'", argv[optind]);
598 if((lconfig = readconfig(lcf)) == NULL) {
599 flog(LOG_ERR, "could not read `%s'", lcf);
602 signal(SIGCHLD, chldhandler);
603 signal(SIGHUP, sighandler);
604 signal(SIGPIPE, sighandler);
610 if((fd = recvreq(0, &req)) < 0) {
614 flog(LOG_ERR, "recvreq: %s", strerror(errno));