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] = smalloc(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)
186 if((s = fopen(sprintf3("%s/.htrc", path), "r")) == NULL)
189 cf->mtime = sb.st_mtime;
190 cf->path = sstrdup(path);
196 if(fgets(line, sizeof(line), s) == NULL) {
201 for(p = line; *p; p++) {
207 ind = isspace(line[0]);
214 flog(LOG_WARNING, "%s%i: unexpected line indentation in global scope", path, lno);
218 } else if(!strcmp(w[0], "child")) {
220 flog(LOG_WARNING, "%s:%i: missing name in child declaration", path, lno);
223 child = newchild(w[1], CH_SOCKET);
225 } else if(!strcmp(w[0], "fchild")) {
227 flog(LOG_WARNING, "%s:%i: missing name in child declaration", path, lno);
230 child = newchild(w[1], CH_FORK);
232 } else if(!strcmp(w[0], "match")) {
236 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
239 } else if(state == 1) {
242 } else if(!strcmp(w[0], "exec")) {
244 flog(LOG_WARNING, "%s:%i: too few parameters to `exec'", path, lno);
247 child->argv = szmalloc(sizeof(*child->argv) * argc);
248 for(i = 0; i < argc - 1; i++)
249 child->argv[i] = sstrdup(w[i + 1]);
251 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
255 if(child->argv == NULL) {
256 flog(LOG_WARNING, "%s:%i: missing `exec' in child declaration %s", path, lno, child->name);
260 child->next = cf->children;
261 cf->children = child;
264 } else if(state == 2) {
267 } else if(!strcmp(w[0], "filename")) {
269 flog(LOG_WARNING, "%s:%i: missing pattern for `filename' match", path, lno);
273 rule->type = PAT_BASENAME;
274 rule->pattern = sstrdup(w[1]);
275 } else if(!strcmp(w[0], "pathname")) {
277 flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", path, lno);
281 rule->type = PAT_PATHNAME;
282 rule->pattern = sstrdup(w[1]);
283 } else if(!strcmp(w[0], "all")) {
284 newrule(pat)->type = PAT_ALL;
285 } else if(!strcmp(w[0], "default")) {
286 newrule(pat)->type = PAT_DEFAULT;
287 } else if(!strcmp(w[0], "handler")) {
289 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", path, lno);
292 if(pat->childnm != NULL)
294 pat->childnm = sstrdup(w[1]);
296 flog(LOG_WARNING, "%s:%i: unknown directive %s", path, lno, w[0]);
300 if(pat->rules[0] == NULL) {
301 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", path, lno);
305 if(pat->childnm == NULL) {
306 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", path, lno);
310 pat->next = cf->patterns;
328 static struct config *getconfig(char *path)
333 for(cf = cflist; cf != NULL; cf = cf->next) {
334 if(!strcmp(cf->path, path)) {
337 if(sb.st_mtime != cf->mtime) {
344 if((cf = readconfig(path)) != NULL) {
351 static struct child *findchild(char *file, char *name)
360 if(!strcmp(buf, "."))
362 if((p = strrchr(buf, '/')) != NULL)
369 if((ch = getchild(cf, name)) != NULL)
376 static struct pattern *findmatch(char *file, int trydefault)
384 if((bn = strrchr(file, '/')) != NULL)
391 if(!strcmp(buf, "."))
393 if((p = strrchr(buf, '/')) != NULL)
400 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
401 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
402 if(rule->type == PAT_BASENAME) {
403 if(fnmatch(rule->pattern, bn, 0))
405 } else if(rule->type == PAT_PATHNAME) {
406 if(fnmatch(rule->pattern, file, FNM_PATHNAME))
408 } else if(rule->type == PAT_ALL) {
409 } else if(rule->type == PAT_DEFAULT) {
424 static void forkchild(struct child *ch)
426 ch->fd = stdmkchild(ch->argv);
429 static void passreq(struct child *ch, struct hthead *req, int fd)
433 if(sendreq(ch->fd, req, fd)) {
435 /* Assume that the child has crashed and restart it. */
437 if(!sendreq(ch->fd, req, fd))
440 flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
446 static void handlefile(struct hthead *req, int fd, char *path)
451 headappheader(req, "X-Ash-File", path);
452 if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
453 /* XXX: Send a 500 error? 404? */
456 if((ch = findchild(path, pat->childnm)) == NULL) {
457 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
458 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
462 if(ch->type == CH_SOCKET) {
463 passreq(ch, req, fd);
464 } else if(ch->type == CH_FORK) {
465 stdforkserve(ch->argv, req, fd);
469 static void handledir(struct hthead *req, int fd, char *path)
472 simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet");
475 static int checkdir(struct hthead *req, int fd, char *path)
480 static void serve(struct hthead *req, int fd)
482 char *p, *p2, *path, *tmp, *buf, *p3, *nm;
491 if((p2 = strchr(p, '/')) == NULL) {
498 if(stat(path, &sb)) {
499 flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
500 simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
505 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
510 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
517 * First, check the name verbatimely:
519 buf = sprintf3("%s/%s", path, p);
520 if(!stat(buf, &sb)) {
521 if(S_ISDIR(sb.st_mode)) {
523 if(!strcmp(path, "."))
526 path = sprintf2("%s/%s", path, p);
528 if(checkdir(req, fd, path))
532 if(S_ISREG(sb.st_mode)) {
534 path = sprintf2("%s/%s", path, p);
538 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
543 * Check the file extensionlessly:
545 if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
546 while((dent = readdir(dir)) != NULL) {
547 buf = sprintf3("%s/%s", path, dent->d_name);
548 if((p3 = strchr(dent->d_name, '.')) != NULL)
550 if(strcmp(dent->d_name, p))
554 if(!S_ISREG(sb.st_mode))
566 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
578 if(!strncmp(path, "./", 2))
579 memmove(path, path + 2, strlen(path + 2) + 1);
580 if(S_ISDIR(sb.st_mode)) {
581 handledir(req, fd, path);
582 } else if(S_ISREG(sb.st_mode)) {
583 handlefile(req, fd, path);
585 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
591 /* No special handling, for now at least. */
596 int main(int argc, char **argv)
602 flog(LOG_ERR, "usage: dirplex DIR");
606 flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
609 signal(SIGCHLD, SIG_IGN);
611 if((fd = recvreq(0, &req)) < 0) {
613 flog(LOG_ERR, "recvreq: %s", strerror(errno));