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 532)
+++ 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);
