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

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

Altered the path of stored response files from [tmpPath]/[serviceProvider]_[cpid].xml to [tmpPath]/[serviceName]_[cpid].xml. Added support for a new parameter, libPath, in the [main] block of main.cfg. The libPath parameter is interpreted as the absolute path of the directory containing library files. If the libPath parameter is set, it will override the metapath parameter such that library files are loaded from [libPath]/[serviceProvider] instead of [CWD]/[metapath]/[serviceProvider]. Added the option to disable the metapath parameter (for security reasons) at compile time: If the Zoo kernel is compiled with the preprocessor directive IGNORE_METAPATH, the metapath will be set to the empty string. Note however, that the libPath and IGNORE_METAPATH options so far only work for C and PHP; the functions for loading libraries written in other languages should be modified correspondingly before the documentation is updated. See also ticket no. 112 (http://www.zoo-project.org/trac/ticket/112). Fixed some spelling errors and modified the language in some of the messages returned by the Zoo kernel.

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