Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/Makefile.in
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/Makefile.in	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/Makefile.in	(revision 549)
@@ -12,6 +12,6 @@
 JAVA_FILE=@JAVA_FILE@
 
-CFLAGS=@DEB_DEF@ -fpic ${GLIB_CPPFLAGS} ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
-LDFLAGS=-lcurl -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${GLIB_LDFLAGS}  ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS} -lfcgi -lcrypto ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${YAML_LDFLAGS} 
+CFLAGS=@DEB_DEF@  -fpic ${GLIB_CPPFLAGS} ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
+LDFLAGS=  -lcurl -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${GLIB_LDFLAGS}  ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS} -lfcgi -lcrypto ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${YAML_LDFLAGS} 
 
 PHPCFLAGS=@PHP_CPPFLAGS@
@@ -59,6 +59,6 @@
 	gcc -fPIC ${XML2CFLAGS} ${CFLAGS} ${JSCFLAGS} ${JS_ENABLED} -c ulinet.c
 
-service_zcfg.o: service_zcfg.c
-	gcc -fPIC ${GLIB_CPPFLAGS} ${XML2CFLAGS} -c service_zcfg.c
+service_zcfg.o: service_zcfg.c service.h
+	g++ ${GLIB_CPPFLAGS} ${XML2CFLAGS} -c service_zcfg.c
 
 service_internal.o: service_internal.c service.h
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/configure.ac
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/configure.ac	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/configure.ac	(revision 549)
@@ -340,5 +340,5 @@
 	LIBS="$PYTHON_LDFLAGS"
 	PY_LIB=`$PYTHONCONFIG --libs | sed -e 's/^.*\(python2\..\)$/\1/'`
-	AC_CHECK_LIB([$PY_LIB], [PyObject_CallObject], [], [AC_MSG_ERROR([could not find libpython])], [])
+	#AC_CHECK_LIB([$PY_LIB], [PyObject_CallObject], [], [AC_MSG_ERROR([could not find libpython])], [])
 	AC_SUBST([PYTHON_CPPFLAGS])
 	AC_SUBST([PYTHON_LDFLAGS])
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal.c
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal.c	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal.c	(revision 549)
@@ -58,5 +58,5 @@
 
 void
-printHeaders (maps * m)
+printHeaders (maps * m,FCGX_Stream *out)
 {
   maps *_tmp = getMaps (m, "headers");
@@ -66,5 +66,6 @@
       while (_tmp1 != NULL)
         {
-          printf ("%s: %s\r\n", _tmp1->name, _tmp1->value);
+          FCGX_FPrintF(out,"%s: %s\r\n", _tmp1->name, _tmp1->value);
+          //printf ("%s: %s\r\n", _tmp1->name, _tmp1->value);
           _tmp1 = _tmp1->next;
         }
@@ -100,5 +101,5 @@
 
 #include <windows.h>
-#include <fcgi_stdio.h>
+//#include <fcgi_stdio.h>
 #include <stdio.h>
 #include <conio.h>
@@ -1689,5 +1690,5 @@
 printProcessResponse (maps * m, map * request, int pid, service * serv,
                       const char *service, int status, maps * inputs,
-                      maps * outputs)
+                      maps * outputs, FCGX_Stream* out)
 {
   xmlNsPtr ns, ns_ows, ns_xlink, ns_xsi;
@@ -1990,5 +1991,5 @@
           map *errormap = createMap ("text", tmpMsg);
           addToMap (errormap, "code", "InternalError");
-          printExceptionReportResponse (m, errormap);
+          printExceptionReportResponse (m, errormap,out);
           freeMap (&errormap);
           free (errormap);
@@ -2005,5 +2006,5 @@
       fclose (output);
     }
-  printDocument (m, doc, pid);
+  printDocument (m, doc, pid,out);
 
   xmlCleanupParser ();
@@ -2013,14 +2014,14 @@
 
 void
-printDocument (maps * m, xmlDocPtr doc, int pid)
+printDocument (maps * m, xmlDocPtr doc, int pid,FCGX_Stream * out)
 {
   char *encoding = getEncoding (m);
   if (pid == getpid ())
     {
-      printHeaders (m);
-      printf ("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",
-              encoding);
-    }
-  fflush (stdout);
+      printHeaders (m,out);
+      FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
+      //printf ("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
+    }
+  //fflush (stdout);
   xmlChar *xmlbuff;
   int buffersize;
@@ -2030,6 +2031,8 @@
    */
   xmlDocDumpFormatMemoryEnc (doc, &xmlbuff, &buffersize, encoding, 1);
-  printf ("%s", xmlbuff);
-  fflush (stdout);
+  FCGX_FPrintF(out,(char *)xmlbuff);
+  FCGX_FFlush(out);
+  //printf ("%s", xmlbuff);
+  //fflush (stdout);
   /*
    * Free associated memory.
@@ -2462,5 +2465,5 @@
 
 void
-printExceptionReportResponse (maps * m, map * s)
+printExceptionReportResponse (maps * m, map * s,FCGX_Stream * out)
 {
   if (getMapFromMaps (m, "lenv", "hasPrinted") != NULL)
@@ -2504,27 +2507,29 @@
           if (getpid () == atoi (tmpSid->value))
             {
-              printHeaders (m);
-              printf
-                ("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",
-                 encoding, exceptionCode);
+              printHeaders (m,out);
+              FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
+              //printf("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
             }
         }
       else
         {
-          printHeaders (m);
-          printf ("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",
-                  encoding, exceptionCode);
+          printHeaders (m,out);
+          FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",
+                            encoding, exceptionCode);
+          //printf ("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
         }
     }
   else
-    {
-      printf ("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",
-              encoding, exceptionCode);
+    { 
+      FCGX_FPrintF(out,"Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
+      //printf ("Content-Type: text/xml; charset=%s\r\nStatus: %s\r\n\r\n",encoding, exceptionCode);
     }
   n = createExceptionReportNode (m, s, 1);
   xmlDocSetRootElement (doc, n);
   xmlDocDumpFormatMemoryEnc (doc, &xmlbuff, &buffersize, encoding, 1);
-  printf ("%s", xmlbuff);
-  fflush (stdout);
+  //printf ("%s", xmlbuff);
+  FCGX_FPrintF(out,"%s",xmlbuff);
+FCGX_FFlush(out);
+  //fflush (stdout);
   xmlFreeDoc (doc);
   xmlFree (xmlbuff);
@@ -2595,6 +2600,8 @@
 void
 outputResponse (service * s, maps * request_inputs, maps * request_outputs,
-                map * request_inputs1, int cpid, maps * m, int res)
-{
+                map * request_inputs1, int cpid, maps * m, int res,FCGX_Stream *out,FCGX_Stream *err)
+{
+
+
 #ifdef DEBUG
   dumpMaps (request_inputs);
@@ -2655,5 +2662,5 @@
           addToMap (errormap, "code", "InternalError");
 
-          printExceptionReportResponse (m, errormap);
+          printExceptionReportResponse (m, errormap,out);
           freeMap (&errormap);
           free (errormap);
@@ -2670,4 +2677,6 @@
   if (asRaw == 0)
     {
+
+
 #ifdef DEBUG
       fprintf (stderr, "REQUEST_OUTPUTS FINAL\n");
@@ -2760,5 +2769,6 @@
                   map *errormap = createMap ("text", tmpMsg);
                   addToMap (errormap, "code", "InternalError");
-                  printExceptionReportResponse (m, errormap);
+
+                  printExceptionReportResponse (m, errormap,out);
                   freeMap (&errormap);
                   free (errormap);
@@ -2833,10 +2843,12 @@
       printProcessResponse (m, request_inputs1, cpid,
                             s, r_inputs->value, res,
-                            request_inputs, request_outputs);
+                            request_inputs, request_outputs,out);
     }
   else
     {
+
       if (res == SERVICE_FAILED)
         {
+
           map *errormap;
           map *lenv;
@@ -2871,5 +2883,5 @@
           free (tmp0);
           addToMap (errormap, "code", "InternalError");
-          printExceptionReportResponse (m, errormap);
+          printExceptionReportResponse (m, errormap,out);
           freeMap (&errormap);
           free (errormap);
@@ -2880,4 +2892,6 @@
        * requested one is not present in the resulting outputs maps.
        */
+
+
       maps *tmpI = NULL;
       map *tmpIV = getMap (request_inputs1, "RawDataOutput");
@@ -2905,5 +2919,5 @@
               map *errormap = createMap ("text", tmpMsg);
               addToMap (errormap, "code", "InvalidParameterValue");
-              printExceptionReportResponse (m, errormap);
+              printExceptionReportResponse (m, errormap,out);
               freeMap (&errormap);
               free (errormap);
@@ -2912,10 +2926,11 @@
           map *fname = getMapFromMaps (tmpI, tmpI->name, "filename");
           if (fname != NULL)
-            printf ("Content-Disposition: attachment; filename=\"%s\"\r\n",
-                    fname->value);
+            FCGX_FPrintF(out,"Content-Disposition: attachment; filename=\"%s\"\r\n",fname->value);
+            //printf ("Content-Disposition: attachment; filename=\"%s\"\r\n",fname->value);
           map *rs = getMapFromMaps (tmpI, tmpI->name, "size");
           if (rs != NULL)
-            printf ("Content-Length: %s\r\n", rs->value);
-          printHeaders (m);
+            FCGX_FPrintF(out,"Content-Length: %s\r\n", rs->value);
+            //printf ("Content-Length: %s\r\n", rs->value);
+          printHeaders (m,out);
           char mime[1024];
           map *mi = getMap (tmpI->content, "mimeType");
@@ -2937,9 +2952,15 @@
             sprintf (mime,
                      "Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
-          printf ("%s", mime);
+          FCGX_FPrintF(out,"%s", mime);
+
+          //printf ("%s", mime);
           if (rs != NULL)
-            fwrite (toto->value, 1, atoi (rs->value), stdout);
+            FCGX_PutStr(toto->value,atoi(rs->value),out);
+            //fwrite (toto->value, 1, atoi (rs->value), stdout);
           else
-            fwrite (toto->value, 1, strlen (toto->value), stdout);
+             FCGX_PutStr(toto->value,strlen (toto->value),out);
+            //fwrite (toto->value, 1, strlen (toto->value), stdout);
+
+          FCGX_FFlush(out);
 #ifdef DEBUG
           dumpMap (toto);
@@ -3400,6 +3421,6 @@
 printBoundingBoxDocument (maps * m, maps * boundingbox, FILE * file)
 {
-  if (file == NULL)
-    rewind (stdout);
+  //if (file == NULL)
+   // rewind (stdout);
   xmlNodePtr n;
   xmlDocPtr doc;
@@ -3421,5 +3442,5 @@
              encoding);
         }
-      fflush (stdout);
+      //fflush (stdout);
     }
 
@@ -3583,5 +3604,5 @@
                       return errorException (*m,
                                              _("Unable to allocate memory."),
-                                             "InternalError", NULL);
+                                             "InternalError", NULL,NULL);
                     }
                   size_t dwRead;
@@ -3603,5 +3624,5 @@
                       return errorException (*m,
                                              _("Unable to allocate memory."),
-                                             "InternalError", NULL);
+                                             "InternalError", NULL,NULL);
                     }
                   memcpy (tmpMap->value, fcontent,
@@ -3667,5 +3688,5 @@
     {
       return errorException (*m, _("Unable to download the file."),
-                             "InternalError", NULL);
+                             "InternalError", NULL,NULL);
     }
   if (mimeType != NULL)
@@ -3680,5 +3701,5 @@
   if (tmpMap->value == NULL)
     return errorException (*m, _("Unable to allocate memory."),
-                           "InternalError", NULL);
+                           "InternalError", NULL,NULL);
   memcpy (tmpMap->value, fcontent, (fsize + 1) * sizeof (char));
 
@@ -3698,5 +3719,5 @@
 int
 errorException (maps * m, const char *message, const char *errorcode,
-                const char *locator)
+                const char *locator,FCGX_Stream * out)
 {
   map *errormap = createMap ("text", message);
@@ -3706,5 +3727,5 @@
   else
     addToMap (errormap, "locator", "NULL");
-  printExceptionReportResponse (m, errormap);
+  printExceptionReportResponse (m, errormap,out);
   freeMap (&errormap);
   free (errormap);
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal.h
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal.h	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal.h	(revision 549)
@@ -39,4 +39,5 @@
 #include <sys/types.h>
 //#include "cgic.h"
+#include <fcgiapp.h>
 #ifndef WIN32
 #include <sys/ipc.h>
@@ -119,8 +120,8 @@
   void zooXmlCleanupDocs ();
   void addPrefix (maps * conf, map * level, service * serv);
-  void printExceptionReportResponse (maps *, map *);
+  void printExceptionReportResponse (maps *, map *,FCGX_Stream * out);
   xmlNodePtr createExceptionReportNode (maps *, map *, int);
   void printProcessResponse (maps *, map *, int, service *, const char *, int,
-                             maps *, maps *);
+                             maps *, maps *,FCGX_Stream *);
   xmlNodePtr printGetCapabilitiesHeader (xmlDocPtr, const char *, maps *);
   void printGetCapabilitiesForProcess (maps *, xmlNodePtr, service *);
@@ -129,5 +130,5 @@
   void printFullDescription (int, elements *, const char *, xmlNsPtr,
                              xmlNodePtr);
-  void printDocument (maps *, xmlDocPtr, int);
+  void printDocument (maps *, xmlDocPtr, int, FCGX_Stream *);
   void printDescription (xmlNodePtr, xmlNsPtr, const char *, map *);
   void printIOType (xmlDocPtr, xmlNodePtr, xmlNsPtr, xmlNsPtr, xmlNsPtr,
@@ -139,5 +140,5 @@
                                 elements *, maps *, const char *);
 
-  void outputResponse (service *, maps *, maps *, map *, int, maps *, int);
+  void outputResponse (service *, maps *, maps *, map *, int, maps *, int,FCGX_Stream *,FCGX_Stream *);
 
   char *base64 (const char *, int);
@@ -147,5 +148,5 @@
   char *addDefaultValues (maps **, elements *, maps *, int);
 
-  int errorException (maps *, const char *, const char *, const char *);
+  int errorException (maps *, const char *, const char *, const char *,FCGX_Stream *);
 
   int checkForSoapEnvelope (xmlDocPtr);
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_js.c
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_js.c	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_js.c	(revision 549)
@@ -216,5 +216,5 @@
       map *err = createMap ("text", tmp1);
       addToMap (err, "code", "NoApplicableCode");
-      printExceptionReportResponse (mc, err);
+      //printExceptionReportResponse (mc, err);
       freeMap (&err);
       free (err);
@@ -268,5 +268,5 @@
       map *err = createMap ("text", tmp1);
       addToMap (err, "code", "NoApplicableCode");
-      printExceptionReportResponse (*main_conf, err);
+      //printExceptionReportResponse (*main_conf, err);
       freeMap (&err);
       free (err);
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_python.c
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_python.c	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_python.c	(revision 549)
@@ -127,5 +127,5 @@
 int
 zoo_python_support (maps ** main_conf, map * request, service * s,
-                    maps ** real_inputs, maps ** real_outputs)
+                    maps ** real_inputs, maps ** real_outputs,FCGX_Stream * err)
 {
   char *pythonpath;
@@ -222,5 +222,5 @@
                             "Unable to parse serviceProvider please check your zcfg file.");
       addToMap (err, "code", "NoApplicableCode");
-      printExceptionReportResponse (m, err);
+      //printExceptionReportResponse (m, err);
       exit (-1);
     }
@@ -299,5 +299,5 @@
               map *err = createMap ("text", pbt);
               addToMap (err, "code", "NoApplicableCode");
-              printExceptionReportResponse (m, err);
+              //printExceptionReportResponse (m, err);
               res = -1;
             }
@@ -309,5 +309,5 @@
                    s->name, tmp->value);
           map *tmps = createMap ("text", tmpS);
-          printExceptionReportResponse (m, tmps);
+          //printExceptionReportResponse (m, tmps);
           res = -1;
         }
@@ -318,5 +318,5 @@
       sprintf (tmpS, "Python module %s cannot be loaded.\n", tmp->value);
       map *tmps = createMap ("text", tmpS);
-      printExceptionReportResponse (m, tmps);
+      //printExceptionReportResponse (m, tmps);
       if (PyErr_Occurred ())
         PyErr_Print ();
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_python.h
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_python.h	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_python.h	(revision 549)
@@ -43,5 +43,5 @@
 map* mapFromPyDict(PyDictObject* t);
 
-int zoo_python_support(maps**,map*,service*,maps**,maps**);
+int zoo_python_support(maps**,map*,service*,maps**,maps**,FCGX_Stream *);
 
 PyObject* PythonTranslate(PyObject*, PyObject*);
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_loader.c
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_loader.c	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_loader.c	(revision 549)
@@ -158,5 +158,5 @@
     else{
       map* tmps=createMap("text","ERROR you provided more inputs than requested.");
-      printExceptionReportResponse(m,tmps);
+      printExceptionReportResponse(m,tmps,out);
       //printf("ERROR you provided more inputs than requested.");
       return -1;
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_zcfg.c
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_zcfg.c	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_zcfg.c	(revision 549)
@@ -6,4 +6,5 @@
 #include "service.h"
 #include "service_internal.h"
+extern "C" {
 #include <libxml/tree.h>
 #include <libxml/xmlmemory.h>
@@ -11,5 +12,7 @@
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
-
+#include "service.h"
+#include "service_internal.h"
+}
 
 
@@ -18,4 +21,21 @@
 
 static GList *serviceCfgList;
+
+//static maps * main_conf = NULL;
+
+
+/*int
+load_main_conf(char * path){
+    if (main_conf != NULL)
+        freeMaps(&main_conf);
+    main_conf = (maps *) malloc (MAP_SIZE);
+    return conf_read (path, main_conf);
+}
+
+maps *  get_main_conf(){
+    return  dupMaps(&main_conf);
+}
+
+*/
 
 static int
@@ -118,5 +138,5 @@
       serviceCfgList = g_list_append (serviceCfgList, s1);
     }
-  freeMap(&m);
+  freeMaps(&m);
 }
 
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_zcfg.h
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_zcfg.h	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_zcfg.h	(revision 549)
@@ -4,6 +4,8 @@
 
 #include "service.h"
+#include "service_internal.h"
 
-
+maps *  get_main_conf();
+int load_main_conf(char *path);
 void init_services_conf (char *rootDir);
 service *search_service (char *identifier);
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/ulinet.h
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/ulinet.h	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/ulinet.h	(revision 549)
@@ -26,5 +26,5 @@
 #define _ULINET_H
 
-#include "fcgi_stdio.h"
+//#include "fcgi_stdio.h"
 #include "service.h"
 #include <stdlib.h>
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c	(revision 548)
+++ /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;
-
 }
+ 
Index: /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 548)
+++ /branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 549)
@@ -37,13 +37,13 @@
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
-#include "service_zcfg.h"
 #include <libgen.h>
 }
 #include "ulinet.h"
+#include "service_zcfg.h"
 #include <libintl.h>
 #include <locale.h>
 #include <string.h>
-#include "service.h"
-#include "service_internal.h"
+//#include "service.h"
+//#include "service_internal.h"
 #ifdef USE_PYTHON
 #include "service_internal_python.h"
@@ -172,5 +172,5 @@
                        ("You set maximum occurences for <%s> as %i but you tried to use it more than the limit you set. Please correct your ZCFG file or your request."),
                        mi->name, atoi (testMap->value));
-              errorException (m, emsg, "InternalError", NULL);
+              //errorException (m, emsg, "InternalError", NULL,stderr);
               return -1;
             }
@@ -193,5 +193,5 @@
                            ("ZOO-Kernel was unable to load your data for %s position %s."),
                            mi->name, tmpMap->value);
-                  errorException (m, emsg, "InternalError", NULL);
+                  //errorException (m, emsg, "InternalError", NULL,stderr);
                   return -1;
                 }
@@ -204,5 +204,5 @@
                        ("You set maximum occurences for <%s> to one but you tried to use it more than once. Please correct your ZCFG file or your request."),
                        mi->name);
-              errorException (m, emsg, "InternalError", NULL);
+              //errorException (m, emsg, "InternalError", NULL,request->out);
               return -1;
             }
@@ -390,5 +390,5 @@
            ("ZOO Kernel failed to process your request receiving signal %d = %s"),
            sig, ssig);
-  errorException (NULL, tmp, "InternalError", NULL);
+  //errorException (NULL, tmp, "InternalError", NULL,request->out);
 #ifdef DEBUG
   fprintf (stderr, "Not this time!\n");
@@ -399,7 +399,9 @@
 void
 loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
-                   maps ** inputs, maps ** ioutputs, int *eres)
+                   maps ** inputs, maps ** ioutputs, int *eres,FCGX_Stream *out, FCGX_Stream *err)
 {
-  char tmps1[1024];
+ 
+
+ char tmps1[1024];
   char ntmp[1024];
   maps *m = *myMap;
@@ -523,4 +525,5 @@
                 (execute_t) GetProcAddress (so, s1->name);
 #else
+
               execute_t execute = (execute_t) dlsym (so, s1->name);
 #endif
@@ -539,13 +542,15 @@
                            ("Error occured while running the %s function: %s"),
                            s1->name, errstr);
-                  errorException (m, tmpMsg, "InternalError", NULL);
+                  errorException (m, tmpMsg, "InternalError", NULL,out);
                   free (tmpMsg);
 #ifdef DEBUG
                   fprintf (stderr, "Function %s error %s\n",
                            s1->name, errstr);
-#endif
+#endif              
+
                   *eres = -1;
                   return;
                 }
+
 #ifdef DEBUG
 #ifdef WIN32
@@ -568,4 +573,7 @@
               fflush (stderr);
 #endif
+
+
+
             }
 #ifdef WIN32
@@ -589,5 +597,5 @@
           sprintf (tmps, _("C Library can't be loaded %s"), errstr);
           map *tmps1 = createMap ("text", tmps);
-          printExceptionReportResponse (m, tmps1);
+          printExceptionReportResponse (m, tmps1,out);
           *eres = -1;
           freeMap (&tmps1);
@@ -602,5 +610,6 @@
         zoo_python_support (&m, request_inputs, s1,
                             &request_input_real_format,
-                            &request_output_real_format);
+                            &request_output_real_format,err);
+
     }
   else
@@ -670,5 +679,5 @@
                r_inputs->value);
       map *tmps = createMap ("text", tmpv);
-      printExceptionReportResponse (m, tmps);
+      printExceptionReportResponse (m, tmps,out);
       *eres = -1;
     }
@@ -794,7 +803,9 @@
 
 int
-runRequest (map ** inputs)
+runRequest (map ** inputs,struct cgi_env ** c,FCGX_Request *request)
 {
 
+
+struct cgi_env *cgi = *c;
 #ifndef USE_GDB
   (void) signal (SIGSEGV, sig_handler);
@@ -813,10 +824,12 @@
    * Parsing service specfic configuration file
    */
+  
   m = (maps *) malloc (MAPS_SIZE);
   if (m == NULL)
     {
       return errorException (m, _("Unable to allocate memory."),
-                             "InternalError", NULL);
-    }
+                             "InternalError", NULL,request->out);
+    }
+   
   char ntmp[1024];
 #ifndef WIN32
@@ -827,14 +840,18 @@
   r_inputs = getMapOrFill (&request_inputs, "metapath", "");
 
-
-  char conf_file[10240];
-  snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value);
+  //m = get_main_conf();
+
+ char conf_file[10240];
+ snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value);
+  
   if (conf_read (conf_file, m) == 2)
     {
       errorException (NULL, _("Unable to load the main.cfg file."),
-                      "InternalError", NULL);
+                      "InternalError", NULL,request->out);
       free (m);
       return 1;
     }
+
+
 #ifdef DEBUG
   fprintf (stderr, "***** BEGIN MAPS\n");
@@ -844,4 +861,8 @@
 
   map *getPath = getMapFromMaps (m, "main", "gettextPath");
+
+
+
+
   if (getPath != NULL)
     {
@@ -854,5 +875,4 @@
       bindtextdomain ("zoo-services", "/usr/share/locale/");
     }
-
 
   /**
@@ -878,5 +898,5 @@
                    ("The value %s is not supported for the <language> parameter"),
                    r_inputs->value);
-          errorException (m, tmp, "InvalidParameterValue", "language");
+          errorException (m, tmp, "InvalidParameterValue", "language",request->out);
           freeMaps (&m);
           free (m);
@@ -929,15 +949,15 @@
     setMapInMaps (m, "main", "isSoap", "false");
 
-  if (strlen (cgiServerName) > 0)
+  if (strlen (cgi->cgiServerName) > 0)
     {
       char tmpUrl[1024];
-      if (strncmp (cgiServerPort, "80", 2) == 0)
-        {
-          sprintf (tmpUrl, "http://%s%s", cgiServerName, cgiScriptName);
+      if (strncmp (cgi->cgiServerPort, "80", 2) == 0)
+        {
+          sprintf (tmpUrl, "http://%s%s", cgi->cgiServerName, cgi->cgiScriptName);
         }
       else
         {
-          sprintf (tmpUrl, "http://%s:%s%s", cgiServerName,
-                   cgiServerPort, cgiScriptName);
+          sprintf (tmpUrl, "http://%s:%s%s", cgi->cgiServerName,
+                   cgi->cgiServerPort, cgi->cgiScriptName);
         }
 #ifdef DEBUG
@@ -954,5 +974,5 @@
     {
       errorException (m, _("Parameter <request> was not specified"),
-                      "MissingParameterValue", "request");
+                      "MissingParameterValue", "request",request->out);
       if (count (request_inputs) == 1)
         {
@@ -974,5 +994,5 @@
                           _
                           ("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."),
-                          "OperationNotSupported", r_inputs->value);
+                          "OperationNotSupported", r_inputs->value,request->out);
           freeMaps (&m);
           free (m);
@@ -986,5 +1006,5 @@
     {
       errorException (m, _("Parameter <service> was not specified"),
-                      "MissingParameterValue", "service");
+                      "MissingParameterValue", "service",request->out);
       freeMaps (&m);
       free (m);
@@ -999,5 +1019,5 @@
                           _
                           ("Unenderstood <service> value, WPS is the only acceptable value."),
-                          "InvalidParameterValue", "service");
+                          "InvalidParameterValue", "service",request->out);
           freeMaps (&m);
           free (m);
@@ -1012,5 +1032,5 @@
         {
           errorException (m, _("Parameter <version> was not specified"),
-                          "MissingParameterValue", "version");
+                          "MissingParameterValue", "version",request->out);
           freeMaps (&m);
           free (m);
@@ -1025,5 +1045,5 @@
                               _
                               ("Unenderstood <version> value, 1.0.0 is the only acceptable value."),
-                              "InvalidParameterValue", "service");
+                              "InvalidParameterValue", "service",request->out);
               freeMaps (&m);
               free (m);
@@ -1043,5 +1063,5 @@
                               _
                               ("Unenderstood <AcceptVersions> value, 1.0.0 is the only acceptable value."),
-                              "VersionNegotiationFailed", NULL);
+                              "VersionNegotiationFailed", NULL,request->out);
               freeMaps (&m);
               free (m);
@@ -1080,4 +1100,7 @@
   else
     snprintf (conf_dir, 1024, "%s", ntmp);
+
+
+
 
   if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0)
@@ -1098,28 +1121,10 @@
      * has been found in the zcfg and then printed on stdout
      */
-      int saved_stdout = dup (fileno (stdout));
-      dup2 (fileno (stderr), fileno (stdout));
-
-        /**
-      if (int res =
-          recursReaddirF (m, n, conf_dir, NULL, saved_stdout, 0,
-                          printGetCapabilitiesForProcess) < 0)
-        {
-          freeMaps (&m);
-          free (m);
-          free (REQUEST);
-          free (SERVICE_URL);
-          fflush (stdout);
-          return res;
-        }
-      **/
       XML_CapabilitiesAllProcess (m, n);
-      dup2 (saved_stdout, fileno (stdout));
-      printDocument (m, doc, getpid ());
+      printDocument (m, doc, getpid (),request->out);
       freeMaps (&m);
       free (m);
       free (REQUEST);
       free (SERVICE_URL);
-      fflush (stdout);
       return 0;
     }
@@ -1132,5 +1137,5 @@
           errorException (m,
                           _("Mandatory <identifier> was not specified"),
-                          "MissingParameterValue", "identifier");
+                          "MissingParameterValue", "identifier",request->out);
           freeMaps (&m);
           free (m);
@@ -1146,5 +1151,5 @@
           errorException (m,
                           _("The specified path path doesn't exist."),
-                          "InvalidParameterValue", conf_dir);
+                          "InvalidParameterValue", conf_dir,request->out);
           freeMaps (&m);
           free (m);
@@ -1173,17 +1178,17 @@
 
 
-          int saved_stdout = dup (fileno (stdout));
-          dup2 (fileno (stderr), fileno (stdout));
+          //int saved_stdout = dup (fileno (stdout));
+          //dup2 (fileno (stderr), fileno (stdout));
           XML_Describe_Process (m, n, orig);
           closedir (dirp);
-          fflush (stdout);
-          dup2 (saved_stdout, fileno (stdout));
+          //fflush (stdout);
+          //dup2 (saved_stdout, fileno (stdout));
           free (orig);
-          printDocument (m, doc, getpid ());
+          printDocument (m, doc, getpid (),request->out);
           freeMaps (&m);
           free (m);
           free (REQUEST);
           free (SERVICE_URL);
-          fflush (stdout);
+          //fflush (stdout);
           return 0;
 
@@ -1191,8 +1196,9 @@
       else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0)
         {
+          
           errorException (m,
                           _
                           ("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."),
-                          "InvalidParameterValue", "request");
+                          "InvalidParameterValue", "request",request->out);
 #ifdef DEBUG
           fprintf (stderr, "No request found %s", REQUEST);
@@ -1203,5 +1209,5 @@
           free (REQUEST);
           free (SERVICE_URL);
-          fflush (stdout);
+          //fflush (stdout);
           return 0;
         }
@@ -1250,6 +1256,6 @@
   fflush (stdout);
   **/
-  int saved_stdout = dup (fileno (stdout));
-  dup2 (saved_stdout, fileno (stdout));
+  //int saved_stdout = dup (fileno (stdout));
+  //dup2 (saved_stdout, fileno (stdout));
   s1 = search_service (r_inputs->value);
   if (s1 == NULL)
@@ -1260,5 +1266,5 @@
                ("The value for <identifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."),
                r_inputs->value);
-      errorException (m, tmpMsg, "InvalidParameterValue", "identifier");
+      errorException (m, tmpMsg, "InvalidParameterValue", "identifier",request->out);
       free (tmpMsg);
       //free (s1);
@@ -1269,5 +1275,5 @@
       return 0;
     }
-  close (saved_stdout);
+  //close (saved_stdout);
 
 #ifdef DEBUG
@@ -1339,5 +1345,5 @@
               return errorException (m,
                                      _("Unable to allocate memory"),
-                                     "InternalError", NULL);
+                                     "InternalError", NULL,request->out);
             }
           i = 0;
@@ -1356,5 +1362,5 @@
                                          _
                                          ("Unable to allocate memory"),
-                                         "InternalError", NULL);
+                                         "InternalError", NULL,request->out);
                 }
               snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s",
@@ -1382,5 +1388,5 @@
                                                      _
                                                      ("Unable to allocate memory."),
-                                                     "InternalError", NULL);
+                                                     "InternalError", NULL,request->out);
                             }
                           tmp_output->name = zStrdup (tmpc);
@@ -1447,5 +1453,5 @@
           errorException (m,
                           _("Parameter <DataInputs> was not specified"),
-                          "MissingParameterValue", "DataInputs");
+                          "MissingParameterValue", "DataInputs",request->out);
           freeMaps (&m);
           free (m);
@@ -1479,5 +1485,5 @@
         {
           return errorException (m, _("Unable to allocate memory."),
-                                 "InternalError", NULL);
+                                 "InternalError", NULL,request->out);
         }
       i = 0;
@@ -1498,5 +1504,5 @@
               return errorException (m,
                                      _("Unable to allocate memory."),
-                                     "InternalError", NULL);
+                                     "InternalError", NULL,request->out);
             }
           pToken = strtok (NULL, ";");
@@ -1540,5 +1546,5 @@
                                              _
                                              ("Unable to allocate memory."),
-                                             "InternalError", NULL);
+                                             "InternalError", NULL,request->out);
                     }
                   tmpmaps->name = zStrdup (tmpn);
@@ -1611,5 +1617,5 @@
                                    ("Unable to find a valid protocol to download the remote file %s"),
                                    tmpv1 + 1);
-                          errorException (m, emsg, "InternalError", NULL);
+                          errorException (m, emsg, "InternalError", NULL,request->out);
                           freeMaps (&m);
                           free (m);
@@ -1702,5 +1708,5 @@
       fflush (stderr);
 #endif
-      xmlDocPtr doc = xmlParseMemory (postRequest->value, cgiContentLength);
+      xmlDocPtr doc = xmlParseMemory (postRequest->value, cgi->cgiContentLength);
 #ifdef DEBUG
       fprintf (stderr, "AFTER\n");
@@ -1753,5 +1759,5 @@
                                                      _
                                                      ("Unable to allocate memory."),
-                                                     "InternalError", NULL);
+                                                     "InternalError", NULL,request->out);
                             }
                           tmpmaps->name = zStrdup ((char *) val);
@@ -1782,5 +1788,5 @@
                                                      _
                                                      ("Unable to allocate memory."),
-                                                     "InternalError", NULL);
+                                                     "InternalError", NULL,request->out);
                             }
                           tmpmaps->name = zStrdup ("missingIndentifier");
@@ -1926,5 +1932,5 @@
                                              _
                                              ("Unable to allocate memory."),
-                                             "InternalError", NULL);
+                                             "InternalError", NULL,request->out);
                                         }
                                       snprintf (has,
@@ -1965,6 +1971,6 @@
                                     (char *)
                                     malloc
-                                    (cgiContentLength + 1 * sizeof (char));
-                                  memset (tmp, 0, cgiContentLength);
+                                    (cgi->cgiContentLength + 1 * sizeof (char));
+                                  memset (tmp, 0, cgi->cgiContentLength);
                                   xmlNodePtr cur4 = cur3->children;
                                   while (cur4 != NULL)
@@ -2058,5 +2064,5 @@
                                          _
                                          ("Unable to allocate memory."),
-                                         "InternalError", NULL);
+                                         "InternalError", NULL,request->out);
                                     }
                                   size_t bRead;
@@ -2380,5 +2386,5 @@
                                          _
                                          ("Unable to allocate memory."),
-                                         "InternalError", NULL);
+                                         "InternalError", NULL,request->out);
                 }
               tmpmaps->name = zStrdup ("unknownIdentifier");
@@ -2464,5 +2470,5 @@
                                                _
                                                ("Unable to allocate memory."),
-                                               "InternalError", NULL);
+                                               "InternalError", NULL,request->out);
                       }
                     tmpmaps->name = zStrdup ("unknownIdentifier");
@@ -2520,5 +2526,5 @@
                                                        _
                                                        ("Unable to allocate memory."),
-                                                       "InternalError", NULL);
+                                                       "InternalError", NULL,request->out);
                               }
                             tmpmaps->name = zStrdup ((char *) val);
@@ -2580,5 +2586,5 @@
                                                        _
                                                        ("Unable to allocate memory."),
-                                                       "InternalError", NULL);
+                                                       "InternalError", NULL,request->out);
                               }
                             tmpmaps->name = zStrdup ("missingIndetifier");
@@ -2659,5 +2665,5 @@
                                            _
                                            ("Unable to allocate memory."),
-                                           "InternalError", NULL);
+                                           "InternalError", NULL,request->out);
                                       }
                                     tmpmaps->name = zStrdup ((char *) val);
@@ -2700,5 +2706,5 @@
                                            _
                                            ("Unable to allocate memory."),
-                                           "InternalError", NULL);
+                                           "InternalError", NULL,request->out);
                                       }
                                     tmpmaps->name =
@@ -2788,5 +2794,5 @@
               addToMap (tmpe, "locator", ptr->name);
               addToMap (tmpe, "text", tmps);
-              printExceptionReportResponse (m, tmpe);
+              printExceptionReportResponse (m, tmpe,request->out);
               //freeService (&s1);
               //free (s1);
@@ -2832,5 +2838,5 @@
         }
       addToMap (tmpe, "text", tmps);
-      printExceptionReportResponse (m, tmpe);
+      printExceptionReportResponse (m, tmpe,request->out);
       //freeService (&s1);
       free (s1);
@@ -2855,5 +2861,5 @@
       if (getMap (tmpReqI->content, "isFile") != NULL)
         {
-          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
+          if (cgiFormFileName (tmpReqI->name, name, sizeof (name),&cgi) ==
               cgiFormSuccess)
             {
@@ -2869,8 +2875,8 @@
               int got, t;
               map *path = getMapFromMaps (m, "main", "tmpPath");
-              cgiFormFileSize (tmpReqI->name, &size);
+              cgiFormFileSize (tmpReqI->name, &size,&cgi);
               cgiFormFileContentType (tmpReqI->name, contentType,
-                                      sizeof (contentType));
-              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
+                                      sizeof (contentType),&cgi);
+              if (cgiFormFileOpen (tmpReqI->name, &file,&cgi) == cgiFormSuccess)
                 {
                   t = -1;
@@ -3018,5 +3024,5 @@
                       _
                       ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."),
-                      "InvalidParameterValue", "storeExecuteResponse");
+                      "InvalidParameterValue", "storeExecuteResponse",request->out);
       //freeService (&s1);
       //free (s1);
@@ -3069,8 +3075,8 @@
   else
     addToMap (_tmpMaps->content, "soap", "false");
-  if (cgiCookie != NULL && strlen (cgiCookie) > 0)
+  if (cgi->cgiCookie != NULL && strlen (cgi->cgiCookie) > 0)
     {
       int hasValidCookie = -1;
-      char *tcook = zStrdup (cgiCookie);
+      char *tcook = zStrdup (cgi->cgiCookie);
       char *tmp = NULL;
       map *testing = getMapFromMaps (m, "main", "cookiePrefix");
@@ -3085,8 +3091,8 @@
           sprintf (tmp, "%s=", testing->value);
         }
-      if (strstr (cgiCookie, ";") != NULL)
+      if (strstr (cgi->cgiCookie, ";") != NULL)
         {
           char *token, *saveptr;
-          token = strtok_r (cgiCookie, ";", &saveptr);
+          token = strtok_r (cgi->cgiCookie, ";", &saveptr);
           while (token != NULL)
             {
@@ -3104,7 +3110,7 @@
         {
           if (strstr
-              (cgiCookie, "=") != NULL && strcasestr (cgiCookie, tmp) != NULL)
+              (cgi->cgiCookie, "=") != NULL && strcasestr (cgi->cgiCookie, tmp) != NULL)
             {
-              tcook = zStrdup (cgiCookie);
+              tcook = zStrdup (cgi->cgiCookie);
               hasValidCookie = 1;
             }
@@ -3131,5 +3137,5 @@
             sprintf
               (session_file_path,
-               "%s/sess_%s.cfg", tmpPath->value, strstr (cgiCookie, "=") + 1);
+               "%s/sess_%s.cfg", tmpPath->value, strstr (cgi->cgiCookie, "=") + 1);
           free (tcook);
           maps *tmpSess = (maps *) malloc (MAPS_SIZE);
@@ -3137,4 +3143,5 @@
           int istat = stat (session_file_path,
                             &file_status);
+          
           if (istat == 0 && file_status.st_size > 0)
             {
@@ -3144,4 +3151,5 @@
               free (tmpSess);
             }
+            
         }
     }
@@ -3166,5 +3174,7 @@
     }
 #endif
-  char *fbkp, *fbkp1;
+  
+
+char *fbkp, *fbkp1;
   FILE *f0, *f1;
   if (status != NULL)
@@ -3176,5 +3186,5 @@
         (&m, s1,
          request_inputs,
-         &request_input_real_format, &request_output_real_format, &eres);
+         &request_input_real_format, &request_output_real_format, &eres,request->out,request->err);
     }
   else
@@ -3261,5 +3271,5 @@
              r_inputs1->value,
              SERVICE_STARTED,
-             request_input_real_format, request_output_real_format);
+             request_input_real_format, request_output_real_format,request->out);
 #ifndef WIN32
           fflush (stdout);
@@ -3277,5 +3287,5 @@
             (&m, s1,
              request_inputs,
-             &request_input_real_format, &request_output_real_format, &eres);
+             &request_input_real_format, &request_output_real_format, &eres,request->out,request->err);
         }
       else
@@ -3289,5 +3299,5 @@
                           _
                           ("Unable to run the child process properly"),
-                          "InternalError", NULL);
+                          "InternalError", NULL,request->out);
         }
     }
@@ -3296,10 +3306,11 @@
   dumpMaps (request_output_real_format);
 #endif
+
   if (eres != -1)
     outputResponse (s1,
                     request_input_real_format,
                     request_output_real_format,
-                    request_inputs, cpid, m, eres);
-  fflush (stdout);
+                    request_inputs, cpid, m, eres,request->out,request->err);
+  //fflush (stdout);
   /**
    * Ensure that if error occurs when freeing memory, no signal will return
@@ -3315,5 +3326,5 @@
   (void) signal (SIGABRT, donothing);
 #endif
-  if (((int) getpid ()) != cpid || cgiSid != NULL)
+  if (((int) getpid ()) != cpid || cgi->cgiSid != NULL)
     {
       fclose (stdout);
