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 struct headmod *headers;
73 static struct config *gconfig, *lconfig;
74 static volatile int reload = 0;
76 static void freepattern(struct pattern *pat)
81 for(rule = pat->rules; *rule; rule++) {
82 if((*rule)->header != NULL)
83 free((*rule)->header);
84 if((*rule)->pattern != NULL) {
85 regfree((*rule)->pattern);
86 free((*rule)->pattern);
90 while((head = pat->headers) != NULL) {
91 pat->headers = head->next;
96 if(pat->childnm != NULL)
101 static void freeconfig(struct config *cf)
103 struct child *ch, *nch;
104 struct pattern *pat, *npat;
106 for(ch = cf->children; ch != NULL; ch = nch) {
110 for(pat = cf->patterns; pat != NULL; pat = npat) {
117 static struct child *getchild(struct config *cf, char *name)
121 for(ch = cf->children; ch; ch = ch->next) {
122 if(!strcmp(ch->name, name))
128 static struct rule *newrule(struct pattern *pat)
133 for(i = 0; pat->rules[i]; i++);
134 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
135 rule = pat->rules[i] = szmalloc(sizeof(*rule));
136 pat->rules[i + 1] = NULL;
140 static struct pattern *newpattern(void)
145 pat->rules = szmalloc(sizeof(*pat->rules));
149 static regex_t *regalloc(char *regex, int flags)
156 if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
157 regerror(res, ret, errbuf, sizeof(errbuf));
158 flog(LOG_WARNING, "%s: %s", regex, errbuf);
165 static struct pattern *parsepattern(struct cfstate *s)
170 struct headmod *head;
174 if(!strcmp(s->argv[0], "match")) {
184 if(!strcmp(s->argv[0], "point") ||
185 !strcmp(s->argv[0], "url") ||
186 !strcmp(s->argv[0], "method")) {
188 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
192 if(strchr(s->argv[2], 'i'))
195 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
196 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
200 if(!strcmp(s->argv[0], "point"))
201 rule->type = PAT_REST;
202 else if(!strcmp(s->argv[0], "url"))
203 rule->type = PAT_URL;
204 else if(!strcmp(s->argv[0], "method"))
205 rule->type = PAT_METHOD;
206 rule->pattern = regex;
208 if(strchr(s->argv[2], 's'))
209 rule->fl |= PATFL_MSS;
211 } else if(!strcmp(s->argv[0], "header")) {
213 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
217 if(strchr(s->argv[3], 'i'))
220 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
221 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
225 rule->type = PAT_HEADER;
226 rule->header = sstrdup(s->argv[1]);
227 rule->pattern = regex;
229 if(strchr(s->argv[3], 's'))
230 rule->fl |= PATFL_MSS;
232 } else if(!strcmp(s->argv[0], "all")) {
233 newrule(pat)->type = PAT_ALL;
234 } else if(!strcmp(s->argv[0], "default")) {
235 newrule(pat)->type = PAT_DEFAULT;
236 } else if(!strcmp(s->argv[0], "handler")) {
238 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
241 if(pat->childnm != NULL)
243 pat->childnm = sstrdup(s->argv[1]);
244 } else if(!strcmp(s->argv[0], "restpat")) {
246 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
249 if(pat->restpat != NULL)
251 pat->restpat = sstrdup(s->argv[1]);
252 } else if(!strcmp(s->argv[0], "set")) {
254 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `set' directive", s->file, s->lno);
258 head->name = sstrdup(s->argv[1]);
259 head->value = sstrdup(s->argv[2]);
260 head->next = pat->headers;
262 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
265 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
269 if(pat->rules[0] == NULL) {
270 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
274 if(pat->childnm == NULL) {
275 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
282 static struct config *readconfig(char *filename)
290 if((in = fopen(filename, "r")) == NULL) {
291 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
294 s = mkcfparser(in, filename);
299 if((ch = parsechild(s)) != NULL) {
300 ch->next = cf->children;
302 } else if((pat = parsepattern(s)) != NULL) {
303 pat->next = cf->patterns;
305 } else if(!strcmp(s->argv[0], "eof")) {
308 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
317 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
326 for(mc = 0; mstr[mc]; mc++);
328 for(p = pat->restpat; *p; ) {
331 if((*p >= '0') && (*p <= '9')) {
333 bufcatstr(buf, mstr[*p - '0']);
335 } else if(*p == '_') {
336 bufcatstr(buf, req->rest);
338 } else if(*p == '$') {
341 } else if(*p == '{') {
342 if((p2 = strchr(p, '}')) == NULL) {
345 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
356 replrest(req, buf.b);
360 static struct pattern *findmatch(struct config *cf, struct hthead *req, int trydefault)
371 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
373 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
375 if(rule->type == PAT_REST) {
376 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
380 } else if(rule->type == PAT_URL) {
381 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
383 } else if(rule->type == PAT_METHOD) {
384 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
386 } else if(rule->type == PAT_HEADER) {
387 if(!(pstr = getheader(req, rule->header)))
389 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
391 } else if(rule->type == PAT_ALL) {
392 } else if(rule->type == PAT_DEFAULT) {
396 if(matched && (rule->fl & PATFL_MSS)) {
398 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
401 for(o = 0; o < 10; o++) {
405 mstr = szmalloc((o + 1) * sizeof(*mstr));
406 for(o = 0; o < 10; o++) {
409 mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
410 memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
411 mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
417 exprestpat(req, pat, mstr);
418 } else if(rmo != -1) {
419 replrest(req, req->rest + rmo);
433 static void serve(struct hthead *req, int fd)
436 struct headmod *head;
441 pat = findmatch(lconfig, req, 0);
443 pat = findmatch(lconfig, req, 1);
444 if(gconfig != NULL) {
446 pat = findmatch(gconfig, req, 0);
448 pat = findmatch(gconfig, req, 1);
451 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
456 ch = getchild(lconfig, pat->childnm);
457 if(gconfig != NULL) {
459 ch = getchild(gconfig, pat->childnm);
462 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
463 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
467 for(head = pat->headers; head != NULL; head = head->next) {
468 headrmheader(req, head->name);
469 headappheader(req, head->name, head->value);
471 if(childhandle(ch, req, fd, NULL, NULL))
472 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
475 static void reloadconf(char *nm)
479 if((cf = readconfig(nm)) == NULL) {
480 flog(LOG_WARNING, "could not reload configuration file `%s'", nm);
483 mergechildren(cf->children, lconfig->children);
488 static void chldhandler(int sig)
493 pid = waitpid(-1, NULL, WNOHANG);
497 static void sighandler(int sig)
503 static void usage(FILE *out)
505 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
508 int main(int argc, char **argv)
517 while((c = getopt(argc, argv, "hN")) >= 0) {
530 if(argc - optind < 1) {
535 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
536 gconfig = readconfig(gcf);
540 if((lconfig = readconfig(argv[optind])) == NULL) {
541 flog(LOG_ERR, "could not read `%s'", argv[optind]);
544 signal(SIGCHLD, chldhandler);
545 signal(SIGHUP, sighandler);
546 signal(SIGPIPE, sighandler);
549 reloadconf(argv[optind]);
552 if((fd = recvreq(0, &req)) < 0) {
556 flog(LOG_ERR, "recvreq: %s", strerror(errno));