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/>.
43 #define PAT_BASENAME 0
44 #define PAT_PATHNAME 1
49 struct config *next, *prev;
52 struct child *children;
53 struct pattern *patterns;
67 struct config *cflist;
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;
95 for(ch = cf->children; ch != NULL; ch = nch) {
99 for(pat = cf->patterns; pat != NULL; pat = npat) {
106 static struct child *getchild(struct config *cf, char *name)
110 for(ch = cf->children; ch; ch = ch->next) {
111 if(!strcmp(ch->name, name))
117 static struct rule *newrule(struct pattern *pat)
122 for(i = 0; pat->rules[i]; i++);
123 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
124 rule = pat->rules[i] = szmalloc(sizeof(*rule));
125 pat->rules[i + 1] = NULL;
129 static struct pattern *newpattern(void)
134 pat->rules = szmalloc(sizeof(*pat->rules));
138 static struct pattern *parsepattern(struct cfstate *s)
144 if(!strcmp(s->argv[0], "match")) {
154 if(!strcmp(s->argv[0], "filename")) {
156 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", s->file, s->lno);
160 rule->type = PAT_BASENAME;
161 rule->pattern = sstrdup(s->argv[1]);
162 } else if(!strcmp(s->argv[0], "pathname")) {
164 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
168 rule->type = PAT_PATHNAME;
169 rule->pattern = sstrdup(s->argv[1]);
170 } else if(!strcmp(s->argv[0], "all")) {
171 newrule(pat)->type = PAT_ALL;
172 } else if(!strcmp(s->argv[0], "default")) {
173 newrule(pat)->type = PAT_DEFAULT;
174 } else if(!strcmp(s->argv[0], "handler")) {
176 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
179 if(pat->childnm != NULL)
181 pat->childnm = sstrdup(s->argv[1]);
182 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
185 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
189 if(pat->rules[0] == NULL) {
190 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
194 if(pat->childnm == NULL) {
195 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
202 static struct config *readconfig(char *path)
212 p = sprintf3("%s/.htrc", path);
215 if((in = fopen(p, "r")) == NULL) {
216 flog(LOG_WARNING, "%s: %s", p, strerror(errno));
219 s = mkcfparser(in, p);
221 cf->mtime = sb.st_mtime;
222 cf->path = sstrdup(path);
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)
249 for(cf = cflist; cf != NULL; cf = cf->next) {
250 if(!strcmp(cf->path, path)) {
251 if(stat(sprintf3("%s/.htrc", path), &sb))
253 if(sb.st_mtime != cf->mtime) {
260 if((cf = readconfig(path)) != NULL) {
267 static struct child *findchild(char *file, char *name)
276 if(!strcmp(buf, "."))
278 if((p = strrchr(buf, '/')) != NULL)
285 if((ch = getchild(cf, name)) != NULL)
292 static struct pattern *findmatch(char *file, int trydefault)
300 if((bn = strrchr(file, '/')) != NULL)
307 if(!strcmp(buf, "."))
309 if((p = strrchr(buf, '/')) != NULL)
316 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
317 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
318 if(rule->type == PAT_BASENAME) {
319 if(fnmatch(rule->pattern, bn, 0))
321 } else if(rule->type == PAT_PATHNAME) {
322 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
324 } else if(rule->type == PAT_ALL) {
325 } else if(rule->type == PAT_DEFAULT) {
340 static void forkchild(struct child *ch)
342 ch->fd = stdmkchild(ch->argv);
345 static void passreq(struct child *ch, struct hthead *req, int fd)
349 if(sendreq(ch->fd, req, fd)) {
351 /* Assume that the child has crashed and restart it. */
353 if(!sendreq(ch->fd, req, fd))
356 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
359 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
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 /* XXX: Send a 500 error? 404? */
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(ch->type == CH_SOCKET) {
380 passreq(ch, req, fd);
381 } else if(ch->type == CH_FORK) {
382 stdforkserve(ch->argv, req, fd);
386 static void handledir(struct hthead *req, int fd, char *path)
389 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet.");
392 static int checkdir(struct hthead *req, int fd, char *path)
397 static void serve(struct hthead *req, int fd)
399 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
408 if((p2 = strchr(p, '/')) == NULL) {
415 if(stat(path, &sb)) {
416 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
417 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
422 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
427 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
434 * First, check the name verbatimely:
436 buf = sprintf3("%s/%s", path, p);
437 if(!stat(buf, &sb)) {
438 if(S_ISDIR(sb.st_mode)) {
440 if(!strcmp(path, "."))
443 path = sprintf2("%s/%s", path, p);
445 if(checkdir(req, fd, path))
449 if(S_ISREG(sb.st_mode)) {
451 path = sprintf2("%s/%s", path, p);
455 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
460 * Check the file extensionlessly:
462 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
463 while((dent = readdir(dir)) != NULL) {
464 buf = sprintf3("%s/%s", path, dent->d_name);
465 if((p3 = strchr(dent->d_name, '.')) != NULL)
467 if(strcmp(dent->d_name, p))
471 if(!S_ISREG(sb.st_mode))
483 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
495 if(!strncmp(path, "./", 2))
496 memmove(path, path + 2, strlen(path + 2) + 1);
497 if(S_ISDIR(sb.st_mode)) {
498 handledir(req, fd, path);
499 } else if(S_ISREG(sb.st_mode)) {
500 handlefile(req, fd, path);
502 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
508 /* No special handling, for now at least. */
513 int main(int argc, char **argv)
519 flog(LOG_ERR, "usage: dirplex DIR");
523 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
526 signal(SIGCHLD, SIG_IGN);
528 if((fd = recvreq(0, &req)) < 0) {
530 flog(LOG_ERR, "recvreq: %s", strerror(errno));