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

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

Added option to specify other program name than zoo_loader.cgi (WIN32 version).

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