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") || !strcmp(s->argv[0], "xset")) {
254 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
258 if(!strcmp(s->argv[0], "xset"))
259 head->name = sprintf2("X-Ash-%s", s->argv[1]);
261 head->name = sstrdup(s->argv[1]);
262 head->value = sstrdup(s->argv[2]);
263 head->next = pat->headers;
265 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
268 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
272 if(pat->rules[0] == NULL) {
273 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
277 if(pat->childnm == NULL) {
278 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
285 static struct config *readconfig(char *filename)
293 if((in = fopen(filename, "r")) == NULL) {
294 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
297 s = mkcfparser(in, filename);
302 if((ch = parsechild(s)) != NULL) {
303 ch->next = cf->children;
305 } else if((pat = parsepattern(s)) != NULL) {
306 pat->next = cf->patterns;
308 } else if(!strcmp(s->argv[0], "eof")) {
311 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
320 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
329 for(mc = 0; mstr[mc]; mc++);
331 for(p = pat->restpat; *p; ) {
334 if((*p >= '0') && (*p <= '9')) {
336 bufcatstr(buf, mstr[*p - '0']);
338 } else if(*p == '_') {
339 bufcatstr(buf, req->rest);
341 } else if(*p == '$') {
344 } else if(*p == '{') {
345 if((p2 = strchr(p, '}')) == NULL) {
348 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
359 replrest(req, buf.b);
363 static struct pattern *findmatch(struct config *cf, struct hthead *req, int trydefault)
374 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
376 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
378 if(rule->type == PAT_REST) {
379 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
383 } else if(rule->type == PAT_URL) {
384 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
386 } else if(rule->type == PAT_METHOD) {
387 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
389 } else if(rule->type == PAT_HEADER) {
390 if(!(pstr = getheader(req, rule->header)))
392 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
394 } else if(rule->type == PAT_ALL) {
395 } else if(rule->type == PAT_DEFAULT) {
399 if(matched && (rule->fl & PATFL_MSS)) {
401 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
404 for(o = 0; o < 10; o++) {
408 mstr = szmalloc((o + 1) * sizeof(*mstr));
409 for(o = 0; o < 10; o++) {
412 mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
413 memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
414 mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
420 exprestpat(req, pat, mstr);
421 } else if(rmo != -1) {
422 replrest(req, req->rest + rmo);
436 static void serve(struct hthead *req, int fd)
439 struct headmod *head;
444 pat = findmatch(lconfig, req, 0);
446 pat = findmatch(lconfig, req, 1);
447 if(gconfig != NULL) {
449 pat = findmatch(gconfig, req, 0);
451 pat = findmatch(gconfig, req, 1);
454 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
459 ch = getchild(lconfig, pat->childnm);
460 if(gconfig != NULL) {
462 ch = getchild(gconfig, pat->childnm);
465 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
466 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
470 for(head = pat->headers; head != NULL; head = head->next) {
471 headrmheader(req, head->name);
472 headappheader(req, head->name, head->value);
474 if(childhandle(ch, req, fd, NULL, NULL))
475 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
478 static void reloadconf(char *nm)
482 if((cf = readconfig(nm)) == NULL) {
483 flog(LOG_WARNING, "could not reload configuration file `%s'", nm);
486 mergechildren(cf->children, lconfig->children);
491 static void chldhandler(int sig)
496 while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
498 flog(LOG_WARNING, "child process %i dumped core", pid);
502 static void sighandler(int sig)
508 static void usage(FILE *out)
510 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
513 int main(int argc, char **argv)
522 while((c = getopt(argc, argv, "hN")) >= 0) {
535 if(argc - optind < 1) {
540 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
541 gconfig = readconfig(gcf);
545 if((lconfig = readconfig(argv[optind])) == NULL) {
546 flog(LOG_ERR, "could not read `%s'", argv[optind]);
549 signal(SIGCHLD, chldhandler);
550 signal(SIGHUP, sighandler);
551 signal(SIGPIPE, sighandler);
554 reloadconf(argv[optind]);
557 if((fd = recvreq(0, &req)) < 0) {
561 flog(LOG_ERR, "recvreq: %s", strerror(errno));