#include <ctype.h>
#include <dirent.h>
#include <time.h>
+#include <sys/wait.h>
#include <sys/signal.h>
#ifdef HAVE_CONFIG_H
}
}
+static void childerror(struct hthead *req, int fd)
+{
+ if(errno == EAGAIN)
+ simpleerror(fd, 500, "Server Error", "The request handler is overloaded.");
+ else
+ simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+}
+
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);
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)] != '/')) {
}
headappheader(req, "X-Ash-File", path);
if(childhandle(ch, req, fd, chinit, twd))
- simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+ childerror(req, fd);
+ }
+}
+
+static void handle404(struct hthead *req, int fd, char *path)
+{
+ struct child *ch;
+ struct config *ccf;
+ struct pattern *pat;
+
+ char tmp[strlen(path) + 1];
+ strcpy(tmp, path);
+ if((pat = findmatch(tmp, 0, PT_NOTFOUND)) != NULL) {
+ handle(req, fd, tmp, pat);
+ } else {
+ ch = findchild(tmp, ".notfound", &ccf);
+ if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
+ childerror(req, fd);
}
}
{
struct pattern *pat;
- if((pat = findmatch(path, 0, 0)) == NULL) {
- simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
+ if((pat = findmatch(path, 0, PT_FILE)) == NULL) {
+ handle404(req, fd, path);
return;
}
handle(req, fd, path, pat);
break;
}
}
- if((pat = findmatch(cpath, 0, 1)) != NULL) {
+ if((pat = findmatch(cpath, 0, PT_DIR)) != NULL) {
handle(req, fd, cpath, pat);
goto out;
}
free(cpath);
}
-static int checkpath(struct hthead *req, int fd, char *path, char *rest);
+static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final);
-static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
+static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el, int final)
{
struct stat sb;
char *newpath;
int rv;
- if(!el == '.') {
- simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
- return(1);
- }
+ if(*el == '.')
+ return(0);
if(!stat(sprintf3("%s/%s", path, el), &sb)) {
if(S_ISDIR(sb.st_mode)) {
if(!*rest) {
return(1);
}
newpath = sprintf2("%s/%s", path, el);
- rv = checkpath(req, fd, newpath, rest + 1);
+ rv = checkpath(req, fd, newpath, rest + 1, final);
free(newpath);
return(rv);
} else if(S_ISREG(sb.st_mode)) {
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)) {
struct child *ch;
cf = getconfig(path);
- if(cf->capture != NULL) {
+ 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);
if(*rest == '/')
rest++;
replrest(req, rest);
- if(childhandle(ch, req, fd, chinit, ccf->path))
- simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+ if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
+ childerror(req, fd);
return(1);
}
return(0);
}
-static int checkpath(struct hthead *req, int fd, char *path, char *rest)
+static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final)
{
char *p, *el;
int rv;
goto out;
}
if(strchr(el, '/') || (!*el && *rest)) {
- simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
- rv = 1;
+ rv = 0;
goto out;
}
if(!*el) {
replrest(req, rest);
handledir(req, fd, path);
- return(1);
+ rv = 1;
+ goto out;
}
- rv = checkentry(req, fd, path, rest, el);
+ rv = checkentry(req, fd, path, rest, el, final);
out:
+ if(final && !rv) {
+ handle404(req, fd, sprintf3("%s/", path));
+ rv = 1;
+ }
if(el != NULL)
free(el);
return(rv);
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.");
+ checkpath(req, fd, ".", req->rest, 1);
+}
+
+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)
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)