discard any remaining data in input stream which otherwise ends up in next request
[catagits/fcgi2.git] / libfcgi / fcgiapp.c
index 9c3d330..2613031 100644 (file)
@@ -11,7 +11,7 @@
  *
  */
 #ifndef lint
-static const char rcsid[] = "$Id: fcgiapp.c,v 1.23 2001/06/20 16:50:02 robs Exp $";
+static const char rcsid[] = "$Id: fcgiapp.c,v 1.35 2003/06/22 00:16:43 robs Exp $";
 #endif /* not lint */
 
 #include <assert.h>
@@ -47,12 +47,10 @@ static const char rcsid[] = "$Id: fcgiapp.c,v 1.23 2001/06/20 16:50:02 robs Exp
 #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
@@ -73,6 +71,11 @@ static int isFastCGI = -1;
 static char *webServerAddressList = NULL;
 static FCGX_Request the_request;
 
+void FCGX_ShutdownPending(void)
+{
+    OS_ShutdownPending();
+}
+
 static void *Malloc(size_t size)
 {
     void *result = malloc(size);
@@ -89,7 +92,7 @@ static char *StringCopy(char *str)
     return newString;
 }
 
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -104,18 +107,24 @@ static char *StringCopy(char *str)
  */
 int FCGX_GetChar(FCGX_Stream *stream)
 {
-    if(stream->rdNext != stream->stop)
-        return *stream->rdNext++;
-    if(stream->isClosed || !stream->isReader)
+    if (stream->isClosed || ! stream->isReader)
         return EOF;
+
+    if (stream->rdNext != stream->stop)
+        return *stream->rdNext++;
+
     stream->fillBuffProc(stream);
+    if (stream->isClosed)
+        return EOF;
+
     stream->stopUnget = stream->rdNext;
-    if(stream->rdNext != stream->stop)
+    if (stream->rdNext != stream->stop)
         return *stream->rdNext++;
+
     ASSERT(stream->isClosed); /* bug in fillBufProc if not */
     return EOF;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -135,7 +144,7 @@ int FCGX_GetStr(char *str, int n, FCGX_Stream *stream)
 {
     int m, bytesMoved;
 
-    if(n <= 0) {
+    if (stream->isClosed || ! stream->isReader || n <= 0) {
         return 0;
     }
     /*
@@ -160,14 +169,17 @@ int FCGX_GetStr(char *str, int n, FCGX_Stream *stream)
             if(bytesMoved == n)
                 return bytesMoved;
             str += m;
-       }
+        }
         if(stream->isClosed || !stream->isReader)
             return bytesMoved;
         stream->fillBuffProc(stream);
+        if (stream->isClosed)
+            return bytesMoved;
+
         stream->stopUnget = stream->rdNext;
     }
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -207,7 +219,7 @@ char *FCGX_GetLine(char *str, int n, FCGX_Stream *stream)
     *p = '\0';
     return str;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -254,7 +266,7 @@ int FCGX_UnGetChar(int c, FCGX_Stream *stream) {
 int FCGX_HasSeenEOF(FCGX_Stream *stream) {
     return (stream->isClosed) ? EOF : 0;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -279,7 +291,7 @@ int FCGX_PutChar(int c, FCGX_Stream *stream)
     ASSERT(stream->isClosed); /* bug in emptyBuffProc if not */
     return EOF;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -327,7 +339,7 @@ int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream)
         stream->emptyBuffProc(stream, FALSE);
     }
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -345,7 +357,7 @@ int FCGX_PutS(const char *str, FCGX_Stream *stream)
 {
     return FCGX_PutStr(str, strlen(str), stream);
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -853,7 +865,7 @@ static void CopyAndAdvance(char **destPtr, char **srcPtr, int n)
     *destPtr = dest;
     *srcPtr = src;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -879,7 +891,7 @@ int FCGX_FFlush(FCGX_Stream *stream)
     stream->emptyBuffProc(stream, FALSE);
     return (stream->isClosed) ? -1 : 0;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -915,7 +927,7 @@ int FCGX_FClose(FCGX_Stream *stream)
     }
     return (stream->FCGI_errno == 0) ? 0 : EOF;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -934,8 +946,9 @@ static void SetError(FCGX_Stream *stream, int FCGI_errno)
      */
     if(stream->FCGI_errno == 0) {
         stream->FCGI_errno = FCGI_errno;
-        stream->isClosed = TRUE;
     }
+  
+    stream->isClosed = TRUE;
 }
 
 /*
@@ -975,7 +988,7 @@ void FCGX_ClearError(FCGX_Stream *stream) {
      * of the stream that are now all lumped under isClosed.
      */
 }
-\f
+
 /*
  *======================================================================
  * Parameters
@@ -994,7 +1007,7 @@ typedef struct Params {
     char **cur;                    /* current item in vec; *cur == NULL */
 } Params;
 typedef Params *ParamsPtr;
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1017,7 +1030,7 @@ static ParamsPtr NewParams(int length)
     *result->cur = NULL;
     return result;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1044,7 +1057,7 @@ static void FreeParams(ParamsPtr *paramsPtrPtr)
     free(paramsPtr);
     *paramsPtrPtr = NULL;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1073,7 +1086,7 @@ static void PutParam(ParamsPtr paramsPtr, char *nameValue)
     }
     *paramsPtr->cur = NULL;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1103,7 +1116,7 @@ char *FCGX_GetParam(const char *name, FCGX_ParamArray envp)
     }
     return NULL;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1111,7 +1124,7 @@ char *FCGX_GetParam(const char *name, FCGX_ParamArray envp)
  *
  *----------------------------------------------------------------------
  */
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1175,7 +1188,7 @@ static int ReadParams(Params *paramsPtr, FCGX_Stream *stream)
     }
     return 0;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1204,7 +1217,7 @@ static FCGI_Header MakeHeader(
     header.reserved         =  0;
     return header;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1227,7 +1240,7 @@ static FCGI_EndRequestBody MakeEndRequestBody(
     memset(body.reserved, 0, sizeof(body.reserved));
     return body;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1245,7 +1258,7 @@ static FCGI_UnknownTypeBody MakeUnknownTypeBody(
     memset(body.reserved, 0, sizeof(body.reserved));
     return body;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1275,7 +1288,7 @@ static unsigned char *AlignPtr8(unsigned char *p) {
     u = ((u + 7) & (ULONG_MAX - 7)) - u;
     return p + u;
 }
-\f
+
 
 /*
  * State associated with a stream
@@ -1298,7 +1311,7 @@ typedef struct FCGX_Stream_Data {
     int rawWrite;             /* writer: write data without stream headers */
     FCGX_Request *reqDataPtr; /* request data not specific to one stream */
 } FCGX_Stream_Data;
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1343,7 +1356,7 @@ static void WriteCloseRecords(struct FCGX_Stream *stream)
     }
     data->reqDataPtr->nWriters--;
 }
-\f
+
 
 
 static int write_it_all(int fd, char *buf, int len)
@@ -1415,7 +1428,7 @@ static void EmptyBuffProc(struct FCGX_Stream *stream, int doClose)
         stream->wrNext += sizeof(FCGI_Header);
     }
 }
-\f
+
 /*
  * Return codes for Process* functions
  */
@@ -1492,7 +1505,7 @@ static int ProcessManagementRecord(int type, FCGX_Stream *stream)
 
     return MGMT_RECORD;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1549,7 +1562,7 @@ static int ProcessBeginRecord(int requestId, FCGX_Stream *stream)
     data->reqDataPtr->isBeginProcessed = TRUE;
     return BEGIN_RECORD;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1598,7 +1611,7 @@ static int ProcessHeader(FCGI_Header header, FCGX_Stream *stream)
     }
     return STREAM_RECORD;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1727,7 +1740,7 @@ static void FillBuffProc(FCGX_Stream *stream)
        }
     }
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1794,18 +1807,18 @@ static FCGX_Stream *NewStream(
     }
     return stream;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
- * FreeStream --
+ * FCGX_FreeStream --
  *
  *      Frees all storage allocated when *streamPtr was created,
  *      and nulls out *streamPtr.
  *
  *----------------------------------------------------------------------
  */
-void FreeStream(FCGX_Stream **streamPtr)
+void FCGX_FreeStream(FCGX_Stream **streamPtr)
 {
     FCGX_Stream *stream = *streamPtr;
     FCGX_Stream_Data *data;
@@ -1819,7 +1832,7 @@ void FreeStream(FCGX_Stream **streamPtr)
     free(stream);
     *streamPtr = NULL;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1842,7 +1855,7 @@ static FCGX_Stream *SetReaderType(FCGX_Stream *stream, int streamType)
     stream->isClosed = FALSE;
     return stream;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1858,7 +1871,6 @@ static FCGX_Stream *NewReader(FCGX_Request *reqDataPtr, int bufflen, int streamT
     return NewStream(reqDataPtr, bufflen, TRUE, streamType);
 }
 
-
 /*
  *----------------------------------------------------------------------
  *
@@ -1875,11 +1887,10 @@ static FCGX_Stream *NewWriter(FCGX_Request *reqDataPtr, int bufflen, int streamT
     return NewStream(reqDataPtr, bufflen, FALSE, streamType);
 }
 
-
 /*
  *----------------------------------------------------------------------
  *
- * CreateWriter --
+ * FCGX_CreateWriter --
  *
  *      Creates a stream to write streamType FastCGI records, using
  *      the given ipcFd and request Id.  This function is provided
@@ -1888,7 +1899,7 @@ static FCGX_Stream *NewWriter(FCGX_Request *reqDataPtr, int bufflen, int streamT
  *
  *----------------------------------------------------------------------
  */
-FCGX_Stream *CreateWriter(
+FCGX_Stream *FCGX_CreateWriter(
         int ipcFd,
         int requestId,
         int bufflen,
@@ -1903,7 +1914,7 @@ FCGX_Stream *CreateWriter(
     reqDataPtr->nWriters = 2;
     return NewWriter(reqDataPtr, bufflen, streamType);
 }
-\f
+
 /*
  *======================================================================
  * Control
@@ -1943,7 +1954,7 @@ int FCGX_IsCGI(void)
 
     return !isFastCGI;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -1990,39 +2001,50 @@ void FCGX_Finish(void)
  */
 void FCGX_Finish_r(FCGX_Request *reqDataPtr)
 {
+    int close;
+
     if (reqDataPtr == NULL) {
         return;
     }
 
+    close = !reqDataPtr->keepConnection;
+
     /* 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);
+        close |= FCGX_FClose(reqDataPtr->err);
+        close |= FCGX_FClose(reqDataPtr->out);
+        close |= FCGX_GetError(reqDataPtr->in);
 
-        if (errStatus || outStatus || FCGX_GetError(reqDataPtr->in))
-        {
-            OS_IpcClose(reqDataPtr->ipcFd);
-            reqDataPtr->ipcFd = -1;
+        /* discard any remaining data in input stream */
+        if (!close && !reqDataPtr->in->isClosed) {
+            FCGX_Stream *stream = reqDataPtr->in;
+
+            do {
+                stream->rdNext = stream->stop;
+                stream->fillBuffProc(stream);
+            } while (!stream->isClosed);
+
+            close |= FCGX_GetError(stream);
         }
     }
 
-    FCGX_Free(reqDataPtr);
+    FCGX_Free(reqDataPtr, close);
 }
 
-void FCGX_Free(FCGX_Request * request)
+void FCGX_Free(FCGX_Request * request, int close)
 {
     if (request == NULL) 
         return;
 
-    FreeStream(&request->in);
-    FreeStream(&request->out);
-    FreeStream(&request->err);
+    FCGX_FreeStream(&request->in);
+    FCGX_FreeStream(&request->out);
+    FCGX_FreeStream(&request->err);
     FreeParams(&request->paramsPtr);
 
-    if (!request->keepConnection)
-    {
-        OS_IpcClose(request->ipcFd);
+    if (close) {
+        OS_IpcClose(request->ipcFd, ! request->detached);
         request->ipcFd = -1;
+        request->detached = 0;
     }
 }
 
@@ -2072,9 +2094,6 @@ int FCGX_Init(void)
         return 0;
     }
 
-    /* If our compiler doesn't play by the ISO rules for struct layout, halt. */
-    ASSERT(sizeof(FCGI_Header) == FCGI_HEADER_LEN);
-
     FCGX_InitRequest(&the_request, FCGI_LISTENSOCK_FILENO, 0);
 
     if (OS_LibInit(NULL) == -1) {
@@ -2233,7 +2252,7 @@ int FCGX_Accept_r(FCGX_Request *reqDataPtr)
          * Close the connection and try again.
          */
 TryAgain:
-        FCGX_Free(reqDataPtr);
+        FCGX_Free(reqDataPtr, 1);
 
     } /* for (;;) */
     /*
@@ -2247,7 +2266,7 @@ TryAgain:
     reqDataPtr->envp = reqDataPtr->paramsPtr->vec;
     return 0;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -2279,7 +2298,7 @@ int FCGX_StartFilterData(FCGX_Stream *stream)
     SetReaderType(stream, FCGI_DATA);
     return 0;
 }
-\f
+
 /*
  *----------------------------------------------------------------------
  *
@@ -2300,3 +2319,23 @@ void FCGX_SetExitStatus(int status, FCGX_Stream *stream)
     data->reqDataPtr->appStatus = status;
 }
 
+
+int 
+FCGX_Attach(FCGX_Request * r)
+{
+    r->detached = FALSE;
+    return 0;
+}
+
+
+int 
+FCGX_Detach(FCGX_Request * r)
+{
+    if (r->ipcFd <= 0)
+    {
+        return -1;
+    }
+
+    r->detached = TRUE;
+    return 0;
+}