source: trunk/zoo-project/zoo-kernel/zoo_service_loader.c @ 589

Last change on this file since 589 was 587, checked in by knut, 10 years ago

Implemented asynchronous HTTP POST Execute requests on Windows platform (via caching). Fixed bug that caused only the last output variable in a HTTP POST Execute request to be returned. Modified the addToCache function so that the path of the cached file can be retrieved. Changed the parsing of KVPs in zoo_loader.c so that keys with missing values (e.g. "metapath=") are assigned the empty string instead of NULL (avoids segmentation fault in some situations). Added conditional definition of ZEND_DEBUG in service_internal_php.c. Deallocation of memory in function createProcess. In zoo_loader.c, applied url_decode to CGI form string (WIN32 only, but should check if this should apply to other platforms as well).

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 121.2 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 *  Copyright 2008-2013 GeoLabs SARL. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25
26
27extern "C" int yylex ();
28extern "C" int crlex ();
29
30#ifdef USE_OTB
31#include "service_internal_otb.h"
32#else
33#define length(x) (sizeof(x) / sizeof(x[0]))
34#endif
35
36#include "cgic.h"
37
38extern "C"
39{
40#include <libxml/tree.h>
41#include <libxml/xmlmemory.h>
42#include <libxml/parser.h>
43#include <libxml/xpath.h>
44#include <libxml/xpathInternals.h>
45}
46
47#include "ulinet.h"
48
49#include <libintl.h>
50#include <locale.h>
51#include <string.h>
52
53#include "service.h"
54
55#include "service_internal.h"
56
57#ifdef USE_PYTHON
58#include "service_internal_python.h"
59#endif
60
61#ifdef USE_JAVA
62#include "service_internal_java.h"
63#endif
64
65#ifdef USE_PHP
66#include "service_internal_php.h"
67#endif
68
69#ifdef USE_JS
70#include "service_internal_js.h"
71#endif
72
73#ifdef USE_RUBY
74#include "service_internal_ruby.h"
75#endif
76
77#ifdef USE_PERL
78#include "service_internal_perl.h"
79#endif
80
81#include <dirent.h>
82#include <signal.h>
83#include <unistd.h>
84#ifndef WIN32
85#include <dlfcn.h>
86#include <libgen.h>
87#else
88#include <windows.h>
89#include <direct.h>
90#include <sys/types.h>
91#include <sys/stat.h>
92#include <unistd.h>
93#define pid_t int;
94#endif
95#include <fcntl.h>
96#include <time.h>
97#include <stdarg.h>
98
99#ifdef WIN32
100extern "C"
101{
102  __declspec (dllexport) char *strcasestr (char const *a, char const *b)
103#ifndef USE_MS
104  {
105    char *x = zStrdup (a);
106    char *y = zStrdup (b);
107
108      x = _strlwr (x);
109      y = _strlwr (y);
110    char *pos = strstr (x, y);
111    char *ret = pos == NULL ? NULL : (char *) (a + (pos - x));
112      free (x);
113      free (y);
114      return ret;
115  };
116#else
117   ;
118#endif
119}
120#endif
121
122#define _(String) dgettext ("zoo-kernel",String)
123#define __(String) dgettext ("zoo-service",String)
124
125#ifdef WIN32
126  #ifndef PROGRAMNAME
127    #define PROGRAMNAME "zoo_loader.cgi"
128  #endif
129#endif
130
131extern int getServiceFromFile (maps *, const char *, service **);
132
133int
134readServiceFile (maps * conf, char *file, service ** service, char *name)
135{
136  int t = getServiceFromFile (conf, file, service);
137#ifdef YAML
138  if (t < 0)
139    {
140      t = getServiceFromYAML (conf, file, service, name);
141    }
142#endif
143  return t;
144}
145
146void
147translateChar (char *str, char toReplace, char toReplaceBy)
148{
149  int i = 0, len = strlen (str);
150  for (i = 0; i < len; i++)
151    {
152      if (str[i] == toReplace)
153        str[i] = toReplaceBy;
154    }
155}
156
157/**
158 * Create (or append to) an array valued maps
159 * value = "["",""]"
160 */
161int
162appendMapsToMaps (maps * m, maps * mo, maps * mi, elements * elem)
163{
164  maps *tmpMaps = getMaps (mo, mi->name);
165  map *tmap = getMapType (tmpMaps->content);
166  elements *el = getElements (elem, mi->name);
167  int hasEl = 1;
168  if (el == NULL)
169    hasEl = -1;
170  if (tmap == NULL)
171    {
172      if (hasEl > 0)
173        tmap = getMapType (el->defaults->content);
174    }
175
176  map *testMap = NULL;
177  if (hasEl > 0)
178    {
179      testMap = getMap (el->content, "maxOccurs");
180    }
181  else
182    {
183      testMap = createMap ("maxOccurs", "unbounded");
184    }
185
186  if (testMap != NULL)
187    {
188      if (strncasecmp (testMap->value, "unbounded", 9) != 0
189          && atoi (testMap->value) > 1)
190        {
191          addMapsArrayToMaps (&mo, mi, tmap->name);
192          map* nb=getMapFromMaps(mo,mi->name,"length");
193          if (nb!=NULL && atoi(nb->value)>atoi(testMap->value))
194            {
195              char emsg[1024];
196              sprintf (emsg,
197                       _("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."),
198                       mi->name, atoi (testMap->value));
199              errorException (m, emsg, "InternalError", NULL);
200              return -1;
201            }
202        }
203      else
204        {
205          if (strncasecmp (testMap->value, "unbounded", 9) == 0)
206            {
207              if (hasEl < 0)
208                {
209                  freeMap (&testMap);
210                  free (testMap);
211                }
212              if (addMapsArrayToMaps (&mo, mi, tmap->name) < 0)
213                {
214                  char emsg[1024];
215                  map *tmpMap = getMap (mi->content, "length");
216                  sprintf (emsg,
217                           _
218                           ("ZOO-Kernel was unable to load your data for %s position %s."),
219                           mi->name, tmpMap->value);
220                  errorException (m, emsg, "InternalError", NULL);
221                  return -1;
222                }
223            }
224          else
225            {
226              char emsg[1024];
227              sprintf (emsg,
228                       _
229                       ("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."),
230                       mi->name);
231              errorException (m, emsg, "InternalError", NULL);
232              return -1;
233            }
234        }
235    }
236  return 0;
237}
238
239int
240recursReaddirF (maps * m, xmlNodePtr n, char *conf_dir, char *prefix,
241                int saved_stdout, int level, void (func) (maps *, xmlNodePtr,
242                                                          service *))
243{
244  struct dirent *dp;
245  int scount = 0;
246
247  if (conf_dir == NULL)
248    return 1;
249  DIR *dirp = opendir (conf_dir);
250  if (dirp == NULL)
251    {
252      if (level > 0)
253        return 1;
254      else
255        return -1;
256    }
257  char tmp1[25];
258  sprintf (tmp1, "sprefix_%d", level);
259  char levels[17];
260  sprintf (levels, "%d", level);
261  setMapInMaps (m, "lenv", "level", levels);
262  while ((dp = readdir (dirp)) != NULL)
263    if ((dp->d_type == DT_DIR || dp->d_type == DT_LNK) && dp->d_name[0] != '.'
264        && strstr (dp->d_name, ".") == NULL)
265      {
266
267        char *tmp =
268          (char *) malloc ((strlen (conf_dir) + strlen (dp->d_name) + 2) *
269                           sizeof (char));
270        sprintf (tmp, "%s/%s", conf_dir, dp->d_name);
271
272        if (prefix != NULL)
273          {
274            prefix = NULL;
275          }
276        prefix = (char *) malloc ((strlen (dp->d_name) + 2) * sizeof (char));
277        sprintf (prefix, "%s.", dp->d_name);
278
279        //map* tmpMap=getMapFromMaps(m,"lenv",tmp1);
280
281        int res;
282        if (prefix != NULL)
283          {
284            setMapInMaps (m, "lenv", tmp1, prefix);
285            char levels1[17];
286            sprintf (levels1, "%d", level + 1);
287            setMapInMaps (m, "lenv", "level", levels1);
288            res =
289              recursReaddirF (m, n, tmp, prefix, saved_stdout, level + 1,
290                              func);
291            sprintf (levels1, "%d", level);
292            setMapInMaps (m, "lenv", "level", levels1);
293            free (prefix);
294            prefix = NULL;
295          }
296        else
297          res = -1;
298        free (tmp);
299        if (res < 0)
300          {
301            return res;
302          }
303      }
304    else
305      {
306        char* extn = strstr(dp->d_name, ".zcfg");
307        if(dp->d_name[0] != '.' && extn != NULL && strlen(extn) == 5)
308          {
309            int t;
310            char tmps1[1024];
311            memset (tmps1, 0, 1024);
312            snprintf (tmps1, 1024, "%s/%s", conf_dir, dp->d_name);
313            service *s1 = (service *) malloc (SERVICE_SIZE);
314            if (s1 == NULL)
315              {
316                dup2 (saved_stdout, fileno (stdout));
317                errorException (m, _("Unable to allocate memory."),
318                                "InternalError", NULL);
319                return -1;
320              }
321#ifdef DEBUG
322            fprintf (stderr, "#################\n%s\n#################\n",
323                     tmps1);
324#endif
325            char *tmpsn = zStrdup (dp->d_name);
326            tmpsn[strlen (tmpsn) - 5] = 0;
327            t = readServiceFile (m, tmps1, &s1, tmpsn);
328            free (tmpsn);
329            if (t < 0)
330              {
331                map *tmp00 = getMapFromMaps (m, "lenv", "message");
332                char tmp01[1024];
333                if (tmp00 != NULL)
334                  sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"),
335                           dp->d_name, tmp00->value);
336                else
337                  sprintf (tmp01, _("Unable to parse the ZCFG file: %s."),
338                           dp->d_name);
339                dup2 (saved_stdout, fileno (stdout));
340                errorException (m, tmp01, "InternalError", NULL);
341                return -1;
342              }
343#ifdef DEBUG
344            dumpService (s1);
345            fflush (stdout);
346            fflush (stderr);
347#endif
348            func (m, n, s1);
349            freeService (&s1);
350            free (s1);
351            scount++;
352          }
353      }
354  (void) closedir (dirp);
355  return 1;
356}
357
358xmlXPathObjectPtr
359extractFromDoc (xmlDocPtr doc, const char *search)
360{
361  xmlXPathContextPtr xpathCtx;
362  xmlXPathObjectPtr xpathObj;
363  xpathCtx = xmlXPathNewContext (doc);
364  xpathObj = xmlXPathEvalExpression (BAD_CAST search, xpathCtx);
365  xmlXPathFreeContext (xpathCtx);
366  return xpathObj;
367}
368
369void
370donothing (int sig)
371{
372#ifdef DEBUG
373  fprintf (stderr, "Signal %d after the ZOO-Kernel returned result !\n", sig);
374#endif
375  exit (0);
376}
377
378void
379sig_handler (int sig)
380{
381  char tmp[100];
382  const char *ssig;
383  switch (sig)
384    {
385    case SIGSEGV:
386      ssig = "SIGSEGV";
387      break;
388    case SIGTERM:
389      ssig = "SIGTERM";
390      break;
391    case SIGINT:
392      ssig = "SIGINT";
393      break;
394    case SIGILL:
395      ssig = "SIGILL";
396      break;
397    case SIGFPE:
398      ssig = "SIGFPE";
399      break;
400    case SIGABRT:
401      ssig = "SIGABRT";
402      break;
403    default:
404      ssig = "UNKNOWN";
405      break;
406    }
407  sprintf (tmp,
408           _
409           ("ZOO Kernel failed to process your request receiving signal %d = %s"),
410           sig, ssig);
411  errorException (NULL, tmp, "InternalError", NULL);
412#ifdef DEBUG
413  fprintf (stderr, "Not this time!\n");
414#endif
415  exit (0);
416}
417
418void
419loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
420                   maps ** inputs, maps ** ioutputs, int *eres)
421{
422  char tmps1[1024];
423  char ntmp[1024];
424  maps *m = *myMap;
425  maps *request_output_real_format = *ioutputs;
426  maps *request_input_real_format = *inputs;
427  /**
428   * Extract serviceType to know what kind of service should be loaded
429   */
430  map *r_inputs = NULL;
431#ifndef WIN32
432  getcwd (ntmp, 1024);
433#else
434  _getcwd (ntmp, 1024);
435#endif
436  r_inputs = getMap (s1->content, "serviceType");
437#ifdef DEBUG
438  fprintf (stderr, "LOAD A %s SERVICE PROVIDER \n", r_inputs->value);
439  fflush (stderr);
440#endif
441  if (strlen (r_inputs->value) == 1
442      && strncasecmp (r_inputs->value, "C", 1) == 0)
443    {
444      r_inputs = getMap (request_inputs, "metapath");
445      if (r_inputs != NULL)
446        sprintf (tmps1, "%s/%s", ntmp, r_inputs->value);
447      else
448        sprintf (tmps1, "%s/", ntmp);
449      char *altPath = zStrdup (tmps1);
450      r_inputs = getMap (s1->content, "ServiceProvider");
451      sprintf (tmps1, "%s/%s", altPath, r_inputs->value);
452      free (altPath);
453#ifdef DEBUG
454      fprintf (stderr, "Trying to load %s\n", tmps1);
455#endif
456#ifdef WIN32
457      HINSTANCE so =
458        LoadLibraryEx (tmps1, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
459#else
460      void *so = dlopen (tmps1, RTLD_LAZY);
461#endif
462#ifdef WIN32
463      char* errstr = getLastErrorMessage();
464#else
465      char *errstr;
466      errstr = dlerror ();
467#endif
468#ifdef DEBUG
469          fprintf (stderr, "%s loaded (%s) \n", tmps1, errstr);
470#endif
471      if (so != NULL)
472        {
473#ifdef DEBUG
474          fprintf (stderr, "Library loaded %s \n", errstr);
475          fprintf (stderr, "Service Shared Object = %s\n", r_inputs->value);
476#endif
477          r_inputs = getMap (s1->content, "serviceType");
478#ifdef DEBUG
479          dumpMap (r_inputs);
480          fprintf (stderr, "%s\n", r_inputs->value);
481          fflush (stderr);
482#endif
483          if (strncasecmp (r_inputs->value, "C-FORTRAN", 9) == 0)
484            {
485              r_inputs = getMap (request_inputs, "Identifier");
486              char fname[1024];
487              sprintf (fname, "%s_", r_inputs->value);
488#ifdef DEBUG
489              fprintf (stderr, "Try to load function %s\n", fname);
490#endif
491#ifdef WIN32
492              typedef int (CALLBACK * execute_t) (char ***, char ***,
493                                                  char ***);
494              execute_t execute = (execute_t) GetProcAddress (so, fname);
495#else
496              typedef int (*execute_t) (char ***, char ***, char ***);
497              execute_t execute = (execute_t) dlsym (so, fname);
498#endif
499#ifdef DEBUG
500#ifdef WIN32
501                          errstr = getLastErrorMessage();
502#else
503              errstr = dlerror ();
504#endif
505              fprintf (stderr, "Function loaded %s\n", errstr);
506#endif
507
508              char main_conf[10][30][1024];
509              char inputs[10][30][1024];
510              char outputs[10][30][1024];
511              for (int i = 0; i < 10; i++)
512                {
513                  for (int j = 0; j < 30; j++)
514                    {
515                      memset (main_conf[i][j], 0, 1024);
516                      memset (inputs[i][j], 0, 1024);
517                      memset (outputs[i][j], 0, 1024);
518                    }
519                }
520              mapsToCharXXX (m, (char ***) main_conf);
521              mapsToCharXXX (request_input_real_format, (char ***) inputs);
522              mapsToCharXXX (request_output_real_format, (char ***) outputs);
523              *eres =
524                execute ((char ***) &main_conf[0], (char ***) &inputs[0],
525                         (char ***) &outputs[0]);
526#ifdef DEBUG
527              fprintf (stderr, "Function run successfully \n");
528#endif
529              charxxxToMaps ((char ***) &outputs[0],
530                             &request_output_real_format);
531            }
532          else
533            {
534#ifdef DEBUG
535#ifdef WIN32
536                          errstr = getLastErrorMessage();
537              fprintf (stderr, "Function %s failed to load because of %s\n",
538                       r_inputs->value, errstr);
539#endif
540#endif
541              r_inputs = getMapFromMaps (m, "lenv", "Identifier");
542#ifdef DEBUG
543              fprintf (stderr, "Try to load function %s\n", r_inputs->value);
544#endif
545              typedef int (*execute_t) (maps **, maps **, maps **);
546#ifdef WIN32
547              execute_t execute =
548                (execute_t) GetProcAddress (so, r_inputs->value);
549#else
550              execute_t execute = (execute_t) dlsym (so, r_inputs->value);
551#endif
552
553              if (execute == NULL)
554                {
555#ifdef WIN32
556                                  errstr = getLastErrorMessage();
557#else
558                  errstr = dlerror ();
559#endif
560                  char *tmpMsg =
561                    (char *) malloc (2048 + strlen (r_inputs->value));
562                  sprintf (tmpMsg,
563                           _
564                           ("Error occured while running the %s function: %s"),
565                           r_inputs->value, errstr);
566                  errorException (m, tmpMsg, "InternalError", NULL);
567                  free (tmpMsg);
568#ifdef DEBUG
569                  fprintf (stderr, "Function %s error %s\n", r_inputs->value,
570                           errstr);
571#endif
572                  *eres = -1;
573                  return;
574                }
575
576#ifdef DEBUG
577#ifdef WIN32
578                          errstr = getLastErrorMessage();
579#else
580              errstr = dlerror ();
581#endif
582              fprintf (stderr, "Function loaded %s\n", errstr);
583#endif
584
585#ifdef DEBUG
586              fprintf (stderr, "Now run the function \n");
587              fflush (stderr);
588#endif
589              *eres =
590                execute (&m, &request_input_real_format,
591                         &request_output_real_format);
592#ifdef DEBUG
593              fprintf (stderr, "Function loaded and returned %d\n", eres);
594              fflush (stderr);
595#endif
596            }
597#ifdef WIN32
598          *ioutputs = dupMaps (&request_output_real_format);
599          FreeLibrary (so);
600#else
601          dlclose (so);
602#endif
603        }
604      else
605        {
606      /**
607       * Unable to load the specified shared library
608       */
609          char tmps[1024];
610#ifdef WIN32
611                  errstr = getLastErrorMessage();
612#else
613              errstr = dlerror ();
614#endif
615          sprintf (tmps, _("C Library can't be loaded %s"), errstr);
616          errorException(m,tmps,"InternalError",NULL);
617          *eres = -1;
618        }
619    }
620  else
621
622#ifdef USE_OTB
623  if (strncasecmp (r_inputs->value, "OTB", 6) == 0)
624    {
625      *eres =
626        zoo_otb_support (&m, request_inputs, s1,
627                            &request_input_real_format,
628                            &request_output_real_format);
629    }
630  else
631#endif
632
633#ifdef USE_PYTHON
634  if (strncasecmp (r_inputs->value, "PYTHON", 6) == 0)
635    {
636      *eres =
637        zoo_python_support (&m, request_inputs, s1,
638                            &request_input_real_format,
639                            &request_output_real_format);
640    }
641  else
642#endif
643
644#ifdef USE_JAVA
645  if (strncasecmp (r_inputs->value, "JAVA", 4) == 0)
646    {
647      *eres =
648        zoo_java_support (&m, request_inputs, s1, &request_input_real_format,
649                          &request_output_real_format);
650    }
651  else
652#endif
653
654#ifdef USE_PHP
655  if (strncasecmp (r_inputs->value, "PHP", 3) == 0)
656    {
657      *eres =
658        zoo_php_support (&m, request_inputs, s1, &request_input_real_format,
659                         &request_output_real_format);
660    }
661  else
662#endif
663
664
665#ifdef USE_PERL
666  if (strncasecmp (r_inputs->value, "PERL", 4) == 0)
667    {
668      *eres =
669        zoo_perl_support (&m, request_inputs, s1, &request_input_real_format,
670                          &request_output_real_format);
671    }
672  else
673#endif
674
675#ifdef USE_JS
676  if (strncasecmp (r_inputs->value, "JS", 2) == 0)
677    {
678      *eres =
679        zoo_js_support (&m, request_inputs, s1, &request_input_real_format,
680                        &request_output_real_format);
681    }
682  else
683#endif
684
685#ifdef USE_RUBY
686  if (strncasecmp (r_inputs->value, "Ruby", 4) == 0)
687    {
688      *eres =
689        zoo_ruby_support (&m, request_inputs, s1, &request_input_real_format,
690                          &request_output_real_format);
691    }
692  else
693#endif
694
695    {
696      char tmpv[1024];
697      sprintf (tmpv,
698               _
699               ("Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n"),
700               r_inputs->value);
701      errorException (m, tmpv, "InternalError", NULL);
702      *eres = -1;
703    }
704  *myMap = m;
705  *ioutputs = request_output_real_format;
706}
707
708
709#ifdef WIN32
710/**
711 * createProcess function: create a new process after setting some env variables
712 */
713void
714createProcess (maps * m, map * request_inputs, service * s1, char *opts,
715               int cpid, maps * inputs, maps * outputs)
716{
717  STARTUPINFO si;
718  PROCESS_INFORMATION pi;
719  ZeroMemory (&si, sizeof (si));
720  si.cb = sizeof (si);
721  ZeroMemory (&pi, sizeof (pi));
722  char *tmp = (char *) malloc ((1024 + cgiContentLength) * sizeof (char));
723  char *tmpq = (char *) malloc ((1024 + cgiContentLength) * sizeof (char));
724  map *req = getMap (request_inputs, "request");
725  map *id = getMap (request_inputs, "identifier");
726  map *di = getMap (request_inputs, "DataInputs");
727
728  // The required size for the dataInputsKVP and dataOutputsKVP buffers
729  // may exceed cgiContentLength, hence a 2 kb extension. However, a
730  // better solution would be to have getMapsAsKVP() determine the required
731  // buffer size before allocating memory.     
732  char *dataInputsKVP = getMapsAsKVP (inputs, cgiContentLength + 2048, 0);
733  char *dataOutputsKVP = getMapsAsKVP (outputs, cgiContentLength + 2048, 1);
734#ifdef DEBUG
735  fprintf (stderr, "DATAINPUTSKVP %s\n", dataInputsKVP);
736  fprintf (stderr, "DATAOUTPUTSKVP %s\n", dataOutputsKVP);
737#endif
738  map *sid = getMapFromMaps (m, "lenv", "sid");
739  map *r_inputs = getMapFromMaps (m, "main", "tmpPath");
740  map *r_inputs1 = getMap (request_inputs, "metapath");
741  int hasIn = -1;
742  if (r_inputs1 == NULL)
743    {
744      r_inputs1 = createMap ("metapath", "");
745      hasIn = 1;
746    }
747  map *r_inputs2 = getMap (request_inputs, "ResponseDocument");
748  if (r_inputs2 == NULL)
749    r_inputs2 = getMap (request_inputs, "RawDataOutput");
750  map *tmpPath = getMapFromMaps (m, "lenv", "cwd");
751
752  map *tmpReq = getMap (request_inputs, "xrequest");
753 
754  if(r_inputs2 != NULL && tmpReq != NULL) {
755        const char key[] = "rfile=";
756        char* kvp = (char*) malloc((FILENAME_MAX + strlen(key))*sizeof(char));
757        char* filepath = kvp + strlen(key);
758        strncpy(kvp, key, strlen(key));
759        addToCache(m, tmpReq->value, tmpReq->value, "text/xml", strlen(tmpReq->value), 
760                   filepath, FILENAME_MAX);                               
761    if (filepath == NULL) {
762        errorException( m, _("Unable to cache HTTP POST Execute request."), "InternalError", NULL); 
763                return;
764    }   
765        sprintf(tmp,"\"metapath=%s&%s&cgiSid=%s",
766                r_inputs1->value,kvp,sid->value);
767    sprintf(tmpq,"metapath=%s&%s",
768                r_inputs1->value,kvp);
769        free(kvp);             
770  }
771  else if (r_inputs2 != NULL)
772    {
773      sprintf (tmp,
774               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s&cgiSid=%s",
775               r_inputs1->value, req->value, id->value, dataInputsKVP,
776               r_inputs2->name, dataOutputsKVP, sid->value);
777      sprintf (tmpq,
778               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s",
779               r_inputs1->value, req->value, id->value, dataInputsKVP,
780               r_inputs2->name, dataOutputsKVP);                   
781    }
782  else
783    {
784      sprintf (tmp,
785               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&cgiSid=%s",
786               r_inputs1->value, req->value, id->value, dataInputsKVP,
787               sid->value);
788      sprintf (tmpq,
789               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s",
790               r_inputs1->value, req->value, id->value, dataInputsKVP,
791               sid->value);   
792    }
793
794  if (hasIn > 0)
795    {
796      freeMap (&r_inputs1);
797      free (r_inputs1);
798    }
799  char *tmp1 = zStrdup (tmp);
800  sprintf (tmp, "\"%s\" %s \"%s\"", PROGRAMNAME, tmp1, sid->value); 
801  free (dataInputsKVP);
802  free (dataOutputsKVP);
803#ifdef DEBUG
804  fprintf (stderr, "REQUEST IS : %s \n", tmp);
805#endif
806
807  map* usid = getMapFromMaps (m, "lenv", "usid");
808  if (usid != NULL && usid->value != NULL) {
809    SetEnvironmentVariable("USID", TEXT (usid->value));
810  }
811
812  SetEnvironmentVariable ("CGISID", TEXT (sid->value));
813  SetEnvironmentVariable ("QUERY_STRING", TEXT (tmpq));
814  // knut: Prevent REQUEST_METHOD=POST in background process call to cgic:main (process hangs when reading cgiIn):
815  SetEnvironmentVariable("REQUEST_METHOD", "GET");
816 
817  char clen[1000];
818  sprintf (clen, "%d", strlen (tmpq));
819  SetEnvironmentVariable ("CONTENT_LENGTH", TEXT (clen));
820
821  if (!CreateProcess (NULL,     // No module name (use command line)
822                      TEXT (tmp),       // Command line
823                      NULL,     // Process handle not inheritable
824                      NULL,     // Thread handle not inheritable
825                      FALSE,    // Set handle inheritance to FALSE
826                      CREATE_NO_WINDOW, // Apache won't wait until the end
827                      NULL,     // Use parent's environment block
828                      NULL,     // Use parent's starting directory
829                      &si,      // Pointer to STARTUPINFO struct
830                      &pi)      // Pointer to PROCESS_INFORMATION struct
831    )
832    {
833#ifdef DEBUG
834      fprintf (stderr, "CreateProcess failed (%d).\n", GetLastError ());
835#endif
836      if (tmp != NULL) {
837        free(tmp);
838      }
839      if (tmpq != NULL) {
840        free(tmpq);
841      }         
842      return;
843    }
844  else
845    {
846#ifdef DEBUG
847      fprintf (stderr, "CreateProcess successful (%d).\n\n\n\n",
848               GetLastError ());
849#endif
850    }
851  CloseHandle (pi.hProcess);
852  CloseHandle (pi.hThread);
853 
854  if (tmp != NULL) {
855    free(tmp);
856  }
857  if (tmpq != NULL) {
858    free(tmpq);
859  }
860 
861#ifdef DEBUG
862  fprintf (stderr, "CreateProcess finished !\n");
863#endif
864}
865#endif
866
867int
868runRequest (map ** inputs)
869{
870
871#ifndef USE_GDB
872#ifndef WIN32
873  signal (SIGCHLD, SIG_IGN);
874#endif 
875  signal (SIGSEGV, sig_handler);
876  signal (SIGTERM, sig_handler);
877  signal (SIGINT, sig_handler);
878  signal (SIGILL, sig_handler);
879  signal (SIGFPE, sig_handler);
880  signal (SIGABRT, sig_handler);
881#endif
882
883  map *r_inputs = NULL;
884  map *request_inputs = *inputs;
885  maps *m = NULL;
886  char *REQUEST = NULL;
887  /**
888   * Parsing service specfic configuration file
889   */
890  m = (maps *) malloc (MAPS_SIZE);
891  if (m == NULL)
892    {
893      return errorException (m, _("Unable to allocate memory."),
894                             "InternalError", NULL);
895    }
896  char ntmp[1024];
897#ifndef WIN32
898  getcwd (ntmp, 1024);
899#else
900  _getcwd (ntmp, 1024);
901#endif
902  r_inputs = getMapOrFill (&request_inputs, "metapath", "");
903
904
905  char conf_file[10240];
906  snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value);
907  if (conf_read (conf_file, m) == 2)
908    {
909      errorException (NULL, _("Unable to load the main.cfg file."),
910                      "InternalError", NULL);
911      free (m);
912      return 1;
913    }
914#ifdef DEBUG
915  fprintf (stderr, "***** BEGIN MAPS\n");
916  dumpMaps (m);
917  fprintf (stderr, "***** END MAPS\n");
918#endif
919
920  map *getPath = getMapFromMaps (m, "main", "gettextPath");
921  if (getPath != NULL)
922    {
923      bindtextdomain ("zoo-kernel", getPath->value);
924      bindtextdomain ("zoo-services", getPath->value);
925    }
926  else
927    {
928      bindtextdomain ("zoo-kernel", "/usr/share/locale/");
929      bindtextdomain ("zoo-services", "/usr/share/locale/");
930    }
931
932
933  /**
934   * Manage our own error log file (usefull to separate standard apache debug
935   * messages from the ZOO-Kernel ones but also for IIS users to avoid wrong
936   * headers messages returned by the CGI due to wrong redirection of stderr)
937   */
938  FILE *fstde = NULL;
939  map *fstdem = getMapFromMaps (m, "main", "logPath");
940  if (fstdem != NULL)
941    fstde = freopen (fstdem->value, "a+", stderr);
942
943  r_inputs = getMap (request_inputs, "language");
944  if (r_inputs == NULL)
945    r_inputs = getMapFromMaps (m, "main", "language");
946  if (r_inputs != NULL)
947    {
948      if (isValidLang (m, r_inputs->value) < 0)
949        {
950          char tmp[1024];
951          sprintf (tmp,
952                   _
953                   ("The value %s is not supported for the <language> parameter"),
954                   r_inputs->value);
955          errorException (m, tmp, "InvalidParameterValue", "language");
956          freeMaps (&m);
957          free (m);
958          free (REQUEST);
959          return 1;
960
961        }
962      char *tmp = zStrdup (r_inputs->value);
963      setMapInMaps (m, "main", "language", tmp);
964#ifdef DEB
965      char tmp2[12];
966      sprintf (tmp2, "%s.utf-8", tmp);
967      translateChar (tmp2, '-', '_');
968      setlocale (LC_ALL, tmp2);
969#else
970      translateChar (tmp, '-', '_');
971      setlocale (LC_ALL, tmp);
972#endif
973#ifndef WIN32
974      setenv ("LC_ALL", tmp, 1);
975#else
976      char tmp1[12];
977      sprintf (tmp1, "LC_ALL=%s", tmp);
978      putenv (tmp1);
979#endif
980      free (tmp);
981    }
982  else
983    {
984      setlocale (LC_ALL, "en_US");
985#ifndef WIN32
986      setenv ("LC_ALL", "en_US", 1);
987#else
988      char tmp1[12];
989      sprintf (tmp1, "LC_ALL=en_US");
990      putenv (tmp1);
991#endif
992      setMapInMaps (m, "main", "language", "en-US");
993    }
994  setlocale (LC_NUMERIC, "en_US");
995  bind_textdomain_codeset ("zoo-kernel", "UTF-8");
996  textdomain ("zoo-kernel");
997  bind_textdomain_codeset ("zoo-services", "UTF-8");
998  textdomain ("zoo-services");
999
1000  map *lsoap = getMap (request_inputs, "soap");
1001  if (lsoap != NULL && strcasecmp (lsoap->value, "true") == 0)
1002    setMapInMaps (m, "main", "isSoap", "true");
1003  else
1004    setMapInMaps (m, "main", "isSoap", "false");
1005
1006  if(strlen(cgiServerName)>0)
1007  {
1008    char tmpUrl[1024];
1009       
1010        if ( getenv("HTTPS") != NULL && strncmp(getenv("HTTPS"), "on", 2) == 0 ) { // Knut: check if non-empty instead of "on"?         
1011                if ( strncmp(cgiServerPort, "443", 3) == 0 ) { 
1012                        sprintf(tmpUrl, "https://%s%s", cgiServerName, cgiScriptName);
1013                }
1014                else {
1015                        sprintf(tmpUrl, "https://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
1016                }
1017        }
1018        else {
1019                if ( strncmp(cgiServerPort, "80", 2) == 0 ) { 
1020                        sprintf(tmpUrl, "http://%s%s", cgiServerName, cgiScriptName);
1021                }
1022                else {
1023                        sprintf(tmpUrl, "http://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
1024                }
1025        }
1026#ifdef DEBUG
1027    fprintf(stderr,"*** %s ***\n",tmpUrl);
1028#endif
1029    setMapInMaps(m,"main","serverAddress",tmpUrl);
1030  }
1031
1032  /**
1033   * Check for minimum inputs
1034   */
1035  map* err=NULL;
1036  const char *vvr[]={
1037    "GetCapabilities",
1038    "DescribeProcess",
1039    "Execute",
1040    NULL
1041  };
1042  checkValidValue(request_inputs,&err,"Request",(const char**)vvr,1);
1043  const char *vvs[]={
1044    "WPS",
1045    NULL
1046  };
1047  if(err!=NULL){
1048    checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
1049    printExceptionReportResponse (m, err);
1050    freeMap(&err);
1051    free(err);
1052    if (count (request_inputs) == 1)
1053      {
1054        freeMap (&request_inputs);
1055        free (request_inputs);
1056      }
1057    freeMaps (&m);
1058    free (m);
1059    return 1;
1060  }
1061  checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
1062  const char *vvv[]={
1063    "1.0.0",
1064    NULL
1065  };
1066  r_inputs = getMap (request_inputs, "Request");
1067  REQUEST = zStrdup (r_inputs->value);
1068  if (strncasecmp (REQUEST, "GetCapabilities", 15) != 0){
1069    checkValidValue(request_inputs,&err,"version",(const char**)vvv,1);
1070    checkValidValue(request_inputs,&err,"identifier",NULL,1);
1071  }else{
1072    checkValidValue(request_inputs,&err,"AcceptVersions",(const char**)vvv,-1);
1073  }
1074  if(err!=NULL){
1075    printExceptionReportResponse (m, err);
1076    freeMap(&err);
1077    free(err);
1078    if (count (request_inputs) == 1)
1079      {
1080        freeMap (&request_inputs);
1081        free (request_inputs);
1082      }
1083    free(REQUEST);
1084    freeMaps (&m);
1085    free (m);
1086    return 1;
1087  }
1088
1089  r_inputs = getMap (request_inputs, "serviceprovider");
1090  if (r_inputs == NULL)
1091    {
1092      addToMap (request_inputs, "serviceprovider", "");
1093    }
1094
1095
1096  maps *request_output_real_format = NULL;
1097  map *tmpm = getMapFromMaps (m, "main", "serverAddress");
1098  if (tmpm != NULL)
1099    SERVICE_URL = zStrdup (tmpm->value);
1100  else
1101    SERVICE_URL = zStrdup (DEFAULT_SERVICE_URL);
1102
1103  service *s1;
1104  int scount = 0;
1105#ifdef DEBUG
1106  dumpMap (r_inputs);
1107#endif
1108  char conf_dir[1024];
1109  int t;
1110  char tmps1[1024];
1111
1112  r_inputs = NULL;
1113  r_inputs = getMap (request_inputs, "metapath");
1114  if (r_inputs != NULL)
1115    snprintf (conf_dir, 1024, "%s/%s", ntmp, r_inputs->value);
1116  else
1117    snprintf (conf_dir, 1024, "%s", ntmp);
1118
1119  if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0)
1120    {
1121#ifdef DEBUG
1122      dumpMap (r_inputs);
1123#endif
1124      xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
1125      r_inputs = NULL;
1126      //r_inputs = getMap (request_inputs, "ServiceProvider");
1127      xmlNodePtr n=printGetCapabilitiesHeader(doc,m);
1128      /**
1129       * Here we need to close stdout to ensure that unsupported chars
1130       * has been found in the zcfg and then printed on stdout
1131       */
1132      int saved_stdout = dup (fileno (stdout));
1133      dup2 (fileno (stderr), fileno (stdout));
1134      if (int res =               
1135          recursReaddirF (m, n, conf_dir, NULL, saved_stdout, 0,
1136                          printGetCapabilitiesForProcess) < 0)
1137        {
1138          freeMaps (&m);
1139          free (m);
1140          free (REQUEST);
1141          free (SERVICE_URL);
1142          fflush (stdout);
1143          return res;
1144        }
1145      dup2 (saved_stdout, fileno (stdout));
1146      printDocument (m, doc, getpid ());
1147      freeMaps (&m);
1148      free (m);
1149      free (REQUEST);
1150      free (SERVICE_URL);
1151      fflush (stdout);
1152      return 0;
1153    }
1154  else
1155    {
1156      r_inputs = getMap (request_inputs, "Identifier");
1157
1158      struct dirent *dp;
1159      DIR *dirp = opendir (conf_dir);
1160      if (dirp == NULL)
1161        {
1162          errorException (m, _("The specified path path doesn't exist."),
1163                          "InvalidParameterValue", conf_dir);
1164          freeMaps (&m);
1165          free (m);
1166          free (REQUEST);
1167          free (SERVICE_URL);
1168          return 0;
1169        }
1170      if (strncasecmp (REQUEST, "DescribeProcess", 15) == 0)
1171        {
1172          /**
1173           * Loop over Identifier list
1174           */
1175          xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
1176          r_inputs = NULL;
1177          xmlNodePtr n = printWPSHeader(doc,m,"DescribeProcess",
1178                                        "ProcessDescriptions");
1179
1180          r_inputs = getMap (request_inputs, "Identifier");
1181
1182          char *orig = zStrdup (r_inputs->value);
1183
1184          int saved_stdout = dup (fileno (stdout));
1185          dup2 (fileno (stderr), fileno (stdout));
1186          if (strcasecmp ("all", orig) == 0)
1187            {
1188              if (int res =
1189                  recursReaddirF (m, n, conf_dir, NULL, saved_stdout, 0,
1190                                  printDescribeProcessForProcess) < 0)
1191                return res;
1192            }
1193          else
1194            {
1195              char *saveptr;
1196              char *tmps = strtok_r (orig, ",", &saveptr);
1197
1198              char buff[256];
1199              char buff1[1024];
1200              while (tmps != NULL)
1201                {
1202                  int hasVal = -1;
1203                  char *corig = zStrdup (tmps);
1204                  if (strstr (corig, ".") != NULL)
1205                    {
1206
1207                      parseIdentifier (m, conf_dir, corig, buff1);
1208                      map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1209                      if (tmpMap != NULL)
1210                        addToMap (request_inputs, "metapath", tmpMap->value);
1211                      map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier");
1212
1213                      s1 = (service *) malloc (SERVICE_SIZE);
1214                      t = readServiceFile (m, buff1, &s1, tmpMapI->value);
1215                      if (t < 0)
1216                        {
1217                          map *tmp00 = getMapFromMaps (m, "lenv", "message");
1218                          char tmp01[1024];
1219                          if (tmp00 != NULL)
1220                            sprintf (tmp01,
1221                                     _
1222                                     ("Unable to parse the ZCFG file for the following ZOO-Service: %s. Message: %s"),
1223                                     tmps, tmp00->value);
1224                          else
1225                            sprintf (tmp01,
1226                                     _
1227                                     ("Unable to parse the ZCFG file for the following ZOO-Service: %s."),
1228                                     tmps);
1229                          dup2 (saved_stdout, fileno (stdout));
1230                          errorException (m, tmp01, "InvalidParameterValue",
1231                                          "identifier");
1232                          freeMaps (&m);
1233                          free (m);
1234                          free (REQUEST);
1235                          free (corig);
1236                          free (orig);
1237                          free (SERVICE_URL);
1238                          free (s1);
1239                          closedir (dirp);
1240                          xmlFreeDoc (doc);
1241                          xmlCleanupParser ();
1242                          zooXmlCleanupNs ();
1243                          return 1;
1244                        }
1245#ifdef DEBUG
1246                      dumpService (s1);
1247#endif
1248                      printDescribeProcessForProcess (m, n, s1);
1249                      freeService (&s1);
1250                      free (s1);
1251                      s1 = NULL;
1252                      scount++;
1253                      hasVal = 1;
1254                      setMapInMaps (m, "lenv", "level", "0");
1255                    }
1256                  else
1257                    {
1258                      memset (buff, 0, 256);
1259                      snprintf (buff, 256, "%s.zcfg", corig);
1260                      memset (buff1, 0, 1024);
1261#ifdef DEBUG
1262                      printf ("\n#######%s\n########\n", buff);
1263#endif
1264                      while ((dp = readdir (dirp)) != NULL)
1265                        {
1266                          if (strcasecmp (dp->d_name, buff) == 0)
1267                            {
1268                              memset (buff1, 0, 1024);
1269                              snprintf (buff1, 1024, "%s/%s", conf_dir,
1270                                        dp->d_name);
1271                              s1 = (service *) malloc (SERVICE_SIZE);
1272                              if (s1 == NULL)
1273                                {
1274                                  dup2 (saved_stdout, fileno (stdout));
1275                                  return errorException (m,
1276                                                         _
1277                                                         ("Unable to allocate memory."),
1278                                                         "InternalError",
1279                                                         NULL);
1280                                }
1281#ifdef DEBUG
1282                              printf
1283                                ("#################\n(%s) %s\n#################\n",
1284                                 r_inputs->value, buff1);
1285#endif
1286                              char *tmp0 = zStrdup (dp->d_name);
1287                              tmp0[strlen (tmp0) - 5] = 0;
1288                              t = readServiceFile (m, buff1, &s1, tmp0);
1289                              free (tmp0);
1290                              if (t < 0)
1291                                {
1292                                  map *tmp00 =
1293                                    getMapFromMaps (m, "lenv", "message");
1294                                  char tmp01[1024];
1295                                  if (tmp00 != NULL)
1296                                    sprintf (tmp01,
1297                                             _
1298                                             ("Unable to parse the ZCFG file: %s (%s)"),
1299                                             dp->d_name, tmp00->value);
1300                                  else
1301                                    sprintf (tmp01,
1302                                             _
1303                                             ("Unable to parse the ZCFG file: %s."),
1304                                             dp->d_name);
1305                                  dup2 (saved_stdout, fileno (stdout));
1306                                  errorException (m, tmp01, "InternalError",
1307                                                  NULL);
1308                                  freeMaps (&m);
1309                                  free (m);
1310                                  free (orig);
1311                                  free (REQUEST);
1312                                  closedir (dirp);
1313                                  xmlFreeDoc (doc);
1314                                  xmlCleanupParser ();
1315                                  zooXmlCleanupNs ();
1316                                  return 1;
1317                                }
1318#ifdef DEBUG
1319                              dumpService (s1);
1320#endif
1321                              printDescribeProcessForProcess (m, n, s1);
1322                              freeService (&s1);
1323                              free (s1);
1324                              s1 = NULL;
1325                              scount++;
1326                              hasVal = 1;
1327                            }
1328                        }
1329                    }
1330                  if (hasVal < 0)
1331                    {
1332                      map *tmp00 = getMapFromMaps (m, "lenv", "message");
1333                      char tmp01[1024];
1334                      if (tmp00 != NULL)
1335                        sprintf (tmp01,
1336                                 _("Unable to parse the ZCFG file: %s (%s)"),
1337                                 buff, tmp00->value);
1338                      else
1339                        sprintf (tmp01,
1340                                 _("Unable to parse the ZCFG file: %s."),
1341                                 buff);
1342                      dup2 (saved_stdout, fileno (stdout));
1343                      errorException (m, tmp01, "InvalidParameterValue",
1344                                      "Identifier");
1345                      freeMaps (&m);
1346                      free (m);
1347                      free (orig);
1348                      free (REQUEST);
1349                      closedir (dirp);
1350                      xmlFreeDoc (doc);
1351                      xmlCleanupParser ();
1352                      zooXmlCleanupNs ();
1353                      return 1;
1354                    }
1355                  rewinddir (dirp);
1356                  tmps = strtok_r (NULL, ",", &saveptr);
1357                  if (corig != NULL)
1358                    free (corig);
1359                }
1360            }
1361          closedir (dirp);
1362          fflush (stdout);
1363          dup2 (saved_stdout, fileno (stdout));
1364          free (orig);
1365          printDocument (m, doc, getpid ());
1366          freeMaps (&m);
1367          free (m);
1368          free (REQUEST);
1369          free (SERVICE_URL);
1370          fflush (stdout);
1371          return 0;
1372        }
1373      else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0)
1374        {
1375          errorException (m,
1376                          _
1377                          ("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."),
1378                          "InvalidParameterValue", "request");
1379#ifdef DEBUG
1380          fprintf (stderr, "No request found %s", REQUEST);
1381#endif
1382          closedir (dirp);
1383          freeMaps (&m);
1384          free (m);
1385          free (REQUEST);
1386          free (SERVICE_URL);
1387          fflush (stdout);
1388          return 0;
1389        }
1390      closedir (dirp);
1391    }
1392
1393  s1 = NULL;
1394  s1 = (service *) malloc (SERVICE_SIZE);
1395  if (s1 == NULL)
1396    {
1397      freeMaps (&m);
1398      free (m);
1399      free (REQUEST);
1400      free (SERVICE_URL);
1401      return errorException (m, _("Unable to allocate memory."),
1402                             "InternalError", NULL);
1403    }
1404
1405  r_inputs = getMap (request_inputs, "MetaPath");
1406  if (r_inputs != NULL)
1407    snprintf (tmps1, 1024, "%s/%s", ntmp, r_inputs->value);
1408  else
1409    snprintf (tmps1, 1024, "%s/", ntmp);
1410  r_inputs = getMap (request_inputs, "Identifier");
1411  char *ttmp = zStrdup (tmps1);
1412  snprintf (tmps1, 1024, "%s/%s.zcfg", ttmp, r_inputs->value);
1413  free (ttmp);
1414#ifdef DEBUG
1415  fprintf (stderr, "Trying to load %s\n", tmps1);
1416#endif
1417  if (strstr (r_inputs->value, ".") != NULL)
1418    {
1419      char *identifier = zStrdup (r_inputs->value);
1420      parseIdentifier (m, conf_dir, identifier, tmps1);
1421      map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1422      if (tmpMap != NULL)
1423        addToMap (request_inputs, "metapath", tmpMap->value);
1424      free (identifier);
1425    }
1426  else
1427    {
1428      setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
1429      setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value);
1430    }
1431
1432  r_inputs = getMapFromMaps (m, "lenv", "Identifier");
1433  int saved_stdout = dup (fileno (stdout));
1434  dup2 (fileno (stderr), fileno (stdout));
1435  t = readServiceFile (m, tmps1, &s1, r_inputs->value);
1436  fflush (stdout);
1437  dup2 (saved_stdout, fileno (stdout));
1438  if (t < 0)
1439    {
1440      char *tmpMsg = (char *) malloc (2048 + strlen (r_inputs->value));
1441      sprintf (tmpMsg,
1442               _
1443               ("The value for <identifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."),
1444               r_inputs->value);
1445      errorException (m, tmpMsg, "InvalidParameterValue", "identifier");
1446      free (tmpMsg);
1447      free (s1);
1448      freeMaps (&m);
1449      free (m);
1450      free (REQUEST);
1451      free (SERVICE_URL);
1452      return 0;
1453    }
1454  close (saved_stdout);
1455
1456#ifdef DEBUG
1457  dumpService (s1);
1458#endif
1459  int j;
1460
1461
1462  /**
1463   * Create the input and output maps data structure
1464   */
1465  int i = 0;
1466  HINTERNET hInternet;
1467  HINTERNET res;
1468  hInternet = InternetOpen (
1469#ifndef WIN32
1470                             (LPCTSTR)
1471#endif
1472                             "ZooWPSClient\0",
1473                             INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1474
1475#ifndef WIN32
1476  if (!CHECK_INET_HANDLE (hInternet))
1477    fprintf (stderr, "WARNING : hInternet handle failed to initialize");
1478#endif
1479  maps *request_input_real_format = NULL;
1480  maps *tmpmaps = request_input_real_format;
1481  map *postRequest = NULL;
1482  postRequest = getMap (request_inputs, "xrequest");
1483  if (postRequest == NULLMAP)
1484    {
1485    /**
1486     * Parsing outputs provided as KVP
1487     */
1488      r_inputs = NULL;
1489#ifdef DEBUG
1490      fprintf (stderr, "OUTPUT Parsing ... \n");
1491#endif
1492      r_inputs = getMap (request_inputs, "ResponseDocument");
1493      if (r_inputs == NULL)
1494        r_inputs = getMap (request_inputs, "RawDataOutput");
1495
1496#ifdef DEBUG
1497      fprintf (stderr, "OUTPUT Parsing ... \n");
1498#endif
1499      if (r_inputs != NULL)
1500        {
1501#ifdef DEBUG
1502          fprintf (stderr, "OUTPUT Parsing start now ... \n");
1503#endif
1504          char cursor_output[10240];
1505          char *cotmp = zStrdup (r_inputs->value);
1506          snprintf (cursor_output, 10240, "%s", cotmp);
1507          free (cotmp);
1508          j = 0;
1509
1510      /**
1511       * Put each Output into the outputs_as_text array
1512       */
1513          char *pToken;
1514          maps *tmp_output = NULL;
1515#ifdef DEBUG
1516          fprintf (stderr, "OUTPUT [%s]\n", cursor_output);
1517#endif
1518          pToken = strtok (cursor_output, ";");
1519          char **outputs_as_text = (char **) malloc (128 * sizeof (char *));
1520          if (outputs_as_text == NULL)
1521            {
1522              return errorException (m, _("Unable to allocate memory"),
1523                                     "InternalError", NULL);
1524            }
1525          i = 0;
1526          while (pToken != NULL)
1527            {
1528#ifdef DEBUG
1529              fprintf (stderr, "***%s***\n", pToken);
1530              fflush (stderr);
1531              fprintf (stderr, "***%s***\n", pToken);
1532#endif
1533              outputs_as_text[i] =
1534                (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
1535              if (outputs_as_text[i] == NULL)
1536                {
1537                  return errorException (m, _("Unable to allocate memory"),
1538                                         "InternalError", NULL);
1539                }
1540              snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s",
1541                        pToken);
1542              pToken = strtok (NULL, ";");
1543              i++;
1544            }
1545          for (j = 0; j < i; j++)
1546            {
1547              char *tmp = zStrdup (outputs_as_text[j]);
1548              free (outputs_as_text[j]);
1549              char *tmpc;
1550              tmpc = strtok (tmp, "@");
1551              int k = 0;
1552              while (tmpc != NULL)
1553                {
1554                  if (k == 0)
1555                    {
1556                      if (tmp_output == NULL)
1557                        {
1558                          tmp_output = (maps *) malloc (MAPS_SIZE);
1559                          if (tmp_output == NULL)
1560                            {
1561                              return errorException (m,
1562                                                     _
1563                                                     ("Unable to allocate memory."),
1564                                                     "InternalError", NULL);
1565                            }
1566                          tmp_output->name = zStrdup (tmpc);
1567                          tmp_output->content = NULL;
1568                          tmp_output->next = NULL;
1569                        }
1570                    }
1571                  else
1572                    {
1573                      char *tmpv = strstr (tmpc, "=");
1574                      char tmpn[256];
1575                      memset (tmpn, 0, 256);
1576                      strncpy (tmpn, tmpc,
1577                               (strlen (tmpc) -
1578                                strlen (tmpv)) * sizeof (char));
1579                      tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
1580#ifdef DEBUG
1581                      fprintf (stderr, "OUTPUT DEF [%s]=[%s]\n", tmpn,
1582                               tmpv + 1);
1583#endif
1584                      if (tmp_output->content == NULL)
1585                        {
1586                          tmp_output->content = createMap (tmpn, tmpv + 1);
1587                          tmp_output->content->next = NULL;
1588                        }
1589                      else
1590                        addToMap (tmp_output->content, tmpn, tmpv + 1);
1591                    }
1592                  k++;
1593#ifdef DEBUG
1594                  fprintf (stderr, "***%s***\n", tmpc);
1595#endif
1596                  tmpc = strtok (NULL, "@");
1597                }
1598              if (request_output_real_format == NULL)
1599                request_output_real_format = dupMaps (&tmp_output);
1600              else
1601                addMapsToMaps (&request_output_real_format, tmp_output);
1602              freeMaps (&tmp_output);
1603              free (tmp_output);
1604              tmp_output = NULL;
1605#ifdef DEBUG
1606              dumpMaps (tmp_output);
1607              fflush (stderr);
1608#endif
1609              free (tmp);
1610            }
1611          free (outputs_as_text);
1612        }
1613
1614
1615    /**
1616     * Parsing inputs provided as KVP
1617     */
1618      r_inputs = getMap (request_inputs, "DataInputs");
1619#ifdef DEBUG
1620      fprintf (stderr, "DATA INPUTS [%s]\n", r_inputs->value);
1621#endif
1622      char cursor_input[40960];
1623      if (r_inputs != NULL){
1624        snprintf (cursor_input, 40960, "%s", r_inputs->value);
1625        j = 0;
1626
1627        /**
1628         * Put each DataInputs into the inputs_as_text array
1629         */
1630        char *tmp1 = zStrdup (cursor_input);
1631        char *pToken;
1632        pToken = strtok (cursor_input, ";");
1633        if (pToken != NULL && strncasecmp (pToken, tmp1, strlen (tmp1)) == 0)
1634          {
1635            char *tmp2 = url_decode (tmp1);
1636            snprintf (cursor_input, (strlen (tmp2) + 1) * sizeof (char), "%s",
1637                      tmp2);
1638            free (tmp2);
1639            pToken = strtok (cursor_input, ";");
1640          }
1641        free (tmp1);
1642       
1643        char **inputs_as_text = (char **) malloc (100 * sizeof (char *));
1644        if (inputs_as_text == NULL)
1645          {
1646            return errorException (m, _("Unable to allocate memory."),
1647                                   "InternalError", NULL);
1648          }
1649        i = 0;
1650        while (pToken != NULL)
1651          {
1652#ifdef DEBUG
1653            fprintf (stderr, "***%s***\n", pToken);
1654            fflush (stderr);
1655            fprintf (stderr, "***%s***\n", pToken);
1656#endif
1657            inputs_as_text[i] =
1658              (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
1659            if (inputs_as_text[i] == NULL)
1660              {
1661                return errorException (m, _("Unable to allocate memory."),
1662                                       "InternalError", NULL);
1663              }
1664            snprintf (inputs_as_text[i], strlen (pToken) + 1, "%s", pToken);
1665            pToken = strtok (NULL, ";");
1666            i++;
1667          }
1668       
1669        for (j = 0; j < i; j++)
1670          {
1671            char *tmp = zStrdup (inputs_as_text[j]);
1672            free (inputs_as_text[j]);
1673            char *tmpc;
1674            tmpc = strtok (tmp, "@");
1675            while (tmpc != NULL)
1676              {
1677#ifdef DEBUG
1678                fprintf (stderr, "***\n***%s***\n", tmpc);
1679#endif
1680                char *tmpv = strstr (tmpc, "=");
1681                char tmpn[256];
1682                memset (tmpn, 0, 256);
1683                if (tmpv != NULL)
1684                  {
1685                    strncpy (tmpn, tmpc,
1686                             (strlen (tmpc) - strlen (tmpv)) * sizeof (char));
1687                    tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
1688                  }
1689                else
1690                  {
1691                    strncpy (tmpn, tmpc, strlen (tmpc) * sizeof (char));
1692                    tmpn[strlen (tmpc)] = 0;
1693                  }
1694#ifdef DEBUG
1695                fprintf (stderr, "***\n*** %s = %s ***\n", tmpn, tmpv + 1);
1696#endif
1697                if (tmpmaps == NULL)
1698                  {
1699                    tmpmaps = (maps *) malloc (MAPS_SIZE);
1700                    if (tmpmaps == NULL)
1701                      {
1702                        return errorException (m,
1703                                               _("Unable to allocate memory."),
1704                                               "InternalError", NULL);
1705                      }
1706                    tmpmaps->name = zStrdup (tmpn);
1707                    if (tmpv != NULL)
1708                      {
1709                        char *tmpvf = url_decode (tmpv + 1);
1710                        tmpmaps->content = createMap ("value", tmpvf);
1711                        free (tmpvf);
1712                      }
1713                    else
1714                      tmpmaps->content = createMap ("value", "Reference");
1715                    tmpmaps->next = NULL;
1716                  }
1717                tmpc = strtok (NULL, "@");
1718                while (tmpc != NULL)
1719                  {
1720#ifdef DEBUG
1721                    fprintf (stderr, "*** KVP NON URL-ENCODED \n***%s***\n",
1722                             tmpc);
1723#endif
1724                    char *tmpv1 = strstr (tmpc, "=");
1725#ifdef DEBUG
1726                    fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n",
1727                             tmpv1 + 1);
1728#endif
1729                    char tmpn1[1024];
1730                    memset (tmpn1, 0, 1024);
1731                    if (tmpv1 != NULL)
1732                      {
1733                        strncpy (tmpn1, tmpc, strlen (tmpc) - strlen (tmpv1));
1734                        tmpn1[strlen (tmpc) - strlen (tmpv1)] = 0;
1735                        addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
1736                      }
1737                    else
1738                      {
1739                        strncpy (tmpn1, tmpc, strlen (tmpc));
1740                        tmpn1[strlen (tmpc)] = 0;
1741                        map *lmap = getLastMap (tmpmaps->content);
1742                        char *tmpValue =
1743                          (char *) malloc ((strlen (tmpv) + strlen (tmpc) + 1) *
1744                                           sizeof (char));
1745                        sprintf (tmpValue, "%s@%s", tmpv + 1, tmpc);
1746                        free (lmap->value);
1747                        lmap->value = zStrdup (tmpValue);
1748                        free (tmpValue);
1749                        tmpc = strtok (NULL, "@");
1750                        continue;
1751                      }
1752#ifdef DEBUG
1753                    fprintf (stderr, "*** NAME NON URL-ENCODED \n***%s***\n",
1754                             tmpn1);
1755                    fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n",
1756                             tmpv1 + 1);
1757#endif
1758                    if (strcmp (tmpn1, "xlink:href") != 0)
1759                      addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
1760                    else if (tmpv1 != NULL)
1761                      {
1762                        char *tmpx2 = url_decode (tmpv1 + 1);
1763                        if (strncasecmp (tmpx2, "http://", 7) != 0 &&
1764                            strncasecmp (tmpx2, "ftp://", 6) != 0 &&
1765                            strncasecmp (tmpx2, "https://", 8) != 0)
1766                          {
1767                            char emsg[1024];
1768                            sprintf (emsg,
1769                                     _
1770                                     ("Unable to find a valid protocol to download the remote file %s"),
1771                                     tmpv1 + 1);
1772                            errorException (m, emsg, "InternalError", NULL);
1773                            freeMaps (&m);
1774                            free (m);
1775                            free (REQUEST);
1776                            free (SERVICE_URL);
1777                            InternetCloseHandle (&hInternet);
1778                            freeService (&s1);
1779                            free (s1);
1780                            return 0;
1781                          }
1782#ifdef DEBUG
1783                        fprintf (stderr,
1784                                 "REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",
1785                                 tmpv1 + 1);
1786#endif
1787                        addToMap (tmpmaps->content, tmpn1, tmpx2);
1788#ifndef WIN32
1789                        if (CHECK_INET_HANDLE (hInternet))
1790#endif
1791                          {
1792                            if (loadRemoteFile
1793                                (&m, &tmpmaps->content, &hInternet, tmpx2) < 0)
1794                              {
1795                                freeMaps (&m);
1796                                free (m);
1797                                free (REQUEST);
1798                                free (SERVICE_URL);
1799                                InternetCloseHandle (&hInternet);
1800                                freeService (&s1);
1801                                free (s1);
1802                                return 0;
1803                              }
1804                          }
1805                        free (tmpx2);
1806                        addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
1807                      }
1808                    tmpc = strtok (NULL, "@");
1809                  }
1810#ifdef DEBUG
1811                dumpMaps (tmpmaps);
1812                fflush (stderr);
1813#endif
1814                if (request_input_real_format == NULL)
1815                  request_input_real_format = dupMaps (&tmpmaps);
1816                else
1817                  {
1818                    maps *testPresence =
1819                      getMaps (request_input_real_format, tmpmaps->name);
1820                    if (testPresence != NULL)
1821                      {
1822                        elements *elem =
1823                          getElements (s1->inputs, tmpmaps->name);
1824                        if (elem != NULL)
1825                          {
1826                            if (appendMapsToMaps
1827                                (m, request_input_real_format, tmpmaps,
1828                                 elem) < 0)
1829                              {
1830                                freeMaps (&m);
1831                                free (m);
1832                                free (REQUEST);
1833                                free (SERVICE_URL);
1834                                InternetCloseHandle (&hInternet);
1835                                freeService (&s1);
1836                                free (s1);
1837                                return 0;
1838                              }
1839                          }
1840                      }
1841                    else
1842                      addMapsToMaps (&request_input_real_format, tmpmaps);
1843                  }
1844                freeMaps (&tmpmaps);
1845                free (tmpmaps);
1846                tmpmaps = NULL;
1847                free (tmp);
1848              }
1849          }
1850        free (inputs_as_text);
1851      }
1852    }
1853  else
1854    {
1855    /**
1856     * Parse XML request
1857     */
1858      xmlInitParser ();
1859#ifdef DEBUG
1860      fflush (stderr);
1861      fprintf (stderr, "BEFORE %s\n", postRequest->value);
1862      fflush (stderr);
1863#endif
1864      xmlDocPtr doc = xmlParseMemory (postRequest->value, cgiContentLength);
1865#ifdef DEBUG
1866      fprintf (stderr, "AFTER\n");
1867      fflush (stderr);
1868#endif
1869    /**
1870     * Parse every Input in DataInputs node.
1871     */
1872      xmlXPathObjectPtr tmpsptr =
1873        extractFromDoc (doc, "/*/*/*[local-name()='Input']");
1874      xmlNodeSet *tmps = tmpsptr->nodesetval;
1875#ifdef DEBUG
1876      fprintf (stderr, "*****%d*****\n", tmps->nodeNr);
1877#endif
1878      for (int k = 0; k < tmps->nodeNr; k++)
1879        {
1880          maps *tmpmaps = NULL;
1881          xmlNodePtr cur = tmps->nodeTab[k];
1882          if (tmps->nodeTab[k]->type == XML_ELEMENT_NODE)
1883            {
1884        /**
1885         * A specific Input node.
1886         */
1887#ifdef DEBUG
1888              fprintf (stderr, "= element 0 node \"%s\"\n", cur->name);
1889#endif
1890              xmlNodePtr cur2 = cur->children;
1891              while (cur2 != NULL)
1892                {
1893                  while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1894                    cur2 = cur2->next;
1895                  if (cur2 == NULL)
1896                    break;
1897          /**
1898           * Indentifier
1899           */
1900                  if (xmlStrncasecmp
1901                      (cur2->name, BAD_CAST "Identifier",
1902                       xmlStrlen (cur2->name)) == 0)
1903                    {
1904                      xmlChar *val =
1905                        xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
1906                      if (tmpmaps == NULL)
1907                        {
1908                          tmpmaps = (maps *) malloc (MAPS_SIZE);
1909                          if (tmpmaps == NULL)
1910                            {
1911                              return errorException (m,
1912                                                     _
1913                                                     ("Unable to allocate memory."),
1914                                                     "InternalError", NULL);
1915                            }
1916                          tmpmaps->name = zStrdup ((char *) val);
1917                          tmpmaps->content = NULL;
1918                          tmpmaps->next = NULL;
1919                        }
1920                      xmlFree (val);
1921                    }
1922          /**
1923           * Title, Asbtract
1924           */
1925                  if (xmlStrncasecmp
1926                      (cur2->name, BAD_CAST "Title",
1927                       xmlStrlen (cur2->name)) == 0
1928                      || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract",
1929                                         xmlStrlen (cur2->name)) == 0)
1930                    {
1931                      xmlChar *val =
1932                        xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
1933                      if (tmpmaps == NULL)
1934                        {
1935                          tmpmaps = (maps *) malloc (MAPS_SIZE);
1936                          if (tmpmaps == NULL)
1937                            {
1938                              return errorException (m,
1939                                                     _
1940                                                     ("Unable to allocate memory."),
1941                                                     "InternalError", NULL);
1942                            }
1943                          tmpmaps->name = zStrdup ("missingIndetifier");
1944                          tmpmaps->content =
1945                            createMap ((char *) cur2->name, (char *) val);
1946                          tmpmaps->next = NULL;
1947                        }
1948                      else
1949                        {
1950                          if (tmpmaps->content != NULL)
1951                            addToMap (tmpmaps->content,
1952                                      (char *) cur2->name, (char *) val);
1953                          else
1954                            tmpmaps->content =
1955                              createMap ((char *) cur2->name, (char *) val);
1956                        }
1957#ifdef DEBUG
1958                      dumpMaps (tmpmaps);
1959#endif
1960                      xmlFree (val);
1961                    }
1962          /**
1963           * InputDataFormChoice (Reference or Data ?)
1964           */
1965                  if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0)
1966                    {
1967            /**
1968             * Get every attribute from a Reference node
1969             * mimeType, encoding, schema, href, method
1970             * Header and Body gesture should be added here
1971             */
1972#ifdef DEBUG
1973                      fprintf (stderr, "REFERENCE\n");
1974#endif
1975                      const char *refs[5] =
1976                        { "mimeType", "encoding", "schema", "method",
1977                        "href"
1978                      };
1979                      for (int l = 0; l < 5; l++)
1980                        {
1981#ifdef DEBUG
1982                          fprintf (stderr, "*** %s ***", refs[l]);
1983#endif
1984                          xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]);
1985                          if (val != NULL && xmlStrlen (val) > 0)
1986                            {
1987                              if (tmpmaps->content != NULL)
1988                                addToMap (tmpmaps->content, refs[l],
1989                                          (char *) val);
1990                              else
1991                                tmpmaps->content =
1992                                  createMap (refs[l], (char *) val);
1993                              map *ltmp = getMap (tmpmaps->content, "method");
1994                              if (l == 4)
1995                                {
1996                                  if (!
1997                                      (ltmp != NULL
1998                                       && strncmp (ltmp->value, "POST",
1999                                                   4) == 0)
2000                                      && CHECK_INET_HANDLE (hInternet))
2001                                    {
2002                                      if (loadRemoteFile
2003                                          (&m, &tmpmaps->content, &hInternet,
2004                                           (char *) val) != 0)
2005                                        {
2006                                          freeMaps (&m);
2007                                          free (m);
2008                                          free (REQUEST);
2009                                          free (SERVICE_URL);
2010                                          InternetCloseHandle (&hInternet);
2011                                          freeService (&s1);
2012                                          free (s1);
2013                                          return 0;
2014                                        }
2015                                    }
2016                                }
2017                            }
2018#ifdef DEBUG
2019                          fprintf (stderr, "%s\n", val);
2020#endif
2021                          xmlFree (val);
2022                        }
2023#ifdef POST_DEBUG
2024                      fprintf (stderr,
2025                               "Parse Header and Body from Reference \n");
2026#endif
2027                      xmlNodePtr cur3 = cur2->children;
2028                      /*      HINTERNET hInternetP;
2029                         hInternetP=InternetOpen(
2030                         #ifndef WIN32
2031                         (LPCTSTR)
2032                         #endif
2033                         "ZooWPSClient\0",
2034                         INTERNET_OPEN_TYPE_PRECONFIG,
2035                         NULL,NULL, 0); */
2036                      //hInternet.ihandle[hInternet.nb].header=NULL;
2037                      while (cur3 != NULL)
2038                        {
2039                          while (cur3 != NULL
2040                                 && cur3->type != XML_ELEMENT_NODE)
2041                            cur3 = cur3->next;
2042                          if (cur3 == NULL)
2043                            break;
2044                          if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
2045                              0)
2046                            {
2047                              const char *ha[2];
2048                              ha[0] = "key";
2049                              ha[1] = "value";
2050                              int hai;
2051                              char *has;
2052                              char *key;
2053                              for (hai = 0; hai < 2; hai++)
2054                                {
2055                                  xmlChar *val =
2056                                    xmlGetProp (cur3, BAD_CAST ha[hai]);
2057#ifdef POST_DEBUG
2058                                  fprintf (stderr, "%s = %s\n", ha[hai],
2059                                           (char *) val);
2060#endif
2061                                  if (hai == 0)
2062                                    {
2063                                      key = zStrdup ((char *) val);
2064                                    }
2065                                  else
2066                                    {
2067                                      has =
2068                                        (char *)
2069                                        malloc ((4 + xmlStrlen (val) +
2070                                                 strlen (key)) *
2071                                                sizeof (char));
2072                                      if (has == NULL)
2073                                        {
2074                                          return errorException (m,
2075                                                                 _
2076                                                                 ("Unable to allocate memory."),
2077                                                                 "InternalError",
2078                                                                 NULL);
2079                                        }
2080                                      snprintf (has,
2081                                                (3 + xmlStrlen (val) +
2082                                                 strlen (key)), "%s: %s", key,
2083                                                (char *) val);
2084                                      free (key);
2085#ifdef POST_DEBUG
2086                                      fprintf (stderr, "%s\n", has);
2087#endif
2088                                    }
2089                                  xmlFree (val);
2090                                }
2091                              hInternet.ihandle[hInternet.nb].header =
2092                                curl_slist_append (hInternet.ihandle
2093                                                   [hInternet.nb].header,
2094                                                   has);
2095                              if (has != NULL)
2096                                free (has);
2097                            }
2098                          else
2099                            {
2100#ifdef POST_DEBUG
2101                              fprintf (stderr,
2102                                       "Try to fetch the body part of the request ...\n");
2103#endif
2104                              if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
2105                                  == 0)
2106                                {
2107#ifdef POST_DEBUG
2108                                  fprintf (stderr, "Body part found !!!\n",
2109                                           (char *) cur3->content);
2110#endif
2111                                  char *tmp =
2112                                    (char *) malloc (cgiContentLength +
2113                                                     1 * sizeof (char));
2114                                  memset (tmp, 0, cgiContentLength);
2115                                  xmlNodePtr cur4 = cur3->children;
2116                                  while (cur4 != NULL)
2117                                    {
2118                                      while (cur4->type != XML_ELEMENT_NODE)
2119                                        cur4 = cur4->next;
2120                                      xmlDocPtr bdoc =
2121                                        xmlNewDoc (BAD_CAST "1.0");
2122                                      bdoc->encoding =
2123                                        xmlCharStrdup ("UTF-8");
2124                                      xmlDocSetRootElement (bdoc, cur4);
2125                                      xmlChar *btmps;
2126                                      int bsize;
2127                                      xmlDocDumpMemory (bdoc, &btmps, &bsize);
2128#ifdef POST_DEBUG
2129                                      fprintf (stderr,
2130                                               "Body part found !!! %s %s\n",
2131                                               tmp, (char *) btmps);
2132#endif
2133                                      if (btmps != NULL)
2134                                        sprintf (tmp, "%s", (char *) btmps);
2135                                      xmlFree (btmps);
2136                                      cur4 = cur4->next;
2137                                      xmlFreeDoc (bdoc);
2138                                    }
2139                                  map *btmp =
2140                                    getMap (tmpmaps->content, "href");
2141                                  if (btmp != NULL)
2142                                    {
2143#ifdef POST_DEBUG
2144                                      fprintf (stderr, "%s %s\n", btmp->value,
2145                                               tmp);
2146                                      curl_easy_setopt (hInternet.handle,
2147                                                        CURLOPT_VERBOSE, 1);
2148#endif
2149                                      hInternet.
2150                                        waitingRequests[hInternet.nb] =
2151                                        strdup (tmp);
2152                                      InternetOpenUrl (&hInternet,
2153                                                       btmp->value,
2154                                                       hInternet.waitingRequests
2155                                                       [hInternet.nb],
2156                                                       strlen
2157                                                       (hInternet.waitingRequests
2158                                                        [hInternet.nb]),
2159                                                       INTERNET_FLAG_NO_CACHE_WRITE,
2160                                                       0);
2161                                    }
2162                                  free (tmp);
2163                                }
2164                              else
2165                                if (xmlStrcasecmp
2166                                    (cur3->name,
2167                                     BAD_CAST "BodyReference") == 0)
2168                                {
2169                                  xmlChar *val =
2170                                    xmlGetProp (cur3, BAD_CAST "href");
2171                                  HINTERNET bInternet, res1;
2172                                  bInternet = InternetOpen (
2173#ifndef WIN32
2174                                                             (LPCTSTR)
2175#endif
2176                                                             "ZooWPSClient\0",
2177                                                             INTERNET_OPEN_TYPE_PRECONFIG,
2178                                                             NULL, NULL, 0);
2179                                  if (!CHECK_INET_HANDLE (hInternet))
2180                                    fprintf (stderr,
2181                                             "WARNING : hInternet handle failed to initialize");
2182#ifdef POST_DEBUG
2183                                  curl_easy_setopt (bInternet.handle,
2184                                                    CURLOPT_VERBOSE, 1);
2185#endif
2186                                  bInternet.waitingRequests[0] =
2187                                    strdup ((char *) val);
2188                                  res1 =
2189                                    InternetOpenUrl (&bInternet,
2190                                                     bInternet.waitingRequests
2191                                                     [0], NULL, 0,
2192                                                     INTERNET_FLAG_NO_CACHE_WRITE,
2193                                                     0);
2194                                  processDownloads (&bInternet);
2195                                  char *tmp =
2196                                    (char *)
2197                                    malloc ((bInternet.ihandle[0].nDataLen +
2198                                             1) * sizeof (char));
2199                                  if (tmp == NULL)
2200                                    {
2201                                      return errorException (m,
2202                                                             _
2203                                                             ("Unable to allocate memory."),
2204                                                             "InternalError",
2205                                                             NULL);
2206                                    }
2207                                  size_t bRead;
2208                                  InternetReadFile (bInternet.ihandle[0],
2209                                                    (LPVOID) tmp,
2210                                                    bInternet.
2211                                                    ihandle[0].nDataLen,
2212                                                    &bRead);
2213                                  tmp[bInternet.ihandle[0].nDataLen] = 0;
2214                                  InternetCloseHandle (&bInternet);
2215                                  map *btmp =
2216                                    getMap (tmpmaps->content, "href");
2217                                  if (btmp != NULL)
2218                                    {
2219#ifdef POST_DEBUG
2220                                      fprintf (stderr, "%s %s\n", btmp->value,
2221                                               tmp);
2222#endif
2223                                      hInternet.
2224                                        waitingRequests[hInternet.nb] =
2225                                        strdup (tmp);
2226                                      res =
2227                                        InternetOpenUrl (&hInternet,
2228                                                         btmp->value,
2229                                                         hInternet.waitingRequests
2230                                                         [hInternet.nb],
2231                                                         strlen
2232                                                         (hInternet.waitingRequests
2233                                                          [hInternet.nb]),
2234                                                         INTERNET_FLAG_NO_CACHE_WRITE,
2235                                                         0);
2236                                    }
2237                                  free (tmp);
2238                                }
2239                            }
2240                          cur3 = cur3->next;
2241                        }
2242#ifdef POST_DEBUG
2243                      fprintf (stderr,
2244                               "Header and Body was parsed from Reference \n");
2245#endif
2246#ifdef DEBUG
2247                      dumpMap (tmpmaps->content);
2248                      fprintf (stderr, "= element 2 node \"%s\" = (%s)\n",
2249                               cur2->name, cur2->content);
2250#endif
2251                    }
2252                  else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
2253                    {
2254#ifdef DEBUG
2255                      fprintf (stderr, "DATA\n");
2256#endif
2257                      xmlNodePtr cur4 = cur2->children;
2258                      while (cur4 != NULL)
2259                        {
2260                          while (cur4 != NULL
2261                                 && cur4->type != XML_ELEMENT_NODE)
2262                            cur4 = cur4->next;
2263                          if (cur4 == NULL)
2264                            break;
2265                          if (xmlStrcasecmp
2266                              (cur4->name, BAD_CAST "LiteralData") == 0)
2267                            {
2268                /**
2269                 * Get every attribute from a LiteralData node
2270                 * dataType , uom
2271                 */
2272                              char *list[2];
2273                              list[0] = zStrdup ("dataType");
2274                              list[1] = zStrdup ("uom");
2275                              for (int l = 0; l < 2; l++)
2276                                {
2277#ifdef DEBUG
2278                                  fprintf (stderr, "*** LiteralData %s ***",
2279                                           list[l]);
2280#endif
2281                                  xmlChar *val =
2282                                    xmlGetProp (cur4, BAD_CAST list[l]);
2283                                  if (val != NULL
2284                                      && strlen ((char *) val) > 0)
2285                                    {
2286                                      if (tmpmaps->content != NULL)
2287                                        addToMap (tmpmaps->content, list[l],
2288                                                  (char *) val);
2289                                      else
2290                                        tmpmaps->content =
2291                                          createMap (list[l], (char *) val);
2292#ifdef DEBUG
2293                                      fprintf (stderr, "%s\n", val);
2294#endif
2295                                    }
2296                                  xmlFree (val);
2297                                  free (list[l]);
2298                                }
2299                            }
2300                          else
2301                            if (xmlStrcasecmp
2302                                (cur4->name, BAD_CAST "ComplexData") == 0)
2303                            {
2304                /**
2305                 * Get every attribute from a Reference node
2306                 * mimeType, encoding, schema
2307                 */
2308                              const char *coms[3] =
2309                                { "mimeType", "encoding", "schema" };
2310                              for (int l = 0; l < 3; l++)
2311                                {
2312#ifdef DEBUG
2313                                  fprintf (stderr, "*** ComplexData %s ***\n",
2314                                           coms[l]);
2315#endif
2316                                  xmlChar *val =
2317                                    xmlGetProp (cur4, BAD_CAST coms[l]);
2318                                  if (val != NULL
2319                                      && strlen ((char *) val) > 0)
2320                                    {
2321                                      if (tmpmaps->content != NULL)
2322                                        addToMap (tmpmaps->content, coms[l],
2323                                                  (char *) val);
2324                                      else
2325                                        tmpmaps->content =
2326                                          createMap (coms[l], (char *) val);
2327#ifdef DEBUG
2328                                      fprintf (stderr, "%s\n", val);
2329#endif
2330                                    }
2331                                  xmlFree (val);
2332                                }
2333                            }
2334
2335                          map *test = getMap (tmpmaps->content, "encoding");
2336                          if (test == NULL)
2337                            {
2338                              if (tmpmaps->content != NULL)
2339                                addToMap (tmpmaps->content, "encoding",
2340                                          "utf-8");
2341                              else
2342                                tmpmaps->content =
2343                                  createMap ("encoding", "utf-8");
2344                              test = getMap (tmpmaps->content, "encoding");
2345                            }
2346
2347                          if (strcasecmp (test->value, "base64") != 0)
2348                            {
2349                              xmlChar *mv = xmlNodeListGetString (doc,
2350                                                                  cur4->
2351                                                                  xmlChildrenNode,
2352                                                                  1);
2353                              map *ltmp =
2354                                getMap (tmpmaps->content, "mimeType");
2355                              if (mv == NULL
2356                                  ||
2357                                  (xmlStrcasecmp
2358                                   (cur4->name, BAD_CAST "ComplexData") == 0
2359                                   && (ltmp == NULL
2360                                       || strncasecmp (ltmp->value,
2361                                                       "text/xml", 8) == 0)))
2362                                {
2363                                  xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
2364                                  int buffersize;
2365                                  xmlNodePtr cur5 = cur4->children;
2366                                  while (cur5 != NULL
2367                                         && cur5->type != XML_ELEMENT_NODE
2368                                         && cur5->type !=
2369                                         XML_CDATA_SECTION_NODE)
2370                                    cur5 = cur5->next;
2371                                  if (cur5 != NULL
2372                                      && cur5->type != XML_CDATA_SECTION_NODE)
2373                                    {
2374                                      xmlDocSetRootElement (doc1, cur5);
2375                                      xmlDocDumpFormatMemoryEnc (doc1, &mv,
2376                                                                 &buffersize,
2377                                                                 "utf-8", 1);
2378                                      char size[1024];
2379                                      sprintf (size, "%d", buffersize);
2380                                      addToMap (tmpmaps->content, "size",
2381                                                size);
2382                                      xmlFreeDoc (doc1);
2383                                    }
2384                                }
2385                              if (mv != NULL)
2386                                {
2387                                  addToMap (tmpmaps->content, "value",
2388                                            (char *) mv);
2389                                  xmlFree (mv);
2390                                }
2391                            }
2392                          else
2393                            {
2394                              xmlChar *tmp = xmlNodeListGetRawString (doc,
2395                                                                      cur4->xmlChildrenNode,
2396                                                                      0);
2397                              addToMap (tmpmaps->content, "value",
2398                                        (char *) tmp);
2399                              map *tmpv = getMap (tmpmaps->content, "value");
2400                              char *res = NULL;
2401                              char *curs = tmpv->value;
2402                              for (int i = 0; i <= strlen (tmpv->value) / 64;
2403                                   i++)
2404                                {
2405                                  if (res == NULL)
2406                                    res =
2407                                      (char *) malloc (67 * sizeof (char));
2408                                  else
2409                                    res =
2410                                      (char *) realloc (res,
2411                                                        (((i + 1) * 65) +
2412                                                         i) * sizeof (char));
2413                                  int csize = i * 65;
2414                                  strncpy (res + csize, curs, 64);
2415                                  if (i == xmlStrlen (tmp) / 64)
2416                                    strcat (res, "\n\0");
2417                                  else
2418                                    {
2419                                      strncpy (res + (((i + 1) * 64) + i),
2420                                               "\n\0", 2);
2421                                      curs += 64;
2422                                    }
2423                                }
2424                              free (tmpv->value);
2425                              tmpv->value = zStrdup (res);
2426                              free (res);
2427                              xmlFree (tmp);
2428                            }
2429                          cur4 = cur4->next;
2430                        }
2431                    }
2432#ifdef DEBUG
2433                  fprintf (stderr, "cur2 next \n");
2434                  fflush (stderr);
2435#endif
2436                  cur2 = cur2->next;
2437                }
2438#ifdef DEBUG
2439              fprintf (stderr, "ADD MAPS TO REQUEST MAPS !\n");
2440              fflush (stderr);
2441#endif
2442
2443              {
2444                maps *testPresence =
2445                  getMaps (request_input_real_format, tmpmaps->name);
2446                if (testPresence != NULL)
2447                  {
2448                    elements *elem = getElements (s1->inputs, tmpmaps->name);
2449                    if (elem != NULL)
2450                      {
2451                        if (appendMapsToMaps
2452                            (m, request_input_real_format, tmpmaps, elem) < 0)
2453                          {
2454                            freeMaps (&m);
2455                            free (m);
2456                            free (REQUEST);
2457                            free (SERVICE_URL);
2458                            InternetCloseHandle (&hInternet);
2459                            freeService (&s1);
2460                            free (s1);
2461                            return 0;
2462                          }
2463                      }
2464                  }
2465                else
2466                  addMapsToMaps (&request_input_real_format, tmpmaps);
2467              }
2468
2469#ifdef DEBUG
2470              fprintf (stderr, "******TMPMAPS*****\n");
2471              dumpMaps (tmpmaps);
2472              fprintf (stderr, "******REQUESTMAPS*****\n");
2473              dumpMaps (request_input_real_format);
2474#endif
2475              freeMaps (&tmpmaps);
2476              free (tmpmaps);
2477              tmpmaps = NULL;
2478            }
2479#ifdef DEBUG
2480          dumpMaps (tmpmaps);
2481#endif
2482        }
2483#ifdef DEBUG
2484      fprintf (stderr, "Search for response document node\n");
2485#endif
2486      xmlXPathFreeObject (tmpsptr);
2487
2488      tmpsptr =
2489        extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
2490      bool asRaw = false;
2491      tmps = tmpsptr->nodesetval;
2492      if (tmps->nodeNr == 0)
2493        {
2494          xmlXPathFreeObject (tmpsptr);
2495          tmpsptr =
2496            extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
2497          tmps = tmpsptr->nodesetval;
2498          asRaw = true;
2499        }
2500#ifdef DEBUG
2501      fprintf (stderr, "*****%d*****\n", tmps->nodeNr);
2502#endif
2503      if (asRaw == true)
2504        {
2505          addToMap (request_inputs, "RawDataOutput", "");
2506          xmlNodePtr cur0 = tmps->nodeTab[0];
2507          if (cur0->type == XML_ELEMENT_NODE)
2508            {
2509
2510              maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
2511              if (tmpmaps == NULL)
2512                {
2513                  return errorException (m, _("Unable to allocate memory."),
2514                                         "InternalError", NULL);
2515                }
2516              tmpmaps->name = zStrdup ("unknownIdentifier");
2517              tmpmaps->content = NULL;
2518              tmpmaps->next = NULL;
2519
2520        /**
2521         * Get every attribute from a RawDataOutput node
2522         * mimeType, encoding, schema, uom
2523         */
2524              const char *outs[4] =
2525                { "mimeType", "encoding", "schema", "uom" };
2526              for (int l = 0; l < 4; l++)
2527                {
2528#ifdef DEBUG
2529                  fprintf (stderr, "*** %s ***\t", outs[l]);
2530#endif
2531                  xmlChar *val = xmlGetProp (cur0, BAD_CAST outs[l]);
2532                  if (val != NULL)
2533                    {
2534                      if (strlen ((char *) val) > 0)
2535                        {
2536                          if (tmpmaps->content != NULL)
2537                            addToMap (tmpmaps->content, outs[l],
2538                                      (char *) val);
2539                          else
2540                            tmpmaps->content =
2541                              createMap (outs[l], (char *) val);
2542                        }
2543                      xmlFree (val);
2544                    }
2545                }
2546              xmlNodePtr cur2 = cur0->children;
2547              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
2548                cur2 = cur2->next;
2549              while (cur2 != NULL)
2550                {
2551                  if (xmlStrncasecmp
2552                      (cur2->name, BAD_CAST "Identifier",
2553                       xmlStrlen (cur2->name)) == 0)
2554                    {
2555                      xmlChar *val =
2556                        xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
2557                      free (tmpmaps->name);
2558                      tmpmaps->name = zStrdup ((char *) val);
2559                      xmlFree (val);
2560                    }
2561                  cur2 = cur2->next;
2562                  while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
2563                    cur2 = cur2->next;
2564                }
2565              if (request_output_real_format == NULL)
2566                request_output_real_format = dupMaps (&tmpmaps);
2567              else
2568                addMapsToMaps (&request_output_real_format, tmpmaps);
2569              if (tmpmaps != NULL)
2570                {
2571                  freeMaps (&tmpmaps);
2572                  free (tmpmaps);
2573                  tmpmaps = NULL;
2574                }
2575            }
2576        }
2577      else
2578        for (int k = 0; k < tmps->nodeNr; k++)
2579          {
2580            //else
2581            addToMap (request_inputs, "ResponseDocument", "");
2582            maps *tmpmaps = NULL;
2583            xmlNodePtr cur = tmps->nodeTab[k];
2584            if (cur->type == XML_ELEMENT_NODE)
2585              {
2586          /**
2587           * A specific responseDocument node.
2588           */
2589                if (tmpmaps == NULL)
2590                  {
2591                    tmpmaps = (maps *) malloc (MAPS_SIZE);
2592                    if (tmpmaps == NULL)
2593                      {
2594                        return errorException (m,
2595                                               _
2596                                               ("Unable to allocate memory."),
2597                                               "InternalError", NULL);
2598                      }
2599                    tmpmaps->name = zStrdup ("unknownIdentifier");
2600                    tmpmaps->content = NULL;
2601                    tmpmaps->next = NULL;
2602                  }
2603          /**
2604           * Get every attribute: storeExecuteResponse, lineage, status
2605           */
2606                const char *ress[3] =
2607                  { "storeExecuteResponse", "lineage", "status" };
2608                xmlChar *val;
2609                for (int l = 0; l < 3; l++)
2610                  {
2611#ifdef DEBUG
2612                    fprintf (stderr, "*** %s ***\t", ress[l]);
2613#endif
2614                    val = xmlGetProp (cur, BAD_CAST ress[l]);
2615                    if (val != NULL && strlen ((char *) val) > 0)
2616                      {
2617                        if (tmpmaps->content != NULL)
2618                          addToMap (tmpmaps->content, ress[l], (char *) val);
2619                        else
2620                          tmpmaps->content =
2621                            createMap (ress[l], (char *) val);
2622                        addToMap (request_inputs, ress[l], (char *) val);
2623                      }
2624#ifdef DEBUG
2625                    fprintf (stderr, "%s\n", val);
2626#endif
2627                    xmlFree (val);
2628                  }
2629                xmlNodePtr cur1 = cur->children;
2630                while (cur1 != NULL && cur1->type != XML_ELEMENT_NODE)
2631                  cur1 = cur1->next;
2632                int cur1cnt = 0;
2633                while (cur1)
2634                  {
2635            /**
2636             * Indentifier
2637             */
2638                    if (xmlStrncasecmp
2639                        (cur1->name, BAD_CAST "Identifier",
2640                         xmlStrlen (cur1->name)) == 0)
2641                      {
2642                        xmlChar *val =
2643                          xmlNodeListGetString (doc, cur1->xmlChildrenNode,
2644                                                1);
2645                        if (tmpmaps == NULL)
2646                          {
2647                            tmpmaps = (maps *) malloc (MAPS_SIZE);
2648                            if (tmpmaps == NULL)
2649                              {
2650                                return errorException (m,
2651                                                       _
2652                                                       ("Unable to allocate memory."),
2653                                                       "InternalError", NULL);
2654                              }
2655                            tmpmaps->name = zStrdup ((char *) val);
2656                            tmpmaps->content = NULL;
2657                            tmpmaps->next = NULL;
2658                          }
2659                        else
2660                          {
2661                            free (tmpmaps->name);
2662                            tmpmaps->name = zStrdup ((char *) val);
2663                          }
2664                        if (asRaw == true)
2665                          addToMap (request_inputs, "RawDataOutput",
2666                                    (char *) val);
2667                        else
2668                          {
2669                            if (cur1cnt == 0)
2670                              addToMap (request_inputs, "ResponseDocument",
2671                                        (char *) val);
2672                            else
2673                              {
2674                                map *tt =
2675                                  getMap (request_inputs, "ResponseDocument");
2676                                char *tmp = zStrdup (tt->value);
2677                                free (tt->value);
2678                                tt->value =
2679                                  (char *)
2680                                  malloc ((strlen (tmp) +
2681                                           strlen ((char *) val) +
2682                                           1) * sizeof (char));
2683                                sprintf (tt->value, "%s;%s", tmp,
2684                                         (char *) val);
2685                                free (tmp);
2686                              }
2687                          }
2688                        cur1cnt += 1;
2689                        xmlFree (val);
2690                      }
2691            /**
2692             * Title, Asbtract
2693             */
2694                    else
2695                      if (xmlStrncasecmp
2696                          (cur1->name, BAD_CAST "Title",
2697                           xmlStrlen (cur1->name)) == 0
2698                          || xmlStrncasecmp (cur1->name, BAD_CAST "Abstract",
2699                                             xmlStrlen (cur1->name)) == 0)
2700                      {
2701                        xmlChar *val =
2702                          xmlNodeListGetString (doc, cur1->xmlChildrenNode,
2703                                                1);
2704                        if (tmpmaps == NULL)
2705                          {
2706                            tmpmaps = (maps *) malloc (MAPS_SIZE);
2707                            if (tmpmaps == NULL)
2708                              {
2709                                return errorException (m,
2710                                                       _
2711                                                       ("Unable to allocate memory."),
2712                                                       "InternalError", NULL);
2713                              }
2714                            tmpmaps->name = zStrdup ("missingIndetifier");
2715                            tmpmaps->content =
2716                              createMap ((char *) cur1->name, (char *) val);
2717                            tmpmaps->next = NULL;
2718                          }
2719                        else
2720                          {
2721                            if (tmpmaps->content != NULL)
2722                              addToMap (tmpmaps->content, (char *) cur1->name,
2723                                        (char *) val);
2724                            else
2725                              tmpmaps->content =
2726                                createMap ((char *) cur1->name, (char *) val);
2727                          }
2728                        xmlFree (val);
2729                      }
2730                    else
2731                      if (xmlStrncasecmp
2732                          (cur1->name, BAD_CAST "Output",
2733                           xmlStrlen (cur1->name)) == 0)
2734                      {
2735              /**
2736               * Get every attribute from an Output node
2737               * mimeType, encoding, schema, uom, asReference
2738               */
2739                        const char *outs[5] =
2740                          { "mimeType", "encoding", "schema", "uom",
2741                          "asReference"
2742                        };
2743                        for (int l = 0; l < 5; l++)
2744                          {
2745#ifdef DEBUG
2746                            fprintf (stderr, "*** %s ***\t", outs[l]);
2747#endif
2748                            xmlChar *val =
2749                              xmlGetProp (cur1, BAD_CAST outs[l]);
2750                            if (val != NULL && strlen ((char *) val) > 0)
2751                              {
2752                                if (tmpmaps->content != NULL)
2753                                  addToMap (tmpmaps->content, outs[l],
2754                                            (char *) val);
2755                                else
2756                                  tmpmaps->content =
2757                                    createMap (outs[l], (char *) val);
2758                              }
2759#ifdef DEBUG
2760                            fprintf (stderr, "%s\n", val);
2761#endif
2762                            xmlFree (val);
2763                          }
2764                        xmlNodePtr cur2 = cur1->children;
2765                        while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
2766                          cur2 = cur2->next;
2767                        while (cur2)
2768                          {
2769                /**
2770                 * Indentifier
2771                 */
2772                            if (xmlStrncasecmp
2773                                (cur2->name, BAD_CAST "Identifier",
2774                                 xmlStrlen (cur2->name)) == 0)
2775                              {
2776                                xmlChar *val = xmlNodeListGetString (doc,
2777                                                                     cur2->
2778                                                                     xmlChildrenNode,
2779                                                                     1);
2780                                if (tmpmaps == NULL)
2781                                  {
2782                                    tmpmaps = (maps *) malloc (MAPS_SIZE);
2783                                    if (tmpmaps == NULL)
2784                                      {
2785                                        return errorException (m,
2786                                                               _
2787                                                               ("Unable to allocate memory."),
2788                                                               "InternalError",
2789                                                               NULL);
2790                                      }
2791                                    tmpmaps->name = zStrdup ((char *) val);
2792                                    tmpmaps->content = NULL;
2793                                    tmpmaps->next = NULL;
2794                                  }
2795                                else
2796                                  {
2797                                    if (tmpmaps->name != NULL)
2798                                      free (tmpmaps->name);
2799                                    tmpmaps->name = zStrdup ((char *) val);;
2800                                  }
2801                                xmlFree (val);
2802                              }
2803                /**
2804                 * Title, Asbtract
2805                 */
2806                            else
2807                              if (xmlStrncasecmp
2808                                  (cur2->name, BAD_CAST "Title",
2809                                   xmlStrlen (cur2->name)) == 0
2810                                  || xmlStrncasecmp (cur2->name,
2811                                                     BAD_CAST "Abstract",
2812                                                     xmlStrlen (cur2->name))
2813                                  == 0)
2814                              {
2815                                xmlChar *val = xmlNodeListGetString (doc,
2816                                                                     cur2->
2817                                                                     xmlChildrenNode,
2818                                                                     1);
2819                                if (tmpmaps == NULL)
2820                                  {
2821                                    tmpmaps = (maps *) malloc (MAPS_SIZE);
2822                                    if (tmpmaps == NULL)
2823                                      {
2824                                        return errorException (m,
2825                                                               _
2826                                                               ("Unable to allocate memory."),
2827                                                               "InternalError",
2828                                                               NULL);
2829                                      }
2830                                    tmpmaps->name =
2831                                      zStrdup ("missingIndetifier");
2832                                    tmpmaps->content =
2833                                      createMap ((char *) cur2->name,
2834                                                 (char *) val);
2835                                    tmpmaps->next = NULL;
2836                                  }
2837                                else
2838                                  {
2839                                    if (tmpmaps->content != NULL)
2840                                      addToMap (tmpmaps->content,
2841                                                (char *) cur2->name,
2842                                                (char *) val);
2843                                    else
2844                                      tmpmaps->content =
2845                                        createMap ((char *) cur2->name,
2846                                                   (char *) val);
2847                                  }
2848                                xmlFree (val);
2849                              }
2850                            cur2 = cur2->next;
2851                            while (cur2 != NULL
2852                                   && cur2->type != XML_ELEMENT_NODE)
2853                              cur2 = cur2->next;
2854                          }
2855                      }
2856                    cur1 = cur1->next;
2857                    while (cur1 != NULL && cur1->type != XML_ELEMENT_NODE)
2858                      cur1 = cur1->next;
2859                                 
2860                                        if (request_output_real_format == NULL)
2861                                          request_output_real_format = dupMaps (&tmpmaps);
2862                                        else
2863                                          addMapsToMaps (&request_output_real_format, tmpmaps);
2864                  }
2865              }
2866          }
2867      xmlXPathFreeObject (tmpsptr);
2868      xmlFreeDoc (doc);
2869      xmlCleanupParser ();
2870    }
2871
2872  runHttpRequests (&m, &request_input_real_format, &hInternet);
2873
2874  //  if(CHECK_INET_HANDLE(hInternet))
2875  InternetCloseHandle (&hInternet);
2876
2877#ifdef DEBUG
2878  fprintf (stderr, "\n%d\n", __LINE__);
2879  fflush (stderr);
2880  dumpMaps (request_input_real_format);
2881  dumpMaps (request_output_real_format);
2882  dumpMap (request_inputs);
2883  fprintf (stderr, "\n%d\n", __LINE__);
2884  fflush (stderr);
2885#endif
2886
2887  /**
2888   * Ensure that each requested arguments are present in the request
2889   * DataInputs and ResponseDocument / RawDataOutput
2890   */
2891  map* errI=NULL;
2892#ifdef DEBUG 
2893  dumpMaps(request_input_real_format);
2894#endif 
2895  char *dfv = addDefaultValues (&request_input_real_format, s1->inputs, m, 0,&errI);
2896#ifdef DEBUG 
2897  dumpMaps(request_input_real_format);
2898#endif 
2899  maps *ptr = request_input_real_format;
2900  while (ptr != NULL)
2901    {
2902      map *tmp0 = getMap (ptr->content, "size");
2903      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
2904      if (tmp1 != NULL && tmp0 != NULL)
2905        {
2906          float i = atof (tmp0->value) / 1048576.0;
2907          if (i >= atoi (tmp1->value))
2908            {
2909              char tmps[1024];
2910              map *tmpe = createMap ("code", "FileSizeExceeded");
2911              snprintf (tmps, 1024,
2912                        _
2913                        ("The <%s> parameter has a limited size (%sMB) defined in ZOO ServicesProvider configuration file but the reference you provided exceed this limitation (%fMB), please correct your query or the ZOO Configuration file."),
2914                        ptr->name, tmp1->value, i);
2915              addToMap (tmpe, "locator", ptr->name);
2916              addToMap (tmpe, "text", tmps);
2917              printExceptionReportResponse (m, tmpe);
2918              freeService (&s1);
2919              free (s1);
2920              freeMap (&tmpe);
2921              free (tmpe);
2922              freeMaps (&m);
2923              free (m);
2924              free (REQUEST);
2925              free (SERVICE_URL);
2926              freeMaps (&request_input_real_format);
2927              free (request_input_real_format);
2928              freeMaps (&request_output_real_format);
2929              free (request_output_real_format);
2930              freeMaps (&tmpmaps);
2931              free (tmpmaps);
2932              return 1;
2933            }
2934        }
2935      ptr = ptr->next;
2936    }
2937
2938  map* errO=NULL;
2939  char *dfv1 =
2940    addDefaultValues (&request_output_real_format, s1->outputs, m, 1,&errO);
2941  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
2942    {
2943      char tmps[1024];
2944      map *tmpe = NULL;
2945      if (strcmp (dfv, "") != 0)
2946        {
2947          tmpe = createMap ("code", "MissingParameterValue");
2948          int nb=0;
2949          int length=1;
2950          map* len=getMap(errI,"length");
2951          if(len!=NULL)
2952            length=atoi(len->value);
2953          for(nb=0;nb<length;nb++){
2954            map* errp=getMapArray(errI,"value",nb);
2955            snprintf (tmps, 1024,
2956                      _
2957                      ("The <%s> argument was not specified in DataInputs but defined as requested in ZOO ServicesProvider configuration file, please correct your query or the ZOO Configuration file."),
2958                      errp->value);
2959            setMapArray (tmpe, "locator", nb , errp->value);
2960            setMapArray (tmpe, "text", nb , tmps);
2961            setMapArray (tmpe, "code", nb , "MissingParameterValue");
2962          }
2963        }
2964      if (strcmp (dfv1, "") != 0)
2965        {
2966          int ilength=0;
2967          if(tmpe==NULL)
2968            tmpe = createMap ("code", "InvalidParameterValue");
2969          else{
2970            map* len=getMap(tmpe,"length");
2971            if(len!=NULL)
2972              ilength=atoi(len->value);
2973          }
2974          int nb=0;
2975          int length=1;
2976          map* len=getMap(errO,"length");
2977          if(len!=NULL)
2978            length=atoi(len->value);
2979          for(nb=0;nb<length;nb++){
2980            map* errp=getMapArray(errO,"value",nb);
2981            snprintf (tmps, 1024,
2982                      _
2983                      ("The <%s> argument was specified as %s identifier but not defined in the ZOO Configuration File. Please, correct your query or the ZOO Configuration File."),
2984                      errp->value,
2985                      ((getMap(request_inputs,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
2986            setMapArray (tmpe, "locator", nb+ilength , errp->value);
2987            setMapArray (tmpe, "text", nb+ilength , tmps);
2988            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
2989          }
2990        }
2991      printExceptionReportResponse (m, tmpe);
2992      freeService (&s1);
2993      free (s1);
2994      freeMap (&tmpe);
2995      free (tmpe);
2996      freeMaps (&m);
2997      free (m);
2998      free (REQUEST);
2999      free (SERVICE_URL);
3000      freeMaps (&request_input_real_format);
3001      free (request_input_real_format);
3002      freeMaps (&request_output_real_format);
3003      free (request_output_real_format);
3004      freeMaps (&tmpmaps);
3005      free (tmpmaps);
3006      if(errI!=NULL){
3007        freeMap(&errI);
3008        free(errI);
3009      }
3010      if(errO!=NULL){
3011        freeMap(&errO);
3012        free(errO);
3013      }
3014      return 1;
3015    }
3016  maps *tmpReqI = request_input_real_format;
3017  while (tmpReqI != NULL)
3018    {
3019      char name[1024];
3020      if (getMap (tmpReqI->content, "isFile") != NULL)
3021        {
3022          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
3023              cgiFormSuccess)
3024            {
3025              int BufferLen = 1024;
3026              cgiFilePtr file;
3027              int targetFile;
3028              char storageNameOnServer[2048];
3029              char fileNameOnServer[64];
3030              char contentType[1024];
3031              char buffer[1024];
3032              char *tmpStr = NULL;
3033              int size;
3034              int got, t;
3035              map *path = getMapFromMaps (m, "main", "tmpPath");
3036              cgiFormFileSize (tmpReqI->name, &size);
3037              cgiFormFileContentType (tmpReqI->name, contentType,
3038                                      sizeof (contentType));
3039              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
3040                {
3041                  t = -1;
3042                  while (1)
3043                    {
3044                      tmpStr = strstr (name + t + 1, "\\");
3045                      if (NULL == tmpStr)
3046                        tmpStr = strstr (name + t + 1, "/");
3047                      if (NULL != tmpStr)
3048                        t = (int) (tmpStr - name);
3049                      else
3050                        break;
3051                    }
3052                  strcpy (fileNameOnServer, name + t + 1);
3053
3054                  sprintf (storageNameOnServer, "%s/%s", path->value,
3055                           fileNameOnServer);
3056#ifdef DEBUG
3057                  fprintf (stderr, "Name on server %s\n",
3058                           storageNameOnServer);
3059                  fprintf (stderr, "fileNameOnServer: %s\n",
3060                           fileNameOnServer);
3061#endif
3062                  targetFile =
3063                    open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
3064                          S_IRWXU | S_IRGRP | S_IROTH);
3065                  if (targetFile < 0)
3066                    {
3067#ifdef DEBUG
3068                      fprintf (stderr, "could not create the new file,%s\n",
3069                               fileNameOnServer);
3070#endif
3071                    }
3072                  else
3073                    {
3074                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
3075                             == cgiFormSuccess)
3076                        {
3077                          if (got > 0)
3078                            write (targetFile, buffer, got);
3079                        }
3080                    }
3081                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
3082                  cgiFormFileClose (file);
3083                  close (targetFile);
3084#ifdef DEBUG
3085                  fprintf (stderr, "File \"%s\" has been uploaded",
3086                           fileNameOnServer);
3087#endif
3088                }
3089            }
3090        }
3091      tmpReqI = tmpReqI->next;
3092    }
3093
3094  ensureDecodedBase64 (&request_input_real_format);
3095
3096#ifdef DEBUG
3097  fprintf (stderr, "REQUEST_INPUTS\n");
3098  dumpMaps (request_input_real_format);
3099  fprintf (stderr, "REQUEST_OUTPUTS\n");
3100  dumpMaps (request_output_real_format);
3101#endif
3102
3103  maps *curs = getMaps (m, "env");
3104  if (curs != NULL)
3105    {
3106      map *mapcs = curs->content;
3107      while (mapcs != NULLMAP)
3108        {
3109#ifndef WIN32
3110          setenv (mapcs->name, mapcs->value, 1);
3111#else
3112#ifdef DEBUG
3113          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
3114                   mapcs->value);
3115#endif
3116          if (mapcs->value[strlen (mapcs->value) - 2] == '\r')
3117            {
3118#ifdef DEBUG
3119              fprintf (stderr, "[ZOO: Env var finish with \r]\n");
3120#endif
3121              mapcs->value[strlen (mapcs->value) - 1] = 0;
3122            }
3123#ifdef DEBUG
3124          if (SetEnvironmentVariable (mapcs->name, mapcs->value) == 0)
3125            {
3126              fflush (stderr);
3127              fprintf (stderr, "setting variable... %s\n", "OK");
3128            }
3129          else
3130            {
3131              fflush (stderr);
3132              fprintf (stderr, "setting variable... %s\n", "OK");
3133            }
3134#else
3135
3136
3137          SetEnvironmentVariable (mapcs->name, mapcs->value);
3138#endif
3139          char *toto =
3140            (char *)
3141            malloc ((strlen (mapcs->name) + strlen (mapcs->value) +
3142                     2) * sizeof (char));
3143          sprintf (toto, "%s=%s", mapcs->name, mapcs->value);
3144          putenv (toto);
3145#ifdef DEBUG
3146          fflush (stderr);
3147#endif
3148#endif
3149#ifdef DEBUG
3150          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
3151                   mapcs->value);
3152          fflush (stderr);
3153#endif
3154          mapcs = mapcs->next;
3155        }
3156    }
3157
3158#ifdef DEBUG
3159  dumpMap (request_inputs);
3160#endif
3161
3162  /**
3163   * Need to check if we need to fork to load a status enabled
3164   */
3165  r_inputs = NULL;
3166  map *store = getMap (request_inputs, "storeExecuteResponse");
3167  map *status = getMap (request_inputs, "status");
3168  /**
3169   * 05-007r7 WPS 1.0.0 page 57 :
3170   * 'If status="true" and storeExecuteResponse is "false" then the service
3171   * shall raise an exception.'
3172   */
3173  if (status != NULL && strcmp (status->value, "true") == 0 &&
3174      store != NULL && strcmp (store->value, "false") == 0)
3175    {
3176      errorException (m,
3177                      _
3178                      ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."),
3179                      "InvalidParameterValue", "storeExecuteResponse");
3180      freeService (&s1);
3181      free (s1);
3182      freeMaps (&m);
3183      free (m);
3184
3185      freeMaps (&request_input_real_format);
3186      free (request_input_real_format);
3187
3188      freeMaps (&request_output_real_format);
3189      free (request_output_real_format);
3190
3191      free (REQUEST);
3192      free (SERVICE_URL);
3193      return 1;
3194    }
3195  r_inputs = getMap (request_inputs, "storeExecuteResponse");
3196  int eres = SERVICE_STARTED;
3197  int cpid = getpid ();
3198
3199  /**
3200   * Initialize the specific [lenv] section which contains runtime variables:
3201   *
3202   *  - usid : it is an unique identification number
3203   *  - sid : it is the process idenfitication number (OS)
3204   *  - status : value between 0 and 100 to express the  completude of
3205   * the operations of the running service
3206   *  - message : is a string where you can store error messages, in case
3207   * service is failing, or o provide details on the ongoing operation.
3208   *  - cwd : is the current working directory
3209   *  - soap : is a boolean value, true if the request was contained in a SOAP
3210   * Envelop
3211   *  - sessid : string storing the session identifier (only when cookie is
3212   * used)
3213   *  - cgiSid : only defined on Window platforms (for being able to identify
3214   * the created process)
3215   *
3216   */
3217  maps *_tmpMaps = (maps *) malloc (MAPS_SIZE);
3218  _tmpMaps->name = zStrdup ("lenv");
3219  char tmpBuff[100];
3220  semid lid = getShmLockId (NULL, 1);
3221  lockShm (lid);
3222  struct ztimeval tp;
3223  if (zGettimeofday (&tp, NULL) == 0)
3224    sprintf (tmpBuff, "%i", (cpid + ((int) tp.tv_sec + (int) tp.tv_usec)));
3225  else
3226    sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
3227  unlockShm (lid);
3228  removeShmLock (NULL, 1);
3229  _tmpMaps->content = createMap ("usid", tmpBuff);
3230  _tmpMaps->next = NULL;
3231  sprintf (tmpBuff, "%i", cpid);
3232  addToMap (_tmpMaps->content, "sid", tmpBuff);
3233  addToMap (_tmpMaps->content, "status", "0");
3234  addToMap (_tmpMaps->content, "cwd", ntmp);
3235  addToMap (_tmpMaps->content, "message", _("No message provided"));
3236  map *ltmp = getMap (request_inputs, "soap");
3237  if (ltmp != NULL)
3238    addToMap (_tmpMaps->content, "soap", ltmp->value);
3239  else
3240    addToMap (_tmpMaps->content, "soap", "false");
3241  if (cgiCookie != NULL && strlen (cgiCookie) > 0)
3242    {
3243      int hasValidCookie = -1;
3244      char *tcook = zStrdup (cgiCookie);
3245      char *tmp = NULL;
3246      map *testing = getMapFromMaps (m, "main", "cookiePrefix");
3247      if (testing == NULL)
3248        {
3249          tmp = zStrdup ("ID=");
3250        }
3251      else
3252        {
3253          tmp =
3254            (char *) malloc ((strlen (testing->value) + 2) * sizeof (char));
3255          sprintf (tmp, "%s=", testing->value);
3256        }
3257      if (strstr (cgiCookie, ";") != NULL)
3258        {
3259          char *token, *saveptr;
3260          token = strtok_r (cgiCookie, ";", &saveptr);
3261          while (token != NULL)
3262            {
3263              if (strcasestr (token, tmp) != NULL)
3264                {
3265                  if (tcook != NULL)
3266                    free (tcook);
3267                  tcook = zStrdup (token);
3268                  hasValidCookie = 1;
3269                }
3270              token = strtok_r (NULL, ";", &saveptr);
3271            }
3272        }
3273      else
3274        {
3275          if (strstr (cgiCookie, "=") != NULL
3276              && strcasestr (cgiCookie, tmp) != NULL)
3277            {
3278              tcook = zStrdup (cgiCookie);
3279              hasValidCookie = 1;
3280            }
3281          if (tmp != NULL)
3282            {
3283              free (tmp);
3284            }
3285        }
3286      if (hasValidCookie > 0)
3287        {
3288          addToMap (_tmpMaps->content, "sessid", strstr (tcook, "=") + 1);
3289          char session_file_path[1024];
3290          map *tmpPath = getMapFromMaps (m, "main", "sessPath");
3291          if (tmpPath == NULL)
3292            tmpPath = getMapFromMaps (m, "main", "tmpPath");
3293          char *tmp1 = strtok (tcook, ";");
3294          if (tmp1 != NULL)
3295            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
3296                     strstr (tmp1, "=") + 1);
3297          else
3298            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
3299                     strstr (cgiCookie, "=") + 1);
3300          free (tcook);
3301          maps *tmpSess = (maps *) malloc (MAPS_SIZE);
3302          struct stat file_status;
3303          int istat = stat (session_file_path, &file_status);
3304          if (istat == 0 && file_status.st_size > 0)
3305            {
3306              conf_read (session_file_path, tmpSess);
3307              addMapsToMaps (&m, tmpSess);
3308              freeMaps (&tmpSess);
3309              free (tmpSess);
3310            }
3311        }
3312    }
3313  addMapsToMaps (&m, _tmpMaps);
3314  freeMaps (&_tmpMaps);
3315  free (_tmpMaps);
3316
3317#ifdef DEBUG
3318  dumpMap (request_inputs);
3319#endif
3320#ifdef WIN32
3321  char *cgiSidL = NULL;
3322  if (getenv ("CGISID") != NULL)
3323    addToMap (request_inputs, "cgiSid", getenv ("CGISID"));
3324
3325  char* usidp;
3326  if ( (usidp = getenv("USID")) != NULL ) {
3327    setMapInMaps (m, "lenv", "usid", usidp);
3328  }
3329
3330  map *test1 = getMap (request_inputs, "cgiSid");
3331  if (test1 != NULL)
3332    {
3333      cgiSid = test1->value;
3334      addToMap (request_inputs, "storeExecuteResponse", "true");
3335      addToMap (request_inputs, "status", "true");
3336      setMapInMaps (m, "lenv", "sid", test1->value);
3337      status = getMap (request_inputs, "status");
3338    }
3339#endif
3340  char *fbkp, *fbkp1;
3341  FILE *f0, *f1;
3342  if (status != NULL)
3343    if (strcasecmp (status->value, "false") == 0)
3344      status = NULLMAP;
3345  if (status == NULLMAP)
3346    {
3347      loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format,
3348                         &request_output_real_format, &eres);
3349    }
3350  else
3351    {
3352      int pid;
3353#ifdef DEBUG
3354      fprintf (stderr, "\nPID : %d\n", cpid);
3355#endif
3356
3357#ifndef WIN32
3358      pid = fork ();
3359#else
3360      if (cgiSid == NULL)
3361        {
3362          createProcess (m, request_inputs, s1, NULL, cpid,
3363                         request_input_real_format,
3364                         request_output_real_format);
3365          pid = cpid;
3366        }
3367      else
3368        {
3369          pid = 0;
3370          cpid = atoi (cgiSid);
3371        }
3372#endif
3373      if (pid > 0)
3374        {
3375      /**
3376       * dady :
3377       * set status to SERVICE_ACCEPTED
3378       */
3379#ifdef DEBUG
3380          fprintf (stderr, "father pid continue (origin %d) %d ...\n", cpid,
3381                   getpid ());
3382#endif
3383          eres = SERVICE_ACCEPTED;
3384        }
3385      else if (pid == 0)
3386        {
3387      /**
3388       * son : have to close the stdout, stdin and stderr to let the parent
3389       * process answer to http client.
3390       */
3391#ifndef WIN32
3392          zSleep (1);
3393#endif
3394          r_inputs = getMapFromMaps (m, "lenv", "usid");
3395          int cpid = atoi (r_inputs->value);
3396          r_inputs = getMapFromMaps (m, "main", "tmpPath");
3397          map *r_inputs1 = getMap (s1->content, "ServiceProvider");
3398          fbkp =
3399            (char *)
3400            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
3401                     1024) * sizeof (char));
3402          sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value, r_inputs1->value,
3403                   cpid);
3404          char *flog =
3405            (char *)
3406            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
3407                     1024) * sizeof (char));
3408          sprintf (flog, "%s/%s_%d_error.log", r_inputs->value,
3409                   r_inputs1->value, cpid);
3410#ifdef DEBUG
3411          fprintf (stderr, "RUN IN BACKGROUND MODE \n");
3412          fprintf (stderr, "son pid continue (origin %d) %d ...\n", cpid,
3413                   getpid ());
3414          fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value);
3415#endif
3416          freopen (flog, "w+", stderr);
3417          semid lid = getShmLockId (m, 1);
3418          fflush (stderr);
3419          if (lid < 0)
3420            {
3421              fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__);
3422              fflush (stderr);
3423              return -1;
3424            }
3425          else
3426            {
3427              if (lockShm (lid) < 0)
3428                {
3429                  fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__);
3430                  fflush (stderr);
3431                  return -1;
3432                }
3433              fflush (stderr);
3434            }
3435          f0 = freopen (fbkp, "w+", stdout);
3436          rewind (stdout);
3437#ifndef WIN32
3438          fclose (stdin);
3439#endif
3440          free (flog);
3441      /**
3442       * set status to SERVICE_STARTED and flush stdout to ensure full
3443       * content was outputed (the file used to store the ResponseDocument).
3444       * The rewind stdout to restart writing from the bgining of the file,
3445       * this way the data will be updated at the end of the process run.
3446       */
3447          printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value,
3448                                SERVICE_STARTED, request_input_real_format,
3449                                request_output_real_format);
3450          fflush (stdout);
3451          unlockShm (lid);
3452          fflush (stderr);
3453          fbkp1 =
3454            (char *)
3455            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
3456                     1024) * sizeof (char));
3457          sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
3458                   r_inputs1->value, cpid);
3459          f1 = freopen (fbkp1, "w+", stdout);
3460          loadServiceAndRun (&m, s1, request_inputs,
3461                             &request_input_real_format,
3462                             &request_output_real_format, &eres);
3463        }
3464      else
3465        {
3466      /**
3467       * error server don't accept the process need to output a valid
3468       * error response here !!!
3469       */
3470          eres = -1;
3471          errorException (m, _("Unable to run the child process properly"),
3472                          "InternalError", NULL);
3473        }
3474    }
3475
3476#ifdef DEBUG
3477  dumpMaps (request_output_real_format);
3478#endif
3479  if (eres != -1)
3480    outputResponse (s1, request_input_real_format,
3481                    request_output_real_format, request_inputs,
3482                    cpid, m, eres);
3483  fflush (stdout);
3484  /**
3485   * Ensure that if error occurs when freeing memory, no signal will return
3486   * an ExceptionReport document as the result was already returned to the
3487   * client.
3488   */
3489#ifndef USE_GDB
3490  signal (SIGSEGV, donothing);
3491  signal (SIGTERM, donothing);
3492  signal (SIGINT, donothing);
3493  signal (SIGILL, donothing);
3494  signal (SIGFPE, donothing);
3495  signal (SIGABRT, donothing);
3496#endif
3497  if (((int) getpid ()) != cpid || cgiSid != NULL)
3498    {
3499      fclose (stdout);
3500      fclose (stderr);
3501    /**
3502     * Dump back the final file fbkp1 to fbkp
3503     */
3504      fclose (f0);
3505      fclose (f1);
3506      FILE *f2 = fopen (fbkp1, "rb");
3507      semid lid = getShmLockId (m, 1);
3508      if (lid < 0)
3509        return -1;
3510      lockShm (lid);
3511      FILE *f3 = fopen (fbkp, "wb+");
3512      free (fbkp);
3513      fseek (f2, 0, SEEK_END);
3514      long flen = ftell (f2);
3515      fseek (f2, 0, SEEK_SET);
3516      char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
3517      fread (tmps1, flen, 1, f2);
3518      fwrite (tmps1, 1, flen, f3);
3519      fclose (f2);
3520      fclose (f3);
3521      unlockShm (lid);
3522      unlink (fbkp1);
3523      free (fbkp1);
3524      free (tmps1);
3525      unhandleStatus (m);
3526    }
3527
3528  freeService (&s1);
3529  free (s1);
3530  freeMaps (&m);
3531  free (m);
3532
3533  freeMaps (&request_input_real_format);
3534  free (request_input_real_format);
3535
3536  freeMaps (&request_output_real_format);
3537  free (request_output_real_format);
3538
3539  free (REQUEST);
3540  free (SERVICE_URL);
3541#ifdef DEBUG
3542  fprintf (stderr, "Processed response \n");
3543  fflush (stdout);
3544  fflush (stderr);
3545#endif
3546
3547  if (((int) getpid ()) != cpid || cgiSid != NULL)
3548    {
3549      exit (0);
3550    }
3551
3552  return 0;
3553}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png