X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdirplex%2Fdirplex.c;h=d6879573cd397d4be6152ba13a7ea06d5479b6c4;hb=54490135194e0474e753ce7d4cb60f935dad1dd4;hp=7f2a12d2bde3dd0144431a9624b1ce0493234918;hpb=da75c835b71b9ed9cf1d2c8fcbcfb7631dd24a05;p=ashd.git diff --git a/src/dirplex/dirplex.c b/src/dirplex/dirplex.c index 7f2a12d..d687957 100644 --- a/src/dirplex/dirplex.c +++ b/src/dirplex/dirplex.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #ifdef HAVE_CONFIG_H @@ -58,8 +59,15 @@ static void handle(struct hthead *req, int fd, char *path, struct pattern *pat) { struct child *ch; struct config *ccf; + struct headmod *head; char *twd; + for(head = pat->headers; head != NULL; head = head->next) { + headrmheader(req, head->name); + headappheader(req, head->name, head->value); + } + if(!strncmp(path, "./", 2) && path[2]) + path += 2; if(pat->fchild) { headappheader(req, "X-Ash-File", path); stdforkserve(pat->fchild, req, fd, NULL, NULL); @@ -69,8 +77,7 @@ static void handle(struct hthead *req, int fd, char *path, struct pattern *pat) simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm); return; } - twd = NULL; - if((twd = ccf->path) != NULL) { + if((twd = ccf?ccf->path:NULL) != NULL) { if(!strcmp(twd, ".")) { twd = NULL; } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) { @@ -88,12 +95,25 @@ static void handle(struct hthead *req, int fd, char *path, struct pattern *pat) } } +static void handle404(struct hthead *req, int fd, char *path) +{ + struct child *ch; + struct config *ccf; + char *tmp; + + tmp = sstrdup(path); + ch = findchild(tmp, ".notfound", &ccf); + if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL)) + simpleerror(fd, 500, "Server Error", "The request handler crashed."); + free(tmp); +} + static void handlefile(struct hthead *req, int fd, char *path) { struct pattern *pat; if((pat = findmatch(path, 0, 0)) == NULL) { - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + handle404(req, fd, path); return; } handle(req, fd, path, pat); @@ -184,8 +204,8 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char * char *newpath; int rv; - if(!el == '.') { - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + if(*el == '.') { + handle404(req, fd, sprintf3("%s/", path)); return(1); } if(!stat(sprintf3("%s/%s", path, el), &sb)) { @@ -205,7 +225,7 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char * free(newpath); return(1); } - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + handle404(req, fd, sprintf3("%s/", path)); return(1); } if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) { @@ -217,9 +237,34 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char * return(0); } +static int checkdir(struct hthead *req, int fd, char *path, char *rest) +{ + char *cpath; + struct config *cf, *ccf; + struct child *ch; + + cf = getconfig(path); + if((cf->capture != NULL) && (cf->caproot || !cf->path || strcmp(cf->path, "."))) { + cpath = sprintf2("%s/", path); + if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) { + free(cpath); + flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture); + simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture); + return(1); + } + free(cpath); + if(*rest == '/') + rest++; + replrest(req, rest); + if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL)) + simpleerror(fd, 500, "Server Error", "The request handler crashed."); + return(1); + } + return(0); +} + static int checkpath(struct hthead *req, int fd, char *path, char *rest) { - struct config *cf; char *p, *el; int rv; @@ -228,7 +273,8 @@ static int checkpath(struct hthead *req, int fd, char *path, char *rest) if(!strncmp(path, "./", 2)) path += 2; - cf = getconfig(path); + if(checkdir(req, fd, path, rest)) + return(1); if((p = strchr(rest, '/')) == NULL) { el = unquoteurl(rest); @@ -246,14 +292,15 @@ static int checkpath(struct hthead *req, int fd, char *path, char *rest) goto out; } if(strchr(el, '/') || (!*el && *rest)) { - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + handle404(req, fd, sprintf3("%s/", path)); rv = 1; goto out; } if(!*el) { replrest(req, rest); handledir(req, fd, path); - return(1); + rv = 1; + goto out; } rv = checkentry(req, fd, path, rest, el); @@ -267,7 +314,22 @@ static void serve(struct hthead *req, int fd) { now = time(NULL); if(!checkpath(req, fd, ".", req->rest)) - simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource."); + handle404(req, fd, "."); +} + +static void chldhandler(int sig) +{ + pid_t pid; + int st; + + while((pid = waitpid(-1, &st, WNOHANG)) > 0) { + if(WCOREDUMP(st)) + flog(LOG_WARNING, "child process %i dumped core", pid); + } +} + +static void sighandler(int sig) +{ } static void usage(FILE *out) @@ -329,7 +391,8 @@ int main(int argc, char **argv) flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno)); exit(1); } - signal(SIGCHLD, SIG_IGN); + signal(SIGCHLD, chldhandler); + signal(SIGPIPE, sighandler); while(1) { if((fd = recvreq(0, &req)) < 0) { if(errno != 0)