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

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

Added code required for HTTPS requests.

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