+ int i, o, c;
+ char *bn;
+ struct config **cfs;
+ struct pattern *pat;
+ struct rule *rule;
+
+ if((bn = strrchr(file, '/')) != NULL)
+ bn++;
+ else
+ bn = file;
+ cfs = getconfigs(file);
+ for(c = 0; cfs[c] != NULL; c++) {
+ for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
+ for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
+ if(rule->type == PAT_BASENAME) {
+ for(o = 0; rule->patterns[o] != NULL; o++) {
+ if(!fnmatch(rule->patterns[o], bn, 0))
+ break;
+ }
+ if(rule->patterns[o] == NULL)
+ break;
+ } else if(rule->type == PAT_PATHNAME) {
+ for(o = 0; rule->patterns[o] != NULL; o++) {
+ if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
+ break;
+ }
+ if(rule->patterns[o] == NULL)
+ break;
+ } else if(rule->type == PAT_ALL) {
+ } else if(rule->type == PAT_DEFAULT) {
+ if(!trydefault)
+ break;
+ }
+ }
+ if(!rule)
+ return(pat);
+ }
+ }
+ return(NULL);
+}
+
+static void handlefile(struct hthead *req, int fd, char *path)
+{
+ struct pattern *pat;
+ struct child *ch;
+
+ headappheader(req, "X-Ash-File", path);
+ if(((pat = findmatch(path, 0)) == NULL) && ((pat = findmatch(path, 1)) == NULL)) {
+ simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
+ return;
+ }
+ if(pat->fchild) {
+ stdforkserve(pat->fchild, req, fd);
+ } else {
+ if((ch = findchild(path, pat->childnm)) == NULL) {
+ flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
+ simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
+ return;
+ }
+ if(childhandle(ch, req, fd))
+ simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+ }
+}
+
+static void handledir(struct hthead *req, int fd, char *path)
+{
+ struct config **cfs;
+ int i, o;
+ struct stat sb;
+ char *inm, *ipath, *p;
+ DIR *dir;
+ struct dirent *dent;
+
+ cfs = getconfigs(sprintf3("%s/", path));
+ for(i = 0; cfs[i] != NULL; i++) {
+ if(cfs[i]->index != NULL) {
+ for(o = 0; cfs[i]->index[o] != NULL; o++) {
+ inm = cfs[i]->index[o];
+ ipath = sprintf2("%s/%s", path, inm);
+ if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
+ handlefile(req, fd, ipath);
+ free(ipath);
+ return;
+ }
+ free(ipath);
+
+ ipath = NULL;
+ if(!strchr(inm, '.') && ((dir = opendir(path)) != NULL)) {
+ while((dent = readdir(dir)) != NULL) {
+ if((p = strchr(dent->d_name, '.')) == NULL)
+ continue;
+ if(strncmp(dent->d_name, inm, strlen(inm)))
+ continue;
+ ipath = sprintf2("%s/%s", path, dent->d_name);
+ if(stat(ipath, &sb) || !S_ISREG(sb.st_mode)) {
+ free(ipath);
+ ipath = NULL;
+ continue;
+ }
+ break;
+ }
+ closedir(dir);
+ }
+ if(ipath != NULL) {
+ handlefile(req, fd, ipath);
+ free(ipath);
+ return;
+ }
+ }
+ break;
+ }
+ }
+ /* XXX: Directory listings */
+ simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
+}
+
+static int checkdir(struct hthead *req, int fd, char *path)
+{
+ return(0);
+}
+
+static void serve(struct hthead *req, int fd)
+{
+ char *p, *p2, *path, *tmp, *buf, *p3, *nm;