FCGI::Stream::READ()
[catagits/fcgi2.git] / cgi-fcgi / cgi-fcgi.c
index b495032..e0f304a 100644 (file)
  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  *
  */
-
 #ifndef lint
-static const char rcsid[] = "$Id: cgi-fcgi.c,v 1.5 1999/07/26 22:03:07 roberts Exp $";
+static const char rcsid[] = "$Id: cgi-fcgi.c,v 1.16 2003/06/22 02:02:33 robs Exp $";
 #endif /* not lint */
 
-#include <stdio.h>
-#if defined HAVE_UNISTD_H || defined __linux__
-#include <unistd.h>
-#endif
-#include <fcntl.h>
 #include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
-#include <ctype.h>
+
+#include "fcgi_config.h"
+
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
+
+#ifdef _WIN32
+#include <stdlib.h>
+#include <io.h>
+#else
+extern char **environ;
 #endif
+
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
 #endif
+
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #include "fcgimisc.h"
 #include "fcgiapp.h"
-#include "fcgiappmisc.h"
 #include "fastcgi.h"
-#include "fcgi_config.h"
 #include "fcgios.h"
 
-#include <stdlib.h>
 
 static int wsReadPending = 0;
 static int fcgiReadPending = 0;
@@ -101,12 +111,12 @@ static FCGI_Header MakeHeader(
     ASSERT(contentLength >= 0 && contentLength <= FCGI_MAX_LENGTH);
     ASSERT(paddingLength >= 0 && paddingLength <= 0xff);
     header.version = FCGI_VERSION_1;
-    header.type             =  type;
-    header.requestIdB1      = (requestId      >> 8) & 0xff;
-    header.requestIdB0      = (requestId          ) & 0xff;
-    header.contentLengthB1  = (contentLength  >> 8) & 0xff;
-    header.contentLengthB0  = (contentLength      ) & 0xff;
-    header.paddingLength    =  paddingLength;
+    header.type             = (unsigned char) type;
+    header.requestIdB1      = (unsigned char) ((requestId     >> 8) & 0xff);
+    header.requestIdB0      = (unsigned char) ((requestId         ) & 0xff);
+    header.contentLengthB1  = (unsigned char) ((contentLength >> 8) & 0xff);
+    header.contentLengthB0  = (unsigned char) ((contentLength     ) & 0xff);
+    header.paddingLength    = (unsigned char) paddingLength;
     header.reserved         =  0;
     return header;
 }
@@ -126,9 +136,9 @@ static FCGI_BeginRequestBody MakeBeginRequestBody(
 {
     FCGI_BeginRequestBody body;
     ASSERT((role >> 16) == 0);
-    body.roleB1 = (role >>  8) & 0xff;
-    body.roleB0 = (role      ) & 0xff;
-    body.flags = (keepConnection) ? FCGI_KEEP_CONN : 0;
+    body.roleB1 = (unsigned char) ((role >>  8) & 0xff);
+    body.roleB0 = (unsigned char) (role         & 0xff);
+    body.flags  = (unsigned char) ((keepConnection) ? FCGI_KEEP_CONN : 0);
     memset(body.reserved, 0, sizeof(body.reserved));
     return body;
 }
@@ -178,7 +188,7 @@ static int stdinFds[3];
 static void FCGIexit(int exitCode)
 {
     if(appServerSock != -1) {
-        OS_Close(appServerSock);
+        OS_Close(appServerSock, TRUE);
        appServerSock = -1;
     }
     OS_LibShutdown();
@@ -202,11 +212,14 @@ static void FCGIexit(int exitCode)
  *----------------------------------------------------------------------
  */
 
-static void AppServerReadHandler(ClientData clientData, int bytesRead)
+static void AppServerReadHandler(ClientData dc, int bytesRead)
 {
     int count, outFD;
     char *ptr;
 
+    /* Touch unused parameters to avoid warnings */
+    dc = NULL;
+
     assert(fcgiReadPending == TRUE);
     fcgiReadPending = FALSE;
     count = bytesRead;
@@ -219,7 +232,7 @@ static void AppServerReadHandler(ClientData clientData, int bytesRead)
             exit(FCGX_PROTOCOL_ERROR);
         }
        if(appServerSock != -1) {
-           OS_Close(appServerSock);
+           OS_Close(appServerSock, TRUE);
            appServerSock = -1;
        }
         /*
@@ -336,6 +349,18 @@ static int webServerReadHandlerEOF;
                          * the Web server. Used in main to prevent
                          * rescheduling WebServerReadHandler. */
 
+static void WriteStdinEof(void)
+{
+    static int stdin_eof_sent = 0;
+
+    if (stdin_eof_sent)
+       return;
+
+    *((FCGI_Header *)fromWS.stop) = MakeHeader(FCGI_STDIN, requestId, 0, 0);
+    fromWS.stop += sizeof(FCGI_Header);
+    stdin_eof_sent = 1;
+}
+
 /*
  *----------------------------------------------------------------------
  *
@@ -348,8 +373,11 @@ static int webServerReadHandlerEOF;
  *----------------------------------------------------------------------
  */
 
-static void WebServerReadHandler(ClientData clientData, int bytesRead)
+static void WebServerReadHandler(ClientData dc, int bytesRead)
 {
+    /* Touch unused parameters to avoid warnings */
+    dc = NULL;
+
     assert(fromWS.next == fromWS.stop);
     assert(fromWS.next == &fromWS.buff[0]);
     assert(wsReadPending == TRUE);
@@ -363,6 +391,10 @@ static void WebServerReadHandler(ClientData clientData, int bytesRead)
     bytesToRead -= bytesRead;
     fromWS.stop = &fromWS.buff[sizeof(FCGI_Header) + bytesRead];
     webServerReadHandlerEOF = (bytesRead == 0);
+
+    if (bytesToRead <= 0)
+       WriteStdinEof();
+
     ScheduleIo();
 }
 \f
@@ -378,10 +410,13 @@ static void WebServerReadHandler(ClientData clientData, int bytesRead)
  *----------------------------------------------------------------------
  */
 
-static void AppServerWriteHandler(ClientData clientData, int bytesWritten)
+static void AppServerWriteHandler(ClientData dc, int bytesWritten)
 {
     int length = fromWS.stop - fromWS.next;
 
+    /* Touch unused parameters to avoid warnings */
+    dc = NULL;
+
     assert(length > 0);
     assert(fcgiWritePending == TRUE);
 
@@ -478,7 +513,8 @@ static void FCGI_Start(char *bindPath, char *appPath, int nServers)
 {
     int listenFd, i;
 
-    if((listenFd = OS_CreateLocalIpcFd(bindPath)) == -1) {
+    /* @@@ Should be able to pick up the backlog as an arg */
+    if((listenFd = OS_CreateLocalIpcFd(bindPath, 5)) == -1) {
         exit(OS_Errno);
     }
 
@@ -495,7 +531,7 @@ static void FCGI_Start(char *bindPath, char *appPath, int nServers)
             exit(OS_Errno);
        }
     }
-    OS_Close(listenFd);
+    OS_Close(listenFd, TRUE);
 }
 \f
 /*
@@ -522,22 +558,22 @@ static void FCGIUtil_BuildNameValueHeader(
     unsigned char *startHeaderBuffPtr = headerBuffPtr;
 
     ASSERT(nameLen >= 0);
-    if (nameLen < 0x80 == 0) {
-        *headerBuffPtr++ = nameLen;
+    if (nameLen < 0x80) {
+        *headerBuffPtr++ = (unsigned char) nameLen;
     } else {
-        *headerBuffPtr++ = (nameLen >> 24) | 0x80;
-        *headerBuffPtr++ = (nameLen >> 16);
-        *headerBuffPtr++ = (nameLen >> 8);
-        *headerBuffPtr++ = nameLen;
+        *headerBuffPtr++ = (unsigned char) ((nameLen >> 24) | 0x80);
+        *headerBuffPtr++ = (unsigned char) (nameLen >> 16);
+        *headerBuffPtr++ = (unsigned char) (nameLen >> 8);
+        *headerBuffPtr++ = (unsigned char) nameLen;
     }
     ASSERT(valueLen >= 0);
-    if (valueLen < 0x80 == 0) {
-        *headerBuffPtr++ = valueLen;
+    if (valueLen < 0x80) {
+        *headerBuffPtr++ = (unsigned char) valueLen;
     } else {
-        *headerBuffPtr++ = (valueLen >> 24) | 0x80;
-        *headerBuffPtr++ = (valueLen >> 16);
-        *headerBuffPtr++ = (valueLen >> 8);
-        *headerBuffPtr++ = valueLen;
+        *headerBuffPtr++ = (unsigned char) ((valueLen >> 24) | 0x80);
+        *headerBuffPtr++ = (unsigned char) (valueLen >> 16);
+        *headerBuffPtr++ = (unsigned char) (valueLen >> 8);
+        *headerBuffPtr++ = (unsigned char) valueLen;
     }
     *headerLenPtr = headerBuffPtr - startHeaderBuffPtr;
 }
@@ -602,7 +638,7 @@ static int ParseArgs(int argc, char *argv[],
                                     "%d is max from a file\n", MAXARGS);
                                exit(-1);
                        }
-                       if((av[ac] = malloc(strlen(tp1)+1)) == NULL) {
+                       if((av[ac] = (char *)malloc(strlen(tp1)+1)) == NULL) {
                            fprintf(stderr, "Cannot allocate %d bytes\n",
                                    strlen(tp1)+1);
                            exit(-1);
@@ -642,7 +678,7 @@ static int ParseArgs(int argc, char *argv[],
            }
        } else if(*appPathPtr == '\0') {
             strcpy(appPathPtr, argv[i]);
-        } else if(isdigit(argv[i][0]) && *nServersPtr == 0) {
+        } else if(isdigit((int)argv[i][0]) && *nServersPtr == 0) {
             *nServersPtr = atoi(argv[i]);
             if(*nServersPtr <= 0) {
                 fprintf(stderr, "Number of servers must be greater than 0\n");
@@ -760,7 +796,7 @@ int main(int argc, char **argv)
     /*
      * Send environment to the FCGI application server
      */
-    paramsStream = CreateWriter(appServerSock, requestId, 8192, FCGI_PARAMS);
+    paramsStream = FCGX_CreateWriter(appServerSock, requestId, 8192, FCGI_PARAMS);
     for( ; *envp != NULL; envp++) {
         equalPtr = strchr(*envp, '=');
         if(equalPtr  == NULL) {
@@ -779,7 +815,7 @@ int main(int argc, char **argv)
         }
     }
     FCGX_FClose(paramsStream);
-    FreeStream(&paramsStream);
+    FCGX_FreeStream(&paramsStream);
     /*
      * Perform the event loop until AppServerReadHander sees FCGI_END_REQUEST
      */
@@ -791,7 +827,11 @@ int main(int argc, char **argv)
     numFDs = max(appServerSock, STDIN_FILENO) + 1;
     OS_SetFlags(appServerSock, O_NONBLOCK);
 
+    if (bytesToRead <= 0)
+       WriteStdinEof();
+
     ScheduleIo();
+
     while(!exitStatusSet) {
         /*
         * NULL = wait forever (or at least until there's something