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;
76 static struct config *config;
78 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 if(pat->childnm != NULL)
96 static void freechild(struct child *ch)
107 static struct child *newchild(char *name, int type)
112 ch->name = sstrdup(name);
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 config *readconfig(char *filename)
184 if((s = fopen(filename, "r")) == NULL)
192 if(fgets(line, sizeof(line), s) == NULL) {
197 for(p = line; *p; p++) {
203 ind = isspace(line[0]);
210 flog(LOG_WARNING, "%s%i: unexpected line indentation in global scope", filename, lno);
214 } else if(!strcmp(w[0], "child")) {
216 flog(LOG_WARNING, "%s:%i: missing name in child declaration", filename, lno);
219 child = newchild(w[1], CH_SOCKET);
221 } else if(!strcmp(w[0], "fchild")) {
223 flog(LOG_WARNING, "%s:%i: missing name in child declaration", filename, lno);
226 child = newchild(w[1], CH_FORK);
228 } else if(!strcmp(w[0], "match")) {
232 flog(LOG_WARNING, "%s:%i: unknown directive %s", filename, lno, w[0]);
235 } else if(state == 1) {
238 } else if(!strcmp(w[0], "exec")) {
240 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", filename, lno);
243 child->argv = szmalloc(sizeof(*child->argv) * argc);
244 for(i = 0; i < argc - 1; i++)
245 child->argv[i] = sstrdup(w[i + 1]);
247 flog(LOG_WARNING, "%s:%i: unknown directive %s", filename, lno, w[0]);
251 if(child->argv == NULL) {
252 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", filename, lno, child->name);
256 child->next = cf->children;
257 cf->children = child;
260 } else if(state == 2) {
264 } else if(!strcmp(w[0], "point") ||
265 !strcmp(w[0], "url") ||
266 !strcmp(w[0], "method")) {
268 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", w[0], filename, lno);
272 if(strchr(w[2], 'i'))
275 if((regex = regalloc(w[1], rxfl)) == NULL) {
276 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", w[0], filename, lno);
280 if(!strcmp(w[0], "point"))
281 rule->type = PAT_REST;
282 else if(!strcmp(w[0], "url"))
283 rule->type = PAT_URL;
284 else if(!strcmp(w[0], "method"))
285 rule->type = PAT_METHOD;
286 rule->pattern = regex;
288 if(strchr(w[2], 's'))
289 rule->fl |= PATFL_MSS;
291 } else if(!strcmp(w[0], "header")) {
293 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", filename, lno);
297 if(strchr(w[3], 'i'))
300 if((regex = regalloc(w[2], rxfl)) == NULL) {
301 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", filename, lno);
305 rule->type = PAT_HEADER;
306 rule->header = sstrdup(w[1]);
307 rule->pattern = regex;
309 if(strchr(w[3], 's'))
310 rule->fl |= PATFL_MSS;
312 } else if(!strcmp(w[0], "all")) {
313 newrule(pat)->type = PAT_ALL;
314 } else if(!strcmp(w[0], "default")) {
315 newrule(pat)->type = PAT_DEFAULT;
316 } else if(!strcmp(w[0], "handler")) {
318 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", filename, lno);
321 if(pat->childnm != NULL)
323 pat->childnm = sstrdup(w[1]);
324 } else if(!strcmp(w[0], "restpat")) {
326 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", filename, lno);
329 if(pat->restpat != NULL)
331 pat->restpat = sstrdup(w[1]);
333 flog(LOG_WARNING, "%s:%i: unknown directive %s", filename, lno, w[0]);
337 if(pat->rules[0] == NULL) {
338 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", filename, lno);
342 if(pat->childnm == NULL) {
343 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", filename, lno);
347 pat->next = cf->patterns;
365 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
374 for(mc = 0; mstr[mc]; mc++);
376 for(p = pat->restpat; *p; ) {
379 if((*p >= '0') && (*p <= '9')) {
381 bufcatstr(buf, mstr[*p - '0']);
383 } else if(*p == '_') {
384 bufcatstr(buf, req->rest);
386 } else if(*p == '$') {
389 } else if(*p == '{') {
390 if((p2 = strchr(p, '{')) == NULL) {
393 hdr = getheader(req, sprintf3("$.*s", p2 - p - 1, p + 1));
404 replrest(req, buf.b);
408 static char *findmatch(struct config *cf, struct hthead *req, int trydefault)
419 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
421 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
423 if(rule->type == PAT_REST) {
424 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
428 } else if(rule->type == PAT_URL) {
429 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
431 } else if(rule->type == PAT_METHOD) {
432 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
434 } else if(rule->type == PAT_HEADER) {
435 if(!(pstr = getheader(req, rule->header)))
437 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
439 } else if(rule->type == PAT_ALL) {
440 } else if(rule->type == PAT_DEFAULT) {
444 if(matched && (rule->fl & PATFL_MSS)) {
446 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
449 for(o = 0; o < 10; o++) {
453 mstr = szmalloc((o + 1) * sizeof(*mstr));
454 for(o = 0; o < 10; o++) {
457 mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
458 memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
459 mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
465 exprestpat(req, pat, mstr);
466 } else if(rmo != -1) {
467 replrest(req, req->rest + rmo);
471 return(pat->childnm);
481 static void forkchild(struct child *ch)
483 ch->fd = stdmkchild(ch->argv);
486 static void passreq(struct child *ch, struct hthead *req, int fd)
490 if(sendreq(ch->fd, req, fd)) {
492 /* Assume that the child has crashed and restart it. */
494 if(!sendreq(ch->fd, req, fd))
497 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
503 static void serve(struct hthead *req, int fd)
508 if(((chnm = findmatch(config, req, 0)) == NULL) && ((chnm = findmatch(config, req, 1)) == NULL)) {
509 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
512 if((ch = getchild(config, chnm)) == NULL) {
513 flog(LOG_ERR, "child %s requested, but was not declared", chnm);
514 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", chnm);
518 if(ch->type == CH_SOCKET) {
519 passreq(ch, req, fd);
520 } else if(ch->type == CH_FORK) {
521 stdforkserve(ch->argv, req, fd);
525 int main(int argc, char **argv)
531 flog(LOG_ERR, "usage: patplex CONFIGFILE");
534 config = readconfig(argv[1]);
535 signal(SIGCHLD, SIG_IGN);
537 if((fd = recvreq(0, &req)) < 0) {
539 flog(LOG_ERR, "recvreq: %s", strerror(errno));