Index: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c
===================================================================
--- branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c	(revision 512)
+++ branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c	(revision 549)
@@ -30,8 +30,14 @@
  */
 #ifndef WIN32
+/*
 #include "fcgio.h"
 #include "fcgi_config.h"
 #include "fcgi_stdio.h"
-#endif
+*/
+#include <unistd.h>
+#include <fcgiapp.h>
+#endif
+#include <sys/wait.h>
+#include <pthread.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -56,11 +62,11 @@
 #include <glib.h>
 #include <sys/stat.h>
+}
+
 #include "service_zcfg.h"
-}
-
-#include "service_internal.h"
+//#include "service_internal.h"
 
 xmlXPathObjectPtr extractFromDoc (xmlDocPtr, const char *);
-int runRequest (map **);
+int runRequest (map **,struct cgi_env **,FCGX_Request *);
 
 using namespace std;
@@ -74,106 +80,88 @@
 
 
-int
-main (int argc, char *argv[])
+static void PrintEnv(FCGX_Stream *out, char *label, char **envp)
 {
-  maps *m;
-  m = (maps *) malloc (MAP_SIZE);
-  conf_read ("main.cfg", m);
-  char ntmp[1024];
-#ifndef WIN32
-  getcwd (ntmp, 1024);
-#else
-  _getcwd (ntmp, 1024);
-#endif
-  char *rootDir = "/var/www/zoo-wps/cgi-bin";
-  init_services_conf (rootDir);
-
-
-  while (FCGI_Accept () >= 0)
-    {
-      cgiMain_init (argc, argv);
-/**
-   * We'll use cgiOut as the default output (stdout) to produce plain text 
-   * response.
-   */
-      dup2 (fileno (cgiOut), fileno (stdout));
-
-#ifdef DEBUG
-      fprintf (cgiOut,
-               "Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
-      fprintf (cgiOut, "Welcome on ZOO verbose debuging mode \r\n\r\n");
-      fflush (cgiOut);
-      fprintf (stderr, "Addr:%s\n", cgiRemoteAddr);
-      fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,
-               strncasecmp (cgiRequestMethod, "post", 4),
-               strncmp (cgiContentType, "text/xml", 8) == 0
-               || strncasecmp (cgiRequestMethod, "post", 4) == 0);
-      fprintf (stderr, "Request: %s\n", cgiQueryString);
-      fprintf (stderr, "ContentType: %s\n", cgiContentType);
-      fprintf (stderr, "ContentLength: %d\n", cgiContentLength);
-      fflush (stderr);
-#endif
-
-
+    FCGX_FPrintF(out, "%s:<br>\n<pre>\n", label);
+    for( ; *envp != NULL; envp++) {
+        FCGX_FPrintF(out, "%s\n", *envp);
+    }
+    FCGX_FPrintF(out, "</pre><p>\n");
+}
+
+#define PATH_SOCKET "/tmp/zoo.sock"
+#define THREAD_COUNT 50
+static int counts[THREAD_COUNT];
+
+
+int process(FCGX_Request *request){
+
+      int pid = getpid();
+      struct cgi_env *cgi;
+      //PrintEnv(request.err, "Request environment", request.envp); 
+      cgi = (struct cgi_env*)malloc(sizeof(struct cgi_env));
+      cgiMain_init (NULL, NULL,&cgi,request);
       char *strQuery = NULL;
-      if (cgiQueryString != NULL)
-        strQuery = zStrdup (cgiQueryString);
+      if (cgi->cgiQueryString != NULL)
+        strQuery = zStrdup (cgi->cgiQueryString);
       map *tmpMap = NULL;
 
-      if (strncmp (cgiContentType, "text/xml", 8) == 0 ||
-          strncasecmp (cgiRequestMethod, "post", 4) == 0)
+      if (strncmp (cgi->cgiContentType, "text/xml", 8) == 0 ||
+          strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0)
         {
-          if (cgiContentLength == 0)
-            {
-              char *buffer = new char[2];
-              char *res = NULL;
-              int r = 0;
-              while ((r = fread (buffer, sizeof (char), 1, cgiIn)))
-                {
-                  buffer[1] = 0;
-                  cgiContentLength += r;
-                  if (res == NULL)
-                    {
-                      res = (char *) malloc (2 * sizeof (char));
-                      sprintf (res, "%s", buffer);
+          if (cgi->cgiContentLength == 0)
+            {
+              char *post_data = NULL;
+              int i = 0;
+              int ch = FCGX_GetChar(request->in);
+              while (ch != -1){
+                {
+                  i++;
+                  if (post_data == NULL)
+                    {
+                    post_data=(char*)malloc(sizeof(char));
+                    post_data[i-1] = (char) ch;
                     }
                   else
                     {
-                      char *tmp = zStrdup (res);
-                      res =
-                        (char *) realloc (res,
-                                          (strlen (tmp) + 2) * sizeof (char));
-                      sprintf (res, "%s%s", tmp, buffer);
-                      free (tmp);
-                    }
-                }
-              delete[]buffer;
-              if (res == NULL && (strQuery == NULL || strlen (strQuery) == 0))
+                    post_data=(char*)realloc(post_data,i*sizeof(char));
+                    post_data[i-1] = (char) ch;
+                    }
+                  ch = FCGX_GetChar(request->in);
+                  if (ch == -1 ){
+                    post_data=(char*)realloc(post_data,(i + 1)*sizeof(char));
+                    post_data[i] = '\0';
+                    }
+                }
+                cgi->cgiContentLength = i;
+              if (post_data == NULL && (strQuery == NULL || strlen (strQuery) == 0))
                 {
                   return errorException (NULL,
                                          "ZOO-Kernel failed to process your request cause the request was emtpty.",
-                                         "InternalError", NULL);
+                                         "InternalError", NULL,request->out);
                 }
               else
                 {
                   if (strQuery == NULL || strlen (strQuery) == 0)
-                    tmpMap = createMap ("request", res);
-                }
-              if (res != NULL)
-                free (res);
+                    tmpMap = createMap ("request", post_data);
+                }
+              if (post_data != NULL)
+                free (post_data);
+                }
             }
           else
             {
-              char *buffer = new char[cgiContentLength + 1];
-              if (fread (buffer, sizeof (char), cgiContentLength, cgiIn) > 0)
-                {
-                  buffer[cgiContentLength] = 0;
-                  tmpMap = createMap ("request", buffer);
+              char *post_data = new char[cgi->cgiContentLength + 1];
+              int r = FCGX_GetStr(post_data,cgi->cgiContentLength,request->in);
+              if ( r > 0)
+                {
+                  post_data[r] = '\0';
+                  cgi->cgiContentLength = r;
+                  tmpMap = createMap ("request", post_data);
                 }
               else
                 {
-                  buffer[0] = 0;
+                  post_data[0] = '\0';
                   char **array, **arrayStep;
-                  if (cgiFormEntries (&array) != cgiFormSuccess)
+                  if (cgiFormEntries (&array,&cgi) != cgiFormSuccess)
                     {
                       return 1;
@@ -182,7 +170,7 @@
                   while (*arrayStep)
                     {
-                      char *ivalue = new char[cgiContentLength];
+                      char *ivalue = new char[cgi->cgiContentLength];
                       cgiFormStringNoNewlines (*arrayStep, ivalue,
-                                               cgiContentLength);
+                                               cgi->cgiContentLength,&cgi);
                       char *tmpValueFinal =
                         (char *)
@@ -190,12 +178,12 @@
                                  1) * sizeof (char));
                       sprintf (tmpValueFinal, "%s=%s", *arrayStep, ivalue);
-                      if (strlen (buffer) == 0)
+                      if (strlen (post_data) == 0)
                         {
-                          sprintf (buffer, "%s", tmpValueFinal);
+                          sprintf (post_data, "%s", tmpValueFinal);
                         }
                       else
                         {
-                          char *tmp = zStrdup (buffer);
-                          sprintf (buffer, "%s&%s", tmp, tmpValueFinal);
+                          char *tmp = zStrdup (post_data);
+                          sprintf (post_data, "%s&%s", tmp, tmpValueFinal);
                           free (tmp);
                         }
@@ -209,9 +197,9 @@
                     }
                   if (tmpMap != NULL)
-                    addToMap (tmpMap, "request", buffer);
+                    addToMap (tmpMap, "request", post_data);
                   else
-                    tmpMap = createMap ("request", buffer);
-                }
-              delete[]buffer;
+                    tmpMap = createMap ("request", post_data);
+                }
+              delete[]post_data;
             }
         }
@@ -221,6 +209,7 @@
           dumpMap (tmpMap);
 #endif
-          char **array, **arrayStep;
-          if (cgiFormEntries (&array) != cgiFormSuccess)
+       
+       char **array, **arrayStep;
+          if (cgiFormEntries (&array,&cgi) != cgiFormSuccess)
             {
               return 1;
@@ -229,6 +218,6 @@
           while (*arrayStep)
             {
-              char *value = new char[cgiContentLength];
-              cgiFormStringNoNewlines (*arrayStep, value, cgiContentLength);
+              char *value = new char[cgi->cgiContentLength];
+              cgiFormStringNoNewlines (*arrayStep, value, cgi->cgiContentLength,&cgi);
 #ifdef DEBUG
               fprintf (stderr, "(( \n %s \n %s \n ))", *arrayStep, value);
@@ -265,5 +254,5 @@
    * one.
    */
-      if (strncasecmp (cgiRequestMethod, "post", 4) == 0 ||
+      if (strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0 ||
           (count (tmpMap) == 1 && strncmp (tmpMap->value, "<", 1) == 0)
 #ifdef WIN32
@@ -280,5 +269,5 @@
               addToMap (tmpMap, "xrequest", t1->value);
               xmlInitParser ();
-              xmlDocPtr doc = xmlParseMemory (t1->value, cgiContentLength);
+              xmlDocPtr doc = xmlParseMemory (t1->value, cgi->cgiContentLength);
               {
                 xmlXPathObjectPtr reqptr = extractFromDoc (doc,
@@ -384,5 +373,5 @@
                           char *identifiers = NULL;
                           identifiers =
-                            (char *) calloc (cgiContentLength, sizeof (char));
+                            (char *) calloc (cgi->cgiContentLength, sizeof (char));
                           identifiers[0] = 0;
                           for (int k = 0; k < id->nodeNr; k++)
@@ -427,5 +416,5 @@
 
           char *token, *saveptr;
-          token = strtok_r (cgiQueryString, "&", &saveptr);
+          token = strtok_r (cgi->cgiQueryString, "&", &saveptr);
           while (token != NULL)
             {
@@ -452,5 +441,5 @@
         }
 
-      if (strncasecmp (cgiContentType, "multipart/form-data", 19) == 0)
+      if (strncasecmp (cgi->cgiContentType, "multipart/form-data", 19) == 0)
         {
           map *tmp = getMap (tmpMap, "dataInputs");
@@ -465,5 +454,5 @@
         free (strQuery);
 
-      runRequest (&tmpMap);
+      runRequest (&tmpMap,&cgi,request);
 
   /** 
@@ -475,8 +464,77 @@
           free (tmpMap);
         }
-      cgiFreeResources ();
+        // a verifier fait planter
+      cgiFreeResources (&cgi);
+
+      FCGX_Finish_r(request);
+      return 0;
+}
+
+
+
+int
+main (int argc, char *argv[])
+{
+  maps *main_config;
+  int max_requests, start_servers;
+  start_servers = 10;
+  max_requests= 100;
+  main_config = (maps *) malloc (MAP_SIZE);
+  conf_read ("main.cfg", main_config);
+  char ntmp[1024];
+#ifndef WIN32
+  getcwd (ntmp, 1024);
+#else
+  _getcwd (ntmp, 1024);
+#endif
+  char *rootDir = "/var/www/zoo-wps/cgi-bin";
+  int fork_status = fork();
+  if (fork_status == 0){
+    //child
+    int forker_pid = getpid();
+    init_services_conf (rootDir);
+    int sock = FCGX_OpenSocket(PATH_SOCKET, 1000);
+    FCGX_Init();
+    FCGX_Request request;
+    FCGX_InitRequest(&request, sock, 0);
+    int i;
+    int count_request = 0;
+    for (i = 0; i< start_servers; i++){
+        fork_status = fork();
+        if (fork_status == 0){
+            fprintf(stderr,"child %d \n",i);
+            fflush(stderr);
+            break;
+        }
     }
-  FCGI_Finish ();
+    while(1){
+        if (forker_pid != getpid()){
+            while(FCGX_Accept_r(&request) == 0){
+                process(&request);
+                count_request ++;
+                if (count_request >= max_requests){
+                    fprintf(stderr,"Max request stop process\n");
+                    fflush(stderr);
+                    exit(0);
+                }
+            }
+        }
+        else {
+            wait(0);
+            fprintf(stderr,"new child\n");
+            fflush(stderr);
+            fork();
+        }
+    }
+  }
+  else {
+  
+  while(1);
+
+
+
+  }
+  
   return 0;
-
 }
+ 
