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/>.
37 static struct config *cflist;
38 struct config *gconfig, *lconfig;
40 static void freerule(struct rule *rule)
42 freeca(rule->patterns);
46 static void freepattern(struct pattern *pat)
51 for(rule = pat->rules; *rule; rule++)
53 while((head = pat->headers) != NULL) {
54 pat->headers = head->next;
59 if(pat->childnm != NULL)
65 static void freeconfig(struct config *cf)
67 struct child *ch, *nch;
68 struct pattern *pat, *npat;
71 cf->prev->next = cf->next;
73 cf->next->prev = cf->prev;
78 for(ch = cf->children; ch != NULL; ch = nch) {
82 for(pat = cf->patterns; pat != NULL; pat = npat) {
88 if(cf->capture != NULL)
90 if(cf->reparse != NULL)
95 struct child *getchild(struct config *cf, char *name)
99 for(ch = cf->children; ch; ch = ch->next) {
100 if(!strcmp(ch->name, name))
106 static struct rule *newrule(struct pattern *pat)
111 for(i = 0; pat->rules[i]; i++);
112 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
113 rule = pat->rules[i] = szmalloc(sizeof(*rule));
114 pat->rules[i + 1] = NULL;
118 static struct pattern *newpattern(void)
123 pat->rules = szmalloc(sizeof(*pat->rules));
127 static char **cadup(char **w)
133 ret = smalloc(sizeof(*ret) * (l + 1));
134 for(i = 0; i < l; i++)
135 ret[i] = sstrdup(w[i]);
140 static struct pattern *parsepattern(struct cfstate *s)
144 struct headmod *head;
147 if(!strcmp(s->argv[0], "match")) {
154 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
156 else if((s->argc > 1) && !strcmp(s->argv[1], "notfound"))
157 pat->type = PT_NOTFOUND;
163 if(!strcmp(s->argv[0], "filename")) {
165 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
169 rule->type = PAT_BASENAME;
170 rule->patterns = cadup(s->argv + 1);
171 } else if(!strcmp(s->argv[0], "pathname")) {
173 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
177 rule->type = PAT_PATHNAME;
178 rule->patterns = cadup(s->argv + 1);
179 } else if(!strcmp(s->argv[0], "all")) {
180 newrule(pat)->type = PAT_ALL;
181 } else if(!strcmp(s->argv[0], "default")) {
182 newrule(pat)->type = PAT_DEFAULT;
183 } else if(!strcmp(s->argv[0], "local")) {
184 newrule(pat)->type = PAT_LOCAL;
185 } else if(!strcmp(s->argv[0], "handler")) {
187 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
190 if(pat->childnm != NULL)
192 pat->childnm = sstrdup(s->argv[1]);
193 } else if(!strcmp(s->argv[0], "fork")) {
194 pat->fchild = cadup(s->argv + 1);
195 } else if(!strcmp(s->argv[0], "set") || !strcmp(s->argv[0], "xset")) {
197 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
201 if(!strcmp(s->argv[0], "xset"))
202 head->name = sprintf2("X-Ash-%s", s->argv[1]);
204 head->name = sstrdup(s->argv[1]);
205 head->value = sstrdup(s->argv[2]);
206 head->next = pat->headers;
208 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
211 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
215 if(pat->rules[0] == NULL) {
216 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
220 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
221 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
228 static struct config *emptyconfig(void)
236 struct config *readconfig(char *file)
244 if((in = fopen(file, "r")) == NULL) {
245 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
248 s = mkcfparser(in, file);
253 if((child = parsechild(s)) != NULL) {
254 child->next = cf->children;
255 cf->children = child;
256 } else if((pat = parsepattern(s)) != NULL) {
257 pat->next = cf->patterns;
259 } else if(!strcmp(s->argv[0], "index-file")) {
261 cf->index = cadup(s->argv + 1);
262 } else if(!strcmp(s->argv[0], "dot-allow")) {
263 freeca(cf->dotallow);
264 cf->dotallow = cadup(s->argv + 1);
265 } else if(!strcmp(s->argv[0], "capture")) {
267 flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
270 if(cf->capture != NULL)
272 cf->capture = sstrdup(s->argv[1]);
274 if((s->argc > 2) && strchr(s->argv[2], 'D'))
276 } else if(!strcmp(s->argv[0], "reparse")) {
278 flog(LOG_WARNING, "%s:%i: missing argument to reparse declaration", s->file, s->lno);
281 if(cf->reparse != NULL)
283 cf->reparse = sstrdup(s->argv[1]);
285 if((s->argc > 2) && strchr(s->argv[2], 'c'))
287 } else if(!strcmp(s->argv[0], "eof")) {
290 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
299 struct config *getconfig(char *path)
301 struct config *cf, *ocf;
306 fn = sprintf3("%s/.htrc", path);
307 for(cf = cflist; cf != NULL; cf = cf->next) {
308 if(!strcmp(cf->path, path)) {
309 if(now - cf->lastck > 5) {
311 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
318 if(access(fn, R_OK) || stat(fn, &sb)) {
322 if((cf = readconfig(fn)) == NULL)
327 mergechildren(cf->children, ocf->children);
330 cf->path = sstrdup(path);
341 struct config **getconfigs(char *file)
343 static struct config **ret = NULL;
354 if(!strncmp(file, "./", 2))
358 if((p = strrchr(tmp, '/')) == NULL)
361 if((cf = getconfig(tmp)) != NULL)
365 if((cf = getconfig(".")) != NULL)
368 bufadd(buf, lconfig);
370 bufadd(buf, gconfig);
375 struct child *findchild(char *file, char *name, struct config **cf)
383 cfs = getconfigs(file);
384 for(i = 0; cfs[i] != NULL; i++) {
385 if((ch = getchild(cfs[i], name)) != NULL) {
391 if(!strcmp(name, ".notfound"))
396 struct pattern *findmatch(char *file, int trydefault, int type)
405 if((bn = strrchr(file, '/')) != NULL)
409 cfs = getconfigs(file);
410 for(c = 0; cfs[c] != NULL; c++) {
411 if(cfs[c]->path == NULL) {
414 pl = strlen(cfs[c]->path);
415 if((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/'))
418 ln = file; /* This should only happen in the base directory. */
420 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
421 if(pat->type != type)
423 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
424 if(rule->type == PAT_BASENAME) {
425 for(o = 0; rule->patterns[o] != NULL; o++) {
426 if(!fnmatch(rule->patterns[o], bn, 0))
429 if(rule->patterns[o] == NULL)
431 } else if(rule->type == PAT_PATHNAME) {
432 for(o = 0; rule->patterns[o] != NULL; o++) {
433 if(!fnmatch(rule->patterns[o], ln, FNM_PATHNAME))
436 if(rule->patterns[o] == NULL)
438 } else if(rule->type == PAT_ALL) {
439 } else if(rule->type == PAT_DEFAULT) {
442 } else if(rule->type == PAT_LOCAL) {
452 return(findmatch(file, 1, type));
456 static int donotfound(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
458 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
462 static struct chandler i_notfound = {
463 .handle = donotfound,
466 static struct child s_notfound = {
468 .iface = &i_notfound,
470 struct child *notfound = &s_notfound;