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/>.
42 #define PAT_BASENAME 0
43 #define PAT_PATHNAME 1
48 struct config *next, *prev;
51 struct child *children;
52 struct pattern *patterns;
74 struct config *cflist;
76 static void freechild(struct child *ch)
87 static void freepattern(struct pattern *pat)
91 for(rule = pat->rules; *rule; rule++) {
92 if((*rule)->pattern != NULL)
93 free((*rule)->pattern);
96 if(pat->childnm != NULL)
101 static void freeconfig(struct config *cf)
103 struct child *ch, *nch;
104 struct pattern *pat, *npat;
107 cf->prev->next = cf->next;
109 cf->next->prev = cf->prev;
113 for(ch = cf->children; ch != NULL; ch = nch) {
117 for(pat = cf->patterns; pat != NULL; pat = npat) {
124 static struct child *newchild(char *name, int type)
129 ch->name = sstrdup(name);
135 static struct child *getchild(struct config *cf, char *name)
139 for(ch = cf->children; ch; ch = ch->next) {
140 if(!strcmp(ch->name, name))
146 static struct rule *newrule(struct pattern *pat)
151 for(i = 0; pat->rules[i]; i++);
152 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
153 rule = pat->rules[i] = szmalloc(sizeof(*rule));
154 pat->rules[i + 1] = NULL;
158 static struct pattern *newpattern(void)
163 pat->rules = szmalloc(sizeof(*pat->rules));
167 static struct config *readconfig(char *path)
184 p = sprintf3("%s/.htrc", path);
187 if((s = fopen(p, "r")) == NULL)
190 cf->mtime = sb.st_mtime;
191 cf->path = sstrdup(path);
197 if(fgets(line, sizeof(line), s) == NULL) {
202 for(p = line; *p; p++) {
208 ind = isspace(line[0]);
215 flog(LOG_WARNING, "%s%i: unexpected line indentation in global scope", path, lno);
219 } else if(!strcmp(w[0], "child")) {
221 flog(LOG_WARNING, "%s:%i: missing name in child declaration", path, lno);
224 child = newchild(w[1], CH_SOCKET);
226 } else if(!strcmp(w[0], "fchild")) {
228 flog(LOG_WARNING, "%s:%i: missing name in child declaration", path, lno);
231 child = newchild(w[1], CH_FORK);
233 } else if(!strcmp(w[0], "match")) {
237 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
240 } else if(state == 1) {
243 } else if(!strcmp(w[0], "exec")) {
245 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", path, lno);
248 child->argv = szmalloc(sizeof(*child->argv) * argc);
249 for(i = 0; i < argc - 1; i++)
250 child->argv[i] = sstrdup(w[i + 1]);
252 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
256 if(child->argv == NULL) {
257 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", path, lno, child->name);
261 child->next = cf->children;
262 cf->children = child;
265 } else if(state == 2) {
268 } else if(!strcmp(w[0], "filename")) {
270 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", path, lno);
274 rule->type = PAT_BASENAME;
275 rule->pattern = sstrdup(w[1]);
276 } else if(!strcmp(w[0], "pathname")) {
278 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", path, lno);
282 rule->type = PAT_PATHNAME;
283 rule->pattern = sstrdup(w[1]);
284 } else if(!strcmp(w[0], "all")) {
285 newrule(pat)->type = PAT_ALL;
286 } else if(!strcmp(w[0], "default")) {
287 newrule(pat)->type = PAT_DEFAULT;
288 } else if(!strcmp(w[0], "handler")) {
290 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", path, lno);
293 if(pat->childnm != NULL)
295 pat->childnm = sstrdup(w[1]);
297 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
301 if(pat->rules[0] == NULL) {
302 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", path, lno);
306 if(pat->childnm == NULL) {
307 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", path, lno);
311 pat->next = cf->patterns;
329 static struct config *getconfig(char *path)
334 for(cf = cflist; cf != NULL; cf = cf->next) {
335 if(!strcmp(cf->path, path)) {
336 if(stat(sprintf3("%s/.htrc", path), &sb))
338 if(sb.st_mtime != cf->mtime) {
345 if((cf = readconfig(path)) != NULL) {
352 static struct child *findchild(char *file, char *name)
361 if(!strcmp(buf, "."))
363 if((p = strrchr(buf, '/')) != NULL)
370 if((ch = getchild(cf, name)) != NULL)
377 static struct pattern *findmatch(char *file, int trydefault)
385 if((bn = strrchr(file, '/')) != NULL)
392 if(!strcmp(buf, "."))
394 if((p = strrchr(buf, '/')) != NULL)
401 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
402 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
403 if(rule->type == PAT_BASENAME) {
404 if(fnmatch(rule->pattern, bn, 0))
406 } else if(rule->type == PAT_PATHNAME) {
407 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
409 } else if(rule->type == PAT_ALL) {
410 } else if(rule->type == PAT_DEFAULT) {
425 static void forkchild(struct child *ch)
427 ch->fd = stdmkchild(ch->argv);
430 static void passreq(struct child *ch, struct hthead *req, int fd)
434 if(sendreq(ch->fd, req, fd)) {
436 /* Assume that the child has crashed and restart it. */
438 if(!sendreq(ch->fd, req, fd))
441 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
447 static void handlefile(struct hthead *req, int fd, char *path)
452 headappheader(req, "X-Ash-File", path);
453 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
454 /* XXX: Send a 500 error? 404? */
457 if((ch = findchild(path, pat->childnm)) == NULL) {
458 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
459 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
463 if(ch->type == CH_SOCKET) {
464 passreq(ch, req, fd);
465 } else if(ch->type == CH_FORK) {
466 stdforkserve(ch->argv, req, fd);
470 static void handledir(struct hthead *req, int fd, char *path)
473 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet");
476 static int checkdir(struct hthead *req, int fd, char *path)
481 static void serve(struct hthead *req, int fd)
483 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
492 if((p2 = strchr(p, '/')) == NULL) {
499 if(stat(path, &sb)) {
500 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
501 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
506 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
511 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
518 * First, check the name verbatimely:
520 buf = sprintf3("%s/%s", path, p);
521 if(!stat(buf, &sb)) {
522 if(S_ISDIR(sb.st_mode)) {
524 if(!strcmp(path, "."))
527 path = sprintf2("%s/%s", path, p);
529 if(checkdir(req, fd, path))
533 if(S_ISREG(sb.st_mode)) {
535 path = sprintf2("%s/%s", path, p);
539 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
544 * Check the file extensionlessly:
546 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
547 while((dent = readdir(dir)) != NULL) {
548 buf = sprintf3("%s/%s", path, dent->d_name);
549 if((p3 = strchr(dent->d_name, '.')) != NULL)
551 if(strcmp(dent->d_name, p))
555 if(!S_ISREG(sb.st_mode))
567 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
579 if(!strncmp(path, "./", 2))
580 memmove(path, path + 2, strlen(path + 2) + 1);
581 if(S_ISDIR(sb.st_mode)) {
582 handledir(req, fd, path);
583 } else if(S_ISREG(sb.st_mode)) {
584 handlefile(req, fd, path);
586 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
592 /* No special handling, for now at least. */
597 int main(int argc, char **argv)
603 flog(LOG_ERR, "usage: dirplex DIR");
607 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
610 signal(SIGCHLD, SIG_IGN);
612 if((fd = recvreq(0, &req)) < 0) {
614 flog(LOG_ERR, "recvreq: %s", strerror(errno));