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 static struct config *cflist;
66 static struct config *gconfig, *lconfig;
69 static void freepattern(struct pattern *pat)
73 for(rule = pat->rules; *rule; rule++) {
74 if((*rule)->pattern != NULL)
75 free((*rule)->pattern);
78 if(pat->childnm != NULL)
83 static void freeconfig(struct config *cf)
85 struct child *ch, *nch;
86 struct pattern *pat, *npat;
89 cf->prev->next = cf->next;
91 cf->next->prev = cf->prev;
96 for(ch = cf->children; ch != NULL; ch = nch) {
100 for(pat = cf->patterns; pat != NULL; pat = npat) {
107 static struct child *getchild(struct config *cf, char *name)
111 for(ch = cf->children; ch; ch = ch->next) {
112 if(!strcmp(ch->name, name))
118 static struct rule *newrule(struct pattern *pat)
123 for(i = 0; pat->rules[i]; i++);
124 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
125 rule = pat->rules[i] = szmalloc(sizeof(*rule));
126 pat->rules[i + 1] = NULL;
130 static struct pattern *newpattern(void)
135 pat->rules = szmalloc(sizeof(*pat->rules));
139 static struct pattern *parsepattern(struct cfstate *s)
145 if(!strcmp(s->argv[0], "match")) {
155 if(!strcmp(s->argv[0], "filename")) {
157 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
161 rule->type = PAT_BASENAME;
162 rule->pattern = sstrdup(s->argv[1]);
163 } else if(!strcmp(s->argv[0], "pathname")) {
165 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
169 rule->type = PAT_PATHNAME;
170 rule->pattern = sstrdup(s->argv[1]);
171 } else if(!strcmp(s->argv[0], "all")) {
172 newrule(pat)->type = PAT_ALL;
173 } else if(!strcmp(s->argv[0], "default")) {
174 newrule(pat)->type = PAT_DEFAULT;
175 } else if(!strcmp(s->argv[0], "handler")) {
177 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
180 if(pat->childnm != NULL)
182 pat->childnm = sstrdup(s->argv[1]);
183 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
186 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
190 if(pat->rules[0] == NULL) {
191 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
195 if(pat->childnm == NULL) {
196 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
203 static struct config *emptyconfig(void)
211 static struct config *readconfig(char *file)
219 if((in = fopen(file, "r")) == NULL) {
220 flog(LOG_WARNING, "%s: %s", file, strerror(errno));
223 s = mkcfparser(in, file);
228 if((child = parsechild(s)) != NULL) {
229 child->next = cf->children;
230 cf->children = child;
231 } else if((pat = parsepattern(s)) != NULL) {
232 pat->next = cf->patterns;
234 } else if(!strcmp(s->argv[0], "eof")) {
237 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
246 static struct config *getconfig(char *path)
253 fn = sprintf3("%s/.htrc", path);
254 for(cf = cflist; cf != NULL; cf = cf->next) {
255 if(!strcmp(cf->path, path)) {
256 if(now - cf->lastck > 5) {
257 if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
266 if(access(fn, R_OK) || stat(fn, &sb)) {
270 if((cf = readconfig(fn)) == NULL)
274 cf->path = sstrdup(path);
282 static struct config **getconfigs(char *file)
284 static struct config **ret = NULL;
297 if((p = strrchr(tmp, '/')) == NULL)
300 if((cf = getconfig(tmp)) != NULL)
304 if((cf = getconfig(".")) != NULL)
307 bufadd(buf, lconfig);
309 bufadd(buf, gconfig);
314 static struct child *findchild(char *file, char *name)
320 cfs = getconfigs(file);
321 for(i = 0; cfs[i] != NULL; i++) {
322 if((ch = getchild(cfs[i], name)) != NULL)
328 static struct pattern *findmatch(char *file, int trydefault)
336 if((bn = strrchr(file, '/')) != NULL)
340 cfs = getconfigs(file);
341 for(c = 0; cfs[c] != NULL; c++) {
342 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
343 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
344 if(rule->type == PAT_BASENAME) {
345 if(fnmatch(rule->pattern, bn, 0))
347 } else if(rule->type == PAT_PATHNAME) {
348 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
350 } else if(rule->type == PAT_ALL) {
351 } else if(rule->type == PAT_DEFAULT) {
363 static void handlefile(struct hthead *req, int fd, char *path)
368 headappheader(req, "X-Ash-File", path);
369 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
370 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
373 if((ch = findchild(path, pat->childnm)) == NULL) {
374 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
375 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
379 if(childhandle(ch, req, fd))
380 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
383 static void handledir(struct hthead *req, int fd, char *path)
386 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet.");
389 static int checkdir(struct hthead *req, int fd, char *path)
394 static void serve(struct hthead *req, int fd)
396 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
406 if((p2 = strchr(p, '/')) == NULL) {
413 if(stat(path, &sb)) {
414 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
415 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
420 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
425 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
432 * First, check the name verbatimely:
434 buf = sprintf3("%s/%s", path, p);
435 if(!stat(buf, &sb)) {
436 if(S_ISDIR(sb.st_mode)) {
438 if(!strcmp(path, "."))
441 path = sprintf2("%s/%s", path, p);
444 stdredir(req, fd, 301, sprintf3("%s/", p));
447 if(checkdir(req, fd, path))
451 if(S_ISREG(sb.st_mode)) {
453 path = sprintf2("%s/%s", path, p);
457 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
462 * Check the file extensionlessly:
464 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
465 while((dent = readdir(dir)) != NULL) {
466 buf = sprintf3("%s/%s", path, dent->d_name);
467 if((p3 = strchr(dent->d_name, '.')) != NULL)
469 if(strcmp(dent->d_name, p))
473 if(!S_ISREG(sb.st_mode))
485 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
497 if(!strncmp(path, "./", 2))
498 memmove(path, path + 2, strlen(path + 2) + 1);
499 if(S_ISDIR(sb.st_mode)) {
500 handledir(req, fd, path);
501 } else if(S_ISREG(sb.st_mode)) {
502 handlefile(req, fd, path);
504 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
510 /* No special handling, for now at least. */
515 static void usage(FILE *out)
517 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
520 int main(int argc, char **argv)
530 while((c = getopt(argc, argv, "hNc:")) >= 0) {
546 if(argc - optind < 1) {
551 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
552 gconfig = readconfig(gcf);
557 if((lconfig = readconfig(lcf)) == NULL)
560 if(chdir(argv[optind])) {
561 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
564 signal(SIGCHLD, SIG_IGN);
566 if((fd = recvreq(0, &req)) < 0) {
568 flog(LOG_ERR, "recvreq: %s", strerror(errno));