dirplex: Replaced serve with a recursive-descent implementation.
[ashd.git] / src / dirplex.c
index 6595201..86d2d9c 100644 (file)
 #define PAT_ALL 2
 #define PAT_DEFAULT 3
 
+#define PT_FILE 0
+#define PT_DIR 1
+
 struct config {
     struct config *next, *prev;
     char *path;
     time_t mtime, lastck;
     struct child *children;
     struct pattern *patterns;
+    char **index;
 };
 
 struct rule {
     int type;
-    char *pattern;
+    char **patterns;
 };
 
 struct pattern {
     struct pattern *next;
+    int type;
     char *childnm;
+    char **fchild;
     struct rule **rules;
 };
 
@@ -66,17 +72,21 @@ static struct config *cflist;
 static struct config *gconfig, *lconfig;
 static time_t now;
 
+static void freerule(struct rule *rule)
+{
+    freeca(rule->patterns);
+    free(rule);
+}
+
 static void freepattern(struct pattern *pat)
 {
     struct rule **rule;
     
-    for(rule = pat->rules; *rule; rule++) {
-       if((*rule)->pattern != NULL)
-           free((*rule)->pattern);
-       free(*rule);
-    }
+    for(rule = pat->rules; *rule; rule++)
+       freerule(*rule);
     if(pat->childnm != NULL)
        free(pat->childnm);
+    freeca(pat->fchild);
     free(pat);
 }
 
@@ -101,6 +111,7 @@ static void freeconfig(struct config *cf)
        npat = pat->next;
        freepattern(pat);
     }
+    freeca(cf->index);
     free(cf);
 }
 
@@ -136,6 +147,19 @@ static struct pattern *newpattern(void)
     return(pat);
 }
 
+static char **cadup(char **w)
+{
+    char **ret;
+    int i, l;
+    
+    l = calen(w);
+    ret = smalloc(sizeof(*ret) * (l + 1));
+    for(i = 0; i < l; i++)
+       ret[i] = sstrdup(w[i]);
+    ret[i] = NULL;
+    return(ret);
+}
+
 static struct pattern *parsepattern(struct cfstate *s)
 {
     struct pattern *pat;
@@ -149,6 +173,8 @@ static struct pattern *parsepattern(struct cfstate *s)
        return(NULL);
     }
     
+    if((s->argc > 1) && !strcmp(s->argv[1], "directory"))
+       pat->type = PT_DIR;
     sl = s->lno;
     while(1) {
        getcfline(s);
@@ -159,7 +185,7 @@ static struct pattern *parsepattern(struct cfstate *s)
            }
            rule = newrule(pat);
            rule->type = PAT_BASENAME;
-           rule->pattern = sstrdup(s->argv[1]);
+           rule->patterns = cadup(s->argv + 1);
        } else if(!strcmp(s->argv[0], "pathname")) {
            if(s->argc < 2) {
                flog(LOG_WARNING, "%s:%i: missing pattern for `pathname' match", s->file, s->lno);
@@ -167,7 +193,7 @@ static struct pattern *parsepattern(struct cfstate *s)
            }
            rule = newrule(pat);
            rule->type = PAT_PATHNAME;
-           rule->pattern = sstrdup(s->argv[1]);
+           rule->patterns = cadup(s->argv + 1);
        } else if(!strcmp(s->argv[0], "all")) {
            newrule(pat)->type = PAT_ALL;
        } else if(!strcmp(s->argv[0], "default")) {
@@ -180,6 +206,8 @@ static struct pattern *parsepattern(struct cfstate *s)
            if(pat->childnm != NULL)
                free(pat->childnm);
            pat->childnm = sstrdup(s->argv[1]);
+       } else if(!strcmp(s->argv[0], "fork")) {
+           pat->fchild = cadup(s->argv + 1);
        } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
            break;
        } else {
@@ -192,7 +220,7 @@ static struct pattern *parsepattern(struct cfstate *s)
        freepattern(pat);
        return(NULL);
     }
-    if(pat->childnm == NULL) {
+    if((pat->childnm == NULL) && (pat->fchild == NULL)) {
        flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
        freepattern(pat);
        return(NULL);
@@ -231,6 +259,11 @@ static struct config *readconfig(char *file)
        } else if((pat = parsepattern(s)) != NULL) {
            pat->next = cf->patterns;
            cf->patterns = pat;
+       } else if(!strcmp(s->argv[0], "index-file")) {
+           freeca(cf->index);
+           cf->index = NULL;
+           if(s->argc > 1)
+               cf->index = cadup(s->argv + 1);
        } else if(!strcmp(s->argv[0], "eof")) {
            break;
        } else {
@@ -275,6 +308,9 @@ static struct config *getconfig(char *path)
     cf->mtime = mtime;
     cf->lastck = now;
     cf->next = cflist;
+    cf->prev = NULL;
+    if(cflist != NULL)
+       cflist->prev = cf;
     cflist = cf;
     return(cf);
 }
@@ -325,9 +361,9 @@ static struct child *findchild(char *file, char *name)
     return(ch);
 }
 
-static struct pattern *findmatch(char *file, int trydefault)
+static struct pattern *findmatch(char *file, int trydefault, int dir)
 {
-    int i, c;
+    int i, o, c;
     char *bn;
     struct config **cfs;
     struct pattern *pat;
@@ -340,12 +376,24 @@ static struct pattern *findmatch(char *file, int trydefault)
     cfs = getconfigs(file);
     for(c = 0; cfs[c] != NULL; c++) {
        for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
+           if(!dir && (pat->type == PT_DIR))
+               continue;
+           if(dir && (pat->type != PT_DIR))
+               continue;
            for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
                if(rule->type == PAT_BASENAME) {
-                   if(fnmatch(rule->pattern, bn, 0))
+                   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) {
-                   if(fnmatch(rule->pattern, file, FNM_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) {
@@ -357,159 +405,207 @@ static struct pattern *findmatch(char *file, int trydefault)
                return(pat);
        }
     }
+    if(!trydefault)
+       return(findmatch(file, 1, dir));
     return(NULL);
 }
 
-static void handlefile(struct hthead *req, int fd, char *path)
+static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
 {
-    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((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(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.");
     }
-    
-    if(childhandle(ch, req, fd))
-       simpleerror(fd, 500, "Server Error", "The request handler crashed.");
 }
 
-static void handledir(struct hthead *req, int fd, char *path)
+static void handlefile(struct hthead *req, int fd, char *path)
 {
-    /* XXX: Todo */
-    simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet.");
+    struct pattern *pat;
+
+    if((pat = findmatch(path, 0, 0)) == NULL) {
+       simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
+       return;
+    }
+    handle(req, fd, path, pat);
 }
 
-static int checkdir(struct hthead *req, int fd, char *path)
+static char *findfile(char *path, char *name, struct stat *sb)
 {
-    return(0);
+    DIR *dir;
+    struct stat sbuf;
+    struct dirent *dent;
+    char *p, *fp, *ret;
+    
+    if(sb == NULL)
+       sb = &sbuf;
+    if((dir = opendir(path)) == NULL)
+       return(NULL);
+    ret = NULL;
+    while((dent = readdir(dir)) != NULL) {
+       /* Ignore backup files.
+        * XXX: There is probably a better and more extensible way to
+        * do this. */
+       if(dent->d_name[strlen(dent->d_name) - 1] == '~')
+           continue;
+       if((p = strchr(dent->d_name, '.')) == NULL)
+           continue;
+       if(strncmp(dent->d_name, name, strlen(name)))
+           continue;
+       fp = sprintf3("%s/%s", path, dent->d_name);
+       if(stat(fp, sb))
+           continue;
+       if(!S_ISREG(sb->st_mode))
+           continue;
+       ret = sstrdup(fp);
+       break;
+    }
+    closedir(dir);
+    return(ret);
 }
 
-static void serve(struct hthead *req, int fd)
+static void handledir(struct hthead *req, int fd, char *path)
 {
-    char *p, *p2, *path, *tmp, *buf, *p3, *nm;
+    struct config **cfs;
+    int i, o;
     struct stat sb;
-    DIR *dir;
-    struct dirent *dent;
+    char *inm, *ipath, *cpath;
+    struct pattern *pat;
     
-    now = time(NULL);
-    nm = req->rest;
-    path = sstrdup(".");
-    p = nm;
-    while(1) {
-       if((p2 = strchr(p, '/')) == NULL) {
-       } else {
-           *(p2++) = 0;
-       }
-       
-       if(!*p) {
-           if(p2 == NULL) {
-               if(stat(path, &sb)) {
-                   flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
-                   simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
-                   goto fail;
+    cpath = sprintf2("%s/", path);
+    cfs = getconfigs(cpath);
+    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);
+                   goto out;
                }
-               break;
-           } else {
-               simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
-               goto fail;
-           }
-       }
-       if(*p == '.') {
-           simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
-           goto fail;
-       }
-       
-       getconfig(path);
-       
-       /*
-        * First, check the name verbatimely:
-        */
-       buf = sprintf3("%s/%s", path, p);
-       if(!stat(buf, &sb)) {
-           if(S_ISDIR(sb.st_mode)) {
-               tmp = path;
-               if(!strcmp(path, "."))
-                   path = sstrdup(p);
-               else
-                   path = sprintf2("%s/%s", path, p);
-               free(tmp);
-               if(p2 == NULL) {
-                   stdredir(req, fd, 301, sprintf3("%s/", p));
+               free(ipath);
+               
+               if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
+                   handlefile(req, fd, ipath);
+                   free(ipath);
                    goto out;
                }
-               if(checkdir(req, fd, path))
-                   break;
-               goto next;
-           }
-           if(S_ISREG(sb.st_mode)) {
-               tmp = path;
-               path = sprintf2("%s/%s", path, p);
-               free(tmp);
-               break;
            }
-           simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
-           goto fail;
+           break;
        }
+    }
+    if((pat = findmatch(cpath, 0, 1)) != NULL) {
+       handle(req, fd, cpath, pat);
+       goto out;
+    }
+    simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
+    
+out:
+    free(cpath);
+}
+
+static int checkpath(struct hthead *req, int fd, char *path, char *rest);
 
-       /*
-        * Check the file extensionlessly:
-        */
-       if(!strchr(p, '.') && ((dir = opendir(path)) != NULL)) {
-           while((dent = readdir(dir)) != NULL) {
-               buf = sprintf3("%s/%s", path, dent->d_name);
-               if((p3 = strchr(dent->d_name, '.')) != NULL)
-                   *p3 = 0;
-               if(strcmp(dent->d_name, p))
-                   continue;
-               if(stat(buf, &sb))
-                   continue;
-               if(!S_ISREG(sb.st_mode))
-                   continue;
-               tmp = path;
-               path = sstrdup(buf);
-               free(tmp);
-               break;
+static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
+{
+    struct stat sb;
+    char *newpath;
+    int rv;
+    
+    if(!el == '.') {
+       simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
+       return(1);
+    }
+    if(!stat(sprintf3("%s/%s", path, el), &sb)) {
+       if(S_ISDIR(sb.st_mode)) {
+           if(!*rest) {
+               stdredir(req, fd, 301, sprintf3("%s/", el));
+               return(1);
            }
-           closedir(dir);
-           if(dent != NULL)
-               break;
+           newpath = sprintf2("%s/%s", path, el);
+           rv = checkpath(req, fd, newpath, rest + 1);
+           free(newpath);
+           return(rv);
+       } else if(S_ISREG(sb.st_mode)) {
+           newpath = sprintf2("%s/%s", path, el);
+           replrest(req, rest);
+           handlefile(req, fd, newpath);
+           free(newpath);
+           return(1);
        }
-       
        simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
-       goto fail;
-       
-    next:
-       if(p2 == NULL)
-           break;
-       p = p2;
+       return(1);
     }
-    if(p2 == NULL)
-       replrest(req, "");
-    else
-       replrest(req, p2);
+    if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
+       replrest(req, rest);
+       handlefile(req, fd, newpath);
+       free(newpath);
+       return(1);
+    }
+    return(0);
+}
+
+static int checkpath(struct hthead *req, int fd, char *path, char *rest)
+{
+    struct config *cf;
+    char *p, *el;
+    int rv;
+    
+    el = NULL;
+    rv = 0;
+    
     if(!strncmp(path, "./", 2))
-       memmove(path, path + 2, strlen(path + 2) + 1);
-    if(S_ISDIR(sb.st_mode)) {
-       handledir(req, fd, path);
-    } else if(S_ISREG(sb.st_mode)) {
-       handlefile(req, fd, path);
+       path += 2;
+    cf = getconfig(path);
+    
+    if((p = strchr(rest, '/')) == NULL) {
+       el = unquoteurl(rest);
+       rest = "";
     } else {
+       char buf[p - rest + 1];
+       memcpy(buf, rest, p - rest);
+       buf[p - rest] = 0;
+       el = unquoteurl(buf);
+       rest = p;
+    }
+    if(el == NULL) {
+       simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
+       rv = 1;
+       goto out;
+    }
+    if(strchr(el, '/') || (!*el && *rest)) {
        simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
-       goto fail;
+       rv = 1;
+       goto out;
+    }
+    if(!*el) {
+       replrest(req, rest);
+       handledir(req, fd, path);
+       return(1);
     }
-    goto out;
+    rv = checkentry(req, fd, path, rest, el);
     
-fail:
-    /* No special handling, for now at least. */
 out:
-    free(path);
+    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.");
 }
 
 static void usage(FILE *out)
@@ -521,7 +617,7 @@ int main(int argc, char **argv)
 {
     int c;
     int nodef;
-    char *gcf, *lcf;
+    char *gcf, *lcf, *clcf;
     struct hthead *req;
     int fd;
     
@@ -554,8 +650,18 @@ int main(int argc, char **argv)
        }
     }
     if(lcf != NULL) {
-       if((lconfig = readconfig(lcf)) == NULL)
-           exit(1);
+       if(strchr(lcf, '/') == NULL) {
+           if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
+               flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
+               exit(1);
+           }
+           if((lconfig = readconfig(clcf)) == NULL)
+               exit(1);
+           free(clcf);
+       } else {
+           if((lconfig = readconfig(lcf)) == NULL)
+               exit(1);
+       }
     }
     if(chdir(argv[optind])) {
        flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));