dirplex: Match pathname directives relative to the config path.
[ashd.git] / src / dirplex / conf.c
index 96a7704..84c035d 100644 (file)
@@ -30,6 +30,7 @@
 #include <utils.h>
 #include <log.h>
 #include <cf.h>
+#include <resp.h>
 
 #include "dirplex.h"
 
@@ -45,9 +46,16 @@ static void freerule(struct rule *rule)
 static void freepattern(struct pattern *pat)
 {
     struct rule **rule;
+    struct headmod *head;
     
     for(rule = pat->rules; *rule; rule++)
        freerule(*rule);
+    while((head = pat->headers) != NULL) {
+       pat->headers = head->next;
+       free(head->name);
+       free(head->value);
+       free(head);
+    }
     if(pat->childnm != NULL)
        free(pat->childnm);
     freeca(pat->fchild);
@@ -76,6 +84,8 @@ static void freeconfig(struct config *cf)
        freepattern(pat);
     }
     freeca(cf->index);
+    if(cf->capture != NULL)
+       free(cf->capture);
     free(cf);
 }
 
@@ -128,6 +138,7 @@ static struct pattern *parsepattern(struct cfstate *s)
 {
     struct pattern *pat;
     struct rule *rule;
+    struct headmod *head;
     int sl;
 
     if(!strcmp(s->argv[0], "match")) {
@@ -162,6 +173,8 @@ static struct pattern *parsepattern(struct cfstate *s)
            newrule(pat)->type = PAT_ALL;
        } else if(!strcmp(s->argv[0], "default")) {
            newrule(pat)->type = PAT_DEFAULT;
+       } else if(!strcmp(s->argv[0], "local")) {
+           newrule(pat)->type = PAT_LOCAL;
        } else if(!strcmp(s->argv[0], "handler")) {
            if(s->argc < 2) {
                flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
@@ -172,6 +185,19 @@ static struct pattern *parsepattern(struct cfstate *s)
            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], "set") || !strcmp(s->argv[0], "xset")) {
+           if(s->argc < 3) {
+               flog(LOG_WARNING, "%s:%i: missing header name or pattern for `%s' directive", s->file, s->lno, s->argv[0]);
+               continue;
+           }
+           omalloc(head);
+           if(!strcmp(s->argv[0], "xset"))
+               head->name = sprintf2("X-Ash-%s", s->argv[1]);
+           else
+               head->name = sstrdup(s->argv[1]);
+           head->value = sstrdup(s->argv[2]);
+           head->next = pat->headers;
+           pat->headers = head;
        } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
            break;
        } else {
@@ -228,6 +254,14 @@ struct config *readconfig(char *file)
            cf->index = NULL;
            if(s->argc > 1)
                cf->index = cadup(s->argv + 1);
+       } else if(!strcmp(s->argv[0], "capture")) {
+           if(s->argc < 2) {
+               flog(LOG_WARNING, "%s:%i: missing argument to capture declaration", s->file, s->lno);
+               continue;
+           }
+           if(cf->capture != NULL)
+               free(cf->capture);
+           cf->capture = sstrdup(s->argv[1]);
        } else if(!strcmp(s->argv[0], "eof")) {
            break;
        } else {
@@ -242,7 +276,7 @@ struct config *readconfig(char *file)
 
 struct config *getconfig(char *path)
 {
-    struct config *cf;
+    struct config *cf, *ocf;
     struct stat sb;
     char *fn;
     time_t mtime;
@@ -251,15 +285,14 @@ struct config *getconfig(char *path)
     for(cf = cflist; cf != NULL; cf = cf->next) {
        if(!strcmp(cf->path, path)) {
            if(now - cf->lastck > 5) {
-               if(stat(fn, &sb) || (sb.st_mtime != cf->mtime)) {
-                   freeconfig(cf);
+               cf->lastck = now;
+               if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
                    break;
-               }
            }
-           cf->lastck = now;
            return(cf);
        }
     }
+    ocf = cf;
     if(access(fn, R_OK) || stat(fn, &sb)) {
        cf = emptyconfig();
        mtime = 0;
@@ -268,6 +301,10 @@ struct config *getconfig(char *path)
            return(NULL);
        mtime = sb.st_mtime;
     }
+    if(ocf != NULL) {
+       mergechildren(cf->children, ocf->children);
+       freeconfig(ocf);
+    }
     cf->path = sstrdup(path);
     cf->mtime = mtime;
     cf->lastck = now;
@@ -292,6 +329,8 @@ struct config **getconfigs(char *file)
     if(ret != NULL)
        free(ret);
     bufinit(buf);
+    if(!strncmp(file, "./", 2))
+       file += 2;
     tmp = sstrdup(file);
     while(1) {
        if((p = strrchr(tmp, '/')) == NULL)
@@ -311,27 +350,35 @@ struct config **getconfigs(char *file)
     return(ret = buf.b);
 }
 
-struct child *findchild(char *file, char *name)
+struct child *findchild(char *file, char *name, struct config **cf)
 {
     int i;
     struct config **cfs;
     struct child *ch;
     
+    if(cf != NULL)
+       *cf = NULL;
     cfs = getconfigs(file);
     for(i = 0; cfs[i] != NULL; i++) {
-       if((ch = getchild(cfs[i], name)) != NULL)
-           break;
+       if((ch = getchild(cfs[i], name)) != NULL) {
+           if(cf != NULL)
+               *cf = cfs[i];
+           return(ch);
+       }
     }
-    return(ch);
+    if(!strcmp(name, ".notfound"))
+       return(notfound);
+    return(NULL);
 }
 
 struct pattern *findmatch(char *file, int trydefault, int dir)
 {
     int i, o, c;
-    char *bn;
+    char *bn, *ln;
     struct config **cfs;
     struct pattern *pat;
     struct rule *rule;
+    size_t pl;
     
     if((bn = strrchr(file, '/')) != NULL)
        bn++;
@@ -339,6 +386,15 @@ struct pattern *findmatch(char *file, int trydefault, int dir)
        bn = file;
     cfs = getconfigs(file);
     for(c = 0; cfs[c] != NULL; c++) {
+       if(cfs[c]->path == NULL) {
+           ln = file;
+       } else {
+           pl = strlen(cfs[c]->path);
+           if((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/'))
+               ln = file + pl + 1;
+           else
+               ln = file;      /* This should only happen in the base directory. */
+       }
        for(pat = cfs[c]->patterns; pat != NULL; pat = pat->next) {
            if(!dir && (pat->type == PT_DIR))
                continue;
@@ -354,7 +410,7 @@ struct pattern *findmatch(char *file, int trydefault, int dir)
                        break;
                } else if(rule->type == PAT_PATHNAME) {
                    for(o = 0; rule->patterns[o] != NULL; o++) {
-                       if(!fnmatch(rule->patterns[o], file, FNM_PATHNAME))
+                       if(!fnmatch(rule->patterns[o], ln, FNM_PATHNAME))
                            break;
                    }
                    if(rule->patterns[o] == NULL)
@@ -363,6 +419,9 @@ struct pattern *findmatch(char *file, int trydefault, int dir)
                } else if(rule->type == PAT_DEFAULT) {
                    if(!trydefault)
                        break;
+               } else if(rule->type == PAT_LOCAL) {
+                   if(strchr(ln, '/'))
+                       break;
                }
            }
            if(!rule)
@@ -373,3 +432,19 @@ struct pattern *findmatch(char *file, int trydefault, int dir)
        return(findmatch(file, 1, dir));
     return(NULL);
 }
+
+static int donotfound(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
+{
+    simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
+    return(0);
+}
+
+static struct chandler i_notfound = {
+    .handle = donotfound,
+};
+
+static struct child s_notfound = {
+    .name = ".notfound",
+    .iface = &i_notfound,
+};
+struct child *notfound = &s_notfound;