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)
50 for(rule = pat->rules; *rule; rule++)
52 if(pat->childnm != NULL)
58 static void freeconfig(struct config *cf)
60 struct child *ch, *nch;
61 struct pattern *pat, *npat;
64 cf->prev->next = cf->next;
66 cf->next->prev = cf->prev;
71 for(ch = cf->children; ch != NULL; ch = nch) {
75 for(pat = cf->patterns; pat != NULL; pat = npat) {
80 if(cf->capture != NULL)
85 struct child *getchild(struct config *cf, char *name)
89 for(ch = cf->children; ch; ch = ch->next) {
90 if(!strcmp(ch->name, name))
96 static struct rule *newrule(struct pattern *pat)
101 for(i = 0; pat->rules[i]; i++);
102 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
103 rule = pat->rules[i] = szmalloc(sizeof(*rule));
104 pat->rules[i + 1] = NULL;
108 static struct pattern *newpattern(void)
113 pat->rules = szmalloc(sizeof(*pat->rules));
117 static char **cadup(char **w)
123 ret = smalloc(sizeof(*ret) * (l + 1));
124 for(i = 0; i < l; i++)
125 ret[i] = sstrdup(w[i]);
130 static struct pattern *parsepattern(struct cfstate *s)
136 if(!strcmp(s->argv[0], "match")) {
143 if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
148 if(!strcmp(s->argv[0], "filename")) {
150 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
154 rule->type = PAT_BASENAME;
155 rule->patterns = cadup(s->argv + 1);
156 } else if(!strcmp(s->argv[0], "pathname")) {
158 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
162 rule->type = PAT_PATHNAME;
163 rule->patterns = cadup(s->argv + 1);
164 } else if(!strcmp(s->argv[0], "all")) {
165 newrule(pat)->type = PAT_ALL;
166 } else if(!strcmp(s->argv[0], "default")) {
167 newrule(pat)->type = PAT_DEFAULT;
168 } else if(!strcmp(s->argv[0], "local")) {
169 newrule(pat)->type = PAT_LOCAL;
170 } else if(!strcmp(s->argv[0], "handler")) {
172 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
175 if(pat->childnm != NULL)
177 pat->childnm = sstrdup(s->argv[1]);
178 } else if(!strcmp(s->argv[0], "fork")) {
179 pat->fchild = cadup(s->argv + 1);
180 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
183 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
187 if(pat->rules[0] == NULL) {
188 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
192 if((pat->childnm == NULL) && (pat->fchild == NULL)) {
193 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
200 static struct config *emptyconfig(void)
208 struct config *readconfig(char *file)
216 if((in = fopen(file, "r")) == NULL) {
217 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
220 s = mkcfparser(in, file);
225 if((child = parsechild(s)) != NULL) {
226 child->next = cf->children;
227 cf->children = child;
228 } else if((pat = parsepattern(s)) != NULL) {
229 pat->next = cf->patterns;
231 } else if(!strcmp(s->argv[0], "index-file")) {
235 cf->index = cadup(s->argv + 1);
236 } else if(!strcmp(s->argv[0], "capture")) {
238 flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
241 if(cf->capture != NULL)
243 cf->capture = sstrdup(s->argv[1]);
244 } else if(!strcmp(s->argv[0], "eof")) {
247 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
256 struct config *getconfig(char *path)
258 struct config *cf, *ocf;
263 fn = sprintf3("%s/.htrc", path);
264 for(cf = cflist; cf != NULL; cf = cf->next) {
265 if(!strcmp(cf->path, path)) {
266 if(now - cf->lastck > 5) {
267 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
275 if(access(fn, R_OK) || stat(fn, &sb)) {
279 if((cf = readconfig(fn)) == NULL)
284 mergechildren(cf->children, ocf->children);
287 cf->path = sstrdup(path);
298 struct config **getconfigs(char *file)
300 static struct config **ret = NULL;
313 if((p = strrchr(tmp, '/')) == NULL)
316 if((cf = getconfig(tmp)) != NULL)
320 if((cf = getconfig(".")) != NULL)
323 bufadd(buf, lconfig);
325 bufadd(buf, gconfig);
330 struct child *findchild(char *file, char *name, struct config **cf)
338 cfs = getconfigs(file);
339 for(i = 0; cfs[i] != NULL; i++) {
340 if((ch = getchild(cfs[i], name)) != NULL) {
346 if(!strcmp(name, ".notfound"))
351 struct pattern *findmatch(char *file, int trydefault, int dir)
360 if((bn = strrchr(file, '/')) != NULL)
364 cfs = getconfigs(file);
365 for(c = 0; cfs[c] != NULL; c++) {
366 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
367 if(!dir && (pat->type == PT_DIR))
369 if(dir && (pat->type != PT_DIR))
371 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
372 if(rule->type == PAT_BASENAME) {
373 for(o = 0; rule->patterns[o] != NULL; o++) {
374 if(!fnmatch(rule->patterns[o], bn, 0))
377 if(rule->patterns[o] == NULL)
379 } else if(rule->type == PAT_PATHNAME) {
380 for(o = 0; rule->patterns[o] != NULL; o++) {
381 if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
384 if(rule->patterns[o] == NULL)
386 } else if(rule->type == PAT_ALL) {
387 } else if(rule->type == PAT_DEFAULT) {
390 } else if(rule->type == PAT_LOCAL) {
391 if(cfs[c]->path == NULL)
393 pl = strlen(cfs[c]->path);
394 if(!((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/') && !strchr(file + pl + 1, '/')))
403 return(findmatch(file, 1, dir));
407 static int donotfound(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
409 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
413 static struct chandler i_notfound = {
414 .handle = donotfound,
417 static struct child s_notfound = {
419 .iface = &i_notfound,
421 struct child *notfound = &s_notfound;