Don't keep connection on failure.
[catagits/fcgi2.git] / libfcgi / fcgiapp.c
index d1bbe31..23e55ac 100644 (file)
  *
  */
 #ifndef lint
-static const char rcsid[] = "$Id: fcgiapp.c,v 1.12 1999/08/14 21:20:56 roberts Exp $";
+static const char rcsid[] = "$Id: fcgiapp.c,v 1.24 2001/06/22 09:12:03 skimo Exp $";
 #endif /* not lint */
 
-#include "fcgi_config.h"
-
-#ifdef _WIN32
-#define DLLAPI  __declspec(dllexport)
-#endif
-
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>      /* for fcntl */
@@ -31,6 +25,8 @@ static const char rcsid[] = "$Id: fcgiapp.c,v 1.12 1999/08/14 21:20:56 roberts E
 #include <string.h>
 #include <sys/types.h>
 
+#include "fcgi_config.h"
+
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h> /* for getpeername */
 #endif
@@ -43,11 +39,20 @@ static const char rcsid[] = "$Id: fcgiapp.c,v 1.12 1999/08/14 21:20:56 roberts E
 #include <unistd.h>
 #endif
 
-#include "fcgimisc.h"
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
+
+#ifdef _WIN32
+#define DLLAPI  __declspec(dllexport)
+#endif
+
+#include "fcgiapp.h"
 #include "fcgiappmisc.h"
+
+#include "fcgimisc.h"
 #include "fastcgi.h"
 #include "fcgios.h"
-#include "fcgiapp.h"
 
 /*
  * This is a workaround for one version of the HP C compiler
@@ -184,6 +189,7 @@ char *FCGX_GetLine(char *str, int n, FCGX_Stream *stream)
 {
     int c;
     char *p = str;
+
     n--;
     while (n > 0) {
         c = FCGX_GetChar(stream);
@@ -193,7 +199,7 @@ char *FCGX_GetLine(char *str, int n, FCGX_Stream *stream)
             else
                 break;
         }
-        *p++ = c;
+        *p++ = (char) c;
         n--;
         if(c == '\n')
             break;
@@ -223,7 +229,7 @@ int FCGX_UnGetChar(int c, FCGX_Stream *stream) {
             || stream->rdNext == stream->stopUnget)
         return EOF;
     --(stream->rdNext);
-    *stream->rdNext = c;
+    *stream->rdNext = (unsigned char) c;
     return c;
 }
 
@@ -264,12 +270,12 @@ int FCGX_HasSeenEOF(FCGX_Stream *stream) {
 int FCGX_PutChar(int c, FCGX_Stream *stream)
 {
     if(stream->wrNext != stream->stop)
-        return (*stream->wrNext++ = c);
+        return (*stream->wrNext++ = (unsigned char) c);
     if(stream->isClosed || stream->isReader)
         return EOF;
     stream->emptyBuffProc(stream, FALSE);
     if(stream->wrNext != stream->stop)
-        return (*stream->wrNext++ = c);
+        return (*stream->wrNext++ = (unsigned char) c);
     ASSERT(stream->isClosed); /* bug in emptyBuffProc if not */
     return EOF;
 }
@@ -393,8 +399,8 @@ static void CopyAndAdvance(char **destPtr, char **srcPtr, int n);
 int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
 {
     char *f, *fStop, *percentPtr, *p, *fmtBuffPtr, *buffPtr;
-    int op, performedOp, sizeModifier, buffCount, buffLen, specifierLength;
-    int fastPath, n, auxBuffLen, buffReqd, minWidth, precision, exp;
+    int op, performedOp, sizeModifier, buffCount = 0, buffLen, specifierLength;
+    int fastPath, n, auxBuffLen = 0, buffReqd, minWidth, precision, exp;
     char *auxBuffPtr = NULL;
     int streamCount = 0;
     char fmtBuff[FMT_BUFFLEN];
@@ -406,13 +412,13 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
     unsigned unsignedArg;
     unsigned long uLongArg;
     unsigned short uShortArg;
-    char *charPtrArg;
+    char *charPtrArg = NULL;
     void *voidPtrArg;
     int *intPtrArg;
     long *longPtrArg;
     short *shortPtrArg;
-    double doubleArg;
-    LONG_DOUBLE lDoubleArg;
+    double doubleArg = 0.0;
+    LONG_DOUBLE lDoubleArg = 0.0L;
 
     fmtBuff[0] = '%';
     f = (char *) format;
@@ -421,7 +427,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
         percentPtr = (char *)memchr(f, '%', fStop - f);
         if(percentPtr == NULL) percentPtr = fStop;
         if(percentPtr != f) {
-            if(FCGX_PutStr(f, percentPtr - f, stream) < 0) goto ErrorReturn;
+            if(FCGX_PutStr(f, percentPtr - f, stream) < 0)
+                goto ErrorReturn;
             streamCount += percentPtr - f;
             f = percentPtr;
             if(f == fStop) break;
@@ -450,14 +457,14 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                     case 'h':
                         sizeModifier = op;
                         op = *(percentPtr + 2);
-                        fmtBuff[1] = sizeModifier;
-                        fmtBuff[2] = op;
+                        fmtBuff[1] = (char) sizeModifier;
+                        fmtBuff[2] = (char) op;
                         fmtBuff[3] = '\0';
                         specifierLength = 3;
                         break;
                    default:
                         sizeModifier = ' ';
-                        fmtBuff[1] = op;
+                        fmtBuff[1] = (char) op;
                         fmtBuff[2] = '\0';
                         specifierLength = 2;
                         break;
@@ -477,7 +484,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                  * Scan flags
                  */
                 n = strspn(p, "-0+ #");
-                if(n > 5) goto ErrorReturn;
+                if(n > 5)
+                    goto ErrorReturn;
                 CopyAndAdvance(&fmtBuffPtr, &p, n);
                 /*
                  * Scan minimum field width
@@ -486,7 +494,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                 if(n == 0) {
                     if(*p == '*') {
                         minWidth = va_arg(arg, int);
-                        if(abs(minWidth) > 999999) goto ErrorReturn;
+                        if(abs(minWidth) > 999999)
+                            goto ErrorReturn;
                        /*
                         * The following use of strlen rather than the
                         * value returned from sprintf is because SUNOS4
@@ -514,7 +523,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                         if(*p == '*') {
                             precision = va_arg(arg, int);
                             if(precision < 0) precision = 0;
-                            if(precision > 999999) goto ErrorReturn;
+                            if(precision > 999999)
+                                goto ErrorReturn;
                        /*
                         * The following use of strlen rather than the
                         * value returned from sprintf is because SUNOS4
@@ -584,11 +594,13 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                         switch(sizeModifier) {
                             case ' ':
                                 doubleArg = va_arg(arg, double);
-                               frexp(doubleArg, &exp);
+                                               frexp(doubleArg, &exp);
                                 break;
                             case 'L':
                                 lDoubleArg = va_arg(arg, LONG_DOUBLE);
-                               frexp(lDoubleArg, &exp);
+                                /* XXX Need to check for the presence of 
+                                 * frexpl() and use it if available */
+                                               frexp((double) lDoubleArg, &exp);
                                 break;
                             default:
                                 goto ErrorReturn;
@@ -621,7 +633,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                        if(auxBuffPtr != NULL) free(auxBuffPtr);
                         auxBuffPtr = (char *)Malloc(buffReqd);
                         auxBuffLen = buffReqd;
-                        if(auxBuffPtr == NULL) goto ErrorReturn;
+                        if(auxBuffPtr == NULL)
+                            goto ErrorReturn;
                    }
                     buffPtr = auxBuffPtr;
                    buffLen = auxBuffLen;
@@ -656,8 +669,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                             buffCount = strlen(buffPtr);
                             break;
                        case 'h':
-                            shortArg = va_arg(arg, short);
-                           sprintf(buffPtr, fmtBuff, shortArg);
+                            shortArg = (short) va_arg(arg, int);
+                            sprintf(buffPtr, fmtBuff, shortArg);
                             buffCount = strlen(buffPtr);
                             break;
                        default:
@@ -680,8 +693,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                             buffCount = strlen(buffPtr);
                             break;
                         case 'h':
-                            uShortArg = va_arg(arg, unsigned short);
-                           sprintf(buffPtr, fmtBuff, uShortArg);
+                            uShortArg = (unsigned short) va_arg(arg, int);
+                            sprintf(buffPtr, fmtBuff, uShortArg);
                             buffCount = strlen(buffPtr);
                             break;
                         default:
@@ -729,7 +742,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                     }
                     break;
                 case 'p':
-                    if(sizeModifier != ' ') goto ErrorReturn;
+                    if(sizeModifier != ' ')
+                        goto ErrorReturn;
                     voidPtrArg = va_arg(arg, void *);
                    sprintf(buffPtr, fmtBuff, voidPtrArg);
                     buffCount = strlen(buffPtr);
@@ -745,8 +759,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                             *longPtrArg = streamCount;
                             break;
                         case 'h':
-                            shortPtrArg = va_arg(arg, short *);
-                            *shortPtrArg = streamCount;
+                            shortPtrArg = (short *) va_arg(arg, short *);
+                            *shortPtrArg = (short) streamCount;
                             break;
                        default:
                             goto ErrorReturn;
@@ -803,7 +817,8 @@ int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg)
                     break;
             } /* switch(op) */
             if(performedOp) break;
-            if(!fastPath) goto ErrorReturn;
+            if(!fastPath)
+                goto ErrorReturn;
             fastPath = FALSE;
         } /* for (;;) */
         ASSERT(buffCount < buffLen);
@@ -884,6 +899,8 @@ int FCGX_FFlush(FCGX_Stream *stream)
  */
 int FCGX_FClose(FCGX_Stream *stream)
 {
+    if (stream == NULL) return 0;
+
     if(!stream->wasFCloseCalled) {
         if(!stream->isReader) {
             stream->emptyBuffProc(stream, TRUE);
@@ -1074,9 +1091,12 @@ char *FCGX_GetParam(const char *name, FCGX_ParamArray envp)
 {
     int len;
     char **p;
+
+       if (name == NULL || envp == NULL) return NULL;
+
     len = strlen(name);
-    if(len == 0) return NULL;
-    for (p = envp; *p != NULL; p++) {
+
+    for (p = envp; *p; ++p) {
         if((strncmp(name, *p, len) == 0) && ((*p)[len] == '=')) {
             return *p+len+1;
         }
@@ -1175,12 +1195,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;
 }
@@ -1199,11 +1219,11 @@ static FCGI_EndRequestBody MakeEndRequestBody(
         int protocolStatus)
 {
     FCGI_EndRequestBody body;
-    body.appStatusB3 = (appStatus >> 24) & 0xff;
-    body.appStatusB2 = (appStatus >> 16) & 0xff;
-    body.appStatusB1 = (appStatus >>  8) & 0xff;
-    body.appStatusB0 = (appStatus      ) & 0xff;
-    body.protocolStatus = protocolStatus;
+    body.appStatusB3    = (unsigned char) ((appStatus >> 24) & 0xff);
+    body.appStatusB2    = (unsigned char) ((appStatus >> 16) & 0xff);
+    body.appStatusB1    = (unsigned char) ((appStatus >>  8) & 0xff);
+    body.appStatusB0    = (unsigned char) ((appStatus      ) & 0xff);
+    body.protocolStatus = (unsigned char) protocolStatus;
     memset(body.reserved, 0, sizeof(body.reserved));
     return body;
 }
@@ -1221,7 +1241,7 @@ static FCGI_UnknownTypeBody MakeUnknownTypeBody(
         int type)
 {
     FCGI_UnknownTypeBody body;
-    body.type = type;
+    body.type = (unsigned char) type;
     memset(body.reserved, 0, sizeof(body.reserved));
     return body;
 }
@@ -1425,7 +1445,7 @@ static int ProcessManagementRecord(int type, FCGX_Stream *stream)
     char **pPtr;
     char response[64]; /* 64 = 8 + 3*(1+1+14+1)* + padding */
     char *responseP = &response[FCGI_HEADER_LEN];
-    char *name, value;
+    char *name, value = '\0';
     int len, paddedLen;
     if(type == FCGI_GET_VALUES) {
         ReadParams(paramsPtr, stream);
@@ -1974,28 +1994,35 @@ void FCGX_Finish_r(FCGX_Request *reqDataPtr)
         return;
     }
 
+    /* This should probably use a 'status' member instead of 'in' */
     if (reqDataPtr->in) {
         int errStatus = FCGX_FClose(reqDataPtr->err);
         int outStatus = FCGX_FClose(reqDataPtr->out);
 
-        if (errStatus  || outStatus
-            || FCGX_GetError(reqDataPtr->in)
-            || !reqDataPtr->keepConnection)
+        if (errStatus || outStatus || FCGX_GetError(reqDataPtr->in))
         {
             OS_IpcClose(reqDataPtr->ipcFd);
+            reqDataPtr->ipcFd = -1;
         }
+    }
 
-        ASSERT(reqDataPtr->nWriters == 0);
+    FCGX_Free(reqDataPtr);
+}
 
-        FreeStream(&reqDataPtr->in);
-        FreeStream(&reqDataPtr->out);
-        FreeStream(&reqDataPtr->err);
+void FCGX_Free(FCGX_Request * request)
+{
+    if (request == NULL) 
+        return;
 
-        FreeParams(&reqDataPtr->paramsPtr);
-    }
+    FreeStream(&request->in);
+    FreeStream(&request->out);
+    FreeStream(&request->err);
+    FreeParams(&request->paramsPtr);
 
-    if (!reqDataPtr->keepConnection) {
-        reqDataPtr->ipcFd = -1;
+    if (!request->keepConnection)
+    {
+        OS_IpcClose(request->ipcFd);
+        request->ipcFd = -1;
     }
 }
 
@@ -2019,6 +2046,8 @@ int FCGX_InitRequest(FCGX_Request *request, int sock, int flags)
     /* @@@ Should validate against "known" flags */
     request->flags = flags;
 
+    request->ipcFd = -1;
+
     return 0;
 }
 
@@ -2095,9 +2124,10 @@ int FCGX_Accept(
 {
     int rc;
 
-    if (!libInitialized) {
-        if ((rc = FCGX_Init())) {
-            return (rc < 0) ? rc : -rc;
+    if (! libInitialized) {
+        rc = FCGX_Init();
+        if (rc) {
+            return rc;
         }
     }
 
@@ -2198,14 +2228,14 @@ int FCGX_Accept_r(FCGX_Request *reqDataPtr)
              */
             break;
         }
+
         /*
          * Close the connection and try again.
          */
-      TryAgain:
-        FreeParams(&reqDataPtr->paramsPtr);
-        FreeStream(&reqDataPtr->in);
-        OS_Close(reqDataPtr->ipcFd);
-        reqDataPtr->ipcFd = -1;
+TryAgain:
+       reqDataPtr->keepConnection = 0;
+        FCGX_Free(reqDataPtr);
+
     } /* for (;;) */
     /*
      * Build the remaining data structures representing the new