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

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