dirplex: Start children in the directory in which they were declared.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Sep 2010 04:30:20 +0000 (06:30 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Sep 2010 04:30:20 +0000 (06:30 +0200)
src/dirplex/conf.c
src/dirplex/dirplex.c
src/dirplex/dirplex.h

index ffa4313..e40a445 100644 (file)
@@ -329,7 +329,7 @@ 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;
@@ -337,10 +337,13 @@ struct child *findchild(char *file, char *name)
     
     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);
+    return(NULL);
 }
 
 struct pattern *findmatch(char *file, int trydefault, int dir)
index c31c6cd..7f2a12d 100644 (file)
 
 time_t now;
 
+static void chinit(void *idata)
+{
+    char *twd = idata;
+    
+    if(twd != NULL) {
+       /* This should never be able to fail other than for critical
+        * I/O errors or some such, since the path has already been
+        * traversed. */
+       if(chdir(twd))
+           exit(127);
+    }
+}
+
 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
 {
     struct child *ch;
+    struct config *ccf;
+    char *twd;
 
-    headappheader(req, "X-Ash-File", path);
     if(pat->fchild) {
+       headappheader(req, "X-Ash-File", path);
        stdforkserve(pat->fchild, req, fd, NULL, NULL);
     } else {
-       if((ch = findchild(path, pat->childnm)) == NULL) {
+       if((ch = findchild(path, pat->childnm, &ccf)) == 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, NULL, NULL))
+       twd = NULL;
+       if((twd = ccf->path) != NULL) {
+           if(!strcmp(twd, ".")) {
+               twd = NULL;
+           } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
+               /* Should be an impossible case under the current (and
+                * foreseeable) scheme. */
+               simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
+               return;
+           } else {
+               path = path + strlen(twd) + 1;
+           }
+       }
+       headappheader(req, "X-Ash-File", path);
+       if(childhandle(ch, req, fd, chinit, twd))
            simpleerror(fd, 500, "Server Error", "The request handler crashed.");
     }
 }
index 14b704c..d486529 100644 (file)
@@ -35,7 +35,7 @@ struct child *getchild(struct config *cf, char *name);
 struct config *readconfig(char *file);
 struct config *getconfig(char *path);
 struct config **getconfigs(char *file);
-struct child *findchild(char *file, char *name);
+struct child *findchild(char *file, char *name, struct config **cf);
 struct pattern *findmatch(char *file, int trydefault, int dir);
 
 extern time_t now;