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

Last change on this file since 576 was 576, checked in by djay, 9 years ago

Code cleanup, description of some functon included in the code, addition of support for multiple error output, beter internal gesture of MapArray?.

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