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/>.
40 #define PAT_BASENAME 0
41 #define PAT_PATHNAME 1
46 struct config *next, *prev;
49 struct child *children;
50 struct pattern *patterns;
64 struct config *cflist;
66 static void freepattern(struct pattern *pat)
70 for(rule = pat->rules; *rule; rule++) {
71 if((*rule)->pattern != NULL)
72 free((*rule)->pattern);
75 if(pat->childnm != NULL)
80 static void freeconfig(struct config *cf)
82 struct child *ch, *nch;
83 struct pattern *pat, *npat;
86 cf->prev->next = cf->next;
88 cf->next->prev = cf->prev;
92 for(ch = cf->children; ch != NULL; ch = nch) {
96 for(pat = cf->patterns; pat != NULL; pat = npat) {
103 static struct child *getchild(struct config *cf, char *name)
107 for(ch = cf->children; ch; ch = ch->next) {
108 if(!strcmp(ch->name, name))
114 static struct rule *newrule(struct pattern *pat)
119 for(i = 0; pat->rules[i]; i++);
120 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
121 rule = pat->rules[i] = szmalloc(sizeof(*rule));
122 pat->rules[i + 1] = NULL;
126 static struct pattern *newpattern(void)
131 pat->rules = szmalloc(sizeof(*pat->rules));
135 static struct pattern *parsepattern(struct cfstate *s)
141 if(!strcmp(s->argv[0], "match")) {
151 if(!strcmp(s->argv[0], "filename")) {
153 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
157 rule->type = PAT_BASENAME;
158 rule->pattern = sstrdup(s->argv[1]);
159 } else if(!strcmp(s->argv[0], "pathname")) {
161 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
165 rule->type = PAT_PATHNAME;
166 rule->pattern = sstrdup(s->argv[1]);
167 } else if(!strcmp(s->argv[0], "all")) {
168 newrule(pat)->type = PAT_ALL;
169 } else if(!strcmp(s->argv[0], "default")) {
170 newrule(pat)->type = PAT_DEFAULT;
171 } else if(!strcmp(s->argv[0], "handler")) {
173 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
176 if(pat->childnm != NULL)
178 pat->childnm = sstrdup(s->argv[1]);
179 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
182 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
186 if(pat->rules[0] == NULL) {
187 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
191 if(pat->childnm == NULL) {
192 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
199 static struct config *readconfig(char *path)
209 p = sprintf3("%s/.htrc", path);
212 if((in = fopen(p, "r")) == NULL) {
213 flog(LOG_WARNING, "%s: %s", p, strerror(errno));
216 s = mkcfparser(in, p);
218 cf->mtime = sb.st_mtime;
219 cf->path = sstrdup(path);
223 if((child = parsechild(s)) != NULL) {
224 child->next = cf->children;
225 cf->children = child;
226 } else if((pat = parsepattern(s)) != NULL) {
227 pat->next = cf->patterns;
229 } else if(!strcmp(s->argv[0], "eof")) {
232 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
241 static struct config *getconfig(char *path)
246 for(cf = cflist; cf != NULL; cf = cf->next) {
247 if(!strcmp(cf->path, path)) {
248 if(stat(sprintf3("%s/.htrc", path), &sb))
250 if(sb.st_mtime != cf->mtime) {
257 if((cf = readconfig(path)) != NULL) {
264 static struct config **getconfigs(char *file)
266 static struct config **ret = NULL;
279 if((p = strrchr(tmp, '/')) == NULL)
282 if((cf = getconfig(tmp)) != NULL)
286 if((cf = getconfig(".")) != NULL)
292 static struct child *findchild(char *file, char *name)
298 cfs = getconfigs(file);
299 for(i = 0; cfs[i] != NULL; i++) {
300 if((ch = getchild(cfs[i], name)) != NULL)
306 static struct pattern *findmatch(char *file, int trydefault)
314 if((bn = strrchr(file, '/')) != NULL)
318 cfs = getconfigs(file);
319 for(c = 0; cfs[c] != NULL; c++) {
320 for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
321 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
322 if(rule->type == PAT_BASENAME) {
323 if(fnmatch(rule->pattern, bn, 0))
325 } else if(rule->type == PAT_PATHNAME) {
326 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
328 } else if(rule->type == PAT_ALL) {
329 } else if(rule->type == PAT_DEFAULT) {
341 static void handlefile(struct hthead *req, int fd, char *path)
346 headappheader(req, "X-Ash-File", path);
347 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
348 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
351 if((ch = findchild(path, pat->childnm)) == NULL) {
352 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
353 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
357 if(childhandle(ch, req, fd))
358 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
361 static void handledir(struct hthead *req, int fd, char *path)
364 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet.");
367 static int checkdir(struct hthead *req, int fd, char *path)
372 static void serve(struct hthead *req, int fd)
374 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
383 if((p2 = strchr(p, '/')) == NULL) {
390 if(stat(path, &sb)) {
391 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
392 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
397 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
402 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
409 * First, check the name verbatimely:
411 buf = sprintf3("%s/%s", path, p);
412 if(!stat(buf, &sb)) {
413 if(S_ISDIR(sb.st_mode)) {
415 if(!strcmp(path, "."))
418 path = sprintf2("%s/%s", path, p);
421 stdredir(req, fd, 301, sprintf3("%s/", p));
424 if(checkdir(req, fd, path))
428 if(S_ISREG(sb.st_mode)) {
430 path = sprintf2("%s/%s", path, p);
434 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
439 * Check the file extensionlessly:
441 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
442 while((dent = readdir(dir)) != NULL) {
443 buf = sprintf3("%s/%s", path, dent->d_name);
444 if((p3 = strchr(dent->d_name, '.')) != NULL)
446 if(strcmp(dent->d_name, p))
450 if(!S_ISREG(sb.st_mode))
462 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
474 if(!strncmp(path, "./", 2))
475 memmove(path, path + 2, strlen(path + 2) + 1);
476 if(S_ISDIR(sb.st_mode)) {
477 handledir(req, fd, path);
478 } else if(S_ISREG(sb.st_mode)) {
479 handlefile(req, fd, path);
481 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
487 /* No special handling, for now at least. */
492 int main(int argc, char **argv)
498 flog(LOG_ERR, "usage: dirplex DIR");
502 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
505 signal(SIGCHLD, SIG_IGN);
507 if((fd = recvreq(0, &req)) < 0) {
509 flog(LOG_ERR, "recvreq: %s", strerror(errno));