dirplex: Made the 404 response indirectible and reusable.
[ashd.git] / src / dirplex / conf.c
index e83d847..03d5477 100644 (file)
@@ -30,6 +30,7 @@
 #include <utils.h>
 #include <log.h>
 #include <cf.h>
+#include <resp.h>
 
 #include "dirplex.h"
 
@@ -164,6 +165,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);
@@ -250,21 +253,6 @@ struct config *readconfig(char *file)
     return(cf);
 }
 
-static void mergechildren(struct config *dst, struct config *src)
-{
-    struct child *ch1, *ch2;
-    
-    for(ch1 = dst->children; ch1 != NULL; ch1 = ch1->next) {
-       for(ch2 = src->children; ch2 != NULL; ch2 = ch2->next) {
-           if(!strcmp(ch1->name, ch2->name)) {
-               ch1->fd = ch2->fd;
-               ch2->fd = -1;
-               break;
-           }
-       }
-    }
-}
-
 struct config *getconfig(char *path)
 {
     struct config *cf, *ocf;
@@ -293,7 +281,7 @@ struct config *getconfig(char *path)
        mtime = sb.st_mtime;
     }
     if(ocf != NULL) {
-       mergechildren(cf, ocf);
+       mergechildren(cf->children, ocf->children);
        freeconfig(ocf);
     }
     cf->path = sstrdup(path);
@@ -345,6 +333,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) {
@@ -353,6 +343,8 @@ struct child *findchild(char *file, char *name, struct config **cf)
            return(ch);
        }
     }
+    if(!strcmp(name, ".notfound"))
+       return(notfound);
     return(NULL);
 }
 
@@ -363,6 +355,7 @@ struct pattern *findmatch(char *file, int trydefault, int dir)
     struct config **cfs;
     struct pattern *pat;
     struct rule *rule;
+    size_t pl;
     
     if((bn = strrchr(file, '/')) != NULL)
        bn++;
@@ -394,6 +387,12 @@ 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(cfs[c]->path == NULL)
+                       break;
+                   pl = strlen(cfs[c]->path);
+                   if(!((strlen(file) > pl) && !strncmp(file, cfs[c]->path, pl) && (file[pl] == '/') && !strchr(file + pl + 1, '/')))
+                       break;
                }
            }
            if(!rule)
@@ -404,3 +403,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;