callcgi: Set SERVER_PORT and canonically consider Host to be SERVER_NAME.
[ashd.git] / src / callcgi.c
index 70a97dd..84d4c69 100644 (file)
@@ -85,21 +85,20 @@ static void forkchild(int inpath, char *prog, char *file, char *method, char *ur
            close(i);
        if((qp = strchr(url, '?')) != NULL)
            *(qp++) = 0;
-       /*
-        * XXX: Currently missing:
-        *  SERVER_NAME (Partially)
-        *  SERVER_PORT
-        */
        putenv(sprintf2("SERVER_SOFTWARE=ashd/%s", VERSION));
        putenv("GATEWAY_INTERFACE=CGI/1.1");
        if(getenv("HTTP_VERSION"))
-           putenv(sprintf2("SERVER_PROTOCOL=HTTP/%s", getenv("HTTP_VERSION")));
+           putenv(sprintf2("SERVER_PROTOCOL=%s", getenv("HTTP_VERSION")));
        putenv(sprintf2("REQUEST_METHOD=%s", method));
        putenv(sprintf2("PATH_INFO=%s", rest));
        putenv(sprintf2("SCRIPT_NAME=%s", url));
        putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
        if(getenv("REQ_HOST"))
            putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
+       if(getenv("REQ_X_ASH_SERVER_PORT"))
+           putenv(sprintf2("SERVER_PORT=%s", getenv("REQ_X_ASH_SERVER_PORT")));
+       if(getenv("REQ_X_ASH_PROTOCOL") && !strcmp(getenv("REQ_X_ASH_PROTOCOL"), "https"))
+           putenv("HTTPS=ON");
        if(getenv("REQ_X_ASH_ADDRESS"))
            putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
        if(getenv("REQ_CONTENT_TYPE"))
@@ -258,13 +257,13 @@ static void sendstatus(char **headers, FILE *out)
     }
     if(status) {
        if(strchr(status, ' '))
-           fprintf(out, "HTTP/1.1 %s\r\n", status);
+           fprintf(out, "HTTP/1.1 %s\n", status);
        else
-           fprintf(out, "HTTP/1.1 %i %s\r\n", atoi(status), defstatus(atoi(status)));
+           fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
     } else if(location) {
-       fprintf(out, "HTTP/1.1 303 See Other\r\n");
+       fprintf(out, "HTTP/1.1 303 See Other\n");
     } else {
-       fprintf(out, "HTTP/1.1 200 OK\r\n");
+       fprintf(out, "HTTP/1.1 200 OK\n");
     }
 }
 
@@ -272,7 +271,7 @@ static void sendheaders(char **headers, FILE *out)
 {
     while(*headers) {
        if(**headers)
-           fprintf(out, "%s: %s\r\n", headers[0], headers[1]);
+           fprintf(out, "%s: %s\n", headers[0], headers[1]);
        headers += 2;
     }
 }
@@ -329,7 +328,7 @@ int main(int argc, char **argv, char **envp)
     }
     sendstatus(headers, stdout);
     sendheaders(headers, stdout);
-    printf("\r\n");
+    printf("\n");
     passdata(out, stdout);
     return(0);
 }