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

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

Changed the WIN32 version of function zGettimeofday. Changed return type for getShmLockId (WIN32). Changed type of _HINTERNET.mimeType from unsigned char* to char*. Fixed interconnected memory issues in functions getKeyValue and getShmLockId (WIN32). Added code to transfer the correct unique process identifier (usid) to background processes (applies to WIN32 version).

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