call[fs]cgi: Fixed shutdown bug.
[ashd.git] / src / callfcgi.c
index a394a42..9969c89 100644 (file)
@@ -37,7 +37,7 @@
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <netdb.h>
-#include <sys/signal.h>
+#include <signal.h>
 #include <errno.h>
 
 #ifdef HAVE_CONFIG_H
@@ -45,6 +45,7 @@
 #endif
 #include <utils.h>
 #include <req.h>
+#include <resp.h>
 #include <log.h>
 #include <mt.h>
 #include <mtio.h>
@@ -208,7 +209,7 @@ static void startlisten(void)
            close(i);
        execvp(*progspec, progspec);
        flog(LOG_ERR, "callfcgi: %s: %s", *progspec, strerror(errno));
-       exit(127);
+       _exit(127);
     }
     close(fd);
 }
@@ -226,13 +227,13 @@ static void startnolisten(void)
            close(i);
        if((fd = open("/dev/null", O_RDONLY)) < 0) {
            flog(LOG_ERR, "/dev/null: %s", strerror(errno));
-           exit(127);
+           _exit(127);
        }
        dup2(fd, 0);
        close(fd);
        execvp(*progspec, progspec);
        flog(LOG_ERR, "callfcgi: %s: %s", *progspec, strerror(errno));
-       exit(127);
+       _exit(127);
     }
 }
 
@@ -441,14 +442,13 @@ static char *absolutify(char *file)
 static void mkcgienv(struct hthead *req, struct charbuf *dst)
 {
     int i;
-    char *url, *qp, *h, *p;
+    char *url, *pi, *tmp, *qp, *h, *p;
     
     bufaddenv(dst, "SERVER_SOFTWARE", "ashd/%s", VERSION);
     bufaddenv(dst, "GATEWAY_INTERFACE", "CGI/1.1");
     bufaddenv(dst, "SERVER_PROTOCOL", "%s", req->ver);
     bufaddenv(dst, "REQUEST_METHOD", "%s", req->method);
     bufaddenv(dst, "REQUEST_URI", "%s", req->url);
-    bufaddenv(dst, "PATH_INFO", req->rest);
     url = sstrdup(req->url);
     if((qp = strchr(url, '?')) != NULL)
        *(qp++) = 0;
@@ -456,27 +456,47 @@ static void mkcgienv(struct hthead *req, struct charbuf *dst)
      * several alternatives, none seem to be better. */
     if(*req->rest && (strlen(url) >= strlen(req->rest)) &&
        !strcmp(req->rest, url + strlen(url) - strlen(req->rest))) {
-       bufaddenv(dst, "SCRIPT_NAME", "%.*s", (int)(strlen(url) - strlen(req->rest)), url);
-    } else {
-       bufaddenv(dst, "SCRIPT_NAME", "%s", url);
+       url[strlen(url) - strlen(req->rest)] = 0;
+    }
+    if((pi = unquoteurl(req->rest)) == NULL)
+       pi = sstrdup(req->rest);
+    if(!strcmp(url, "/")) {
+       /* This seems to be normal CGI behavior, but see callcgi.c for
+        * details. */
+       url[0] = 0;
+       pi = sprintf2("/%s", tmp = pi);
+       free(tmp);
     }
+    bufaddenv(dst, "PATH_INFO", pi);
+    bufaddenv(dst, "SCRIPT_NAME", url);
     bufaddenv(dst, "QUERY_STRING", "%s", qp?qp:"");
+    free(pi);
+    free(url);
     if((h = getheader(req, "Host")) != NULL)
        bufaddenv(dst, "SERVER_NAME", "%s", h);
+    if((h = getheader(req, "X-Ash-Server-Address")) != NULL)
+       bufaddenv(dst, "SERVER_ADDR", "%s", h);
     if((h = getheader(req, "X-Ash-Server-Port")) != NULL)
        bufaddenv(dst, "SERVER_PORT", "%s", h);
+    if((h = getheader(req, "X-Ash-Remote-User")) != NULL)
+       bufaddenv(dst, "REMOTE_USER", "%s", h);
     if(((h = getheader(req, "X-Ash-Protocol")) != NULL) && !strcmp(h, "https"))
        bufaddenv(dst, "HTTPS", "on");
     if((h = getheader(req, "X-Ash-Address")) != NULL)
        bufaddenv(dst, "REMOTE_ADDR", "%s", h);
+    if((h = getheader(req, "X-Ash-Port")) != NULL)
+       bufaddenv(dst, "REMOTE_PORT", "%s", h);
     if((h = getheader(req, "Content-Type")) != NULL)
        bufaddenv(dst, "CONTENT_TYPE", "%s", h);
     if((h = getheader(req, "Content-Length")) != NULL)
        bufaddenv(dst, "CONTENT_LENGTH", "%s", h);
     else
        bufaddenv(dst, "CONTENT_LENGTH", "0");
-    if((h = getheader(req, "X-Ash-File")) != NULL)
-       bufaddenv(dst, "SCRIPT_FILENAME", "%s", absolutify(h));
+    if((h = getheader(req, "X-Ash-File")) != NULL) {
+       h = absolutify(h);
+       bufaddenv(dst, "SCRIPT_FILENAME", "%s", h);
+       free(h);
+    }
     for(i = 0; i < req->noheaders; i++) {
        h = sprintf2("HTTP_%s", req->headers[i][0]);
        for(p = h; *p; p++) {
@@ -490,46 +510,6 @@ static void mkcgienv(struct hthead *req, struct charbuf *dst)
     }
 }
 
-static char *defstatus(int code)
-{
-    if(code == 200)
-       return("OK");
-    else if(code == 201)
-       return("Created");
-    else if(code == 202)
-       return("Accepted");
-    else if(code == 204)
-       return("No Content");
-    else if(code == 300)
-       return("Multiple Choices");
-    else if(code == 301)
-       return("Moved Permanently");
-    else if(code == 302)
-       return("Found");
-    else if(code == 303)
-       return("See Other");
-    else if(code == 304)
-       return("Not Modified");
-    else if(code == 307)
-       return("Moved Temporarily");
-    else if(code == 400)
-       return("Bad Request");
-    else if(code == 401)
-       return("Unauthorized");
-    else if(code == 403)
-       return("Forbidden");
-    else if(code == 404)
-       return("Not Found");
-    else if(code == 500)
-       return("Internal Server Error");
-    else if(code == 501)
-       return("Not Implemented");
-    else if(code == 503)
-       return("Service Unavailable");
-    else
-       return("Unknown status");
-}
-
 static struct hthead *parseresp(FILE *in)
 {
     struct hthead *resp;
@@ -548,7 +528,7 @@ static struct hthead *parseresp(FILE *in)
            resp->msg = sstrdup(p);
        } else {
            resp->code = atoi(st);
-           resp->msg = sstrdup(defstatus(resp->code));
+           resp->msg = sstrdup(httpdefstatus(resp->code));
        }
        headrmheader(resp, "Status");
     } else if(getheader(resp, "Location")) {
@@ -589,34 +569,25 @@ static int sendrec(FILE *out, int type, int rid, char *data, size_t dlen)
     return(0);
 }
 
-#define fgetc2(f) ({int __c__ = fgetc(f); if(__c__ == EOF) return(-1); __c__;})
-
 static int recvrec(FILE *in, int *type, int *rid, char **data, size_t *dlen)
 {
-    int b1, b2, pl;
+    unsigned char header[8];
+    int tl;
     
-    if(fgetc2(in) != 1)
+    if(fread(header, 1, 8, in) != 8)
        return(-1);
-    *type = fgetc2(in);
-    b1 = fgetc2(in);
-    b2 = fgetc2(in);
-    *rid = (b1 << 8) | b2;
-    b1 = fgetc2(in);
-    b2 = fgetc2(in);
-    *dlen = (b1 << 8) | b2;
-    pl = fgetc2(in);
-    if(fgetc2(in) != 0)
+    if(header[0] != 1)
        return(-1);
-    *data = smalloc(max(*dlen, 1));
-    if(fread(*data, 1, *dlen, in) != *dlen) {
-       free(data);
+    *type = header[1];
+    *rid  = (header[2] << 8) | header[3];
+    *dlen = (header[4] << 8) | header[5];
+    tl = *dlen + header[6];
+    if(header[7] != 0)
+       return(-1);
+    *data = smalloc(max(tl, 1));
+    if(fread(*data, 1, tl, in) != tl) {
+       free(*data);
        return(-1);
-    }
-    for(; pl > 0; pl--) {
-       if(fgetc(in) == EOF) {
-           free(data);
-           return(-1);
-       }
     }
     return(0);
 }
@@ -628,15 +599,6 @@ static int begreq(FILE *out, int rid)
     return(sendrec(out, FCGI_BEGIN_REQUEST, rid, rec, 8));
 }
 
-static void mtiopipe(FILE **read, FILE **write)
-{
-    int fds[2];
-    
-    pipe(fds);
-    *read = mtstdopen(fds[0], 0, 600, "r");
-    *write = mtstdopen(fds[1], 0, 600, "w");
-}
-
 static void outplex(struct muth *muth, va_list args)
 {
     vavar(FILE *, sk);
@@ -795,7 +757,7 @@ static void sigign(int sig)
 
 static void sigexit(int sig)
 {
-    exit(0);
+    shutdown(0, SHUT_RDWR);
 }
 
 static void usage(FILE *out)