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

Last change on this file since 571 was 557, checked in by knut, 10 years ago

Fix to ensure that only configuration files whose name ends with .zcfg are read (e.g. avoid reading ServiceName?.zcfg~).

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