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

Last change on this file since 583 was 583, checked in by knut, 9 years ago

Increased memory requirement in calls to getMapsAsKVP() (see comment in zoo_service_loader.c).

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 120.1 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  if (r_inputs2 != NULL)
754    {
755      sprintf (tmp,
756               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s&cgiSid=%s\"",
757               r_inputs1->value, req->value, id->value, dataInputsKVP,
758               r_inputs2->name, dataOutputsKVP, sid->value);
759      sprintf (tmpq,
760               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s",
761               r_inputs1->value, req->value, id->value, dataInputsKVP,
762               r_inputs2->name, dataOutputsKVP);
763    }
764  else
765    {
766      sprintf (tmp,
767               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&cgiSid=%s\"",
768               r_inputs1->value, req->value, id->value, dataInputsKVP,
769               sid->value);
770      sprintf (tmpq,
771               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s",
772               r_inputs1->value, req->value, id->value, dataInputsKVP,
773               sid->value);
774    }
775
776  if (hasIn > 0)
777    {
778      freeMap (&r_inputs1);
779      free (r_inputs1);
780    }
781  char *tmp1 = zStrdup (tmp);
782  sprintf (tmp, "\"%s\" %s \"%s\"", PROGRAMNAME, tmp1, sid->value);
783  free (dataInputsKVP);
784  free (dataOutputsKVP);
785#ifdef DEBUG
786  fprintf (stderr, "REQUEST IS : %s \n", tmp);
787#endif
788
789  map* usid = getMapFromMaps (m, "lenv", "usid");
790  if (usid != NULL && usid->value != NULL) {
791    SetEnvironmentVariable("USID", TEXT (usid->value));
792  }
793
794  SetEnvironmentVariable ("CGISID", TEXT (sid->value));
795  SetEnvironmentVariable ("QUERY_STRING", TEXT (tmpq));
796  char clen[1000];
797  sprintf (clen, "%d", strlen (tmpq));
798  SetEnvironmentVariable ("CONTENT_LENGTH", TEXT (clen));
799
800  if (!CreateProcess (NULL,     // No module name (use command line)
801                      TEXT (tmp),       // Command line
802                      NULL,     // Process handle not inheritable
803                      NULL,     // Thread handle not inheritable
804                      FALSE,    // Set handle inheritance to FALSE
805                      CREATE_NO_WINDOW, // Apache won't wait until the end
806                      NULL,     // Use parent's environment block
807                      NULL,     // Use parent's starting directory
808                      &si,      // Pointer to STARTUPINFO struct
809                      &pi)      // Pointer to PROCESS_INFORMATION struct
810    )
811    {
812#ifdef DEBUG
813      fprintf (stderr, "CreateProcess failed (%d).\n", GetLastError ());
814#endif
815      return;
816    }
817  else
818    {
819#ifdef DEBUG
820      fprintf (stderr, "CreateProcess successfull (%d).\n\n\n\n",
821               GetLastError ());
822#endif
823    }
824  CloseHandle (pi.hProcess);
825  CloseHandle (pi.hThread);
826#ifdef DEBUG
827  fprintf (stderr, "CreateProcess finished !\n");
828#endif
829}
830#endif
831
832int
833runRequest (map ** inputs)
834{
835
836#ifndef USE_GDB
837#ifndef WIN32
838  signal (SIGCHLD, SIG_IGN);
839#endif 
840  signal (SIGSEGV, sig_handler);
841  signal (SIGTERM, sig_handler);
842  signal (SIGINT, sig_handler);
843  signal (SIGILL, sig_handler);
844  signal (SIGFPE, sig_handler);
845  signal (SIGABRT, sig_handler);
846#endif
847
848  map *r_inputs = NULL;
849  map *request_inputs = *inputs;
850  maps *m = NULL;
851  char *REQUEST = NULL;
852  /**
853   * Parsing service specfic configuration file
854   */
855  m = (maps *) malloc (MAPS_SIZE);
856  if (m == NULL)
857    {
858      return errorException (m, _("Unable to allocate memory."),
859                             "InternalError", NULL);
860    }
861  char ntmp[1024];
862#ifndef WIN32
863  getcwd (ntmp, 1024);
864#else
865  _getcwd (ntmp, 1024);
866#endif
867  r_inputs = getMapOrFill (&request_inputs, "metapath", "");
868
869
870  char conf_file[10240];
871  snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value);
872  if (conf_read (conf_file, m) == 2)
873    {
874      errorException (NULL, _("Unable to load the main.cfg file."),
875                      "InternalError", NULL);
876      free (m);
877      return 1;
878    }
879#ifdef DEBUG
880  fprintf (stderr, "***** BEGIN MAPS\n");
881  dumpMaps (m);
882  fprintf (stderr, "***** END MAPS\n");
883#endif
884
885  map *getPath = getMapFromMaps (m, "main", "gettextPath");
886  if (getPath != NULL)
887    {
888      bindtextdomain ("zoo-kernel", getPath->value);
889      bindtextdomain ("zoo-services", getPath->value);
890    }
891  else
892    {
893      bindtextdomain ("zoo-kernel", "/usr/share/locale/");
894      bindtextdomain ("zoo-services", "/usr/share/locale/");
895    }
896
897
898  /**
899   * Manage our own error log file (usefull to separate standard apache debug
900   * messages from the ZOO-Kernel ones but also for IIS users to avoid wrong
901   * headers messages returned by the CGI due to wrong redirection of stderr)
902   */
903  FILE *fstde = NULL;
904  map *fstdem = getMapFromMaps (m, "main", "logPath");
905  if (fstdem != NULL)
906    fstde = freopen (fstdem->value, "a+", stderr);
907
908  r_inputs = getMap (request_inputs, "language");
909  if (r_inputs == NULL)
910    r_inputs = getMapFromMaps (m, "main", "language");
911  if (r_inputs != NULL)
912    {
913      if (isValidLang (m, r_inputs->value) < 0)
914        {
915          char tmp[1024];
916          sprintf (tmp,
917                   _
918                   ("The value %s is not supported for the <language> parameter"),
919                   r_inputs->value);
920          errorException (m, tmp, "InvalidParameterValue", "language");
921          freeMaps (&m);
922          free (m);
923          free (REQUEST);
924          return 1;
925
926        }
927      char *tmp = zStrdup (r_inputs->value);
928      setMapInMaps (m, "main", "language", tmp);
929#ifdef DEB
930      char tmp2[12];
931      sprintf (tmp2, "%s.utf-8", tmp);
932      translateChar (tmp2, '-', '_');
933      setlocale (LC_ALL, tmp2);
934#else
935      translateChar (tmp, '-', '_');
936      setlocale (LC_ALL, tmp);
937#endif
938#ifndef WIN32
939      setenv ("LC_ALL", tmp, 1);
940#else
941      char tmp1[12];
942      sprintf (tmp1, "LC_ALL=%s", tmp);
943      putenv (tmp1);
944#endif
945      free (tmp);
946    }
947  else
948    {
949      setlocale (LC_ALL, "en_US");
950#ifndef WIN32
951      setenv ("LC_ALL", "en_US", 1);
952#else
953      char tmp1[12];
954      sprintf (tmp1, "LC_ALL=en_US");
955      putenv (tmp1);
956#endif
957      setMapInMaps (m, "main", "language", "en-US");
958    }
959  setlocale (LC_NUMERIC, "en_US");
960  bind_textdomain_codeset ("zoo-kernel", "UTF-8");
961  textdomain ("zoo-kernel");
962  bind_textdomain_codeset ("zoo-services", "UTF-8");
963  textdomain ("zoo-services");
964
965  map *lsoap = getMap (request_inputs, "soap");
966  if (lsoap != NULL && strcasecmp (lsoap->value, "true") == 0)
967    setMapInMaps (m, "main", "isSoap", "true");
968  else
969    setMapInMaps (m, "main", "isSoap", "false");
970
971  if (strlen (cgiServerName) > 0)
972    {
973      char tmpUrl[1024];
974      if (strncmp (cgiServerPort, "80", 2) == 0)
975        {
976          sprintf (tmpUrl, "http://%s%s", cgiServerName, cgiScriptName);
977        }
978      else
979        {
980          sprintf (tmpUrl, "http://%s:%s%s", cgiServerName, cgiServerPort,
981                   cgiScriptName);
982        }
983#ifdef DEBUG
984      fprintf (stderr, "*** %s ***\n", tmpUrl);
985#endif
986      setMapInMaps (m, "main", "serverAddress", tmpUrl);
987    }
988
989  /**
990   * Check for minimum inputs
991   */
992  map* err=NULL;
993  const char *vvr[]={
994    "GetCapabilities",
995    "DescribeProcess",
996    "Execute",
997    NULL
998  };
999  checkValidValue(request_inputs,&err,"Request",(const char**)vvr,1);
1000  const char *vvs[]={
1001    "WPS",
1002    NULL
1003  };
1004  if(err!=NULL){
1005    checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
1006    printExceptionReportResponse (m, err);
1007    freeMap(&err);
1008    free(err);
1009    if (count (request_inputs) == 1)
1010      {
1011        freeMap (&request_inputs);
1012        free (request_inputs);
1013      }
1014    freeMaps (&m);
1015    free (m);
1016    return 1;
1017  }
1018  checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
1019  const char *vvv[]={
1020    "1.0.0",
1021    NULL
1022  };
1023  r_inputs = getMap (request_inputs, "Request");
1024  REQUEST = zStrdup (r_inputs->value);
1025  if (strncasecmp (REQUEST, "GetCapabilities", 15) != 0){
1026    checkValidValue(request_inputs,&err,"version",(const char**)vvv,1);
1027    checkValidValue(request_inputs,&err,"identifier",NULL,1);
1028  }else{
1029    checkValidValue(request_inputs,&err,"AcceptVersions",(const char**)vvv,-1);
1030  }
1031  if(err!=NULL){
1032    printExceptionReportResponse (m, err);
1033    freeMap(&err);
1034    free(err);
1035    if (count (request_inputs) == 1)
1036      {
1037        freeMap (&request_inputs);
1038        free (request_inputs);
1039      }
1040    free(REQUEST);
1041    freeMaps (&m);
1042    free (m);
1043    return 1;
1044  }
1045
1046  r_inputs = getMap (request_inputs, "serviceprovider");
1047  if (r_inputs == NULL)
1048    {
1049      addToMap (request_inputs, "serviceprovider", "");
1050    }
1051
1052
1053  maps *request_output_real_format = NULL;
1054  map *tmpm = getMapFromMaps (m, "main", "serverAddress");
1055  if (tmpm != NULL)
1056    SERVICE_URL = zStrdup (tmpm->value);
1057  else
1058    SERVICE_URL = zStrdup (DEFAULT_SERVICE_URL);
1059
1060  service *s1;
1061  int scount = 0;
1062#ifdef DEBUG
1063  dumpMap (r_inputs);
1064#endif
1065  char conf_dir[1024];
1066  int t;
1067  char tmps1[1024];
1068
1069  r_inputs = NULL;
1070  r_inputs = getMap (request_inputs, "metapath");
1071  if (r_inputs != NULL)
1072    snprintf (conf_dir, 1024, "%s/%s", ntmp, r_inputs->value);
1073  else
1074    snprintf (conf_dir, 1024, "%s", ntmp);
1075
1076  if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0)
1077    {
1078#ifdef DEBUG
1079      dumpMap (r_inputs);
1080#endif
1081      xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
1082      r_inputs = NULL;
1083      //r_inputs = getMap (request_inputs, "ServiceProvider");
1084      xmlNodePtr n=printGetCapabilitiesHeader(doc,m);
1085      /**
1086       * Here we need to close stdout to ensure that unsupported chars
1087       * has been found in the zcfg and then printed on stdout
1088       */
1089      int saved_stdout = dup (fileno (stdout));
1090      dup2 (fileno (stderr), fileno (stdout));
1091      if (int res =
1092          recursReaddirF (m, n, conf_dir, NULL, saved_stdout, 0,
1093                          printGetCapabilitiesForProcess) < 0)
1094        {
1095          freeMaps (&m);
1096          free (m);
1097          free (REQUEST);
1098          free (SERVICE_URL);
1099          fflush (stdout);
1100          return res;
1101        }
1102      dup2 (saved_stdout, fileno (stdout));
1103      printDocument (m, doc, getpid ());
1104      freeMaps (&m);
1105      free (m);
1106      free (REQUEST);
1107      free (SERVICE_URL);
1108      fflush (stdout);
1109      return 0;
1110    }
1111  else
1112    {
1113      r_inputs = getMap (request_inputs, "Identifier");
1114
1115      struct dirent *dp;
1116      DIR *dirp = opendir (conf_dir);
1117      if (dirp == NULL)
1118        {
1119          errorException (m, _("The specified path path doesn't exist."),
1120                          "InvalidParameterValue", conf_dir);
1121          freeMaps (&m);
1122          free (m);
1123          free (REQUEST);
1124          free (SERVICE_URL);
1125          return 0;
1126        }
1127      if (strncasecmp (REQUEST, "DescribeProcess", 15) == 0)
1128        {
1129          /**
1130           * Loop over Identifier list
1131           */
1132          xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
1133          r_inputs = NULL;
1134          xmlNodePtr n = printWPSHeader(doc,m,"DescribeProcess",
1135                                        "ProcessDescriptions");
1136
1137          r_inputs = getMap (request_inputs, "Identifier");
1138
1139          char *orig = zStrdup (r_inputs->value);
1140
1141          int saved_stdout = dup (fileno (stdout));
1142          dup2 (fileno (stderr), fileno (stdout));
1143          if (strcasecmp ("all", orig) == 0)
1144            {
1145              if (int res =
1146                  recursReaddirF (m, n, conf_dir, NULL, saved_stdout, 0,
1147                                  printDescribeProcessForProcess) < 0)
1148                return res;
1149            }
1150          else
1151            {
1152              char *saveptr;
1153              char *tmps = strtok_r (orig, ",", &saveptr);
1154
1155              char buff[256];
1156              char buff1[1024];
1157              while (tmps != NULL)
1158                {
1159                  int hasVal = -1;
1160                  char *corig = zStrdup (tmps);
1161                  if (strstr (corig, ".") != NULL)
1162                    {
1163
1164                      parseIdentifier (m, conf_dir, corig, buff1);
1165                      map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1166                      if (tmpMap != NULL)
1167                        addToMap (request_inputs, "metapath", tmpMap->value);
1168                      map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier");
1169
1170                      s1 = (service *) malloc (SERVICE_SIZE);
1171                      t = readServiceFile (m, buff1, &s1, tmpMapI->value);
1172                      if (t < 0)
1173                        {
1174                          map *tmp00 = getMapFromMaps (m, "lenv", "message");
1175                          char tmp01[1024];
1176                          if (tmp00 != NULL)
1177                            sprintf (tmp01,
1178                                     _
1179                                     ("Unable to parse the ZCFG file for the following ZOO-Service: %s. Message: %s"),
1180                                     tmps, tmp00->value);
1181                          else
1182                            sprintf (tmp01,
1183                                     _
1184                                     ("Unable to parse the ZCFG file for the following ZOO-Service: %s."),
1185                                     tmps);
1186                          dup2 (saved_stdout, fileno (stdout));
1187                          errorException (m, tmp01, "InvalidParameterValue",
1188                                          "identifier");
1189                          freeMaps (&m);
1190                          free (m);
1191                          free (REQUEST);
1192                          free (corig);
1193                          free (orig);
1194                          free (SERVICE_URL);
1195                          free (s1);
1196                          closedir (dirp);
1197                          xmlFreeDoc (doc);
1198                          xmlCleanupParser ();
1199                          zooXmlCleanupNs ();
1200                          return 1;
1201                        }
1202#ifdef DEBUG
1203                      dumpService (s1);
1204#endif
1205                      printDescribeProcessForProcess (m, n, s1);
1206                      freeService (&s1);
1207                      free (s1);
1208                      s1 = NULL;
1209                      scount++;
1210                      hasVal = 1;
1211                      setMapInMaps (m, "lenv", "level", "0");
1212                    }
1213                  else
1214                    {
1215                      memset (buff, 0, 256);
1216                      snprintf (buff, 256, "%s.zcfg", corig);
1217                      memset (buff1, 0, 1024);
1218#ifdef DEBUG
1219                      printf ("\n#######%s\n########\n", buff);
1220#endif
1221                      while ((dp = readdir (dirp)) != NULL)
1222                        {
1223                          if (strcasecmp (dp->d_name, buff) == 0)
1224                            {
1225                              memset (buff1, 0, 1024);
1226                              snprintf (buff1, 1024, "%s/%s", conf_dir,
1227                                        dp->d_name);
1228                              s1 = (service *) malloc (SERVICE_SIZE);
1229                              if (s1 == NULL)
1230                                {
1231                                  dup2 (saved_stdout, fileno (stdout));
1232                                  return errorException (m,
1233                                                         _
1234                                                         ("Unable to allocate memory."),
1235                                                         "InternalError",
1236                                                         NULL);
1237                                }
1238#ifdef DEBUG
1239                              printf
1240                                ("#################\n(%s) %s\n#################\n",
1241                                 r_inputs->value, buff1);
1242#endif
1243                              char *tmp0 = zStrdup (dp->d_name);
1244                              tmp0[strlen (tmp0) - 5] = 0;
1245                              t = readServiceFile (m, buff1, &s1, tmp0);
1246                              free (tmp0);
1247                              if (t < 0)
1248                                {
1249                                  map *tmp00 =
1250                                    getMapFromMaps (m, "lenv", "message");
1251                                  char tmp01[1024];
1252                                  if (tmp00 != NULL)
1253                                    sprintf (tmp01,
1254                                             _
1255                                             ("Unable to parse the ZCFG file: %s (%s)"),
1256                                             dp->d_name, tmp00->value);
1257                                  else
1258                                    sprintf (tmp01,
1259                                             _
1260                                             ("Unable to parse the ZCFG file: %s."),
1261                                             dp->d_name);
1262                                  dup2 (saved_stdout, fileno (stdout));
1263                                  errorException (m, tmp01, "InternalError",
1264                                                  NULL);
1265                                  freeMaps (&m);
1266                                  free (m);
1267                                  free (orig);
1268                                  free (REQUEST);
1269                                  closedir (dirp);
1270                                  xmlFreeDoc (doc);
1271                                  xmlCleanupParser ();
1272                                  zooXmlCleanupNs ();
1273                                  return 1;
1274                                }
1275#ifdef DEBUG
1276                              dumpService (s1);
1277#endif
1278                              printDescribeProcessForProcess (m, n, s1);
1279                              freeService (&s1);
1280                              free (s1);
1281                              s1 = NULL;
1282                              scount++;
1283                              hasVal = 1;
1284                            }
1285                        }
1286                    }
1287                  if (hasVal < 0)
1288                    {
1289                      map *tmp00 = getMapFromMaps (m, "lenv", "message");
1290                      char tmp01[1024];
1291                      if (tmp00 != NULL)
1292                        sprintf (tmp01,
1293                                 _("Unable to parse the ZCFG file: %s (%s)"),
1294                                 buff, tmp00->value);
1295                      else
1296                        sprintf (tmp01,
1297                                 _("Unable to parse the ZCFG file: %s."),
1298                                 buff);
1299                      dup2 (saved_stdout, fileno (stdout));
1300                      errorException (m, tmp01, "InvalidParameterValue",
1301                                      "Identifier");
1302                      freeMaps (&m);
1303                      free (m);
1304                      free (orig);
1305                      free (REQUEST);
1306                      closedir (dirp);
1307                      xmlFreeDoc (doc);
1308                      xmlCleanupParser ();
1309                      zooXmlCleanupNs ();
1310                      return 1;
1311                    }
1312                  rewinddir (dirp);
1313                  tmps = strtok_r (NULL, ",", &saveptr);
1314                  if (corig != NULL)
1315                    free (corig);
1316                }
1317            }
1318          closedir (dirp);
1319          fflush (stdout);
1320          dup2 (saved_stdout, fileno (stdout));
1321          free (orig);
1322          printDocument (m, doc, getpid ());
1323          freeMaps (&m);
1324          free (m);
1325          free (REQUEST);
1326          free (SERVICE_URL);
1327          fflush (stdout);
1328          return 0;
1329        }
1330      else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0)
1331        {
1332          errorException (m,
1333                          _
1334                          ("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."),
1335                          "InvalidParameterValue", "request");
1336#ifdef DEBUG
1337          fprintf (stderr, "No request found %s", REQUEST);
1338#endif
1339          closedir (dirp);
1340          freeMaps (&m);
1341          free (m);
1342          free (REQUEST);
1343          free (SERVICE_URL);
1344          fflush (stdout);
1345          return 0;
1346        }
1347      closedir (dirp);
1348    }
1349
1350  s1 = NULL;
1351  s1 = (service *) malloc (SERVICE_SIZE);
1352  if (s1 == NULL)
1353    {
1354      freeMaps (&m);
1355      free (m);
1356      free (REQUEST);
1357      free (SERVICE_URL);
1358      return errorException (m, _("Unable to allocate memory."),
1359                             "InternalError", NULL);
1360    }
1361  r_inputs = getMap (request_inputs, "MetaPath");
1362  if (r_inputs != NULL)
1363    snprintf (tmps1, 1024, "%s/%s", ntmp, r_inputs->value);
1364  else
1365    snprintf (tmps1, 1024, "%s/", ntmp);
1366  r_inputs = getMap (request_inputs, "Identifier");
1367  char *ttmp = zStrdup (tmps1);
1368  snprintf (tmps1, 1024, "%s/%s.zcfg", ttmp, r_inputs->value);
1369  free (ttmp);
1370#ifdef DEBUG
1371  fprintf (stderr, "Trying to load %s\n", tmps1);
1372#endif
1373  if (strstr (r_inputs->value, ".") != NULL)
1374    {
1375      char *identifier = zStrdup (r_inputs->value);
1376      parseIdentifier (m, conf_dir, identifier, tmps1);
1377      map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1378      if (tmpMap != NULL)
1379        addToMap (request_inputs, "metapath", tmpMap->value);
1380      free (identifier);
1381    }
1382  else
1383    {
1384      setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
1385      setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value);
1386    }
1387
1388  r_inputs = getMapFromMaps (m, "lenv", "Identifier");
1389  int saved_stdout = dup (fileno (stdout));
1390  dup2 (fileno (stderr), fileno (stdout));
1391  t = readServiceFile (m, tmps1, &s1, r_inputs->value);
1392  fflush (stdout);
1393  dup2 (saved_stdout, fileno (stdout));
1394  if (t < 0)
1395    {
1396      char *tmpMsg = (char *) malloc (2048 + strlen (r_inputs->value));
1397      sprintf (tmpMsg,
1398               _
1399               ("The value for <identifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."),
1400               r_inputs->value);
1401      errorException (m, tmpMsg, "InvalidParameterValue", "identifier");
1402      free (tmpMsg);
1403      free (s1);
1404      freeMaps (&m);
1405      free (m);
1406      free (REQUEST);
1407      free (SERVICE_URL);
1408      return 0;
1409    }
1410  close (saved_stdout);
1411
1412#ifdef DEBUG
1413  dumpService (s1);
1414#endif
1415  int j;
1416
1417
1418  /**
1419   * Create the input and output maps data structure
1420   */
1421  int i = 0;
1422  HINTERNET hInternet;
1423  HINTERNET res;
1424  hInternet = InternetOpen (
1425#ifndef WIN32
1426                             (LPCTSTR)
1427#endif
1428                             "ZooWPSClient\0",
1429                             INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1430
1431#ifndef WIN32
1432  if (!CHECK_INET_HANDLE (hInternet))
1433    fprintf (stderr, "WARNING : hInternet handle failed to initialize");
1434#endif
1435  maps *request_input_real_format = NULL;
1436  maps *tmpmaps = request_input_real_format;
1437  map *postRequest = NULL;
1438  postRequest = getMap (request_inputs, "xrequest");
1439  if (postRequest == NULLMAP)
1440    {
1441    /**
1442     * Parsing outputs provided as KVP
1443     */
1444      r_inputs = NULL;
1445#ifdef DEBUG
1446      fprintf (stderr, "OUTPUT Parsing ... \n");
1447#endif
1448      r_inputs = getMap (request_inputs, "ResponseDocument");
1449      if (r_inputs == NULL)
1450        r_inputs = getMap (request_inputs, "RawDataOutput");
1451
1452#ifdef DEBUG
1453      fprintf (stderr, "OUTPUT Parsing ... \n");
1454#endif
1455      if (r_inputs != NULL)
1456        {
1457#ifdef DEBUG
1458          fprintf (stderr, "OUTPUT Parsing start now ... \n");
1459#endif
1460          char cursor_output[10240];
1461          char *cotmp = zStrdup (r_inputs->value);
1462          snprintf (cursor_output, 10240, "%s", cotmp);
1463          free (cotmp);
1464          j = 0;
1465
1466      /**
1467       * Put each Output into the outputs_as_text array
1468       */
1469          char *pToken;
1470          maps *tmp_output = NULL;
1471#ifdef DEBUG
1472          fprintf (stderr, "OUTPUT [%s]\n", cursor_output);
1473#endif
1474          pToken = strtok (cursor_output, ";");
1475          char **outputs_as_text = (char **) malloc (128 * sizeof (char *));
1476          if (outputs_as_text == NULL)
1477            {
1478              return errorException (m, _("Unable to allocate memory"),
1479                                     "InternalError", NULL);
1480            }
1481          i = 0;
1482          while (pToken != NULL)
1483            {
1484#ifdef DEBUG
1485              fprintf (stderr, "***%s***\n", pToken);
1486              fflush (stderr);
1487              fprintf (stderr, "***%s***\n", pToken);
1488#endif
1489              outputs_as_text[i] =
1490                (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
1491              if (outputs_as_text[i] == NULL)
1492                {
1493                  return errorException (m, _("Unable to allocate memory"),
1494                                         "InternalError", NULL);
1495                }
1496              snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s",
1497                        pToken);
1498              pToken = strtok (NULL, ";");
1499              i++;
1500            }
1501          for (j = 0; j < i; j++)
1502            {
1503              char *tmp = zStrdup (outputs_as_text[j]);
1504              free (outputs_as_text[j]);
1505              char *tmpc;
1506              tmpc = strtok (tmp, "@");
1507              int k = 0;
1508              while (tmpc != NULL)
1509                {
1510                  if (k == 0)
1511                    {
1512                      if (tmp_output == NULL)
1513                        {
1514                          tmp_output = (maps *) malloc (MAPS_SIZE);
1515                          if (tmp_output == NULL)
1516                            {
1517                              return errorException (m,
1518                                                     _
1519                                                     ("Unable to allocate memory."),
1520                                                     "InternalError", NULL);
1521                            }
1522                          tmp_output->name = zStrdup (tmpc);
1523                          tmp_output->content = NULL;
1524                          tmp_output->next = NULL;
1525                        }
1526                    }
1527                  else
1528                    {
1529                      char *tmpv = strstr (tmpc, "=");
1530                      char tmpn[256];
1531                      memset (tmpn, 0, 256);
1532                      strncpy (tmpn, tmpc,
1533                               (strlen (tmpc) -
1534                                strlen (tmpv)) * sizeof (char));
1535                      tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
1536#ifdef DEBUG
1537                      fprintf (stderr, "OUTPUT DEF [%s]=[%s]\n", tmpn,
1538                               tmpv + 1);
1539#endif
1540                      if (tmp_output->content == NULL)
1541                        {
1542                          tmp_output->content = createMap (tmpn, tmpv + 1);
1543                          tmp_output->content->next = NULL;
1544                        }
1545                      else
1546                        addToMap (tmp_output->content, tmpn, tmpv + 1);
1547                    }
1548                  k++;
1549#ifdef DEBUG
1550                  fprintf (stderr, "***%s***\n", tmpc);
1551#endif
1552                  tmpc = strtok (NULL, "@");
1553                }
1554              if (request_output_real_format == NULL)
1555                request_output_real_format = dupMaps (&tmp_output);
1556              else
1557                addMapsToMaps (&request_output_real_format, tmp_output);
1558              freeMaps (&tmp_output);
1559              free (tmp_output);
1560              tmp_output = NULL;
1561#ifdef DEBUG
1562              dumpMaps (tmp_output);
1563              fflush (stderr);
1564#endif
1565              free (tmp);
1566            }
1567          free (outputs_as_text);
1568        }
1569
1570
1571    /**
1572     * Parsing inputs provided as KVP
1573     */
1574      r_inputs = getMap (request_inputs, "DataInputs");
1575#ifdef DEBUG
1576      fprintf (stderr, "DATA INPUTS [%s]\n", r_inputs->value);
1577#endif
1578      char cursor_input[40960];
1579      if (r_inputs != NULL){
1580        snprintf (cursor_input, 40960, "%s", r_inputs->value);
1581        j = 0;
1582
1583        /**
1584         * Put each DataInputs into the inputs_as_text array
1585         */
1586        char *tmp1 = zStrdup (cursor_input);
1587        char *pToken;
1588        pToken = strtok (cursor_input, ";");
1589        if (pToken != NULL && strncasecmp (pToken, tmp1, strlen (tmp1)) == 0)
1590          {
1591            char *tmp2 = url_decode (tmp1);
1592            snprintf (cursor_input, (strlen (tmp2) + 1) * sizeof (char), "%s",
1593                      tmp2);
1594            free (tmp2);
1595            pToken = strtok (cursor_input, ";");
1596          }
1597        free (tmp1);
1598       
1599        char **inputs_as_text = (char **) malloc (100 * sizeof (char *));
1600        if (inputs_as_text == NULL)
1601          {
1602            return errorException (m, _("Unable to allocate memory."),
1603                                   "InternalError", NULL);
1604          }
1605        i = 0;
1606        while (pToken != NULL)
1607          {
1608#ifdef DEBUG
1609            fprintf (stderr, "***%s***\n", pToken);
1610            fflush (stderr);
1611            fprintf (stderr, "***%s***\n", pToken);
1612#endif
1613            inputs_as_text[i] =
1614              (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
1615            if (inputs_as_text[i] == NULL)
1616              {
1617                return errorException (m, _("Unable to allocate memory."),
1618                                       "InternalError", NULL);
1619              }
1620            snprintf (inputs_as_text[i], strlen (pToken) + 1, "%s", pToken);
1621            pToken = strtok (NULL, ";");
1622            i++;
1623          }
1624       
1625        for (j = 0; j < i; j++)
1626          {
1627            char *tmp = zStrdup (inputs_as_text[j]);
1628            free (inputs_as_text[j]);
1629            char *tmpc;
1630            tmpc = strtok (tmp, "@");
1631            while (tmpc != NULL)
1632              {
1633#ifdef DEBUG
1634                fprintf (stderr, "***\n***%s***\n", tmpc);
1635#endif
1636                char *tmpv = strstr (tmpc, "=");
1637                char tmpn[256];
1638                memset (tmpn, 0, 256);
1639                if (tmpv != NULL)
1640                  {
1641                    strncpy (tmpn, tmpc,
1642                             (strlen (tmpc) - strlen (tmpv)) * sizeof (char));
1643                    tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
1644                  }
1645                else
1646                  {
1647                    strncpy (tmpn, tmpc, strlen (tmpc) * sizeof (char));
1648                    tmpn[strlen (tmpc)] = 0;
1649                  }
1650#ifdef DEBUG
1651                fprintf (stderr, "***\n*** %s = %s ***\n", tmpn, tmpv + 1);
1652#endif
1653                if (tmpmaps == NULL)
1654                  {
1655                    tmpmaps = (maps *) malloc (MAPS_SIZE);
1656                    if (tmpmaps == NULL)
1657                      {
1658                        return errorException (m,
1659                                               _("Unable to allocate memory."),
1660                                               "InternalError", NULL);
1661                      }
1662                    tmpmaps->name = zStrdup (tmpn);
1663                    if (tmpv != NULL)
1664                      {
1665                        char *tmpvf = url_decode (tmpv + 1);
1666                        tmpmaps->content = createMap ("value", tmpvf);
1667                        free (tmpvf);
1668                      }
1669                    else
1670                      tmpmaps->content = createMap ("value", "Reference");
1671                    tmpmaps->next = NULL;
1672                  }
1673                tmpc = strtok (NULL, "@");
1674                while (tmpc != NULL)
1675                  {
1676#ifdef DEBUG
1677                    fprintf (stderr, "*** KVP NON URL-ENCODED \n***%s***\n",
1678                             tmpc);
1679#endif
1680                    char *tmpv1 = strstr (tmpc, "=");
1681#ifdef DEBUG
1682                    fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n",
1683                             tmpv1 + 1);
1684#endif
1685                    char tmpn1[1024];
1686                    memset (tmpn1, 0, 1024);
1687                    if (tmpv1 != NULL)
1688                      {
1689                        strncpy (tmpn1, tmpc, strlen (tmpc) - strlen (tmpv1));
1690                        tmpn1[strlen (tmpc) - strlen (tmpv1)] = 0;
1691                        addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
1692                      }
1693                    else
1694                      {
1695                        strncpy (tmpn1, tmpc, strlen (tmpc));
1696                        tmpn1[strlen (tmpc)] = 0;
1697                        map *lmap = getLastMap (tmpmaps->content);
1698                        char *tmpValue =
1699                          (char *) malloc ((strlen (tmpv) + strlen (tmpc) + 1) *
1700                                           sizeof (char));
1701                        sprintf (tmpValue, "%s@%s", tmpv + 1, tmpc);
1702                        free (lmap->value);
1703                        lmap->value = zStrdup (tmpValue);
1704                        free (tmpValue);
1705                        tmpc = strtok (NULL, "@");
1706                        continue;
1707                      }
1708#ifdef DEBUG
1709                    fprintf (stderr, "*** NAME NON URL-ENCODED \n***%s***\n",
1710                             tmpn1);
1711                    fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n",
1712                             tmpv1 + 1);
1713#endif
1714                    if (strcmp (tmpn1, "xlink:href") != 0)
1715                      addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
1716                    else if (tmpv1 != NULL)
1717                      {
1718                        char *tmpx2 = url_decode (tmpv1 + 1);
1719                        if (strncasecmp (tmpx2, "http://", 7) != 0 &&
1720                            strncasecmp (tmpx2, "ftp://", 6) != 0 &&
1721                            strncasecmp (tmpx2, "https://", 8) != 0)
1722                          {
1723                            char emsg[1024];
1724                            sprintf (emsg,
1725                                     _
1726                                     ("Unable to find a valid protocol to download the remote file %s"),
1727                                     tmpv1 + 1);
1728                            errorException (m, emsg, "InternalError", NULL);
1729                            freeMaps (&m);
1730                            free (m);
1731                            free (REQUEST);
1732                            free (SERVICE_URL);
1733                            InternetCloseHandle (&hInternet);
1734                            freeService (&s1);
1735                            free (s1);
1736                            return 0;
1737                          }
1738#ifdef DEBUG
1739                        fprintf (stderr,
1740                                 "REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",
1741                                 tmpv1 + 1);
1742#endif
1743                        addToMap (tmpmaps->content, tmpn1, tmpx2);
1744#ifndef WIN32
1745                        if (CHECK_INET_HANDLE (hInternet))
1746#endif
1747                          {
1748                            if (loadRemoteFile
1749                                (&m, &tmpmaps->content, &hInternet, tmpx2) < 0)
1750                              {
1751                                freeMaps (&m);
1752                                free (m);
1753                                free (REQUEST);
1754                                free (SERVICE_URL);
1755                                InternetCloseHandle (&hInternet);
1756                                freeService (&s1);
1757                                free (s1);
1758                                return 0;
1759                              }
1760                          }
1761                        free (tmpx2);
1762                        addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
1763                      }
1764                    tmpc = strtok (NULL, "@");
1765                  }
1766#ifdef DEBUG
1767                dumpMaps (tmpmaps);
1768                fflush (stderr);
1769#endif
1770                if (request_input_real_format == NULL)
1771                  request_input_real_format = dupMaps (&tmpmaps);
1772                else
1773                  {
1774                    maps *testPresence =
1775                      getMaps (request_input_real_format, tmpmaps->name);
1776                    if (testPresence != NULL)
1777                      {
1778                        elements *elem =
1779                          getElements (s1->inputs, tmpmaps->name);
1780                        if (elem != NULL)
1781                          {
1782                            if (appendMapsToMaps
1783                                (m, request_input_real_format, tmpmaps,
1784                                 elem) < 0)
1785                              {
1786                                freeMaps (&m);
1787                                free (m);
1788                                free (REQUEST);
1789                                free (SERVICE_URL);
1790                                InternetCloseHandle (&hInternet);
1791                                freeService (&s1);
1792                                free (s1);
1793                                return 0;
1794                              }
1795                          }
1796                      }
1797                    else
1798                      addMapsToMaps (&request_input_real_format, tmpmaps);
1799                  }
1800                freeMaps (&tmpmaps);
1801                free (tmpmaps);
1802                tmpmaps = NULL;
1803                free (tmp);
1804              }
1805          }
1806        free (inputs_as_text);
1807      }
1808    }
1809  else
1810    {
1811    /**
1812     * Parse XML request
1813     */
1814      xmlInitParser ();
1815#ifdef DEBUG
1816      fflush (stderr);
1817      fprintf (stderr, "BEFORE %s\n", postRequest->value);
1818      fflush (stderr);
1819#endif
1820      xmlDocPtr doc = xmlParseMemory (postRequest->value, cgiContentLength);
1821#ifdef DEBUG
1822      fprintf (stderr, "AFTER\n");
1823      fflush (stderr);
1824#endif
1825    /**
1826     * Parse every Input in DataInputs node.
1827     */
1828      xmlXPathObjectPtr tmpsptr =
1829        extractFromDoc (doc, "/*/*/*[local-name()='Input']");
1830      xmlNodeSet *tmps = tmpsptr->nodesetval;
1831#ifdef DEBUG
1832      fprintf (stderr, "*****%d*****\n", tmps->nodeNr);
1833#endif
1834      for (int k = 0; k < tmps->nodeNr; k++)
1835        {
1836          maps *tmpmaps = NULL;
1837          xmlNodePtr cur = tmps->nodeTab[k];
1838          if (tmps->nodeTab[k]->type == XML_ELEMENT_NODE)
1839            {
1840        /**
1841         * A specific Input node.
1842         */
1843#ifdef DEBUG
1844              fprintf (stderr, "= element 0 node \"%s\"\n", cur->name);
1845#endif
1846              xmlNodePtr cur2 = cur->children;
1847              while (cur2 != NULL)
1848                {
1849                  while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1850                    cur2 = cur2->next;
1851                  if (cur2 == NULL)
1852                    break;
1853          /**
1854           * Indentifier
1855           */
1856                  if (xmlStrncasecmp
1857                      (cur2->name, BAD_CAST "Identifier",
1858                       xmlStrlen (cur2->name)) == 0)
1859                    {
1860                      xmlChar *val =
1861                        xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
1862                      if (tmpmaps == NULL)
1863                        {
1864                          tmpmaps = (maps *) malloc (MAPS_SIZE);
1865                          if (tmpmaps == NULL)
1866                            {
1867                              return errorException (m,
1868                                                     _
1869                                                     ("Unable to allocate memory."),
1870                                                     "InternalError", NULL);
1871                            }
1872                          tmpmaps->name = zStrdup ((char *) val);
1873                          tmpmaps->content = NULL;
1874                          tmpmaps->next = NULL;
1875                        }
1876                      xmlFree (val);
1877                    }
1878          /**
1879           * Title, Asbtract
1880           */
1881                  if (xmlStrncasecmp
1882                      (cur2->name, BAD_CAST "Title",
1883                       xmlStrlen (cur2->name)) == 0
1884                      || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract",
1885                                         xmlStrlen (cur2->name)) == 0)
1886                    {
1887                      xmlChar *val =
1888                        xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
1889                      if (tmpmaps == NULL)
1890                        {
1891                          tmpmaps = (maps *) malloc (MAPS_SIZE);
1892                          if (tmpmaps == NULL)
1893                            {
1894                              return errorException (m,
1895                                                     _
1896                                                     ("Unable to allocate memory."),
1897                                                     "InternalError", NULL);
1898                            }
1899                          tmpmaps->name = zStrdup ("missingIndetifier");
1900                          tmpmaps->content =
1901                            createMap ((char *) cur2->name, (char *) val);
1902                          tmpmaps->next = NULL;
1903                        }
1904                      else
1905                        {
1906                          if (tmpmaps->content != NULL)
1907                            addToMap (tmpmaps->content,
1908                                      (char *) cur2->name, (char *) val);
1909                          else
1910                            tmpmaps->content =
1911                              createMap ((char *) cur2->name, (char *) val);
1912                        }
1913#ifdef DEBUG
1914                      dumpMaps (tmpmaps);
1915#endif
1916                      xmlFree (val);
1917                    }
1918          /**
1919           * InputDataFormChoice (Reference or Data ?)
1920           */
1921                  if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0)
1922                    {
1923            /**
1924             * Get every attribute from a Reference node
1925             * mimeType, encoding, schema, href, method
1926             * Header and Body gesture should be added here
1927             */
1928#ifdef DEBUG
1929                      fprintf (stderr, "REFERENCE\n");
1930#endif
1931                      const char *refs[5] =
1932                        { "mimeType", "encoding", "schema", "method",
1933                        "href"
1934                      };
1935                      for (int l = 0; l < 5; l++)
1936                        {
1937#ifdef DEBUG
1938                          fprintf (stderr, "*** %s ***", refs[l]);
1939#endif
1940                          xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]);
1941                          if (val != NULL && xmlStrlen (val) > 0)
1942                            {
1943                              if (tmpmaps->content != NULL)
1944                                addToMap (tmpmaps->content, refs[l],
1945                                          (char *) val);
1946                              else
1947                                tmpmaps->content =
1948                                  createMap (refs[l], (char *) val);
1949                              map *ltmp = getMap (tmpmaps->content, "method");
1950                              if (l == 4)
1951                                {
1952                                  if (!
1953                                      (ltmp != NULL
1954                                       && strncmp (ltmp->value, "POST",
1955                                                   4) == 0)
1956                                      && CHECK_INET_HANDLE (hInternet))
1957                                    {
1958                                      if (loadRemoteFile
1959                                          (&m, &tmpmaps->content, &hInternet,
1960                                           (char *) val) != 0)
1961                                        {
1962                                          freeMaps (&m);
1963                                          free (m);
1964                                          free (REQUEST);
1965                                          free (SERVICE_URL);
1966                                          InternetCloseHandle (&hInternet);
1967                                          freeService (&s1);
1968                                          free (s1);
1969                                          return 0;
1970                                        }
1971                                    }
1972                                }
1973                            }
1974#ifdef DEBUG
1975                          fprintf (stderr, "%s\n", val);
1976#endif
1977                          xmlFree (val);
1978                        }
1979#ifdef POST_DEBUG
1980                      fprintf (stderr,
1981                               "Parse Header and Body from Reference \n");
1982#endif
1983                      xmlNodePtr cur3 = cur2->children;
1984                      /*      HINTERNET hInternetP;
1985                         hInternetP=InternetOpen(
1986                         #ifndef WIN32
1987                         (LPCTSTR)
1988                         #endif
1989                         "ZooWPSClient\0",
1990                         INTERNET_OPEN_TYPE_PRECONFIG,
1991                         NULL,NULL, 0); */
1992                      //hInternet.ihandle[hInternet.nb].header=NULL;
1993                      while (cur3 != NULL)
1994                        {
1995                          while (cur3 != NULL
1996                                 && cur3->type != XML_ELEMENT_NODE)
1997                            cur3 = cur3->next;
1998                          if (cur3 == NULL)
1999                            break;
2000                          if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
2001                              0)
2002                            {
2003                              const char *ha[2];
2004                              ha[0] = "key";
2005                              ha[1] = "value";
2006                              int hai;
2007                              char *has;
2008                              char *key;
2009                              for (hai = 0; hai < 2; hai++)
2010                                {
2011                                  xmlChar *val =
2012                                    xmlGetProp (cur3, BAD_CAST ha[hai]);
2013#ifdef POST_DEBUG
2014                                  fprintf (stderr, "%s = %s\n", ha[hai],
2015                                           (char *) val);
2016#endif
2017                                  if (hai == 0)
2018                                    {
2019                                      key = zStrdup ((char *) val);
2020                                    }
2021                                  else
2022                                    {
2023                                      has =
2024                                        (char *)
2025                                        malloc ((4 + xmlStrlen (val) +
2026                                                 strlen (key)) *
2027                                                sizeof (char));
2028                                      if (has == NULL)
2029                                        {
2030                                          return errorException (m,
2031                                                                 _
2032                                                                 ("Unable to allocate memory."),
2033                                                                 "InternalError",
2034                                                                 NULL);
2035                                        }
2036                                      snprintf (has,
2037                                                (3 + xmlStrlen (val) +
2038                                                 strlen (key)), "%s: %s", key,
2039                                                (char *) val);
2040                                      free (key);
2041#ifdef POST_DEBUG
2042                                      fprintf (stderr, "%s\n", has);
2043#endif
2044                                    }
2045                                  xmlFree (val);
2046                                }
2047                              hInternet.ihandle[hInternet.nb].header =
2048                                curl_slist_append (hInternet.ihandle
2049                                                   [hInternet.nb].header,
2050                                                   has);
2051                              if (has != NULL)
2052                                free (has);
2053                            }
2054                          else
2055                            {
2056#ifdef POST_DEBUG
2057                              fprintf (stderr,
2058                                       "Try to fetch the body part of the request ...\n");
2059#endif
2060                              if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
2061                                  == 0)
2062                                {
2063#ifdef POST_DEBUG
2064                                  fprintf (stderr, "Body part found !!!\n",
2065                                           (char *) cur3->content);
2066#endif
2067                                  char *tmp =
2068                                    (char *) malloc (cgiContentLength +
2069                                                     1 * sizeof (char));
2070                                  memset (tmp, 0, cgiContentLength);
2071                                  xmlNodePtr cur4 = cur3->children;
2072                                  while (cur4 != NULL)
2073                                    {
2074                                      while (cur4->type != XML_ELEMENT_NODE)
2075                                        cur4 = cur4->next;
2076                                      xmlDocPtr bdoc =
2077                                        xmlNewDoc (BAD_CAST "1.0");
2078                                      bdoc->encoding =
2079                                        xmlCharStrdup ("UTF-8");
2080                                      xmlDocSetRootElement (bdoc, cur4);
2081                                      xmlChar *btmps;
2082                                      int bsize;
2083                                      xmlDocDumpMemory (bdoc, &btmps, &bsize);
2084#ifdef POST_DEBUG
2085                                      fprintf (stderr,
2086                                               "Body part found !!! %s %s\n",
2087                                               tmp, (char *) btmps);
2088#endif
2089                                      if (btmps != NULL)
2090                                        sprintf (tmp, "%s", (char *) btmps);
2091                                      xmlFree (btmps);
2092                                      cur4 = cur4->next;
2093                                      xmlFreeDoc (bdoc);
2094                                    }
2095                                  map *btmp =
2096                                    getMap (tmpmaps->content, "href");
2097                                  if (btmp != NULL)
2098                                    {
2099#ifdef POST_DEBUG
2100                                      fprintf (stderr, "%s %s\n", btmp->value,
2101                                               tmp);
2102                                      curl_easy_setopt (hInternet.handle,
2103                                                        CURLOPT_VERBOSE, 1);
2104#endif
2105                                      hInternet.
2106                                        waitingRequests[hInternet.nb] =
2107                                        strdup (tmp);
2108                                      InternetOpenUrl (&hInternet,
2109                                                       btmp->value,
2110                                                       hInternet.waitingRequests
2111                                                       [hInternet.nb],
2112                                                       strlen
2113                                                       (hInternet.waitingRequests
2114                                                        [hInternet.nb]),
2115                                                       INTERNET_FLAG_NO_CACHE_WRITE,
2116                                                       0);
2117                                    }
2118                                  free (tmp);
2119                                }
2120                              else
2121                                if (xmlStrcasecmp
2122                                    (cur3->name,
2123                                     BAD_CAST "BodyReference") == 0)
2124                                {
2125                                  xmlChar *val =
2126                                    xmlGetProp (cur3, BAD_CAST "href");
2127                                  HINTERNET bInternet, res1;
2128                                  bInternet = InternetOpen (
2129#ifndef WIN32
2130                                                             (LPCTSTR)
2131#endif
2132                                                             "ZooWPSClient\0",
2133                                                             INTERNET_OPEN_TYPE_PRECONFIG,
2134                                                             NULL, NULL, 0);
2135                                  if (!CHECK_INET_HANDLE (hInternet))
2136                                    fprintf (stderr,
2137                                             "WARNING : hInternet handle failed to initialize");
2138#ifdef POST_DEBUG
2139                                  curl_easy_setopt (bInternet.handle,
2140                                                    CURLOPT_VERBOSE, 1);
2141#endif
2142                                  bInternet.waitingRequests[0] =
2143                                    strdup ((char *) val);
2144                                  res1 =
2145                                    InternetOpenUrl (&bInternet,
2146                                                     bInternet.waitingRequests
2147                                                     [0], NULL, 0,
2148                                                     INTERNET_FLAG_NO_CACHE_WRITE,
2149                                                     0);
2150                                  processDownloads (&bInternet);
2151                                  char *tmp =
2152                                    (char *)
2153                                    malloc ((bInternet.ihandle[0].nDataLen +
2154                                             1) * sizeof (char));
2155                                  if (tmp == NULL)
2156                                    {
2157                                      return errorException (m,
2158                                                             _
2159                                                             ("Unable to allocate memory."),
2160                                                             "InternalError",
2161                                                             NULL);
2162                                    }
2163                                  size_t bRead;
2164                                  InternetReadFile (bInternet.ihandle[0],
2165                                                    (LPVOID) tmp,
2166                                                    bInternet.
2167                                                    ihandle[0].nDataLen,
2168                                                    &bRead);
2169                                  tmp[bInternet.ihandle[0].nDataLen] = 0;
2170                                  InternetCloseHandle (&bInternet);
2171                                  map *btmp =
2172                                    getMap (tmpmaps->content, "href");
2173                                  if (btmp != NULL)
2174                                    {
2175#ifdef POST_DEBUG
2176                                      fprintf (stderr, "%s %s\n", btmp->value,
2177                                               tmp);
2178#endif
2179                                      hInternet.
2180                                        waitingRequests[hInternet.nb] =
2181                                        strdup (tmp);
2182                                      res =
2183                                        InternetOpenUrl (&hInternet,
2184                                                         btmp->value,
2185                                                         hInternet.waitingRequests
2186                                                         [hInternet.nb],
2187                                                         strlen
2188                                                         (hInternet.waitingRequests
2189                                                          [hInternet.nb]),
2190                                                         INTERNET_FLAG_NO_CACHE_WRITE,
2191                                                         0);
2192                                    }
2193                                  free (tmp);
2194                                }
2195                            }
2196                          cur3 = cur3->next;
2197                        }
2198#ifdef POST_DEBUG
2199                      fprintf (stderr,
2200                               "Header and Body was parsed from Reference \n");
2201#endif
2202#ifdef DEBUG
2203                      dumpMap (tmpmaps->content);
2204                      fprintf (stderr, "= element 2 node \"%s\" = (%s)\n",
2205                               cur2->name, cur2->content);
2206#endif
2207                    }
2208                  else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
2209                    {
2210#ifdef DEBUG
2211                      fprintf (stderr, "DATA\n");
2212#endif
2213                      xmlNodePtr cur4 = cur2->children;
2214                      while (cur4 != NULL)
2215                        {
2216                          while (cur4 != NULL
2217                                 && cur4->type != XML_ELEMENT_NODE)
2218                            cur4 = cur4->next;
2219                          if (cur4 == NULL)
2220                            break;
2221                          if (xmlStrcasecmp
2222                              (cur4->name, BAD_CAST "LiteralData") == 0)
2223                            {
2224                /**
2225                 * Get every attribute from a LiteralData node
2226                 * dataType , uom
2227                 */
2228                              char *list[2];
2229                              list[0] = zStrdup ("dataType");
2230                              list[1] = zStrdup ("uom");
2231                              for (int l = 0; l < 2; l++)
2232                                {
2233#ifdef DEBUG
2234                                  fprintf (stderr, "*** LiteralData %s ***",
2235                                           list[l]);
2236#endif
2237                                  xmlChar *val =
2238                                    xmlGetProp (cur4, BAD_CAST list[l]);
2239                                  if (val != NULL
2240                                      && strlen ((char *) val) > 0)
2241                                    {
2242                                      if (tmpmaps->content != NULL)
2243                                        addToMap (tmpmaps->content, list[l],
2244                                                  (char *) val);
2245                                      else
2246                                        tmpmaps->content =
2247                                          createMap (list[l], (char *) val);
2248#ifdef DEBUG
2249                                      fprintf (stderr, "%s\n", val);
2250#endif
2251                                    }
2252                                  xmlFree (val);
2253                                  free (list[l]);
2254                                }
2255                            }
2256                          else
2257                            if (xmlStrcasecmp
2258                                (cur4->name, BAD_CAST "ComplexData") == 0)
2259                            {
2260                /**
2261                 * Get every attribute from a Reference node
2262                 * mimeType, encoding, schema
2263                 */
2264                              const char *coms[3] =
2265                                { "mimeType", "encoding", "schema" };
2266                              for (int l = 0; l < 3; l++)
2267                                {
2268#ifdef DEBUG
2269                                  fprintf (stderr, "*** ComplexData %s ***\n",
2270                                           coms[l]);
2271#endif
2272                                  xmlChar *val =
2273                                    xmlGetProp (cur4, BAD_CAST coms[l]);
2274                                  if (val != NULL
2275                                      && strlen ((char *) val) > 0)
2276                                    {
2277                                      if (tmpmaps->content != NULL)
2278                                        addToMap (tmpmaps->content, coms[l],
2279                                                  (char *) val);
2280                                      else
2281                                        tmpmaps->content =
2282                                          createMap (coms[l], (char *) val);
2283#ifdef DEBUG
2284                                      fprintf (stderr, "%s\n", val);
2285#endif
2286                                    }
2287                                  xmlFree (val);
2288                                }
2289                            }
2290
2291                          map *test = getMap (tmpmaps->content, "encoding");
2292                          if (test == NULL)
2293                            {
2294                              if (tmpmaps->content != NULL)
2295                                addToMap (tmpmaps->content, "encoding",
2296                                          "utf-8");
2297                              else
2298                                tmpmaps->content =
2299                                  createMap ("encoding", "utf-8");
2300                              test = getMap (tmpmaps->content, "encoding");
2301                            }
2302
2303                          if (strcasecmp (test->value, "base64") != 0)
2304                            {
2305                              xmlChar *mv = xmlNodeListGetString (doc,
2306                                                                  cur4->
2307                                                                  xmlChildrenNode,
2308                                                                  1);
2309                              map *ltmp =
2310                                getMap (tmpmaps->content, "mimeType");
2311                              if (mv == NULL
2312                                  ||
2313                                  (xmlStrcasecmp
2314                                   (cur4->name, BAD_CAST "ComplexData") == 0
2315                                   && (ltmp == NULL
2316                                       || strncasecmp (ltmp->value,
2317                                                       "text/xml", 8) == 0)))
2318                                {
2319                                  xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
2320                                  int buffersize;
2321                                  xmlNodePtr cur5 = cur4->children;
2322                                  while (cur5 != NULL
2323                                         && cur5->type != XML_ELEMENT_NODE
2324                                         && cur5->type !=
2325                                         XML_CDATA_SECTION_NODE)
2326                                    cur5 = cur5->next;
2327                                  if (cur5 != NULL
2328                                      && cur5->type != XML_CDATA_SECTION_NODE)
2329                                    {
2330                                      xmlDocSetRootElement (doc1, cur5);
2331                                      xmlDocDumpFormatMemoryEnc (doc1, &mv,
2332                                                                 &buffersize,
2333                                                                 "utf-8", 1);
2334                                      char size[1024];
2335                                      sprintf (size, "%d", buffersize);
2336                                      addToMap (tmpmaps->content, "size",
2337                                                size);
2338                                      xmlFreeDoc (doc1);
2339                                    }
2340                                }
2341                              if (mv != NULL)
2342                                {
2343                                  addToMap (tmpmaps->content, "value",
2344                                            (char *) mv);
2345                                  xmlFree (mv);
2346                                }
2347                            }
2348                          else
2349                            {
2350                              xmlChar *tmp = xmlNodeListGetRawString (doc,
2351                                                                      cur4->xmlChildrenNode,
2352                                                                      0);
2353                              addToMap (tmpmaps->content, "value",
2354                                        (char *) tmp);
2355                              map *tmpv = getMap (tmpmaps->content, "value");
2356                              char *res = NULL;
2357                              char *curs = tmpv->value;
2358                              for (int i = 0; i <= strlen (tmpv->value) / 64;
2359                                   i++)
2360                                {
2361                                  if (res == NULL)
2362                                    res =
2363                                      (char *) malloc (67 * sizeof (char));
2364                                  else
2365                                    res =
2366                                      (char *) realloc (res,
2367                                                        (((i + 1) * 65) +
2368                                                         i) * sizeof (char));
2369                                  int csize = i * 65;
2370                                  strncpy (res + csize, curs, 64);
2371                                  if (i == xmlStrlen (tmp) / 64)
2372                                    strcat (res, "\n\0");
2373                                  else
2374                                    {
2375                                      strncpy (res + (((i + 1) * 64) + i),
2376                                               "\n\0", 2);
2377                                      curs += 64;
2378                                    }
2379                                }
2380                              free (tmpv->value);
2381                              tmpv->value = zStrdup (res);
2382                              free (res);
2383                              xmlFree (tmp);
2384                            }
2385                          cur4 = cur4->next;
2386                        }
2387                    }
2388#ifdef DEBUG
2389                  fprintf (stderr, "cur2 next \n");
2390                  fflush (stderr);
2391#endif
2392                  cur2 = cur2->next;
2393                }
2394#ifdef DEBUG
2395              fprintf (stderr, "ADD MAPS TO REQUEST MAPS !\n");
2396              fflush (stderr);
2397#endif
2398
2399              {
2400                maps *testPresence =
2401                  getMaps (request_input_real_format, tmpmaps->name);
2402                if (testPresence != NULL)
2403                  {
2404                    elements *elem = getElements (s1->inputs, tmpmaps->name);
2405                    if (elem != NULL)
2406                      {
2407                        if (appendMapsToMaps
2408                            (m, request_input_real_format, tmpmaps, elem) < 0)
2409                          {
2410                            freeMaps (&m);
2411                            free (m);
2412                            free (REQUEST);
2413                            free (SERVICE_URL);
2414                            InternetCloseHandle (&hInternet);
2415                            freeService (&s1);
2416                            free (s1);
2417                            return 0;
2418                          }
2419                      }
2420                  }
2421                else
2422                  addMapsToMaps (&request_input_real_format, tmpmaps);
2423              }
2424
2425#ifdef DEBUG
2426              fprintf (stderr, "******TMPMAPS*****\n");
2427              dumpMaps (tmpmaps);
2428              fprintf (stderr, "******REQUESTMAPS*****\n");
2429              dumpMaps (request_input_real_format);
2430#endif
2431              freeMaps (&tmpmaps);
2432              free (tmpmaps);
2433              tmpmaps = NULL;
2434            }
2435#ifdef DEBUG
2436          dumpMaps (tmpmaps);
2437#endif
2438        }
2439#ifdef DEBUG
2440      fprintf (stderr, "Search for response document node\n");
2441#endif
2442      xmlXPathFreeObject (tmpsptr);
2443
2444      tmpsptr =
2445        extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
2446      bool asRaw = false;
2447      tmps = tmpsptr->nodesetval;
2448      if (tmps->nodeNr == 0)
2449        {
2450          xmlXPathFreeObject (tmpsptr);
2451          tmpsptr =
2452            extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
2453          tmps = tmpsptr->nodesetval;
2454          asRaw = true;
2455        }
2456#ifdef DEBUG
2457      fprintf (stderr, "*****%d*****\n", tmps->nodeNr);
2458#endif
2459      if (asRaw == true)
2460        {
2461          addToMap (request_inputs, "RawDataOutput", "");
2462          xmlNodePtr cur0 = tmps->nodeTab[0];
2463          if (cur0->type == XML_ELEMENT_NODE)
2464            {
2465
2466              maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
2467              if (tmpmaps == NULL)
2468                {
2469                  return errorException (m, _("Unable to allocate memory."),
2470                                         "InternalError", NULL);
2471                }
2472              tmpmaps->name = zStrdup ("unknownIdentifier");
2473              tmpmaps->content = NULL;
2474              tmpmaps->next = NULL;
2475
2476        /**
2477         * Get every attribute from a RawDataOutput node
2478         * mimeType, encoding, schema, uom
2479         */
2480              const char *outs[4] =
2481                { "mimeType", "encoding", "schema", "uom" };
2482              for (int l = 0; l < 4; l++)
2483                {
2484#ifdef DEBUG
2485                  fprintf (stderr, "*** %s ***\t", outs[l]);
2486#endif
2487                  xmlChar *val = xmlGetProp (cur0, BAD_CAST outs[l]);
2488                  if (val != NULL)
2489                    {
2490                      if (strlen ((char *) val) > 0)
2491                        {
2492                          if (tmpmaps->content != NULL)
2493                            addToMap (tmpmaps->content, outs[l],
2494                                      (char *) val);
2495                          else
2496                            tmpmaps->content =
2497                              createMap (outs[l], (char *) val);
2498                        }
2499                      xmlFree (val);
2500                    }
2501                }
2502              xmlNodePtr cur2 = cur0->children;
2503              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
2504                cur2 = cur2->next;
2505              while (cur2 != NULL)
2506                {
2507                  if (xmlStrncasecmp
2508                      (cur2->name, BAD_CAST "Identifier",
2509                       xmlStrlen (cur2->name)) == 0)
2510                    {
2511                      xmlChar *val =
2512                        xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
2513                      free (tmpmaps->name);
2514                      tmpmaps->name = zStrdup ((char *) val);
2515                      xmlFree (val);
2516                    }
2517                  cur2 = cur2->next;
2518                  while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
2519                    cur2 = cur2->next;
2520                }
2521              if (request_output_real_format == NULL)
2522                request_output_real_format = dupMaps (&tmpmaps);
2523              else
2524                addMapsToMaps (&request_output_real_format, tmpmaps);
2525              if (tmpmaps != NULL)
2526                {
2527                  freeMaps (&tmpmaps);
2528                  free (tmpmaps);
2529                  tmpmaps = NULL;
2530                }
2531            }
2532        }
2533      else
2534        for (int k = 0; k < tmps->nodeNr; k++)
2535          {
2536            //else
2537            addToMap (request_inputs, "ResponseDocument", "");
2538            maps *tmpmaps = NULL;
2539            xmlNodePtr cur = tmps->nodeTab[k];
2540            if (cur->type == XML_ELEMENT_NODE)
2541              {
2542          /**
2543           * A specific responseDocument node.
2544           */
2545                if (tmpmaps == NULL)
2546                  {
2547                    tmpmaps = (maps *) malloc (MAPS_SIZE);
2548                    if (tmpmaps == NULL)
2549                      {
2550                        return errorException (m,
2551                                               _
2552                                               ("Unable to allocate memory."),
2553                                               "InternalError", NULL);
2554                      }
2555                    tmpmaps->name = zStrdup ("unknownIdentifier");
2556                    tmpmaps->content = NULL;
2557                    tmpmaps->next = NULL;
2558                  }
2559          /**
2560           * Get every attribute: storeExecuteResponse, lineage, status
2561           */
2562                const char *ress[3] =
2563                  { "storeExecuteResponse", "lineage", "status" };
2564                xmlChar *val;
2565                for (int l = 0; l < 3; l++)
2566                  {
2567#ifdef DEBUG
2568                    fprintf (stderr, "*** %s ***\t", ress[l]);
2569#endif
2570                    val = xmlGetProp (cur, BAD_CAST ress[l]);
2571                    if (val != NULL && strlen ((char *) val) > 0)
2572                      {
2573                        if (tmpmaps->content != NULL)
2574                          addToMap (tmpmaps->content, ress[l], (char *) val);
2575                        else
2576                          tmpmaps->content =
2577                            createMap (ress[l], (char *) val);
2578                        addToMap (request_inputs, ress[l], (char *) val);
2579                      }
2580#ifdef DEBUG
2581                    fprintf (stderr, "%s\n", val);
2582#endif
2583                    xmlFree (val);
2584                  }
2585                xmlNodePtr cur1 = cur->children;
2586                while (cur1 != NULL && cur1->type != XML_ELEMENT_NODE)
2587                  cur1 = cur1->next;
2588                int cur1cnt = 0;
2589                while (cur1)
2590                  {
2591            /**
2592             * Indentifier
2593             */
2594                    if (xmlStrncasecmp
2595                        (cur1->name, BAD_CAST "Identifier",
2596                         xmlStrlen (cur1->name)) == 0)
2597                      {
2598                        xmlChar *val =
2599                          xmlNodeListGetString (doc, cur1->xmlChildrenNode,
2600                                                1);
2601                        if (tmpmaps == NULL)
2602                          {
2603                            tmpmaps = (maps *) malloc (MAPS_SIZE);
2604                            if (tmpmaps == NULL)
2605                              {
2606                                return errorException (m,
2607                                                       _
2608                                                       ("Unable to allocate memory."),
2609                                                       "InternalError", NULL);
2610                              }
2611                            tmpmaps->name = zStrdup ((char *) val);
2612                            tmpmaps->content = NULL;
2613                            tmpmaps->next = NULL;
2614                          }
2615                        else
2616                          {
2617                            free (tmpmaps->name);
2618                            tmpmaps->name = zStrdup ((char *) val);
2619                          }
2620                        if (asRaw == true)
2621                          addToMap (request_inputs, "RawDataOutput",
2622                                    (char *) val);
2623                        else
2624                          {
2625                            if (cur1cnt == 0)
2626                              addToMap (request_inputs, "ResponseDocument",
2627                                        (char *) val);
2628                            else
2629                              {
2630                                map *tt =
2631                                  getMap (request_inputs, "ResponseDocument");
2632                                char *tmp = zStrdup (tt->value);
2633                                free (tt->value);
2634                                tt->value =
2635                                  (char *)
2636                                  malloc ((strlen (tmp) +
2637                                           strlen ((char *) val) +
2638                                           1) * sizeof (char));
2639                                sprintf (tt->value, "%s;%s", tmp,
2640                                         (char *) val);
2641                                free (tmp);
2642                              }
2643                          }
2644                        cur1cnt += 1;
2645                        xmlFree (val);
2646                      }
2647            /**
2648             * Title, Asbtract
2649             */
2650                    else
2651                      if (xmlStrncasecmp
2652                          (cur1->name, BAD_CAST "Title",
2653                           xmlStrlen (cur1->name)) == 0
2654                          || xmlStrncasecmp (cur1->name, BAD_CAST "Abstract",
2655                                             xmlStrlen (cur1->name)) == 0)
2656                      {
2657                        xmlChar *val =
2658                          xmlNodeListGetString (doc, cur1->xmlChildrenNode,
2659                                                1);
2660                        if (tmpmaps == NULL)
2661                          {
2662                            tmpmaps = (maps *) malloc (MAPS_SIZE);
2663                            if (tmpmaps == NULL)
2664                              {
2665                                return errorException (m,
2666                                                       _
2667                                                       ("Unable to allocate memory."),
2668                                                       "InternalError", NULL);
2669                              }
2670                            tmpmaps->name = zStrdup ("missingIndetifier");
2671                            tmpmaps->content =
2672                              createMap ((char *) cur1->name, (char *) val);
2673                            tmpmaps->next = NULL;
2674                          }
2675                        else
2676                          {
2677                            if (tmpmaps->content != NULL)
2678                              addToMap (tmpmaps->content, (char *) cur1->name,
2679                                        (char *) val);
2680                            else
2681                              tmpmaps->content =
2682                                createMap ((char *) cur1->name, (char *) val);
2683                          }
2684                        xmlFree (val);
2685                      }
2686                    else
2687                      if (xmlStrncasecmp
2688                          (cur1->name, BAD_CAST "Output",
2689                           xmlStrlen (cur1->name)) == 0)
2690                      {
2691              /**
2692               * Get every attribute from an Output node
2693               * mimeType, encoding, schema, uom, asReference
2694               */
2695                        const char *outs[5] =
2696                          { "mimeType", "encoding", "schema", "uom",
2697                          "asReference"
2698                        };
2699                        for (int l = 0; l < 5; l++)
2700                          {
2701#ifdef DEBUG
2702                            fprintf (stderr, "*** %s ***\t", outs[l]);
2703#endif
2704                            xmlChar *val =
2705                              xmlGetProp (cur1, BAD_CAST outs[l]);
2706                            if (val != NULL && strlen ((char *) val) > 0)
2707                              {
2708                                if (tmpmaps->content != NULL)
2709                                  addToMap (tmpmaps->content, outs[l],
2710                                            (char *) val);
2711                                else
2712                                  tmpmaps->content =
2713                                    createMap (outs[l], (char *) val);
2714                              }
2715#ifdef DEBUG
2716                            fprintf (stderr, "%s\n", val);
2717#endif
2718                            xmlFree (val);
2719                          }
2720                        xmlNodePtr cur2 = cur1->children;
2721                        while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
2722                          cur2 = cur2->next;
2723                        while (cur2)
2724                          {
2725                /**
2726                 * Indentifier
2727                 */
2728                            if (xmlStrncasecmp
2729                                (cur2->name, BAD_CAST "Identifier",
2730                                 xmlStrlen (cur2->name)) == 0)
2731                              {
2732                                xmlChar *val = xmlNodeListGetString (doc,
2733                                                                     cur2->
2734                                                                     xmlChildrenNode,
2735                                                                     1);
2736                                if (tmpmaps == NULL)
2737                                  {
2738                                    tmpmaps = (maps *) malloc (MAPS_SIZE);
2739                                    if (tmpmaps == NULL)
2740                                      {
2741                                        return errorException (m,
2742                                                               _
2743                                                               ("Unable to allocate memory."),
2744                                                               "InternalError",
2745                                                               NULL);
2746                                      }
2747                                    tmpmaps->name = zStrdup ((char *) val);
2748                                    tmpmaps->content = NULL;
2749                                    tmpmaps->next = NULL;
2750                                  }
2751                                else
2752                                  {
2753                                    if (tmpmaps->name != NULL)
2754                                      free (tmpmaps->name);
2755                                    tmpmaps->name = zStrdup ((char *) val);;
2756                                  }
2757                                xmlFree (val);
2758                              }
2759                /**
2760                 * Title, Asbtract
2761                 */
2762                            else
2763                              if (xmlStrncasecmp
2764                                  (cur2->name, BAD_CAST "Title",
2765                                   xmlStrlen (cur2->name)) == 0
2766                                  || xmlStrncasecmp (cur2->name,
2767                                                     BAD_CAST "Abstract",
2768                                                     xmlStrlen (cur2->name))
2769                                  == 0)
2770                              {
2771                                xmlChar *val = xmlNodeListGetString (doc,
2772                                                                     cur2->
2773                                                                     xmlChildrenNode,
2774                                                                     1);
2775                                if (tmpmaps == NULL)
2776                                  {
2777                                    tmpmaps = (maps *) malloc (MAPS_SIZE);
2778                                    if (tmpmaps == NULL)
2779                                      {
2780                                        return errorException (m,
2781                                                               _
2782                                                               ("Unable to allocate memory."),
2783                                                               "InternalError",
2784                                                               NULL);
2785                                      }
2786                                    tmpmaps->name =
2787                                      zStrdup ("missingIndetifier");
2788                                    tmpmaps->content =
2789                                      createMap ((char *) cur2->name,
2790                                                 (char *) val);
2791                                    tmpmaps->next = NULL;
2792                                  }
2793                                else
2794                                  {
2795                                    if (tmpmaps->content != NULL)
2796                                      addToMap (tmpmaps->content,
2797                                                (char *) cur2->name,
2798                                                (char *) val);
2799                                    else
2800                                      tmpmaps->content =
2801                                        createMap ((char *) cur2->name,
2802                                                   (char *) val);
2803                                  }
2804                                xmlFree (val);
2805                              }
2806                            cur2 = cur2->next;
2807                            while (cur2 != NULL
2808                                   && cur2->type != XML_ELEMENT_NODE)
2809                              cur2 = cur2->next;
2810                          }
2811                      }
2812                    cur1 = cur1->next;
2813                    while (cur1 != NULL && cur1->type != XML_ELEMENT_NODE)
2814                      cur1 = cur1->next;
2815                  }
2816              }
2817            if (request_output_real_format == NULL)
2818              request_output_real_format = dupMaps (&tmpmaps);
2819            else
2820              addMapsToMaps (&request_output_real_format, tmpmaps);
2821            if (tmpmaps != NULL)
2822              {
2823                freeMaps (&tmpmaps);
2824                free (tmpmaps);
2825                tmpmaps = NULL;
2826              }
2827          }
2828      xmlXPathFreeObject (tmpsptr);
2829      xmlFreeDoc (doc);
2830      xmlCleanupParser ();
2831    }
2832
2833  runHttpRequests (&m, &request_input_real_format, &hInternet);
2834
2835  //  if(CHECK_INET_HANDLE(hInternet))
2836  InternetCloseHandle (&hInternet);
2837
2838#ifdef DEBUG
2839  fprintf (stderr, "\n%d\n", __LINE__);
2840  fflush (stderr);
2841  dumpMaps (request_input_real_format);
2842  dumpMaps (request_output_real_format);
2843  dumpMap (request_inputs);
2844  fprintf (stderr, "\n%d\n", __LINE__);
2845  fflush (stderr);
2846#endif
2847
2848  /**
2849   * Ensure that each requested arguments are present in the request
2850   * DataInputs and ResponseDocument / RawDataOutput
2851   */
2852  map* errI=NULL;
2853#ifdef DEBUG 
2854  dumpMaps(request_input_real_format);
2855#endif 
2856  char *dfv = addDefaultValues (&request_input_real_format, s1->inputs, m, 0,&errI);
2857#ifdef DEBUG 
2858  dumpMaps(request_input_real_format);
2859#endif 
2860  maps *ptr = request_input_real_format;
2861  while (ptr != NULL)
2862    {
2863      map *tmp0 = getMap (ptr->content, "size");
2864      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
2865      if (tmp1 != NULL && tmp0 != NULL)
2866        {
2867          float i = atof (tmp0->value) / 1048576.0;
2868          if (i >= atoi (tmp1->value))
2869            {
2870              char tmps[1024];
2871              map *tmpe = createMap ("code", "FileSizeExceeded");
2872              snprintf (tmps, 1024,
2873                        _
2874                        ("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."),
2875                        ptr->name, tmp1->value, i);
2876              addToMap (tmpe, "locator", ptr->name);
2877              addToMap (tmpe, "text", tmps);
2878              printExceptionReportResponse (m, tmpe);
2879              freeService (&s1);
2880              free (s1);
2881              freeMap (&tmpe);
2882              free (tmpe);
2883              freeMaps (&m);
2884              free (m);
2885              free (REQUEST);
2886              free (SERVICE_URL);
2887              freeMaps (&request_input_real_format);
2888              free (request_input_real_format);
2889              freeMaps (&request_output_real_format);
2890              free (request_output_real_format);
2891              freeMaps (&tmpmaps);
2892              free (tmpmaps);
2893              return 1;
2894            }
2895        }
2896      ptr = ptr->next;
2897    }
2898
2899  map* errO=NULL;
2900  char *dfv1 =
2901    addDefaultValues (&request_output_real_format, s1->outputs, m, 1,&errO);
2902  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
2903    {
2904      char tmps[1024];
2905      map *tmpe = NULL;
2906      if (strcmp (dfv, "") != 0)
2907        {
2908          tmpe = createMap ("code", "MissingParameterValue");
2909          int nb=0;
2910          int length=1;
2911          map* len=getMap(errI,"length");
2912          if(len!=NULL)
2913            length=atoi(len->value);
2914          for(nb=0;nb<length;nb++){
2915            map* errp=getMapArray(errI,"value",nb);
2916            snprintf (tmps, 1024,
2917                      _
2918                      ("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."),
2919                      errp->value);
2920            setMapArray (tmpe, "locator", nb , errp->value);
2921            setMapArray (tmpe, "text", nb , tmps);
2922            setMapArray (tmpe, "code", nb , "MissingParameterValue");
2923          }
2924        }
2925      if (strcmp (dfv1, "") != 0)
2926        {
2927          int ilength=0;
2928          if(tmpe==NULL)
2929            tmpe = createMap ("code", "InvalidParameterValue");
2930          else{
2931            map* len=getMap(tmpe,"length");
2932            if(len!=NULL)
2933              ilength=atoi(len->value);
2934          }
2935          int nb=0;
2936          int length=1;
2937          map* len=getMap(errO,"length");
2938          if(len!=NULL)
2939            length=atoi(len->value);
2940          for(nb=0;nb<length;nb++){
2941            map* errp=getMapArray(errO,"value",nb);
2942            snprintf (tmps, 1024,
2943                      _
2944                      ("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."),
2945                      errp->value,
2946                      ((getMap(request_inputs,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
2947            setMapArray (tmpe, "locator", nb+ilength , errp->value);
2948            setMapArray (tmpe, "text", nb+ilength , tmps);
2949            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
2950          }
2951        }
2952      printExceptionReportResponse (m, tmpe);
2953      freeService (&s1);
2954      free (s1);
2955      freeMap (&tmpe);
2956      free (tmpe);
2957      freeMaps (&m);
2958      free (m);
2959      free (REQUEST);
2960      free (SERVICE_URL);
2961      freeMaps (&request_input_real_format);
2962      free (request_input_real_format);
2963      freeMaps (&request_output_real_format);
2964      free (request_output_real_format);
2965      freeMaps (&tmpmaps);
2966      free (tmpmaps);
2967      if(errI!=NULL){
2968        freeMap(&errI);
2969        free(errI);
2970      }
2971      if(errO!=NULL){
2972        freeMap(&errO);
2973        free(errO);
2974      }
2975      return 1;
2976    }
2977  maps *tmpReqI = request_input_real_format;
2978  while (tmpReqI != NULL)
2979    {
2980      char name[1024];
2981      if (getMap (tmpReqI->content, "isFile") != NULL)
2982        {
2983          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
2984              cgiFormSuccess)
2985            {
2986              int BufferLen = 1024;
2987              cgiFilePtr file;
2988              int targetFile;
2989              char storageNameOnServer[2048];
2990              char fileNameOnServer[64];
2991              char contentType[1024];
2992              char buffer[1024];
2993              char *tmpStr = NULL;
2994              int size;
2995              int got, t;
2996              map *path = getMapFromMaps (m, "main", "tmpPath");
2997              cgiFormFileSize (tmpReqI->name, &size);
2998              cgiFormFileContentType (tmpReqI->name, contentType,
2999                                      sizeof (contentType));
3000              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
3001                {
3002                  t = -1;
3003                  while (1)
3004                    {
3005                      tmpStr = strstr (name + t + 1, "\\");
3006                      if (NULL == tmpStr)
3007                        tmpStr = strstr (name + t + 1, "/");
3008                      if (NULL != tmpStr)
3009                        t = (int) (tmpStr - name);
3010                      else
3011                        break;
3012                    }
3013                  strcpy (fileNameOnServer, name + t + 1);
3014
3015                  sprintf (storageNameOnServer, "%s/%s", path->value,
3016                           fileNameOnServer);
3017#ifdef DEBUG
3018                  fprintf (stderr, "Name on server %s\n",
3019                           storageNameOnServer);
3020                  fprintf (stderr, "fileNameOnServer: %s\n",
3021                           fileNameOnServer);
3022#endif
3023                  targetFile =
3024                    open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
3025                          S_IRWXU | S_IRGRP | S_IROTH);
3026                  if (targetFile < 0)
3027                    {
3028#ifdef DEBUG
3029                      fprintf (stderr, "could not create the new file,%s\n",
3030                               fileNameOnServer);
3031#endif
3032                    }
3033                  else
3034                    {
3035                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
3036                             == cgiFormSuccess)
3037                        {
3038                          if (got > 0)
3039                            write (targetFile, buffer, got);
3040                        }
3041                    }
3042                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
3043                  cgiFormFileClose (file);
3044                  close (targetFile);
3045#ifdef DEBUG
3046                  fprintf (stderr, "File \"%s\" has been uploaded",
3047                           fileNameOnServer);
3048#endif
3049                }
3050            }
3051        }
3052      tmpReqI = tmpReqI->next;
3053    }
3054
3055  ensureDecodedBase64 (&request_input_real_format);
3056
3057#ifdef DEBUG
3058  fprintf (stderr, "REQUEST_INPUTS\n");
3059  dumpMaps (request_input_real_format);
3060  fprintf (stderr, "REQUEST_OUTPUTS\n");
3061  dumpMaps (request_output_real_format);
3062#endif
3063
3064  maps *curs = getMaps (m, "env");
3065  if (curs != NULL)
3066    {
3067      map *mapcs = curs->content;
3068      while (mapcs != NULLMAP)
3069        {
3070#ifndef WIN32
3071          setenv (mapcs->name, mapcs->value, 1);
3072#else
3073#ifdef DEBUG
3074          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
3075                   mapcs->value);
3076#endif
3077          if (mapcs->value[strlen (mapcs->value) - 2] == '\r')
3078            {
3079#ifdef DEBUG
3080              fprintf (stderr, "[ZOO: Env var finish with \r]\n");
3081#endif
3082              mapcs->value[strlen (mapcs->value) - 1] = 0;
3083            }
3084#ifdef DEBUG
3085          if (SetEnvironmentVariable (mapcs->name, mapcs->value) == 0)
3086            {
3087              fflush (stderr);
3088              fprintf (stderr, "setting variable... %s\n", "OK");
3089            }
3090          else
3091            {
3092              fflush (stderr);
3093              fprintf (stderr, "setting variable... %s\n", "OK");
3094            }
3095#else
3096
3097
3098          SetEnvironmentVariable (mapcs->name, mapcs->value);
3099#endif
3100          char *toto =
3101            (char *)
3102            malloc ((strlen (mapcs->name) + strlen (mapcs->value) +
3103                     2) * sizeof (char));
3104          sprintf (toto, "%s=%s", mapcs->name, mapcs->value);
3105          putenv (toto);
3106#ifdef DEBUG
3107          fflush (stderr);
3108#endif
3109#endif
3110#ifdef DEBUG
3111          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
3112                   mapcs->value);
3113          fflush (stderr);
3114#endif
3115          mapcs = mapcs->next;
3116        }
3117    }
3118
3119#ifdef DEBUG
3120  dumpMap (request_inputs);
3121#endif
3122
3123  /**
3124   * Need to check if we need to fork to load a status enabled
3125   */
3126  r_inputs = NULL;
3127  map *store = getMap (request_inputs, "storeExecuteResponse");
3128  map *status = getMap (request_inputs, "status");
3129  /**
3130   * 05-007r7 WPS 1.0.0 page 57 :
3131   * 'If status="true" and storeExecuteResponse is "false" then the service
3132   * shall raise an exception.'
3133   */
3134  if (status != NULL && strcmp (status->value, "true") == 0 &&
3135      store != NULL && strcmp (store->value, "false") == 0)
3136    {
3137      errorException (m,
3138                      _
3139                      ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."),
3140                      "InvalidParameterValue", "storeExecuteResponse");
3141      freeService (&s1);
3142      free (s1);
3143      freeMaps (&m);
3144      free (m);
3145
3146      freeMaps (&request_input_real_format);
3147      free (request_input_real_format);
3148
3149      freeMaps (&request_output_real_format);
3150      free (request_output_real_format);
3151
3152      free (REQUEST);
3153      free (SERVICE_URL);
3154      return 1;
3155    }
3156  r_inputs = getMap (request_inputs, "storeExecuteResponse");
3157  int eres = SERVICE_STARTED;
3158  int cpid = getpid ();
3159
3160  /**
3161   * Initialize the specific [lenv] section which contains runtime variables:
3162   *
3163   *  - usid : it is an unique identification number
3164   *  - sid : it is the process idenfitication number (OS)
3165   *  - status : value between 0 and 100 to express the  completude of
3166   * the operations of the running service
3167   *  - message : is a string where you can store error messages, in case
3168   * service is failing, or o provide details on the ongoing operation.
3169   *  - cwd : is the current working directory
3170   *  - soap : is a boolean value, true if the request was contained in a SOAP
3171   * Envelop
3172   *  - sessid : string storing the session identifier (only when cookie is
3173   * used)
3174   *  - cgiSid : only defined on Window platforms (for being able to identify
3175   * the created process)
3176   *
3177   */
3178  maps *_tmpMaps = (maps *) malloc (MAPS_SIZE);
3179  _tmpMaps->name = zStrdup ("lenv");
3180  char tmpBuff[100];
3181  semid lid = getShmLockId (NULL, 1);
3182  lockShm (lid);
3183  struct ztimeval tp;
3184  if (zGettimeofday (&tp, NULL) == 0)
3185    sprintf (tmpBuff, "%i", (cpid + ((int) tp.tv_sec + (int) tp.tv_usec)));
3186  else
3187    sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
3188  unlockShm (lid);
3189  removeShmLock (NULL, 1);
3190  _tmpMaps->content = createMap ("usid", tmpBuff);
3191  _tmpMaps->next = NULL;
3192  sprintf (tmpBuff, "%i", cpid);
3193  addToMap (_tmpMaps->content, "sid", tmpBuff);
3194  addToMap (_tmpMaps->content, "status", "0");
3195  addToMap (_tmpMaps->content, "cwd", ntmp);
3196  addToMap (_tmpMaps->content, "message", _("No message provided"));
3197  map *ltmp = getMap (request_inputs, "soap");
3198  if (ltmp != NULL)
3199    addToMap (_tmpMaps->content, "soap", ltmp->value);
3200  else
3201    addToMap (_tmpMaps->content, "soap", "false");
3202  if (cgiCookie != NULL && strlen (cgiCookie) > 0)
3203    {
3204      int hasValidCookie = -1;
3205      char *tcook = zStrdup (cgiCookie);
3206      char *tmp = NULL;
3207      map *testing = getMapFromMaps (m, "main", "cookiePrefix");
3208      if (testing == NULL)
3209        {
3210          tmp = zStrdup ("ID=");
3211        }
3212      else
3213        {
3214          tmp =
3215            (char *) malloc ((strlen (testing->value) + 2) * sizeof (char));
3216          sprintf (tmp, "%s=", testing->value);
3217        }
3218      if (strstr (cgiCookie, ";") != NULL)
3219        {
3220          char *token, *saveptr;
3221          token = strtok_r (cgiCookie, ";", &saveptr);
3222          while (token != NULL)
3223            {
3224              if (strcasestr (token, tmp) != NULL)
3225                {
3226                  if (tcook != NULL)
3227                    free (tcook);
3228                  tcook = zStrdup (token);
3229                  hasValidCookie = 1;
3230                }
3231              token = strtok_r (NULL, ";", &saveptr);
3232            }
3233        }
3234      else
3235        {
3236          if (strstr (cgiCookie, "=") != NULL
3237              && strcasestr (cgiCookie, tmp) != NULL)
3238            {
3239              tcook = zStrdup (cgiCookie);
3240              hasValidCookie = 1;
3241            }
3242          if (tmp != NULL)
3243            {
3244              free (tmp);
3245            }
3246        }
3247      if (hasValidCookie > 0)
3248        {
3249          addToMap (_tmpMaps->content, "sessid", strstr (tcook, "=") + 1);
3250          char session_file_path[1024];
3251          map *tmpPath = getMapFromMaps (m, "main", "sessPath");
3252          if (tmpPath == NULL)
3253            tmpPath = getMapFromMaps (m, "main", "tmpPath");
3254          char *tmp1 = strtok (tcook, ";");
3255          if (tmp1 != NULL)
3256            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
3257                     strstr (tmp1, "=") + 1);
3258          else
3259            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
3260                     strstr (cgiCookie, "=") + 1);
3261          free (tcook);
3262          maps *tmpSess = (maps *) malloc (MAPS_SIZE);
3263          struct stat file_status;
3264          int istat = stat (session_file_path, &file_status);
3265          if (istat == 0 && file_status.st_size > 0)
3266            {
3267              conf_read (session_file_path, tmpSess);
3268              addMapsToMaps (&m, tmpSess);
3269              freeMaps (&tmpSess);
3270              free (tmpSess);
3271            }
3272        }
3273    }
3274  addMapsToMaps (&m, _tmpMaps);
3275  freeMaps (&_tmpMaps);
3276  free (_tmpMaps);
3277
3278#ifdef DEBUG
3279  dumpMap (request_inputs);
3280#endif
3281#ifdef WIN32
3282  char *cgiSidL = NULL;
3283  if (getenv ("CGISID") != NULL)
3284    addToMap (request_inputs, "cgiSid", getenv ("CGISID"));
3285
3286  char* usidp;
3287  if ( (usidp = getenv("USID")) != NULL ) {
3288    setMapInMaps (m, "lenv", "usid", usidp);
3289  }
3290
3291  map *test1 = getMap (request_inputs, "cgiSid");
3292  if (test1 != NULL)
3293    {
3294      cgiSid = test1->value;
3295      addToMap (request_inputs, "storeExecuteResponse", "true");
3296      addToMap (request_inputs, "status", "true");
3297      setMapInMaps (m, "lenv", "sid", test1->value);
3298      status = getMap (request_inputs, "status");
3299    }
3300#endif
3301  char *fbkp, *fbkp1;
3302  FILE *f0, *f1;
3303  if (status != NULL)
3304    if (strcasecmp (status->value, "false") == 0)
3305      status = NULLMAP;
3306  if (status == NULLMAP)
3307    {
3308      loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format,
3309                         &request_output_real_format, &eres);
3310    }
3311  else
3312    {
3313      int pid;
3314#ifdef DEBUG
3315      fprintf (stderr, "\nPID : %d\n", cpid);
3316#endif
3317
3318#ifndef WIN32
3319      pid = fork ();
3320#else
3321      if (cgiSid == NULL)
3322        {
3323          createProcess (m, request_inputs, s1, NULL, cpid,
3324                         request_input_real_format,
3325                         request_output_real_format);
3326          pid = cpid;
3327        }
3328      else
3329        {
3330          pid = 0;
3331          cpid = atoi (cgiSid);
3332        }
3333#endif
3334      if (pid > 0)
3335        {
3336      /**
3337       * dady :
3338       * set status to SERVICE_ACCEPTED
3339       */
3340#ifdef DEBUG
3341          fprintf (stderr, "father pid continue (origin %d) %d ...\n", cpid,
3342                   getpid ());
3343#endif
3344          eres = SERVICE_ACCEPTED;
3345        }
3346      else if (pid == 0)
3347        {
3348      /**
3349       * son : have to close the stdout, stdin and stderr to let the parent
3350       * process answer to http client.
3351       */
3352#ifndef WIN32
3353          zSleep (1);
3354#endif
3355          r_inputs = getMapFromMaps (m, "lenv", "usid");
3356          int cpid = atoi (r_inputs->value);
3357          r_inputs = getMapFromMaps (m, "main", "tmpPath");
3358          map *r_inputs1 = getMap (s1->content, "ServiceProvider");
3359          fbkp =
3360            (char *)
3361            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
3362                     1024) * sizeof (char));
3363          sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value, r_inputs1->value,
3364                   cpid);
3365          char *flog =
3366            (char *)
3367            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
3368                     1024) * sizeof (char));
3369          sprintf (flog, "%s/%s_%d_error.log", r_inputs->value,
3370                   r_inputs1->value, cpid);
3371#ifdef DEBUG
3372          fprintf (stderr, "RUN IN BACKGROUND MODE \n");
3373          fprintf (stderr, "son pid continue (origin %d) %d ...\n", cpid,
3374                   getpid ());
3375          fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value);
3376#endif
3377          freopen (flog, "w+", stderr);
3378          semid lid = getShmLockId (m, 1);
3379          fflush (stderr);
3380          if (lid < 0)
3381            {
3382              fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__);
3383              fflush (stderr);
3384              return -1;
3385            }
3386          else
3387            {
3388              if (lockShm (lid) < 0)
3389                {
3390                  fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__);
3391                  fflush (stderr);
3392                  return -1;
3393                }
3394              fflush (stderr);
3395            }
3396          f0 = freopen (fbkp, "w+", stdout);
3397          rewind (stdout);
3398#ifndef WIN32
3399          fclose (stdin);
3400#endif
3401          free (flog);
3402      /**
3403       * set status to SERVICE_STARTED and flush stdout to ensure full
3404       * content was outputed (the file used to store the ResponseDocument).
3405       * The rewind stdout to restart writing from the bgining of the file,
3406       * this way the data will be updated at the end of the process run.
3407       */
3408          printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value,
3409                                SERVICE_STARTED, request_input_real_format,
3410                                request_output_real_format);
3411          fflush (stdout);
3412          unlockShm (lid);
3413          fflush (stderr);
3414          fbkp1 =
3415            (char *)
3416            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
3417                     1024) * sizeof (char));
3418          sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
3419                   r_inputs1->value, cpid);
3420          f1 = freopen (fbkp1, "w+", stdout);
3421          loadServiceAndRun (&m, s1, request_inputs,
3422                             &request_input_real_format,
3423                             &request_output_real_format, &eres);
3424        }
3425      else
3426        {
3427      /**
3428       * error server don't accept the process need to output a valid
3429       * error response here !!!
3430       */
3431          eres = -1;
3432          errorException (m, _("Unable to run the child process properly"),
3433                          "InternalError", NULL);
3434        }
3435    }
3436
3437#ifdef DEBUG
3438  dumpMaps (request_output_real_format);
3439#endif
3440  if (eres != -1)
3441    outputResponse (s1, request_input_real_format,
3442                    request_output_real_format, request_inputs,
3443                    cpid, m, eres);
3444  fflush (stdout);
3445  /**
3446   * Ensure that if error occurs when freeing memory, no signal will return
3447   * an ExceptionReport document as the result was already returned to the
3448   * client.
3449   */
3450#ifndef USE_GDB
3451  signal (SIGSEGV, donothing);
3452  signal (SIGTERM, donothing);
3453  signal (SIGINT, donothing);
3454  signal (SIGILL, donothing);
3455  signal (SIGFPE, donothing);
3456  signal (SIGABRT, donothing);
3457#endif
3458  if (((int) getpid ()) != cpid || cgiSid != NULL)
3459    {
3460      fclose (stdout);
3461      fclose (stderr);
3462    /**
3463     * Dump back the final file fbkp1 to fbkp
3464     */
3465      fclose (f0);
3466      fclose (f1);
3467      FILE *f2 = fopen (fbkp1, "rb");
3468      semid lid = getShmLockId (m, 1);
3469      if (lid < 0)
3470        return -1;
3471      lockShm (lid);
3472      FILE *f3 = fopen (fbkp, "wb+");
3473      free (fbkp);
3474      fseek (f2, 0, SEEK_END);
3475      long flen = ftell (f2);
3476      fseek (f2, 0, SEEK_SET);
3477      char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
3478      fread (tmps1, flen, 1, f2);
3479      fwrite (tmps1, 1, flen, f3);
3480      fclose (f2);
3481      fclose (f3);
3482      unlockShm (lid);
3483      unlink (fbkp1);
3484      free (fbkp1);
3485      free (tmps1);
3486      unhandleStatus (m);
3487    }
3488
3489  freeService (&s1);
3490  free (s1);
3491  freeMaps (&m);
3492  free (m);
3493
3494  freeMaps (&request_input_real_format);
3495  free (request_input_real_format);
3496
3497  freeMaps (&request_output_real_format);
3498  free (request_output_real_format);
3499
3500  free (REQUEST);
3501  free (SERVICE_URL);
3502#ifdef DEBUG
3503  fprintf (stderr, "Processed response \n");
3504  fflush (stdout);
3505  fflush (stderr);
3506#endif
3507
3508  if (((int) getpid ()) != cpid || cgiSid != NULL)
3509    {
3510      exit (0);
3511    }
3512
3513  return 0;
3514}
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