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/>.
41 #define PAT_BASENAME 0
42 #define PAT_PATHNAME 1
47 struct config *next, *prev;
50 struct child *children;
51 struct pattern *patterns;
65 struct config *cflist;
68 static void freepattern(struct pattern *pat)
72 for(rule = pat->rules; *rule; rule++) {
73 if((*rule)->pattern != NULL)
74 free((*rule)->pattern);
77 if(pat->childnm != NULL)
82 static void freeconfig(struct config *cf)
84 struct child *ch, *nch;
85 struct pattern *pat, *npat;
88 cf->prev->next = cf->next;
90 cf->next->prev = cf->prev;
94 for(ch = cf->children; ch != NULL; ch = nch) {
98 for(pat = cf->patterns; pat != NULL; pat = npat) {
105 static struct child *getchild(struct config *cf, char *name)
109 for(ch = cf->children; ch; ch = ch->next) {
110 if(!strcmp(ch->name, name))
116 static struct rule *newrule(struct pattern *pat)
121 for(i = 0; pat->rules[i]; i++);
122 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
123 rule = pat->rules[i] = szmalloc(sizeof(*rule));
124 pat->rules[i + 1] = NULL;
128 static struct pattern *newpattern(void)
133 pat->rules = szmalloc(sizeof(*pat->rules));
137 static struct pattern *parsepattern(struct cfstate *s)
143 if(!strcmp(s->argv[0], "match")) {
153 if(!strcmp(s->argv[0], "filename")) {
155 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
159 rule->type = PAT_BASENAME;
160 rule->pattern = sstrdup(s->argv[1]);
161 } else if(!strcmp(s->argv[0], "pathname")) {
163 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
167 rule->type = PAT_PATHNAME;
168 rule->pattern = sstrdup(s->argv[1]);
169 } else if(!strcmp(s->argv[0], "all")) {
170 newrule(pat)->type = PAT_ALL;
171 } else if(!strcmp(s->argv[0], "default")) {
172 newrule(pat)->type = PAT_DEFAULT;
173 } else if(!strcmp(s->argv[0], "handler")) {
175 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
178 if(pat->childnm != NULL)
180 pat->childnm = sstrdup(s->argv[1]);
181 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
184 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
188 if(pat->rules[0] == NULL) {
189 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
193 if(pat->childnm == NULL) {
194 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
201 static struct config *emptyconfig(void)
209 static struct config *readconfig(char *file)
217 if((in = fopen(file, "r")) == NULL) {
218 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
221 s = mkcfparser(in, file);
226 if((child = parsechild(s)) != NULL) {
227 child->next = cf->children;
228 cf->children = child;
229 } else if((pat = parsepattern(s)) != NULL) {
230 pat->next = cf->patterns;
232 } else if(!strcmp(s->argv[0], "eof")) {
235 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
244 static struct config *getconfig(char *path)
251 fn = sprintf3("%s/.htrc", path);
252 for(cf = cflist; cf != NULL; cf = cf->next) {
253 if(!strcmp(cf->path, path)) {
254 if(now - cf->lastck > 5) {
255 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
264 if(access(fn, R_OK) || stat(fn, &sb)) {
268 if((cf = readconfig(fn)) == NULL)
272 cf->path = sstrdup(path);
280 static struct config **getconfigs(char *file)
282 static struct config **ret = NULL;
295 if((p = strrchr(tmp, '/')) == NULL)
298 if((cf = getconfig(tmp)) != NULL)
302 if((cf = getconfig(".")) != NULL)
308 static struct child *findchild(char *file, char *name)
314 cfs = getconfigs(file);
315 for(i = 0; cfs[i] != NULL; i++) {
316 if((ch = getchild(cfs[i], name)) != NULL)
322 static struct pattern *findmatch(char *file, int trydefault)
330 if((bn = strrchr(file, '/')) != NULL)
334 cfs = getconfigs(file);
335 for(c = 0; cfs[c] != NULL; c++) {
336 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
337 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
338 if(rule->type == PAT_BASENAME) {
339 if(fnmatch(rule->pattern, bn, 0))
341 } else if(rule->type == PAT_PATHNAME) {
342 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
344 } else if(rule->type == PAT_ALL) {
345 } else if(rule->type == PAT_DEFAULT) {
357 static void handlefile(struct hthead *req, int fd, char *path)
362 headappheader(req, "X-Ash-File", path);
363 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
364 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
367 if((ch = findchild(path, pat->childnm)) == NULL) {
368 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
369 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
373 if(childhandle(ch, req, fd))
374 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
377 static void handledir(struct hthead *req, int fd, char *path)
380 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet.");
383 static int checkdir(struct hthead *req, int fd, char *path)
388 static void serve(struct hthead *req, int fd)
390 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
400 if((p2 = strchr(p, '/')) == NULL) {
407 if(stat(path, &sb)) {
408 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
409 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
414 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
419 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
426 * First, check the name verbatimely:
428 buf = sprintf3("%s/%s", path, p);
429 if(!stat(buf, &sb)) {
430 if(S_ISDIR(sb.st_mode)) {
432 if(!strcmp(path, "."))
435 path = sprintf2("%s/%s", path, p);
438 stdredir(req, fd, 301, sprintf3("%s/", p));
441 if(checkdir(req, fd, path))
445 if(S_ISREG(sb.st_mode)) {
447 path = sprintf2("%s/%s", path, p);
451 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
456 * Check the file extensionlessly:
458 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
459 while((dent = readdir(dir)) != NULL) {
460 buf = sprintf3("%s/%s", path, dent->d_name);
461 if((p3 = strchr(dent->d_name, '.')) != NULL)
463 if(strcmp(dent->d_name, p))
467 if(!S_ISREG(sb.st_mode))
479 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
491 if(!strncmp(path, "./", 2))
492 memmove(path, path + 2, strlen(path + 2) + 1);
493 if(S_ISDIR(sb.st_mode)) {
494 handledir(req, fd, path);
495 } else if(S_ISREG(sb.st_mode)) {
496 handlefile(req, fd, path);
498 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
504 /* No special handling, for now at least. */
509 int main(int argc, char **argv)
515 flog(LOG_ERR, "usage: dirplex DIR");
519 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
522 signal(SIGCHLD, SIG_IGN);
524 if((fd = recvreq(0, &req)) < 0) {
526 flog(LOG_ERR, "recvreq: %s", strerror(errno));