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/>.
48 struct child *children;
49 struct pattern *patterns;
66 static struct config *gconfig, *lconfig;
68 static void freepattern(struct pattern *pat)
72 for(rule = pat->rules; *rule; rule++) {
73 if((*rule)->header != NULL)
74 free((*rule)->header);
75 if((*rule)->pattern != NULL) {
76 regfree((*rule)->pattern);
77 free((*rule)->pattern);
81 if(pat->childnm != NULL)
86 static struct child *getchild(struct config *cf, char *name)
90 for(ch = cf->children; ch; ch = ch->next) {
91 if(!strcmp(ch->name, name))
97 static struct rule *newrule(struct pattern *pat)
102 for(i = 0; pat->rules[i]; i++);
103 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
104 rule = pat->rules[i] = szmalloc(sizeof(*rule));
105 pat->rules[i + 1] = NULL;
109 static struct pattern *newpattern(void)
114 pat->rules = szmalloc(sizeof(*pat->rules));
118 static regex_t *regalloc(char *regex, int flags)
125 if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
126 regerror(res, ret, errbuf, sizeof(errbuf));
127 flog(LOG_WARNING, "%s: %s", regex, errbuf);
134 static struct pattern *parsepattern(struct cfstate *s)
142 if(!strcmp(s->argv[0], "match")) {
152 if(!strcmp(s->argv[0], "point") ||
153 !strcmp(s->argv[0], "url") ||
154 !strcmp(s->argv[0], "method")) {
156 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
160 if(strchr(s->argv[2], 'i'))
163 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
164 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
168 if(!strcmp(s->argv[0], "point"))
169 rule->type = PAT_REST;
170 else if(!strcmp(s->argv[0], "url"))
171 rule->type = PAT_URL;
172 else if(!strcmp(s->argv[0], "method"))
173 rule->type = PAT_METHOD;
174 rule->pattern = regex;
176 if(strchr(s->argv[2], 's'))
177 rule->fl |= PATFL_MSS;
179 } else if(!strcmp(s->argv[0], "header")) {
181 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
185 if(strchr(s->argv[3], 'i'))
188 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
189 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
193 rule->type = PAT_HEADER;
194 rule->header = sstrdup(s->argv[1]);
195 rule->pattern = regex;
197 if(strchr(s->argv[3], 's'))
198 rule->fl |= PATFL_MSS;
200 } else if(!strcmp(s->argv[0], "all")) {
201 newrule(pat)->type = PAT_ALL;
202 } else if(!strcmp(s->argv[0], "default")) {
203 newrule(pat)->type = PAT_DEFAULT;
204 } else if(!strcmp(s->argv[0], "handler")) {
206 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
209 if(pat->childnm != NULL)
211 pat->childnm = sstrdup(s->argv[1]);
212 } else if(!strcmp(s->argv[0], "restpat")) {
214 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
217 if(pat->restpat != NULL)
219 pat->restpat = sstrdup(s->argv[1]);
220 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
223 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
227 if(pat->rules[0] == NULL) {
228 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
232 if(pat->childnm == NULL) {
233 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
240 static struct config *readconfig(char *filename)
248 if((in = fopen(filename, "r")) == NULL) {
249 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
252 s = mkcfparser(in, filename);
257 if((ch = parsechild(s)) != NULL) {
258 ch->next = cf->children;
260 } else if((pat = parsepattern(s)) != NULL) {
261 pat->next = cf->patterns;
263 } else if(!strcmp(s->argv[0], "eof")) {
266 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
275 static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
284 for(mc = 0; mstr[mc]; mc++);
286 for(p = pat->restpat; *p; ) {
289 if((*p >= '0') && (*p <= '9')) {
291 bufcatstr(buf, mstr[*p - '0']);
293 } else if(*p == '_') {
294 bufcatstr(buf, req->rest);
296 } else if(*p == '$') {
299 } else if(*p == '{') {
300 if((p2 = strchr(p, '}')) == NULL) {
303 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
314 replrest(req, buf.b);
318 static char *findmatch(struct config *cf, struct hthead *req, int trydefault)
329 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
331 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
333 if(rule->type == PAT_REST) {
334 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
338 } else if(rule->type == PAT_URL) {
339 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
341 } else if(rule->type == PAT_METHOD) {
342 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
344 } else if(rule->type == PAT_HEADER) {
345 if(!(pstr = getheader(req, rule->header)))
347 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
349 } else if(rule->type == PAT_ALL) {
350 } else if(rule->type == PAT_DEFAULT) {
354 if(matched && (rule->fl & PATFL_MSS)) {
356 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
359 for(o = 0; o < 10; o++) {
363 mstr = szmalloc((o + 1) * sizeof(*mstr));
364 for(o = 0; o < 10; o++) {
367 mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
368 memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
369 mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
375 exprestpat(req, pat, mstr);
376 } else if(rmo != -1) {
377 replrest(req, req->rest + rmo);
381 return(pat->childnm);
391 static void serve(struct hthead *req, int fd)
398 chnm = findmatch(lconfig, req, 0);
400 chnm = findmatch(lconfig, req, 1);
401 if(gconfig != NULL) {
403 chnm = findmatch(gconfig, req, 0);
405 chnm = findmatch(gconfig, req, 1);
408 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
413 ch = getchild(lconfig, chnm);
414 if(gconfig != NULL) {
416 ch = getchild(gconfig, chnm);
419 flog(LOG_ERR, "child %s requested, but was not declared", chnm);
420 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", chnm);
424 if(childhandle(ch, req, fd))
425 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
428 static void usage(FILE *out)
430 fprintf(out, "usage: patplex [-hN] CONFIGFILE\n");
433 int main(int argc, char **argv)
442 while((c = getopt(argc, argv, "hN")) >= 0) {
455 if(argc - optind < 1) {
460 if((gcf = findstdconf("ashd/patplex.rc")) != NULL) {
461 gconfig = readconfig(gcf);
465 lconfig = readconfig(argv[optind]);
466 signal(SIGCHLD, SIG_IGN);
468 if((fd = recvreq(0, &req)) < 0) {
470 flog(LOG_ERR, "recvreq: %s", strerror(errno));