Added ability to have alternative patterns for name matches.
[ashd.git] / src / dirplex.c
index 6595201..f4cc612 100644 (file)
@@ -53,12 +53,13 @@ struct config {
 
 struct rule {
     int type;
-    char *pattern;
+    char **patterns;
 };
 
 struct pattern {
     struct pattern *next;
     char *childnm;
+    char **fchild;
     struct rule **rules;
 };
 
@@ -66,17 +67,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);
 }
 
@@ -136,6 +141,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;
@@ -159,7 +177,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 +185,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 +198,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 +212,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);
@@ -327,7 +347,7 @@ static struct child *findchild(char *file, char *name)
 
 static struct pattern *findmatch(char *file, int trydefault)
 {
-    int i, c;
+    int i, o, c;
     char *bn;
     struct config **cfs;
     struct pattern *pat;
@@ -342,10 +362,18 @@ static struct pattern *findmatch(char *file, int trydefault)
        for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
            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) {
@@ -370,14 +398,17 @@ static void handlefile(struct hthead *req, int fd, char *path)
        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)