dirplex: Correct config resolution for base directory.
[ashd.git] / src / dirplex / conf.c
index b90163a..10d501f 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);
@@ -130,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")) {
@@ -176,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 {
@@ -263,10 +285,10 @@ struct config *getconfig(char *path)
     for(cf = cflist; cf != NULL; cf = cf->next) {
        if(!strcmp(cf->path, path)) {
            if(now - cf->lastck > 5) {
+               cf->lastck = now;
                if(stat(fn, &sb) || (sb.st_mtime != cf->mtime))
                    break;
            }
-           cf->lastck = now;
            return(cf);
        }
     }
@@ -307,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)
@@ -332,6 +356,8 @@ struct child *findchild(char *file, char *name, struct config **cf)
     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) {
@@ -340,6 +366,8 @@ struct child *findchild(char *file, char *name, struct config **cf)
            return(ch);
        }
     }
+    if(!strcmp(name, ".notfound"))
+       return(notfound);
     return(NULL);
 }
 
@@ -398,3 +426,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;