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

Last change on this file since 931 was 931, checked in by djay, 5 years ago

Make sure to select the right WPS version

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 74.1 KB
RevLine 
[607]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 */
[788]24 
[541]25extern "C" int yylex ();
26extern "C" int crlex ();
[9]27
[917]28#ifdef META_DB
29#include "ogrsf_frmts.h"
30#if GDAL_VERSION_MAJOR >= 2
31#include <gdal_priv.h>
32#endif
33#endif
34
[550]35#ifdef USE_OTB
36#include "service_internal_otb.h"
37#endif
38
[917]39#ifdef USE_R
40#include "service_internal_r.h"
41#endif
42
43#ifdef USE_HPC
44#include "service_internal_hpc.h"
45#endif
46
[921]47#ifdef USE_PYTHON
48#include "service_internal_python.h"
49#endif
50
[376]51#include "cgic.h"
52
[1]53#include <libxml/tree.h>
54#include <libxml/xmlmemory.h>
55#include <libxml/parser.h>
56#include <libxml/xpath.h>
57#include <libxml/xpathInternals.h>
58
59#include "ulinet.h"
60
[844]61#include <libintl.h>
[34]62#include <locale.h>
[1]63#include <string.h>
64
65#include "service.h"
[34]66
[1]67#include "service_internal.h"
[640]68#include "server_internal.h"
69#include "response_print.h"
[621]70#include "request_parser.h"
[640]71#include "sqlapi.h"
[680]72#ifdef WIN32
73#include "caching.h"
74#endif
[33]75
[917]76#ifdef META_DB
77#include "meta_sql.h"
78#endif
79
[634]80#ifdef USE_SAGA
81#include "service_internal_saga.h"
82#endif
83
[1]84#ifdef USE_JAVA
85#include "service_internal_java.h"
86#endif
87
88#ifdef USE_PHP
89#include "service_internal_php.h"
90#endif
91
92#ifdef USE_JS
93#include "service_internal_js.h"
94#endif
95
[453]96#ifdef USE_RUBY
97#include "service_internal_ruby.h"
98#endif
99
[25]100#ifdef USE_PERL
101#include "service_internal_perl.h"
102#endif
[1]103
[794]104#ifdef USE_MONO
105#include "service_internal_mono.h"
106#endif
107
[917]108#ifdef USE_CALLBACK
109#include "service_json.h"
110#include "service_callback.h"
111#endif
112
[1]113#include <dirent.h>
114#include <signal.h>
[917]115#ifndef WIN32
116#include <execinfo.h>
117#endif
[1]118#include <unistd.h>
119#ifndef WIN32
120#include <dlfcn.h>
121#include <libgen.h>
122#else
123#include <windows.h>
124#include <direct.h>
[364]125#include <sys/types.h>
126#include <sys/stat.h>
127#include <unistd.h>
128#define pid_t int;
[1]129#endif
130#include <fcntl.h>
131#include <time.h>
132#include <stdarg.h>
133
[917]134#include <libxml/tree.h>
135#include <libxml/parser.h>
136#include <libxml/xpath.h>
137#include <libxml/xpathInternals.h>
138
139#include <libxslt/xslt.h>
140#include <libxslt/xsltInternals.h>
141#include <libxslt/transform.h>
142#include <libxslt/xsltutils.h>
143
[772]144#ifndef WIN32
145extern char **environ;
146#endif
147
[917]148
149#ifdef WIN32
150extern "C"
151{
152  __declspec (dllexport) char *strcasestr (char const *a, char const *b)
153#ifndef USE_MS
154  {
155    char *x = zStrdup (a);
156    char *y = zStrdup (b);
157
158      x = _strlwr (x);
159      y = _strlwr (y);
160    char *pos = strstr (x, y);
161    char *ret = pos == NULL ? NULL : (char *) (a + (pos - x));
162      free (x);
163      free (y);
164      return ret;
165  };
166#else
167   ;
168#endif
169}
170#endif
171
[607]172/**
173 * Translation function for zoo-kernel
174 */
[34]175#define _(String) dgettext ("zoo-kernel",String)
[607]176/**
177 * Translation function for zoo-service
178 */
[376]179#define __(String) dgettext ("zoo-service",String)
[34]180
[582]181#ifdef WIN32
[917]182#ifndef PROGRAMNAME
183#define PROGRAMNAME "zoo_loader.cgi"
[582]184#endif
[917]185#endif
[582]186
[34]187
[607]188/**
189 * Replace a char by another one in a string
190 *
191 * @param str the string to update
192 * @param toReplace the char to replace
193 * @param toReplaceBy the char that will be used
194 */
[541]195void
196translateChar (char *str, char toReplace, char toReplaceBy)
197{
198  int i = 0, len = strlen (str);
199  for (i = 0; i < len; i++)
200    {
201      if (str[i] == toReplace)
202        str[i] = toReplaceBy;
203    }
[34]204}
205
[607]206/**
[765]207 * Dump back the final file fbkp1 to fbkp
208 *
209 * @param m the conf maps containing the main.cfg settings
210 * @param fbkp the string corresponding to the name of the file
211 * @param fbkp1 the string corresponding to the name of the file
212 */
213int dumpBackFinalFile(maps* m,char* fbkp,char* fbkp1)
214{
215  FILE *f2 = fopen (fbkp1, "rb");
216#ifndef RELY_ON_DB
217  semid lid = getShmLockId (m, 1);
218  if (lid < 0)
219    return -1;
220  lockShm (lid);
221#endif
[917]222  FILE *f3 = fopen (fbkp, "wb+");
223  free (fbkp);
[765]224  fseek (f2, 0, SEEK_END);
225  long flen = ftell (f2);
226  fseek (f2, 0, SEEK_SET);
227  char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));
[917]228  fread (tmps1, flen, 1, f2);
[765]229#ifdef WIN32
[889]230  /* knut: I think this block can be dropped; pchr may be NULL if result is not in XML format
[917]231  char *pchr=strrchr(tmps1,'>');
232  flen=strlen(tmps1)-strlen(pchr)+1;
233  tmps1[flen]=0;
[889]234  */
[917]235#endif
[765]236  fwrite (tmps1, 1, flen, f3);
[917]237  fclose (f2);
238  fclose (f3);
[889]239  free(tmps1);
[765]240  return 1;
241}
242
243/**
[607]244 * Recursivelly parse zcfg starting from the ZOO-Kernel cwd.
245 * Call the func function given in arguments after parsing the ZCFG file.
246 *
247 * @param m the conf maps containing the main.cfg settings
248 * @param r the registry containing profiles hierarchy
249 * @param n the root XML Node to add the sub-elements
250 * @param conf_dir the location of the main.cfg file (basically cwd)
251 * @param prefix the current prefix if any, or NULL
252 * @param saved_stdout the saved stdout identifier
253 * @param level the current level (number of sub-directories to reach the
254 * current path)
[676]255 * @param func a pointer to a function having 4 parameters
256 *  (registry*, maps*, xmlNodePtr and service*).
[607]257 * @see inheritance, readServiceFile
258 */
259int
[917]260recursReaddirF ( maps * m, registry *r, xmlDocPtr doc, xmlNodePtr n, char *conf_dir, 
[676]261                 char *prefix, int saved_stdout, int level, 
[917]262                 void (func) (registry *, maps *, xmlDocPtr, xmlNodePtr, service *) )
[541]263{
[469]264  struct dirent *dp;
[541]265  int scount = 0;
[469]266
[541]267  if (conf_dir == NULL)
[469]268    return 1;
[541]269  DIR *dirp = opendir (conf_dir);
270  if (dirp == NULL)
271    {
272      if (level > 0)
273        return 1;
274      else
275        return -1;
276    }
[469]277  char tmp1[25];
[541]278  sprintf (tmp1, "sprefix_%d", level);
[469]279  char levels[17];
[541]280  sprintf (levels, "%d", level);
281  setMapInMaps (m, "lenv", "level", levels);
282  while ((dp = readdir (dirp)) != NULL)
283    if ((dp->d_type == DT_DIR || dp->d_type == DT_LNK) && dp->d_name[0] != '.'
284        && strstr (dp->d_name, ".") == NULL)
285      {
[469]286
[541]287        char *tmp =
288          (char *) malloc ((strlen (conf_dir) + strlen (dp->d_name) + 2) *
289                           sizeof (char));
290        sprintf (tmp, "%s/%s", conf_dir, dp->d_name);
[469]291
[541]292        if (prefix != NULL)
293          {
294            prefix = NULL;
295          }
296        prefix = (char *) malloc ((strlen (dp->d_name) + 2) * sizeof (char));
297        sprintf (prefix, "%s.", dp->d_name);
298
299        //map* tmpMap=getMapFromMaps(m,"lenv",tmp1);
300
301        int res;
302        if (prefix != NULL)
303          {
304            setMapInMaps (m, "lenv", tmp1, prefix);
305            char levels1[17];
306            sprintf (levels1, "%d", level + 1);
307            setMapInMaps (m, "lenv", "level", levels1);
308            res =
[917]309              recursReaddirF (m, r, doc, n, tmp, prefix, saved_stdout, level + 1,
[541]310                              func);
311            sprintf (levels1, "%d", level);
312            setMapInMaps (m, "lenv", "level", levels1);
313            free (prefix);
314            prefix = NULL;
315          }
316        else
317          res = -1;
318        free (tmp);
319        if (res < 0)
320          {
321            return res;
322          }
[469]323      }
[541]324    else
325      {
[557]326        char* extn = strstr(dp->d_name, ".zcfg");
[917]327        if(dp->d_name[0] != '.' && extn != NULL && strlen(extn) == 5 && strlen(dp->d_name)>6)
[541]328          {
329            int t;
330            char tmps1[1024];
331            memset (tmps1, 0, 1024);
332            snprintf (tmps1, 1024, "%s/%s", conf_dir, dp->d_name);
[789]333
[917]334            char *tmpsn = (char*)malloc((strlen(dp->d_name)-4)*sizeof(char));//zStrdup (dp->d_name);
335            memset (tmpsn, 0, strlen(dp->d_name)-4);
336            snprintf(tmpsn,strlen(dp->d_name)-4,"%s",dp->d_name);
[789]337           
338            map* import = getMapFromMaps (m, IMPORTSERVICE, tmpsn);
339            if (import == NULL || import->value == NULL || zoo_path_compare(tmps1, import->value) != 0 ) { // service is not in [include] block
[917]340              service *s1 = createService();
[789]341              if (s1 == NULL)
342                {
[917]343                  zDup2 (saved_stdout, fileno (stdout));
[789]344                  errorException (m, _("Unable to allocate memory"),
345                                  "InternalError", NULL);
346                  return -1;
347                }
348  #ifdef DEBUG
349              fprintf (stderr, "#################\n%s\n#################\n",
350                       tmps1);
351  #endif
352              t = readServiceFile (m, tmps1, &s1, tmpsn);
353              free (tmpsn);
354              if (t < 0)
355                {
356                  map *tmp00 = getMapFromMaps (m, "lenv", "message");
357                  char tmp01[1024];
358                  if (tmp00 != NULL)
359                    sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"),
360                             dp->d_name, tmp00->value);
361                  else
362                    sprintf (tmp01, _("Unable to parse the ZCFG file: %s."),
363                             dp->d_name);
[917]364                  zDup2 (saved_stdout, fileno (stdout));
[789]365                  errorException (m, tmp01, "InternalError", NULL);
366                  return -1;
367                }
368  #ifdef DEBUG
369              dumpService (s1);
370              fflush (stdout);
371              fflush (stderr);
372  #endif
[917]373              if(s1!=NULL)
374                inheritance(r,&s1);
375              func (r, m, doc, n, s1);
[789]376              freeService (&s1);
377              free (s1);
378              scount++;
379            }
[541]380          }
[469]381      }
[541]382  (void) closedir (dirp);
[469]383  return 1;
384}
385
[607]386/**
387 * Signal handling function which simply call exit(0).
388 *
389 * @param sig the signal number
390 */
[541]391void
392donothing (int sig)
393{
[478]394#ifdef DEBUG
[605]395  fprintf (stderr, "Signal %d after the ZOO-Kernel returned result!\n", sig);
[478]396#endif
[541]397  exit (0);
[105]398}
399
[607]400/**
401 * Signal handling function which create an ExceptionReport node containing the
402 * information message corresponding to the signal number.
403 *
404 * @param sig the signal number
405 */
[541]406void
407sig_handler (int sig)
408{
[917]409 
[9]410  char tmp[100];
[114]411  const char *ssig;
[541]412  switch (sig)
413    {
414    case SIGSEGV:
415      ssig = "SIGSEGV";
416      break;
417    case SIGTERM:
418      ssig = "SIGTERM";
419      break;
420    case SIGINT:
421      ssig = "SIGINT";
422      break;
423    case SIGILL:
424      ssig = "SIGILL";
425      break;
426    case SIGFPE:
427      ssig = "SIGFPE";
428      break;
429    case SIGABRT:
430      ssig = "SIGABRT";
431      break;
432    default:
433      ssig = "UNKNOWN";
434      break;
435    }
436  sprintf (tmp,
437           _
[917]438           ("ZOO Kernel failed to process your request, receiving signal %d = %s "),
[541]439           sig, ssig);
440  errorException (NULL, tmp, "InternalError", NULL);
[10]441#ifdef DEBUG
[541]442  fprintf (stderr, "Not this time!\n");
[10]443#endif
[541]444  exit (0);
[1]445}
446
[607]447/**
448 * Load a service provider and run the service function.
449 *
450 * @param myMap the conf maps containing the main.cfg settings
451 * @param s1 the service structure
452 * @param request_inputs map storing all the request parameters
453 * @param inputs the inputs maps
454 * @param ioutputs the outputs maps
455 * @param eres the result returned by the service execution
456 */
[541]457void
458loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs,
459                   maps ** inputs, maps ** ioutputs, int *eres)
460{
[34]461  char tmps1[1024];
462  char ntmp[1024];
[541]463  maps *m = *myMap;
464  maps *request_output_real_format = *ioutputs;
465  maps *request_input_real_format = *inputs;
[34]466  /**
467   * Extract serviceType to know what kind of service should be loaded
468   */
[541]469  map *r_inputs = NULL;
[784]470  map* cwdMap=getMapFromMaps(m,"main","servicePath");
471  if(cwdMap!=NULL){
472    sprintf(ntmp,"%s",cwdMap->value);
473  }else{
[34]474#ifndef WIN32
[784]475    getcwd (ntmp, 1024);
[34]476#else
[784]477    _getcwd (ntmp, 1024);
[34]478#endif
[784]479  }
[541]480  r_inputs = getMap (s1->content, "serviceType");
[34]481#ifdef DEBUG
[541]482  fprintf (stderr, "LOAD A %s SERVICE PROVIDER \n", r_inputs->value);
483  fflush (stderr);
[34]484#endif
[605]485
[917]486  map* libp = getMapFromMaps(m, "main", "libPath"); 
[541]487  if (strlen (r_inputs->value) == 1
488      && strncasecmp (r_inputs->value, "C", 1) == 0)
[605]489  {
490     if (libp != NULL && libp->value != NULL) {
491            r_inputs = getMap (s1->content, "ServiceProvider");
492                sprintf (tmps1, "%s/%s", libp->value, r_inputs->value);
493         }
494     else {     
495        r_inputs = getMap (request_inputs, "metapath");
496        if (r_inputs != NULL)
497          sprintf (tmps1, "%s/%s", ntmp, r_inputs->value);
498        else
499          sprintf (tmps1, "%s/", ntmp);
500         
501        char *altPath = zStrdup (tmps1);
502        r_inputs = getMap (s1->content, "ServiceProvider");
503        sprintf (tmps1, "%s/%s", altPath, r_inputs->value);
504        free (altPath);
505         }
[34]506#ifdef DEBUG
[541]507      fprintf (stderr, "Trying to load %s\n", tmps1);
[34]508#endif
509#ifdef WIN32
[541]510      HINSTANCE so =
511        LoadLibraryEx (tmps1, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
[34]512#else
[541]513      void *so = dlopen (tmps1, RTLD_LAZY);
[34]514#endif
[57]515#ifdef WIN32
[578]516      char* errstr = getLastErrorMessage();
[34]517#else
[541]518      char *errstr;
519      errstr = dlerror ();
[34]520#endif
[478]521#ifdef DEBUG
[578]522          fprintf (stderr, "%s loaded (%s) \n", tmps1, errstr);
[478]523#endif
[541]524      if (so != NULL)
525        {
[34]526#ifdef DEBUG
[541]527          fprintf (stderr, "Library loaded %s \n", errstr);
528          fprintf (stderr, "Service Shared Object = %s\n", r_inputs->value);
[34]529#endif
[541]530          r_inputs = getMap (s1->content, "serviceType");
[34]531#ifdef DEBUG
[541]532          dumpMap (r_inputs);
533          fprintf (stderr, "%s\n", r_inputs->value);
534          fflush (stderr);
[34]535#endif
[541]536          if (strncasecmp (r_inputs->value, "C-FORTRAN", 9) == 0)
537            {
538              r_inputs = getMap (request_inputs, "Identifier");
539              char fname[1024];
540              sprintf (fname, "%s_", r_inputs->value);
[34]541#ifdef DEBUG
[541]542              fprintf (stderr, "Try to load function %s\n", fname);
[34]543#endif
544#ifdef WIN32
[541]545              typedef int (CALLBACK * execute_t) (char ***, char ***,
546                                                  char ***);
547              execute_t execute = (execute_t) GetProcAddress (so, fname);
[34]548#else
[541]549              typedef int (*execute_t) (char ***, char ***, char ***);
550              execute_t execute = (execute_t) dlsym (so, fname);
[34]551#endif
552#ifdef DEBUG
553#ifdef WIN32
[578]554                          errstr = getLastErrorMessage();
[34]555#else
[541]556              errstr = dlerror ();
[34]557#endif
[541]558              fprintf (stderr, "Function loaded %s\n", errstr);
559#endif
[34]560
[541]561              char main_conf[10][30][1024];
562              char inputs[10][30][1024];
563              char outputs[10][30][1024];
564              for (int i = 0; i < 10; i++)
565                {
566                  for (int j = 0; j < 30; j++)
567                    {
568                      memset (main_conf[i][j], 0, 1024);
569                      memset (inputs[i][j], 0, 1024);
570                      memset (outputs[i][j], 0, 1024);
571                    }
572                }
573              mapsToCharXXX (m, (char ***) main_conf);
574              mapsToCharXXX (request_input_real_format, (char ***) inputs);
575              mapsToCharXXX (request_output_real_format, (char ***) outputs);
576              *eres =
577                execute ((char ***) &main_conf[0], (char ***) &inputs[0],
578                         (char ***) &outputs[0]);
[34]579#ifdef DEBUG
[541]580              fprintf (stderr, "Function run successfully \n");
[34]581#endif
[541]582              charxxxToMaps ((char ***) &outputs[0],
583                             &request_output_real_format);
584            }
585          else
586            {
[34]587#ifdef DEBUG
588#ifdef WIN32
[578]589                          errstr = getLastErrorMessage();
590              fprintf (stderr, "Function %s failed to load because of %s\n",
[541]591                       r_inputs->value, errstr);
[34]592#endif
593#endif
[541]594              r_inputs = getMapFromMaps (m, "lenv", "Identifier");
[34]595#ifdef DEBUG
[541]596              fprintf (stderr, "Try to load function %s\n", r_inputs->value);
[34]597#endif
[541]598              typedef int (*execute_t) (maps **, maps **, maps **);
[34]599#ifdef WIN32
[541]600              execute_t execute =
601                (execute_t) GetProcAddress (so, r_inputs->value);
[34]602#else
[541]603              execute_t execute = (execute_t) dlsym (so, r_inputs->value);
[34]604#endif
605
[541]606              if (execute == NULL)
607                {
[469]608#ifdef WIN32
[578]609                                  errstr = getLastErrorMessage();
[469]610#else
[541]611                  errstr = dlerror ();
[469]612#endif
[541]613                  char *tmpMsg =
614                    (char *) malloc (2048 + strlen (r_inputs->value));
615                  sprintf (tmpMsg,
616                           _
[781]617                           ("Error occurred while running the %s function: %s"),
[541]618                           r_inputs->value, errstr);
619                  errorException (m, tmpMsg, "InternalError", NULL);
620                  free (tmpMsg);
[478]621#ifdef DEBUG
[541]622                  fprintf (stderr, "Function %s error %s\n", r_inputs->value,
623                           errstr);
[478]624#endif
[541]625                  *eres = -1;
626                  return;
627                }
[469]628
[34]629#ifdef DEBUG
630#ifdef WIN32
[578]631                          errstr = getLastErrorMessage();
[34]632#else
[541]633              errstr = dlerror ();
[34]634#endif
[541]635              fprintf (stderr, "Function loaded %s\n", errstr);
636#endif
[34]637
638#ifdef DEBUG
[541]639              fprintf (stderr, "Now run the function \n");
640              fflush (stderr);
[34]641#endif
[541]642              *eres =
643                execute (&m, &request_input_real_format,
644                         &request_output_real_format);
[34]645#ifdef DEBUG
[541]646              fprintf (stderr, "Function loaded and returned %d\n", eres);
647              fflush (stderr);
[34]648#endif
[541]649            }
[216]650#ifdef WIN32
[541]651          *ioutputs = dupMaps (&request_output_real_format);
652          FreeLibrary (so);
[216]653#else
[541]654          dlclose (so);
[216]655#endif
[541]656        }
657      else
658        {
[34]659      /**
660       * Unable to load the specified shared library
661       */
[541]662          char tmps[1024];
[34]663#ifdef WIN32
[578]664                  errstr = getLastErrorMessage();
[34]665#else
[578]666              errstr = dlerror ();
[34]667#endif
[605]668          sprintf (tmps, _("Unable to load C Library %s"), errstr);
[576]669          errorException(m,tmps,"InternalError",NULL);
[541]670          *eres = -1;
671        }
[34]672    }
673  else
[550]674
[917]675#ifdef USE_HPC
676  if (strncasecmp (r_inputs->value, "HPC", 3) == 0)
677    {
678      *eres =
679        zoo_hpc_support (&m, request_inputs, s1,
680                            &request_input_real_format,
681                            &request_output_real_format);
682    }
683  else
684#endif
685
[634]686#ifdef USE_SAGA
[917]687  if (strncasecmp (r_inputs->value, "SAGA", 4) == 0)
[634]688    {
689      *eres =
690        zoo_saga_support (&m, request_inputs, s1,
691                            &request_input_real_format,
692                            &request_output_real_format);
693    }
694  else
695#endif
696
[550]697#ifdef USE_OTB
[917]698  if (strncasecmp (r_inputs->value, "OTB", 3) == 0)
[550]699    {
700      *eres =
701        zoo_otb_support (&m, request_inputs, s1,
702                            &request_input_real_format,
703                            &request_output_real_format);
704    }
705  else
706#endif
[34]707#ifdef USE_PYTHON
[541]708  if (strncasecmp (r_inputs->value, "PYTHON", 6) == 0)
[917]709    {             
[541]710      *eres =
711        zoo_python_support (&m, request_inputs, s1,
712                            &request_input_real_format,
[917]713                            &request_output_real_format);         
714    }
715  else
716#endif
717
718#ifdef USE_R
719  if (strncasecmp (r_inputs->value, "R", 6) == 0)
720    {     
721      *eres =
722        zoo_r_support (&m, request_inputs, s1,
723                            &request_input_real_format,
[541]724                            &request_output_real_format);
[34]725    }
[541]726  else
[34]727#endif
[541]728
[34]729#ifdef USE_JAVA
[541]730  if (strncasecmp (r_inputs->value, "JAVA", 4) == 0)
731    {
732      *eres =
733        zoo_java_support (&m, request_inputs, s1, &request_input_real_format,
734                          &request_output_real_format);
735    }
736  else
[34]737#endif
738
739#ifdef USE_PHP
[541]740  if (strncasecmp (r_inputs->value, "PHP", 3) == 0)
741    {
742      *eres =
743        zoo_php_support (&m, request_inputs, s1, &request_input_real_format,
[788]744                         &request_output_real_format);         
[541]745    }
746  else
[34]747#endif
[541]748
749
[34]750#ifdef USE_PERL
[541]751  if (strncasecmp (r_inputs->value, "PERL", 4) == 0)
752    {
753      *eres =
754        zoo_perl_support (&m, request_inputs, s1, &request_input_real_format,
755                          &request_output_real_format);
756    }
757  else
[34]758#endif
759
760#ifdef USE_JS
[541]761  if (strncasecmp (r_inputs->value, "JS", 2) == 0)
762    {
763      *eres =
764        zoo_js_support (&m, request_inputs, s1, &request_input_real_format,
765                        &request_output_real_format);
766    }
767  else
[34]768#endif
[453]769
770#ifdef USE_RUBY
[541]771  if (strncasecmp (r_inputs->value, "Ruby", 4) == 0)
772    {
773      *eres =
774        zoo_ruby_support (&m, request_inputs, s1, &request_input_real_format,
775                          &request_output_real_format);
776    }
777  else
[453]778#endif
779
[794]780#ifdef USE_MONO
781  if (strncasecmp (r_inputs->value, "Mono", 4) == 0)
[541]782    {
[794]783      *eres =
784        zoo_mono_support (&m, request_inputs, s1, &request_input_real_format,
785                          &request_output_real_format);
786    }
787  else
788#endif
789
790    {
[541]791      char tmpv[1024];
792      sprintf (tmpv,
793               _
794               ("Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n"),
795               r_inputs->value);
[576]796      errorException (m, tmpv, "InternalError", NULL);
[541]797      *eres = -1;
798    }
799  *myMap = m;
[917]800  *ioutputs = request_output_real_format; 
[34]801}
802
[384]803
[216]804#ifdef WIN32
805/**
806 * createProcess function: create a new process after setting some env variables
807 */
[541]808void
809createProcess (maps * m, map * request_inputs, service * s1, char *opts,
810               int cpid, maps * inputs, maps * outputs)
811{
[216]812  STARTUPINFO si;
813  PROCESS_INFORMATION pi;
[541]814  ZeroMemory (&si, sizeof (si));
815  si.cb = sizeof (si);
816  ZeroMemory (&pi, sizeof (pi));
817  char *tmp = (char *) malloc ((1024 + cgiContentLength) * sizeof (char));
818  char *tmpq = (char *) malloc ((1024 + cgiContentLength) * sizeof (char));
819  map *req = getMap (request_inputs, "request");
820  map *id = getMap (request_inputs, "identifier");
821  map *di = getMap (request_inputs, "DataInputs");
[216]822
[583]823  // The required size for the dataInputsKVP and dataOutputsKVP buffers
824  // may exceed cgiContentLength, hence a 2 kb extension. However, a
825  // better solution would be to have getMapsAsKVP() determine the required
826  // buffer size before allocating memory.     
827  char *dataInputsKVP = getMapsAsKVP (inputs, cgiContentLength + 2048, 0);
828  char *dataOutputsKVP = getMapsAsKVP (outputs, cgiContentLength + 2048, 1);
[384]829#ifdef DEBUG
[541]830  fprintf (stderr, "DATAINPUTSKVP %s\n", dataInputsKVP);
831  fprintf (stderr, "DATAOUTPUTSKVP %s\n", dataOutputsKVP);
[384]832#endif
[680]833  map *sid = getMapFromMaps (m, "lenv", "osid");
834  map *usid = getMapFromMaps (m, "lenv", "usid");
[541]835  map *r_inputs = getMapFromMaps (m, "main", "tmpPath");
836  map *r_inputs1 = getMap (request_inputs, "metapath");
[605]837 
[541]838  int hasIn = -1;
839  if (r_inputs1 == NULL)
840    {
841      r_inputs1 = createMap ("metapath", "");
842      hasIn = 1;
843    }
844  map *r_inputs2 = getMap (request_inputs, "ResponseDocument");
845  if (r_inputs2 == NULL)
846    r_inputs2 = getMap (request_inputs, "RawDataOutput");
847  map *tmpPath = getMapFromMaps (m, "lenv", "cwd");
[216]848
[541]849  map *tmpReq = getMap (request_inputs, "xrequest");
[790]850
[587]851  if(r_inputs2 != NULL && tmpReq != NULL) {
[680]852    const char key[] = "rfile=";
853    char* kvp = (char*) malloc((FILENAME_MAX + strlen(key))*sizeof(char));
854    char* filepath = kvp + strlen(key);
855    strncpy(kvp, key, strlen(key));
856    addToCache(m, tmpReq->value, tmpReq->value, "text/xml", strlen(tmpReq->value), 
[682]857               filepath, FILENAME_MAX);
[587]858    if (filepath == NULL) {
[680]859      errorException( m, _("Unable to cache HTTP POST Execute request."), "InternalError", NULL); 
860      return;
[587]861    }   
[680]862    sprintf(tmp,"\"metapath=%s&%s&cgiSid=%s&usid=%s\"",
863            r_inputs1->value,kvp,sid->value,usid->value);
[587]864    sprintf(tmpq,"metapath=%s&%s",
[680]865            r_inputs1->value,kvp);
866    free(kvp); 
[587]867  }
868  else if (r_inputs2 != NULL)
[541]869    {
870      sprintf (tmp,
[680]871               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s&cgiSid=%s&usid=%s\"",
[541]872               r_inputs1->value, req->value, id->value, dataInputsKVP,
[680]873               r_inputs2->name, dataOutputsKVP, sid->value, usid->value);
[541]874      sprintf (tmpq,
875               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s",
876               r_inputs1->value, req->value, id->value, dataInputsKVP,
[587]877               r_inputs2->name, dataOutputsKVP);                   
[541]878    }
879  else
880    {
881      sprintf (tmp,
[680]882               "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&cgiSid=%s&usid=%s\"",
[541]883               r_inputs1->value, req->value, id->value, dataInputsKVP,
[680]884               sid->value, usid->value);
[541]885      sprintf (tmpq,
886               "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s",
887               r_inputs1->value, req->value, id->value, dataInputsKVP,
[587]888               sid->value);   
[541]889    }
890
891  if (hasIn > 0)
892    {
893      freeMap (&r_inputs1);
894      free (r_inputs1);
895    }
896  char *tmp1 = zStrdup (tmp);
[587]897  sprintf (tmp, "\"%s\" %s \"%s\"", PROGRAMNAME, tmp1, sid->value); 
[541]898  free (dataInputsKVP);
899  free (dataOutputsKVP);
[384]900#ifdef DEBUG
[541]901  fprintf (stderr, "REQUEST IS : %s \n", tmp);
[384]902#endif
[554]903
[680]904  usid = getMapFromMaps (m, "lenv", "usid");
[554]905  if (usid != NULL && usid->value != NULL) {
906    SetEnvironmentVariable("USID", TEXT (usid->value));
907  }
[541]908  SetEnvironmentVariable ("CGISID", TEXT (sid->value));
909  SetEnvironmentVariable ("QUERY_STRING", TEXT (tmpq));
[682]910  // knut: Prevent REQUEST_METHOD=POST in background process call to cgic:main
911  // (process hangs when reading cgiIn):
[587]912  SetEnvironmentVariable("REQUEST_METHOD", "GET");
[682]913  SetEnvironmentVariable("CONTENT_TYPE", "text/plain");
[587]914 
[216]915  char clen[1000];
[541]916  sprintf (clen, "%d", strlen (tmpq));
917  SetEnvironmentVariable ("CONTENT_LENGTH", TEXT (clen));
918
[682]919  // ref. https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863%28v=vs.85%29.aspx
[541]920  if (!CreateProcess (NULL,     // No module name (use command line)
921                      TEXT (tmp),       // Command line
922                      NULL,     // Process handle not inheritable
923                      NULL,     // Thread handle not inheritable
924                      FALSE,    // Set handle inheritance to FALSE
925                      CREATE_NO_WINDOW, // Apache won't wait until the end
926                      NULL,     // Use parent's environment block
927                      NULL,     // Use parent's starting directory
928                      &si,      // Pointer to STARTUPINFO struct
929                      &pi)      // Pointer to PROCESS_INFORMATION struct
930    )
931    {
[384]932#ifdef DEBUG
[541]933      fprintf (stderr, "CreateProcess failed (%d).\n", GetLastError ());
[384]934#endif
[587]935      if (tmp != NULL) {
936        free(tmp);
937      }
938      if (tmpq != NULL) {
939        free(tmpq);
940      }         
[541]941      return;
942    }
943  else
944    {
[384]945#ifdef DEBUG
[587]946      fprintf (stderr, "CreateProcess successful (%d).\n\n\n\n",
[541]947               GetLastError ());
[384]948#endif
[541]949    }
950  CloseHandle (pi.hProcess);
951  CloseHandle (pi.hThread);
[587]952 
953  if (tmp != NULL) {
954    free(tmp);
955  }
956  if (tmpq != NULL) {
957    free(tmpq);
958  }
959 
[384]960#ifdef DEBUG
[541]961  fprintf (stderr, "CreateProcess finished !\n");
[384]962#endif
[216]963}
964#endif
965
[607]966/**
967 * Process the request.
968 *
969 * @param inputs the request parameters map
970 * @return 0 on sucess, other value on failure
971 * @see conf_read,recursReaddirF
972 */
[541]973int
974runRequest (map ** inputs)
[1]975{
[788]976 
[53]977#ifndef USE_GDB
[554]978#ifndef WIN32
[541]979  signal (SIGCHLD, SIG_IGN);
[554]980#endif 
[541]981  signal (SIGSEGV, sig_handler);
982  signal (SIGTERM, sig_handler);
983  signal (SIGINT, sig_handler);
984  signal (SIGILL, sig_handler);
985  signal (SIGFPE, sig_handler);
986  signal (SIGABRT, sig_handler);
[53]987#endif
[9]988
[917]989 
[541]990  map *r_inputs = NULL;
991  map *request_inputs = *inputs;
[917]992  //fprintf(stderr,"%s \n",json_object_to_json_string_ext(mapToJson(request_inputs),JSON_C_TO_STRING_PLAIN));
993 
[605]994#ifdef IGNORE_METAPATH
995  addToMap(request_inputs, "metapath", "");
996#endif 
[541]997  maps *m = NULL;
998  char *REQUEST = NULL;
[1]999  /**
1000   * Parsing service specfic configuration file
1001   */
[541]1002  m = (maps *) malloc (MAPS_SIZE);
1003  if (m == NULL)
1004    {
[790]1005      return errorException (NULL, _("Unable to allocate memory"),
[541]1006                             "InternalError", NULL);
1007    }
[790]1008  m->child=NULL;
[1]1009  char ntmp[1024];
[784]1010#ifndef ETC_DIR
[1]1011#ifndef WIN32
[541]1012  getcwd (ntmp, 1024);
[1]1013#else
[541]1014  _getcwd (ntmp, 1024);
[1]1015#endif
[784]1016#else
1017  sprintf(ntmp,"%s",ETC_DIR);
1018#endif
[541]1019  r_inputs = getMapOrFill (&request_inputs, "metapath", "");
[282]1020
[9]1021  char conf_file[10240];
[541]1022  snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value);
[784]1023#ifdef ETC_DIR
1024#ifndef WIN32
[917]1025  getcwd (ntmp, 1024);
[784]1026#else
[917]1027  _getcwd (ntmp, 1024);
[784]1028#endif
1029#endif
[788]1030
[541]1031  if (conf_read (conf_file, m) == 2)
1032    {
1033      errorException (NULL, _("Unable to load the main.cfg file."),
1034                      "InternalError", NULL);
1035      free (m);
1036      return 1;
1037    }
[9]1038#ifdef DEBUG
[541]1039  fprintf (stderr, "***** BEGIN MAPS\n");
1040  dumpMaps (m);
1041  fprintf (stderr, "***** END MAPS\n");
[9]1042#endif
1043
[541]1044  map *getPath = getMapFromMaps (m, "main", "gettextPath");
1045  if (getPath != NULL)
1046    {
1047      bindtextdomain ("zoo-kernel", getPath->value);
1048      bindtextdomain ("zoo-services", getPath->value);
1049    }
1050  else
1051    {
[784]1052      bindtextdomain ("zoo-kernel", LOCALEDIR);
1053      bindtextdomain ("zoo-services", LOCALEDIR);
[541]1054    }
[364]1055
1056  /**
1057   * Manage our own error log file (usefull to separate standard apache debug
1058   * messages from the ZOO-Kernel ones but also for IIS users to avoid wrong
1059   * headers messages returned by the CGI due to wrong redirection of stderr)
1060   */
[541]1061  FILE *fstde = NULL;
1062  map *fstdem = getMapFromMaps (m, "main", "logPath");
1063  if (fstdem != NULL)
1064    fstde = freopen (fstdem->value, "a+", stderr);
[364]1065
[541]1066  r_inputs = getMap (request_inputs, "language");
1067  if (r_inputs == NULL)
[640]1068    r_inputs = getMap (request_inputs, "AcceptLanguages");
1069  if (r_inputs == NULL)
[541]1070    r_inputs = getMapFromMaps (m, "main", "language");
1071  if (r_inputs != NULL)
1072    {
1073      if (isValidLang (m, r_inputs->value) < 0)
1074        {
1075          char tmp[1024];
1076          sprintf (tmp,
1077                   _
1078                   ("The value %s is not supported for the <language> parameter"),
1079                   r_inputs->value);
1080          errorException (m, tmp, "InvalidParameterValue", "language");
1081          freeMaps (&m);
1082          free (m);
1083          free (REQUEST);
1084          return 1;
[501]1085
[541]1086        }
1087      char *tmp = zStrdup (r_inputs->value);
1088      setMapInMaps (m, "main", "language", tmp);
[466]1089#ifdef DEB
[541]1090      char tmp2[12];
1091      sprintf (tmp2, "%s.utf-8", tmp);
1092      translateChar (tmp2, '-', '_');
1093      setlocale (LC_ALL, tmp2);
[466]1094#else
[541]1095      translateChar (tmp, '-', '_');
1096      setlocale (LC_ALL, tmp);
[466]1097#endif
[444]1098#ifndef WIN32
[541]1099      setenv ("LC_ALL", tmp, 1);
[444]1100#else
[745]1101      char tmp1[13];
[541]1102      sprintf (tmp1, "LC_ALL=%s", tmp);
[917]1103      _putenv (tmp1);
[376]1104#endif
[541]1105      free (tmp);
1106    }
1107  else
1108    {
1109      setlocale (LC_ALL, "en_US");
[444]1110#ifndef WIN32
[541]1111      setenv ("LC_ALL", "en_US", 1);
[444]1112#else
[745]1113      char tmp1[13];
[541]1114      sprintf (tmp1, "LC_ALL=en_US");
[917]1115      _putenv (tmp1);
[376]1116#endif
[541]1117      setMapInMaps (m, "main", "language", "en-US");
1118    }
[745]1119  setlocale (LC_NUMERIC, "C");
1120#ifndef WIN32
1121  setenv ("LC_NUMERIC", "C", 1);
1122#else
1123  char tmp1[17];
1124  sprintf (tmp1, "LC_NUMERIC=C");
[917]1125  _putenv (tmp1);
[745]1126#endif
[541]1127  bind_textdomain_codeset ("zoo-kernel", "UTF-8");
1128  textdomain ("zoo-kernel");
1129  bind_textdomain_codeset ("zoo-services", "UTF-8");
1130  textdomain ("zoo-services");
[34]1131
[541]1132  map *lsoap = getMap (request_inputs, "soap");
1133  if (lsoap != NULL && strcasecmp (lsoap->value, "true") == 0)
1134    setMapInMaps (m, "main", "isSoap", "true");
[280]1135  else
[541]1136    setMapInMaps (m, "main", "isSoap", "false");
[34]1137
[584]1138  if(strlen(cgiServerName)>0)
[654]1139    {
1140      char tmpUrl[1024];
[584]1141       
[680]1142      if ( getenv("HTTPS") != NULL && strncmp(getenv("HTTPS"), "on", 2) == 0 ) {
1143        // Knut: check if non-empty instead of "on"?           
[654]1144        if ( strncmp(cgiServerPort, "443", 3) == 0 ) { 
1145          sprintf(tmpUrl, "https://%s%s", cgiServerName, cgiScriptName);
[584]1146        }
1147        else {
[654]1148          sprintf(tmpUrl, "https://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
[584]1149        }
[654]1150      }
1151      else {
1152        if ( strncmp(cgiServerPort, "80", 2) == 0 ) { 
1153          sprintf(tmpUrl, "http://%s%s", cgiServerName, cgiScriptName);
1154        }
1155        else {
1156          sprintf(tmpUrl, "http://%s:%s%s", cgiServerName, cgiServerPort, cgiScriptName);
1157        }
1158      }
[445]1159#ifdef DEBUG
[654]1160      fprintf(stderr,"*** %s ***\n",tmpUrl);
[445]1161#endif
[654]1162      setMapInMaps(m,"main","serverAddress",tmpUrl);
1163    }
[381]1164
[752]1165  // CORS Support
1166  if(strncasecmp(cgiRequestMethod,"OPTIONS",7)==0){
1167    map* cors=getMapFromMaps(m,"main","cors");
1168    if(cors!=NULL && strncasecmp(cors->value,"true",4)==0){
1169      char *encoding=getEncoding(m);
1170      printHeaders(m);
1171      printf("Content-Type: text/plain; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
1172      printf(_("CORS is enabled.\r\n"));
1173      freeMaps (&m);
1174      free (m);
1175      fflush (stdout);
1176      return 3;
1177    }
1178  }
1179
[654]1180  //Check for minimum inputs
1181  map* version=getMap(request_inputs,"version");
1182  if(version==NULL)
1183    version=getMapFromMaps(m,"main","version");
1184  setMapInMaps(m,"main","rversion",version->value);
1185  int vid=getVersionId(version->value);
1186  if(vid<0)
1187    vid=0;
[576]1188  map* err=NULL;
[654]1189  const char **vvr=(const char**)requests[vid];
1190  checkValidValue(request_inputs,&err,"request",vvr,1);
[576]1191  const char *vvs[]={
1192    "WPS",
1193    NULL
1194  };
1195  if(err!=NULL){
1196    checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
1197    printExceptionReportResponse (m, err);
1198    freeMap(&err);
1199    free(err);
1200    if (count (request_inputs) == 1)
1201      {
1202        freeMap (&request_inputs);
1203        free (request_inputs);
1204      }
1205    freeMaps (&m);
1206    free (m);
1207    return 1;
1208  }
1209  checkValidValue(request_inputs,&err,"service",(const char**)vvs,1);
[640]1210
[576]1211  const char *vvv[]={
1212    "1.0.0",
[640]1213    "2.0.0",
[576]1214    NULL
1215  };
[541]1216  r_inputs = getMap (request_inputs, "Request");
[917]1217  if(r_inputs!=NULL)
1218    REQUEST = zStrdup (r_inputs->value);
[654]1219  int reqId=-1;
[576]1220  if (strncasecmp (REQUEST, "GetCapabilities", 15) != 0){
1221    checkValidValue(request_inputs,&err,"version",(const char**)vvv,1);
[654]1222    int j=0;
1223    for(j=0;j<nbSupportedRequests;j++){
1224      if(requests[vid][j]!=NULL && requests[vid][j+1]!=NULL){
[917]1225        if(j<nbReqIdentifier && strncasecmp(REQUEST,requests[vid][j+1],strlen(requests[vid][j+1]))==0){
[654]1226          checkValidValue(request_inputs,&err,"identifier",NULL,1);
1227          reqId=j+1;
1228          break;
1229        }
1230        else
1231          if(j>=nbReqIdentifier && j<nbReqIdentifier+nbReqJob && 
[917]1232             strncasecmp(REQUEST,requests[vid][j+1],strlen(requests[vid][j+1]))==0){
[654]1233            checkValidValue(request_inputs,&err,"jobid",NULL,1);
1234            reqId=j+1;
1235            break;
1236          }
1237      }else
1238        break;
1239    }
[576]1240  }else{
1241    checkValidValue(request_inputs,&err,"AcceptVersions",(const char**)vvv,-1);
[658]1242    map* version1=getMap(request_inputs,"AcceptVersions");
1243    if(version1!=NULL){
[931]1244      if(strstr(version1->value,schemas[1][0])!=NULL){
[640]1245        addToMap(request_inputs,"version",schemas[1][0]);
[931]1246        setMapInMaps(m,"main","rversion",schemas[1][0]);
1247      }
1248      else{
[658]1249        addToMap(request_inputs,"version",version1->value);
[931]1250        setMapInMaps(m,"main","rversion",version1->value);
1251      }
[658]1252      version=getMap(request_inputs,"version");
[640]1253    }
[576]1254  }
1255  if(err!=NULL){
1256    printExceptionReportResponse (m, err);
1257    freeMap(&err);
1258    free(err);
1259    if (count (request_inputs) == 1)
1260      {
1261        freeMap (&request_inputs);
1262        free (request_inputs);
1263      }
1264    free(REQUEST);
1265    freeMaps (&m);
1266    free (m);
1267    return 1;
1268  }
[1]1269
[541]1270  r_inputs = getMap (request_inputs, "serviceprovider");
1271  if (r_inputs == NULL)
1272    {
1273      addToMap (request_inputs, "serviceprovider", "");
1274    }
[1]1275
[541]1276  maps *request_output_real_format = NULL;
1277  map *tmpm = getMapFromMaps (m, "main", "serverAddress");
1278  if (tmpm != NULL)
1279    SERVICE_URL = zStrdup (tmpm->value);
[1]1280  else
[541]1281    SERVICE_URL = zStrdup (DEFAULT_SERVICE_URL);
[1]1282
[607]1283
1284
[541]1285  service *s1;
1286  int scount = 0;
[1]1287#ifdef DEBUG
[541]1288  dumpMap (r_inputs);
[1]1289#endif
1290  char conf_dir[1024];
1291  int t;
1292  char tmps1[1024];
1293
[541]1294  r_inputs = NULL;
1295  r_inputs = getMap (request_inputs, "metapath");
[784]1296  map* cwdMap0=getMapFromMaps(m,"main","servicePath");
[541]1297  if (r_inputs != NULL)
[784]1298    if(cwdMap0!=NULL)
1299      snprintf (conf_dir, 1024, "%s/%s", cwdMap0->value, r_inputs->value);
1300    else
1301      snprintf (conf_dir, 1024, "%s/%s", ntmp, r_inputs->value);
[9]1302  else
[784]1303    if(cwdMap0!=NULL)
1304      snprintf (conf_dir, 1024, "%s", cwdMap0->value);
1305    else
1306      snprintf (conf_dir, 1024, "%s", ntmp);
[607]1307  map* reg = getMapFromMaps (m, "main", "registry");
1308  registry* zooRegistry=NULL;
1309  if(reg!=NULL){
[917]1310#ifndef WIN32
1311    int saved_stdout = zDup (fileno (stdout));
1312    zDup2 (fileno (stderr), fileno (stdout));
1313#endif
[676]1314    if(createRegistry (m,&zooRegistry,reg->value)<0){
1315      map *message=getMapFromMaps(m,"lenv","message");
1316      map *type=getMapFromMaps(m,"lenv","type");
[917]1317#ifndef WIN32
1318      zDup2 (saved_stdout, fileno (stdout));
1319#endif
[676]1320      errorException (m, message->value,
1321                      type->value, NULL);
1322      return 0;
1323    }
[917]1324#ifndef WIN32
1325    zDup2 (saved_stdout, fileno (stdout));
1326    zClose(saved_stdout);
1327#endif
[607]1328  }
1329
[541]1330  if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0)
1331    {
[1]1332#ifdef DEBUG
[541]1333      dumpMap (r_inputs);
[1]1334#endif
[541]1335      xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
[656]1336      xmlNodePtr n=printGetCapabilitiesHeader(doc,m,(version!=NULL?version->value:"1.0.0"));
[576]1337      /**
1338       * Here we need to close stdout to ensure that unsupported chars
1339       * has been found in the zcfg and then printed on stdout
1340       */
[917]1341      int saved_stdout = zDup (fileno (stdout));
1342      zDup2 (fileno (stderr), fileno (stdout));
[789]1343
1344      maps* imports = getMaps(m, IMPORTSERVICE);
1345      if (imports != NULL) {       
1346        map* zcfg = imports->content;
1347       
1348        while (zcfg != NULL) {
1349          if (zcfg->value != NULL) {
1350            service* svc = (service*) malloc(SERVICE_SIZE);
1351            if (svc == NULL || readServiceFile(m, zcfg->value, &svc, zcfg->name) < 0) {
1352              // pass over silently
1353              zcfg = zcfg->next;
1354              continue;
1355            }
1356            inheritance(zooRegistry, &svc);
[917]1357            printGetCapabilitiesForProcess(zooRegistry, m, doc, n, svc);
[789]1358            freeService(&svc);
1359            free(svc);                             
1360          }
1361          zcfg = zcfg->next;
1362        }           
1363      }
1364
[584]1365      if (int res =               
[917]1366          recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0,
[541]1367                          printGetCapabilitiesForProcess) < 0)
1368        {
1369          freeMaps (&m);
1370          free (m);
[607]1371          if(zooRegistry!=NULL){
1372            freeRegistry(&zooRegistry);
1373            free(zooRegistry);
1374          }
[541]1375          free (REQUEST);
1376          free (SERVICE_URL);
1377          fflush (stdout);
1378          return res;
1379        }
[656]1380      fflush (stdout);
[917]1381      zDup2 (saved_stdout, fileno (stdout));
1382#ifdef META_DB
1383      fetchServicesFromDb(zooRegistry,m,doc,n,printGetCapabilitiesForProcess,1);
1384      close_sql(m,0);
1385#endif     
1386      printDocument (m, doc, zGetpid ());
[541]1387      freeMaps (&m);
1388      free (m);
[607]1389      if(zooRegistry!=NULL){
1390        freeRegistry(&zooRegistry);
1391        free(zooRegistry);
1392      }
[541]1393      free (REQUEST);
1394      free (SERVICE_URL);
1395      fflush (stdout);
[9]1396      return 0;
[1]1397    }
[541]1398  else
1399    {
[654]1400      r_inputs = getMap (request_inputs, "JobId");
1401      if(reqId>nbReqIdentifier){
[917]1402        if (strncasecmp (REQUEST, "GetStatus", 9) == 0 ||
1403            strncasecmp (REQUEST, "GetResult", 9) == 0){
[654]1404          runGetStatus(m,r_inputs->value,REQUEST);
[917]1405#ifdef RELY_ON_DB
1406          map* dsNb=getMapFromMaps(m,"lenv","ds_nb");
1407          if(dsNb!=NULL && atoi(dsNb->value)>1)
1408            close_sql(m,1);
1409          close_sql(m,0);
1410#endif
1411         
[654]1412          freeMaps (&m);
[917]1413          free(m);
[607]1414          if(zooRegistry!=NULL){
1415            freeRegistry(&zooRegistry);
1416            free(zooRegistry);
1417          }
[654]1418          free (REQUEST);
1419          free (SERVICE_URL);
1420          return 0;
1421        }
1422        else
1423          if (strncasecmp (REQUEST, "Dismiss", strlen(REQUEST)) == 0){
1424            runDismiss(m,r_inputs->value);
1425            freeMaps (&m);
1426            free (m);
1427            if(zooRegistry!=NULL){
1428              freeRegistry(&zooRegistry);
1429              free(zooRegistry);
1430            }
1431            free (REQUEST);
1432            free (SERVICE_URL);
1433            return 0;
1434           
1435          }
1436        return 0;
1437      }
1438      if(reqId<=nbReqIdentifier){
1439        r_inputs = getMap (request_inputs, "Identifier");
[9]1440
[654]1441        struct dirent *dp;
1442        DIR *dirp = opendir (conf_dir);
1443        if (dirp == NULL)
1444          {
[725]1445            errorException (m, _("The specified path does not exist."),
[784]1446                            "InternalError", NULL);
[654]1447            freeMaps (&m);
1448            free (m);
1449            if(zooRegistry!=NULL){
1450              freeRegistry(&zooRegistry);
1451              free(zooRegistry);
1452            }
1453            free (REQUEST);
1454            free (SERVICE_URL);
1455            return 0;
1456          }
1457        if (strncasecmp (REQUEST, "DescribeProcess", 15) == 0)
1458          {
1459            /**
1460             * Loop over Identifier list
1461             */
1462            xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0");
1463            r_inputs = NULL;
1464            r_inputs = getMap (request_inputs, "version");
[917]1465#ifdef DEBUG
1466            fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
1467            fflush(stderr);
1468#endif
[654]1469            xmlNodePtr n = printWPSHeader(doc,m,"DescribeProcess",
[656]1470                                          root_nodes[vid][1],(version!=NULL?version->value:"1.0.0"),1);
[469]1471
[654]1472            r_inputs = getMap (request_inputs, "Identifier");
[503]1473
[654]1474            char *orig = zStrdup (r_inputs->value);
[541]1475
[917]1476            int saved_stdout = zDup (fileno (stdout));
1477            zDup2 (fileno (stderr), fileno (stdout));
[654]1478            if (strcasecmp ("all", orig) == 0)
1479              {
[789]1480                maps* imports = getMaps(m, IMPORTSERVICE); 
1481                if (imports != NULL) {       
1482                  map* zcfg = imports->content;
1483           
1484                  while (zcfg != NULL) {
1485                    if (zcfg->value != NULL) {
1486                      service* svc = (service*) malloc(SERVICE_SIZE);
1487                      if (svc == NULL || readServiceFile(m, zcfg->value, &svc, zcfg->name) < 0) {
1488                        // pass over silently
1489                        zcfg = zcfg->next;
1490                        continue;
1491                      }
1492                      inheritance(zooRegistry, &svc);
[917]1493#ifdef USE_HPC
1494                      addNestedOutputs(&svc);
1495#endif
1496
1497                      printDescribeProcessForProcess(zooRegistry, m, doc, n, svc);
[789]1498                      freeService(&svc);
1499                      free(svc);                             
1500                    }
1501                    zcfg = zcfg->next;
1502                  }           
1503                }
1504 
[654]1505                if (int res =
[917]1506                    recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0,
[654]1507                                    printDescribeProcessForProcess) < 0)
1508                  return res;
[917]1509#ifdef META_DB
1510                fetchServicesFromDb(zooRegistry,m,doc,n,printDescribeProcessForProcess,0);
1511                close_sql(m,0);
1512#endif     
1513
[654]1514              }
1515            else
1516              {
1517                char *saveptr;
1518                char *tmps = strtok_r (orig, ",", &saveptr);
[541]1519
[654]1520                char buff[256];
1521                char buff1[1024];
1522                while (tmps != NULL)
1523                  {
1524                    int hasVal = -1;
1525                    char *corig = zStrdup (tmps);
[789]1526                    map* import = getMapFromMaps (m, IMPORTSERVICE, corig);   
1527                    if (import != NULL && import->value != NULL) 
[654]1528                      {
[917]1529#ifdef META_DB                 
1530                        service* s2=extractServiceFromDb(m,import->name,0);
1531                        if(s2==NULL){
1532#endif
1533                          s1 = createService();
1534                          t = readServiceFile (m, import->value, &s1, import->name);
[789]1535               
[917]1536                          if (t < 0) // failure reading zcfg
1537                            {
1538                              map *tmp00 = getMapFromMaps (m, "lenv", "message");
1539                              char tmp01[1024];
1540                              if (tmp00 != NULL)
1541                                sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"), import->value, tmp00->value);
1542                              else
1543                                sprintf (tmp01, _("Unable to parse the ZCFG file: %s."), import->value);
1544                             
1545                              zDup2 (saved_stdout, fileno (stdout));
1546                              errorException (m, tmp01, "InternalError", NULL);
1547                             
1548                              freeMaps (&m);
1549                              free (m);
[541]1550
[917]1551                              if(zooRegistry!=NULL){
1552                                freeRegistry(&zooRegistry);
1553                                free(zooRegistry);
1554                              }
1555                              free (orig);
1556                              free (REQUEST);
1557                              closedir (dirp);
1558                              //xmlFreeDoc (doc);
1559                              xmlCleanupParser ();
1560                              zooXmlCleanupNs ();
1561                   
1562                              return 1;
[789]1563                            }
1564#ifdef DEBUG
[917]1565                          dumpService (s1);
[789]1566#endif
[917]1567                          inheritance(zooRegistry,&s1);
1568#ifdef USE_HPC
1569                          addNestedOutputs(&s1);
1570#endif
1571                          printDescribeProcessForProcess (zooRegistry, m, doc, n, s1);
1572                          freeService (&s1);
1573                          free (s1);
1574                          s1 = NULL;
1575                          scount++;
1576                          hasVal = 1;               
1577#ifdef META_DB
1578                        }
1579#endif
1580                      }
[789]1581                    else if (strstr (corig, ".") != NULL)
1582                      {
1583
[654]1584                        parseIdentifier (m, conf_dir, corig, buff1);
1585                        map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1586                        if (tmpMap != NULL)
1587                          addToMap (request_inputs, "metapath", tmpMap->value);
1588                        map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier");
[917]1589                        /**
1590                         * No support for dot in service name stored in metadb!?
1591                         #ifdef META_DB
1592                         service* s2=extractServiceFromDb(m,tmpMapI->value,0);
1593                         if(s2==NULL){
1594                         #endif
1595                        */
1596                        s1 = createService();
[654]1597                        t = readServiceFile (m, buff1, &s1, tmpMapI->value);
1598                        if (t < 0)
1599                          {
1600                            map *tmp00 = getMapFromMaps (m, "lenv", "message");
1601                            char tmp01[1024];
1602                            if (tmp00 != NULL)
1603                              sprintf (tmp01,
1604                                       _
1605                                       ("Unable to parse the ZCFG file for the following ZOO-Service: %s. Message: %s"),
1606                                       tmps, tmp00->value);
1607                            else
1608                              sprintf (tmp01,
1609                                       _
1610                                       ("Unable to parse the ZCFG file for the following ZOO-Service: %s."),
1611                                       tmps);
[917]1612                            zDup2 (saved_stdout, fileno (stdout));
[654]1613                            errorException (m, tmp01, "InvalidParameterValue",
1614                                            "identifier");
1615                            freeMaps (&m);
1616                            free (m);
1617                            if(zooRegistry!=NULL){
1618                              freeRegistry(&zooRegistry);
1619                              free(zooRegistry);
1620                            }
1621                            free (REQUEST);
1622                            free (corig);
1623                            free (orig);
1624                            free (SERVICE_URL);
1625                            free (s1);
1626                            closedir (dirp);
[917]1627                            //xmlFreeDoc (doc);
[654]1628                            xmlCleanupParser ();
1629                            zooXmlCleanupNs ();
1630                            return 1;
[607]1631                          }
[9]1632#ifdef DEBUG
[654]1633                        dumpService (s1);
[9]1634#endif
[654]1635                        inheritance(zooRegistry,&s1);
[917]1636#ifdef USE_HPC
1637                        addNestedOutputs(&s1);
1638#endif
1639                        printDescribeProcessForProcess (zooRegistry, m, doc, n, s1);
[654]1640                        freeService (&s1);
1641                        free (s1);
1642                        s1 = NULL;
1643                        scount++;
1644                        hasVal = 1;
1645                        setMapInMaps (m, "lenv", "level", "0");
1646                      }
1647                    else
1648                      {
[917]1649#ifdef META_DB
1650                        _init_sql(m,"metadb");
1651                        //FAILED CONNECTING DB
1652                        if(getMapFromMaps(m,"lenv","dbIssue")!=NULL){
1653                          fprintf(stderr,"ERROR CONNECTING METADB");
1654                        }
1655                        service* s2=extractServiceFromDb(m,corig,0);
1656                        if(s2!=NULL){
1657                          inheritance(zooRegistry,&s2);
1658#ifdef USE_HPC
1659                          addNestedOutputs(&s2);
1660#endif
1661                          printDescribeProcessForProcess (zooRegistry,m, doc, n, s2);
1662                          freeService (&s2);
1663                          free (s2);
1664                          s2 = NULL;
1665                          hasVal = 1;
1666                        }else /*TOTO*/{
1667#endif
1668                          memset (buff, 0, 256);
1669                          snprintf (buff, 256, "%s.zcfg", corig);
1670                          memset (buff1, 0, 1024);
[469]1671#ifdef DEBUG
[917]1672                          printf ("\n#######%s\n########\n", buff);
[469]1673#endif
[917]1674                          while ((dp = readdir (dirp)) != NULL)
1675                            {
1676                              if (strcasecmp (dp->d_name, buff) == 0)
1677                                {
1678                                  memset (buff1, 0, 1024);
1679                                  snprintf (buff1, 1024, "%s/%s", conf_dir,
1680                                            dp->d_name);
1681                                  s1 = createService();
1682                                  if (s1 == NULL)
1683                                    {
1684                                      zDup2 (saved_stdout, fileno (stdout));
1685                                      return errorException (m,
1686                                                             _
1687                                                             ("Unable to allocate memory"),
1688                                                             "InternalError",
1689                                                             NULL);
1690                                    }
[790]1691#ifdef DEBUG_SERVICE_CONF
[917]1692                                  fprintf
1693                                    (stderr,"#################\n(%s) %s\n#################\n",
1694                                     r_inputs->value, buff1);
[469]1695#endif
[917]1696                                  char *tmp0 = zStrdup (dp->d_name);
1697                                  tmp0[strlen (tmp0) - 5] = 0;
1698                                  t = readServiceFile (m, buff1, &s1, tmp0);
1699                                  free (tmp0);
1700                                  if (t < 0)
1701                                    {
1702                                      map *tmp00 =
1703                                        getMapFromMaps (m, "lenv", "message");
1704                                      char tmp01[1024];
1705                                      if (tmp00 != NULL)
1706                                        sprintf (tmp01,
1707                                                 _
1708                                                 ("Unable to parse the ZCFG file: %s (%s)"),
1709                                                 dp->d_name, tmp00->value);
1710                                      else
1711                                        sprintf (tmp01,
1712                                                 _
1713                                                 ("Unable to parse the ZCFG file: %s."),
1714                                                 dp->d_name);
1715                                      zDup2 (saved_stdout, fileno (stdout));
1716                                      errorException (m, tmp01, "InternalError",
1717                                                      NULL);
1718                                      freeMaps (&m);
1719                                      free (m);
1720                                      if(zooRegistry!=NULL){
1721                                        freeRegistry(&zooRegistry);
1722                                        free(zooRegistry);
1723                                      }
1724                                      free (orig);
1725                                      free (REQUEST);
1726                                      closedir (dirp);
1727                                      //xmlFreeDoc (doc);
1728                                      xmlCleanupParser ();
1729                                      zooXmlCleanupNs ();
1730                                      return 1;
[654]1731                                    }
[469]1732#ifdef DEBUG
[917]1733                                  dumpService (s1);
[469]1734#endif
[917]1735                                  inheritance(zooRegistry,&s1);
1736#ifdef USE_HPC
1737                                  addNestedOutputs(&s1);
1738#endif
1739                                  /*json_object* jobj=serviceToJson(s1);
1740                                  const char* jsonStr=json_object_to_json_string_ext(jobj,JSON_C_TO_STRING_PLAIN);
1741                                  fprintf(stderr,"*** %s %d %s \n",__FILE__,__LINE__,jsonStr);*/
1742
1743                                  printDescribeProcessForProcess (zooRegistry,m, doc, n, s1);
1744                                  freeService (&s1);
1745                                  free (s1);
1746                                  s1 = NULL;
1747                                  scount++;
1748                                  hasVal = 1;
1749                                }
1750                            }
1751#ifdef META_DB
1752                        }
1753#endif
1754                      }               
[654]1755                    if (hasVal < 0)
1756                      {
1757                        map *tmp00 = getMapFromMaps (m, "lenv", "message");
1758                        char tmp01[1024];
1759                        if (tmp00 != NULL)
1760                          sprintf (tmp01,
1761                                   _("Unable to parse the ZCFG file: %s (%s)"),
1762                                   buff, tmp00->value);
1763                        else
1764                          sprintf (tmp01,
1765                                   _("Unable to parse the ZCFG file: %s."),
1766                                   buff);
[917]1767                        zDup2 (saved_stdout, fileno (stdout));
[654]1768                        errorException (m, tmp01, "InvalidParameterValue",
1769                                        "Identifier");
1770                        freeMaps (&m);
1771                        free (m);
1772                        if(zooRegistry!=NULL){
1773                          freeRegistry(&zooRegistry);
1774                          free(zooRegistry);
1775                        }
1776                        free (orig);
1777                        free (REQUEST);
1778                        closedir (dirp);
[917]1779                        if (corig != NULL)
1780                          free (corig);
[654]1781                        xmlFreeDoc (doc);
1782                        xmlCleanupParser ();
1783                        zooXmlCleanupNs ();
1784                        return 1;
1785                      }
1786                    rewinddir (dirp);
1787                    tmps = strtok_r (NULL, ",", &saveptr);
1788                    if (corig != NULL)
1789                      free (corig);
[917]1790                  }               
[654]1791              }
1792            closedir (dirp);
1793            fflush (stdout);
[917]1794            zDup2 (saved_stdout, fileno (stdout));
[654]1795            free (orig);
[917]1796            printDocument (m, doc, zGetpid ());
[654]1797            freeMaps (&m);
1798            free (m);
1799            if(zooRegistry!=NULL){
1800              freeRegistry(&zooRegistry);
1801              free(zooRegistry);
1802            }
1803            free (REQUEST);
1804            free (SERVICE_URL);
1805            fflush (stdout);
[917]1806#ifdef META_DB
1807            close_sql(m,0);
1808            //end_sql();
1809#endif
[654]1810            return 0;
[607]1811          }
[654]1812        else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0)
1813          {
1814            map* version=getMapFromMaps(m,"main","rversion");
[738]1815            int vid=getVersionId(version->value);           
[917]1816            int len = 0;
1817            int j = 0;
[654]1818            for(j=0;j<nbSupportedRequests;j++){
1819              if(requests[vid][j]!=NULL)
1820                len+=strlen(requests[vid][j])+2;
1821              else{
1822                len+=4;
1823                break;
1824              }
1825            }
1826            char *tmpStr=(char*)malloc(len*sizeof(char));
1827            int it=0;
1828            for(j=0;j<nbSupportedRequests;j++){
1829              if(requests[vid][j]!=NULL){
1830                if(it==0){
1831                  sprintf(tmpStr,"%s",requests[vid][j]);
1832                  it++;
1833                }else{
1834                  char *tmpS=zStrdup(tmpStr);
1835                  if(j+1<nbSupportedRequests && requests[vid][j+1]==NULL){
1836                    sprintf(tmpStr,"%s and %s",tmpS,requests[vid][j]);
1837                  }else{
1838                    sprintf(tmpStr,"%s, %s",tmpS,requests[vid][j]);
1839                 
1840                  }
1841                  free(tmpS);
1842                }
1843              }
1844              else{
1845                len+=4;
1846                break;
1847              }
1848            }
1849            char* message=(char*)malloc((61+len)*sizeof(char));
1850            sprintf(message,"The <request> value was not recognized. Allowed values are %s.",tmpStr);
1851            errorException (m,_(message),"InvalidParameterValue", "request");
[541]1852#ifdef DEBUG
[654]1853            fprintf (stderr, "No request found %s", REQUEST);
[541]1854#endif
[654]1855            closedir (dirp);
1856            freeMaps (&m);
1857            free (m);
1858            if(zooRegistry!=NULL){
1859              freeRegistry(&zooRegistry);
1860              free(zooRegistry);
1861            }
1862            free (REQUEST);
1863            free (SERVICE_URL);
1864            fflush (stdout);
1865            return 0;
[607]1866          }
[654]1867        closedir (dirp);
1868      }
[1]1869    }
[541]1870
[654]1871  map *postRequest = NULL;
1872  postRequest = getMap (request_inputs, "xrequest");
[790]1873 
[654]1874  if(vid==1 && postRequest==NULL){
1875    errorException (m,_("Unable to run Execute request using the GET HTTP method"),"InvalidParameterValue", "request"); 
1876    freeMaps (&m);
1877    free (m);
1878    if(zooRegistry!=NULL){
1879      freeRegistry(&zooRegistry);
1880      free(zooRegistry);
1881    }
1882    free (REQUEST);
1883    free (SERVICE_URL);
1884    fflush (stdout);
1885    return 0;
1886  }
[541]1887  s1 = NULL;
[917]1888 
[541]1889  r_inputs = getMap (request_inputs, "Identifier");
[789]1890  map* import = getMapFromMaps (m, IMPORTSERVICE, r_inputs->value); 
1891  if (import != NULL && import->value != NULL) { 
[917]1892    strncpy(tmps1, import->value, 1024);
1893    setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
1894    setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value);
[789]1895  } 
1896  else {
1897    snprintf (tmps1, 1024, "%s/%s.zcfg", conf_dir, r_inputs->value);
[1]1898#ifdef DEBUG
[789]1899    fprintf (stderr, "Trying to load %s\n", tmps1);
[1]1900#endif
[789]1901    if (strstr (r_inputs->value, ".") != NULL)
1902      {
1903        char *identifier = zStrdup (r_inputs->value);
1904        parseIdentifier (m, conf_dir, identifier, tmps1);
1905        map *tmpMap = getMapFromMaps (m, "lenv", "metapath");
1906        if (tmpMap != NULL)
1907          addToMap (request_inputs, "metapath", tmpMap->value);
1908        free (identifier);
1909      }
1910    else
1911      {
1912        setMapInMaps (m, "lenv", "Identifier", r_inputs->value);
1913        setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value);
1914      }
1915  }
[539]1916
[541]1917  r_inputs = getMapFromMaps (m, "lenv", "Identifier");
[917]1918 
1919#ifdef META_DB
1920  int metadb_id=_init_sql(m,"metadb");
1921  //FAILED CONNECTING DB
1922  if(getMapFromMaps(m,"lenv","dbIssue")!=NULL || metadb_id<0){
1923    fprintf(stderr,"ERROR CONNECTING METADB\n");
[607]1924  }
[917]1925  if(metadb_id>=0)
1926    s1=extractServiceFromDb(m,r_inputs->value,0);
1927  //close_sql(m,0);
1928  if(s1!=NULL){
1929    inheritance(zooRegistry,&s1);
1930#ifdef USE_HPC
1931    addNestedOutputs(&s1);
1932#endif
1933    if(zooRegistry!=NULL){
1934      freeRegistry(&zooRegistry);
1935      free(zooRegistry);
[541]1936    }
[917]1937  }else /* Not found in MetaDB */{
1938#endif
1939    s1 = createService();
1940    if (s1 == NULL)
1941      {
1942        freeMaps (&m);
1943        free (m);
1944        if(zooRegistry!=NULL){
1945          freeRegistry(&zooRegistry);
1946          free(zooRegistry);
1947        }
1948        free (REQUEST);
1949        free (SERVICE_URL);
1950        return errorException (m, _("Unable to allocate memory"),
1951                               "InternalError", NULL);
1952      }
[1]1953
[917]1954    int saved_stdout = zDup (fileno (stdout));
1955    zDup2 (fileno (stderr), fileno (stdout));
1956    t = readServiceFile (m, tmps1, &s1, r_inputs->value);
1957    if(t>=0){
1958      inheritance(zooRegistry,&s1);
1959#ifdef USE_HPC
1960      addNestedOutputs(&s1);
1961#endif
1962    }
1963    if(zooRegistry!=NULL){
1964      freeRegistry(&zooRegistry);
1965      free(zooRegistry);
1966    }
1967    fflush (stdout);
1968    zDup2 (saved_stdout, fileno (stdout));
1969    if (t < 0)
1970      {
1971        char *tmpMsg = (char *) malloc (2048 + strlen (r_inputs->value));
1972        sprintf (tmpMsg,
1973                 _
1974                 ("The value for <identifier> seems to be wrong (%s). Please specify one of the processes in the list returned by a GetCapabilities request."),
1975                 r_inputs->value);
1976        errorException (m, tmpMsg, "InvalidParameterValue", "identifier");
1977        free (tmpMsg);
1978        free (s1);
1979        freeMaps (&m);
1980        free (m);
1981        free (REQUEST);
1982        free (SERVICE_URL);
1983        return 0;
1984      }
1985    zClose (saved_stdout);
1986#ifdef META_DB
1987  }
1988#endif
1989 
[1]1990#ifdef DEBUG
[541]1991  dumpService (s1);
[1]1992#endif
1993  int j;
[381]1994
[541]1995
[1]1996  /**
[344]1997   * Create the input and output maps data structure
[1]1998   */
[541]1999  int i = 0;
[1]2000  HINTERNET hInternet;
2001  HINTERNET res;
[541]2002  hInternet = InternetOpen (
[1]2003#ifndef WIN32
[654]2004                            (LPCTSTR)
[1]2005#endif
[654]2006                            "ZooWPSClient\0",
2007                            INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
[1]2008
2009#ifndef WIN32
[541]2010  if (!CHECK_INET_HANDLE (hInternet))
2011    fprintf (stderr, "WARNING : hInternet handle failed to initialize");
[1]2012#endif
[541]2013  maps *request_input_real_format = NULL;
2014  maps *tmpmaps = request_input_real_format;
2015
[621]2016  if(parseRequest(&m,&request_inputs,s1,&request_input_real_format,&request_output_real_format,&hInternet)<0){
2017    freeMaps (&m);
2018    free (m);
2019    free (REQUEST);
2020    free (SERVICE_URL);
2021    InternetCloseHandle (&hInternet);
2022    freeService (&s1);
2023    free (s1);
2024    return 0;
2025  }
[917]2026  //InternetCloseHandle (&hInternet);
2027 
[652]2028  // Define each env variable in runing environment
[541]2029  maps *curs = getMaps (m, "env");
2030  if (curs != NULL)
2031    {
2032      map *mapcs = curs->content;
2033      while (mapcs != NULLMAP)
2034        {
[1]2035#ifndef WIN32
[541]2036          setenv (mapcs->name, mapcs->value, 1);
[1]2037#ifdef DEBUG
[541]2038          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
2039                   mapcs->value);
[1]2040#endif
[767]2041#else
[541]2042          if (mapcs->value[strlen (mapcs->value) - 2] == '\r')
2043            {
[1]2044#ifdef DEBUG
[541]2045              fprintf (stderr, "[ZOO: Env var finish with \r]\n");
[1]2046#endif
[541]2047              mapcs->value[strlen (mapcs->value) - 1] = 0;
2048            }
[1]2049#ifdef DEBUG
[541]2050          if (SetEnvironmentVariable (mapcs->name, mapcs->value) == 0)
2051            {
2052              fflush (stderr);
2053              fprintf (stderr, "setting variable... %s\n", "OK");
2054            }
2055          else
2056            {
2057              fflush (stderr);
2058              fprintf (stderr, "setting variable... %s\n", "OK");
2059            }
[1]2060#else
[541]2061          SetEnvironmentVariable (mapcs->name, mapcs->value);
[1]2062#endif
[541]2063          char *toto =
2064            (char *)
2065            malloc ((strlen (mapcs->name) + strlen (mapcs->value) +
2066                     2) * sizeof (char));
2067          sprintf (toto, "%s=%s", mapcs->name, mapcs->value);
[917]2068          _putenv (toto);
[1]2069#ifdef DEBUG
[541]2070          fflush (stderr);
[1]2071#endif
2072#endif
[917]2073
[1]2074#ifdef DEBUG
[541]2075          fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name,
2076                   mapcs->value);
2077          fflush (stderr);
[1]2078#endif
[541]2079          mapcs = mapcs->next;
2080        }
[1]2081    }
[541]2082
[1]2083#ifdef DEBUG
[541]2084  dumpMap (request_inputs);
[1]2085#endif
2086
[541]2087  map *status = getMap (request_inputs, "status");
[654]2088  if(vid==0){
2089    // Need to check if we need to fork to load a status enabled
2090    r_inputs = NULL;
2091    map *store = getMap (request_inputs, "storeExecuteResponse");
2092    /**
2093     * 05-007r7 WPS 1.0.0 page 57 :
2094     * 'If status="true" and storeExecuteResponse is "false" then the service
2095     * shall raise an exception.'
2096     */
2097    if (status != NULL && strcmp (status->value, "true") == 0 &&
2098        store != NULL && strcmp (store->value, "false") == 0)
2099      {
2100        errorException (m,
2101                        _
2102                        ("The status parameter cannot be set to true if storeExecuteResponse is set to false. Please modify your request parameters."),
2103                        "InvalidParameterValue", "storeExecuteResponse");
2104        freeService (&s1);
2105        free (s1);
2106        freeMaps (&m);
2107        free (m);
2108       
2109        freeMaps (&request_input_real_format);
2110        free (request_input_real_format);
2111       
2112        freeMaps (&request_output_real_format);
2113        free (request_output_real_format);
[94]2114
[654]2115        free (REQUEST);
2116        free (SERVICE_URL);
2117        return 1;
2118      }
2119    r_inputs = getMap (request_inputs, "storeExecuteResponse");
2120  }else{
2121    // Define status depending on the WPS 2.0.0 mode attribute
2122    status = getMap (request_inputs, "mode");
2123    map* mode=getMap(s1->content,"mode");
2124    if(strcasecmp(status->value,"async")==0){
2125      if(mode!=NULL && strcasecmp(mode->value,"async")==0)
2126        addToMap(request_inputs,"status","true");
2127      else{
2128        if(mode!=NULL){
2129          // see ref. http://docs.opengeospatial.org/is/14-065/14-065.html#61
2130          errorException (m,_("The process does not permit the desired execution mode."),"NoSuchMode", mode->value); 
2131          fflush (stdout);
2132          freeMaps (&m);
2133          free (m);
2134          if(zooRegistry!=NULL){
2135            freeRegistry(&zooRegistry);
2136            free(zooRegistry);
2137          }
2138          freeMaps (&request_input_real_format);
2139          free (request_input_real_format);
2140          freeMaps (&request_output_real_format);
2141          free (request_output_real_format);
2142          free (REQUEST);
2143          free (SERVICE_URL);
2144          return 0;
2145        }else
2146          addToMap(request_inputs,"status","true");
2147      }
2148    }
2149    else{
2150      if(strcasecmp(status->value,"auto")==0){
2151        if(mode!=NULL){
2152          if(strcasecmp(mode->value,"async")==0)
2153            addToMap(request_inputs,"status","false");
2154          else
2155            addToMap(request_inputs,"status","true");
2156        }
2157        else
2158          addToMap(request_inputs,"status","false");
2159      }else
2160        addToMap(request_inputs,"status","false");
2161    }
2162    status = getMap (request_inputs, "status");
2163  }
[541]2164
2165  int eres = SERVICE_STARTED;
[917]2166  int cpid = zGetpid ();
[541]2167
[917]2168  // Create a map containing a copy of the request map
2169  maps *_tmpMaps = createMaps("request");
2170  addMapToMap(&_tmpMaps->content,request_inputs);
2171  addMapsToMaps (&m, _tmpMaps);
2172  freeMaps (&_tmpMaps);
2173  free (_tmpMaps);
[453]2174  /**
2175   * Initialize the specific [lenv] section which contains runtime variables:
2176   *
[682]2177   *  - usid : it is an universally unique identifier 
2178   *  - osid : it is an idenfitication number
[453]2179   *  - sid : it is the process idenfitication number (OS)
[682]2180   *  - uusid : it is an universally unique identifier
[453]2181   *  - status : value between 0 and 100 to express the  completude of
2182   * the operations of the running service
2183   *  - message : is a string where you can store error messages, in case
2184   * service is failing, or o provide details on the ongoing operation.
[784]2185   *  - cwd : the current working directory or servicePath if defined
[453]2186   *  - soap : is a boolean value, true if the request was contained in a SOAP
2187   * Envelop
2188   *  - sessid : string storing the session identifier (only when cookie is
2189   * used)
2190   *  - cgiSid : only defined on Window platforms (for being able to identify
2191   * the created process)
2192   *
2193   */
[917]2194  _tmpMaps = createMaps("lenv");
[32]2195  char tmpBuff[100];
[514]2196  struct ztimeval tp;
[541]2197  if (zGettimeofday (&tp, NULL) == 0)
2198    sprintf (tmpBuff, "%i", (cpid + ((int) tp.tv_sec + (int) tp.tv_usec)));
[514]2199  else
[541]2200    sprintf (tmpBuff, "%i", (cpid + (int) time (NULL)));
[652]2201  _tmpMaps->content = createMap ("osid", tmpBuff);
[541]2202  sprintf (tmpBuff, "%i", cpid);
2203  addToMap (_tmpMaps->content, "sid", tmpBuff);
[640]2204  char* tmpUuid=get_uuid();
2205  addToMap (_tmpMaps->content, "uusid", tmpUuid);
[652]2206  addToMap (_tmpMaps->content, "usid", tmpUuid);
[640]2207  free(tmpUuid);
[541]2208  addToMap (_tmpMaps->content, "status", "0");
[784]2209  if(cwdMap0!=NULL){
2210    addToMap (_tmpMaps->content, "cwd", cwdMap0->value);
2211    addToMap (_tmpMaps->content, "rcwd", ntmp);
2212  }
2213  else
2214    addToMap (_tmpMaps->content, "cwd", ntmp);
[541]2215  addToMap (_tmpMaps->content, "message", _("No message provided"));
2216  map *ltmp = getMap (request_inputs, "soap");
2217  if (ltmp != NULL)
2218    addToMap (_tmpMaps->content, "soap", ltmp->value);
[280]2219  else
[541]2220    addToMap (_tmpMaps->content, "soap", "false");
[654]2221
[917]2222  // Parse the session file and add it to the main maps
2223  char* originalCookie=NULL;
[541]2224  if (cgiCookie != NULL && strlen (cgiCookie) > 0)
2225    {
2226      int hasValidCookie = -1;
[917]2227      char *tcook = originalCookie = zStrdup (cgiCookie);
[541]2228      map *testing = getMapFromMaps (m, "main", "cookiePrefix");
[917]2229      parseCookie(&m,originalCookie);
2230      map *sessId=getMapFromMaps(m,"cookies",(testing==NULL?"ID":testing->value));
2231      if (sessId!=NULL)
[541]2232        {
[917]2233          addToMap (_tmpMaps->content, "sessid", sessId->value);
[541]2234          char session_file_path[1024];
2235          map *tmpPath = getMapFromMaps (m, "main", "sessPath");
2236          if (tmpPath == NULL)
2237            tmpPath = getMapFromMaps (m, "main", "tmpPath");
2238          char *tmp1 = strtok (tcook, ";");
2239          if (tmp1 != NULL)
2240            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
[917]2241                     sessId->value);
[541]2242          else
2243            sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value,
[917]2244                     sessId->value);
[541]2245          free (tcook);
2246          maps *tmpSess = (maps *) malloc (MAPS_SIZE);
[790]2247          tmpSess->child=NULL;
[541]2248          struct stat file_status;
2249          int istat = stat (session_file_path, &file_status);
2250          if (istat == 0 && file_status.st_size > 0)
2251            {
[917]2252              int saved_stdout = zDup (fileno (stdout));
2253              zDup2 (fileno (stderr), fileno (stdout));
[541]2254              conf_read (session_file_path, tmpSess);
2255              addMapsToMaps (&m, tmpSess);
2256              freeMaps (&tmpSess);
[917]2257              fflush(stdout);
2258              zDup2 (saved_stdout, fileno (stdout));
2259              zClose(saved_stdout);
[541]2260            }
[817]2261          free (tmpSess);
[541]2262        }
[92]2263    }
[541]2264  addMapsToMaps (&m, _tmpMaps);
2265  freeMaps (&_tmpMaps);
2266  free (_tmpMaps);
[654]2267  maps* bmap=NULL;
[1]2268#ifdef DEBUG
[541]2269  dumpMap (request_inputs);
[1]2270#endif
[772]2271  int ei = 1;
[917]2272 
[216]2273#ifdef WIN32
[917]2274  LPVOID orig = GetEnvironmentStrings();
2275  LPTSTR s = (LPTSTR) orig;
[772]2276#else
[917]2277  char **orig = environ;
2278  char *s=*orig;
[772]2279#endif
[917]2280
[790]2281  _tmpMaps = createMaps("renv");
[917]2282if(orig!=NULL)
2283  for (; 
2284#ifdef WIN32
2285       *s;
2286       s++
2287#else
2288       s;
2289       ei++
2290#endif
2291       ) {
2292    if(strstr(s,"=")!=NULL && strlen(strstr(s,"="))>1){
2293      int len=strlen(s);
2294      char* tmpName=zStrdup(s);
2295      char* tmpValue=strstr(s,"=")+1;
2296      char* tmpName1=(char*)malloc((1+(len-(strlen(tmpValue)+1)))*sizeof(char));
2297      snprintf(tmpName1,(len-strlen(tmpValue)),"%s",tmpName);
2298      if(_tmpMaps->content == NULL)
2299        _tmpMaps->content = createMap (tmpName1,tmpValue);
2300      else
2301        addToMap (_tmpMaps->content,tmpName1,tmpValue);
2302      free(tmpName1);
2303      free(tmpName);
2304    }
2305#ifndef WIN32
2306    s = *(orig+ei);
2307#endif
[772]2308  }
[917]2309  if(_tmpMaps->content!=NULL && getMap(_tmpMaps->content,"HTTP_COOKIE")!=NULL){
2310    addToMap(_tmpMaps->content,"HTTP_COOKIE1",&cgiCookie[0]);
2311  }
[772]2312  addMapsToMaps (&m, _tmpMaps);
2313  freeMaps (&_tmpMaps);
2314  free (_tmpMaps);
2315#ifdef WIN32
[917]2316  FreeEnvironmentStrings((LPCH)orig);
2317#endif
2318  if(postRequest!=NULL)
2319    setMapInMaps (m, "renv", "xrequest", postRequest->value);
2320  //dumpMaps(m);
2321#ifdef WIN32
[541]2322  char *cgiSidL = NULL;
2323  if (getenv ("CGISID") != NULL)
2324    addToMap (request_inputs, "cgiSid", getenv ("CGISID"));
[554]2325
2326  char* usidp;
2327  if ( (usidp = getenv("USID")) != NULL ) {
2328    setMapInMaps (m, "lenv", "usid", usidp);
2329  }
2330
[541]2331  map *test1 = getMap (request_inputs, "cgiSid");
[680]2332  if (test1 != NULL){
[682]2333    cgiSid = zStrdup(test1->value);
2334    addToMap (request_inputs, "storeExecuteResponse", "true");
2335    addToMap (request_inputs, "status", "true");
2336    setMapInMaps (m, "lenv", "osid", test1->value);
2337    status = getMap (request_inputs, "status");
2338  }
[680]2339  test1 = getMap (request_inputs, "usid");
2340  if (test1 != NULL){
2341    setMapInMaps (m, "lenv", "usid", test1->value);
2342    setMapInMaps (m, "lenv", "uusid", test1->value);
2343  }
[216]2344#endif
[788]2345
[654]2346  char *fbkp, *fbkpid, *fbkpres, *fbkp1, *flog;
[541]2347  FILE *f0, *f1;
2348  if (status != NULL)
2349    if (strcasecmp (status->value, "false") == 0)
2350      status = NULLMAP;
2351  if (status == NULLMAP)
2352    {
[621]2353      if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
2354        freeService (&s1);
2355        free (s1);
2356        freeMaps (&m);
2357        free (m);
2358        free (REQUEST);
2359        free (SERVICE_URL);
2360        freeMaps (&request_input_real_format);
2361        free (request_input_real_format);
2362        freeMaps (&request_output_real_format);
2363        free (request_output_real_format);
2364        freeMaps (&tmpmaps);
2365        free (tmpmaps);
2366        return -1;
2367      }
[917]2368      map* testMap=getMapFromMaps(m,"main","memory");
2369      if(testMap==NULL || strcasecmp(testMap->value,"load")!=0)
2370        dumpMapsValuesToFiles(&m,&request_input_real_format);
[541]2371      loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format,
[917]2372                         &request_output_real_format, &eres);     
2373
2374#ifdef META_DB
2375      close_sql(m,0);     
2376#endif     
[541]2377    }
2378  else
2379    {
2380      int pid;
[1]2381#ifdef DEBUG
[541]2382      fprintf (stderr, "\nPID : %d\n", cpid);
[1]2383#endif
2384#ifndef WIN32
[541]2385      pid = fork ();
[1]2386#else
[541]2387      if (cgiSid == NULL)
2388        {
[917]2389          createProcess (m, request_inputs, s1, NULL, cpid,
2390                         request_input_real_format,
2391                         request_output_real_format);
2392          pid = cpid;
[541]2393        }
2394      else
2395        {
[917]2396          pid = 0;
2397          cpid = atoi (cgiSid);
[680]2398          updateStatus(m,0,_("Initializing"));
[541]2399        }
[1]2400#endif
[541]2401      if (pid > 0)
[917]2402        {
2403          //
2404          // dady :
2405          // set status to SERVICE_ACCEPTED
2406          //
[1]2407#ifdef DEBUG
[917]2408          fprintf (stderr, "father pid continue (origin %d) %d ...\n", cpid,
2409                   zGetpid ());
[1]2410#endif
[917]2411          eres = SERVICE_ACCEPTED;
2412        }
[541]2413      else if (pid == 0)
[917]2414        {
2415          eres = SERVICE_ACCEPTED;
2416          //
2417          // son : have to close the stdout, stdin and stderr to let the parent
2418          // process answer to http client.
2419          //
[652]2420          map* usid = getMapFromMaps (m, "lenv", "uusid");
[917]2421          map* tmpm = getMapFromMaps (m, "lenv", "osid");
2422          int cpid = atoi (tmpm->value);
2423          pid=cpid;
2424          r_inputs = getMapFromMaps (m, "main", "tmpPath");
2425          setMapInMaps (m, "lenv", "async","true");
[652]2426          map* r_inputs1 = createMap("ServiceName", s1->name);
[605]2427
[654]2428          // Create the filename for the result file (.res)
[917]2429          fbkpres =
2430            (char *)
2431            malloc ((strlen (r_inputs->value) +
2432                     strlen (usid->value) + 7) * sizeof (char));                   
2433          sprintf (fbkpres, "%s/%s.res", r_inputs->value, usid->value);
[790]2434          bmap = createMaps("status");
[654]2435          bmap->content=createMap("usid",usid->value);
2436          addToMap(bmap->content,"sid",tmpm->value);
[917]2437          addIntToMap(bmap->content,"pid",zGetpid());
2438         
[652]2439          // Create PID file referencing the OS process identifier
[917]2440          fbkpid =
2441            (char *)
2442            malloc ((strlen (r_inputs->value) +
2443                     strlen (usid->value) + 7) * sizeof (char));
2444          sprintf (fbkpid, "%s/%s.pid", r_inputs->value, usid->value);
[772]2445          setMapInMaps (m, "lenv", "file.pid", fbkpid);
[652]2446
[917]2447          f0 = freopen (fbkpid, "w+",stdout);
2448          printf("%d",zGetpid());
[652]2449          fflush(stdout);
2450
2451          // Create SID file referencing the semaphore name
[917]2452          fbkp =
2453            (char *)
2454            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
2455                     strlen (usid->value) + 7) * sizeof (char));
2456          sprintf (fbkp, "%s/%s.sid", r_inputs->value, usid->value);
[772]2457          setMapInMaps (m, "lenv", "file.sid", fbkp);
[917]2458          FILE* f2 = freopen (fbkp, "w+",stdout);
[680]2459          printf("%s",tmpm->value);
[652]2460          fflush(f2);
2461          free(fbkp);
2462
[917]2463          fbkp =
2464            (char *)
2465            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
2466                     strlen (usid->value) + 7) * sizeof (char));
2467          sprintf (fbkp, "%s/%s_%s.xml", r_inputs->value, r_inputs1->value,
2468                   usid->value);
[772]2469          setMapInMaps (m, "lenv", "file.responseInit", fbkp);
[917]2470          flog =
2471            (char *)
2472            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
2473                     strlen (usid->value) + 13) * sizeof (char));
2474          sprintf (flog, "%s/%s_%s_error.log", r_inputs->value,
2475                   r_inputs1->value, usid->value);
[772]2476          setMapInMaps (m, "lenv", "file.log", flog);
[1]2477#ifdef DEBUG
[917]2478          fprintf (stderr, "RUN IN BACKGROUND MODE \n");
2479          fprintf (stderr, "son pid continue (origin %d) %d ...\n", cpid,
2480                   zGetpid ());
2481          fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value);
[1]2482#endif
[917]2483          freopen (flog, "w+", stderr);
2484          fflush (stderr);
[654]2485          f0 = freopen (fbkp, "w+", stdout);
2486          rewind (stdout);
[458]2487#ifndef WIN32
[654]2488          fclose (stdin);
[458]2489#endif
[652]2490#ifdef RELY_ON_DB
2491          init_sql(m);
2492          recordServiceStatus(m);
2493#endif
[917]2494#ifdef USE_CALLBACK
2495          invokeCallback(m,NULL,NULL,0,0);
2496#endif
[654]2497          if(vid==0){
[917]2498            //
2499            // set status to SERVICE_STARTED and flush stdout to ensure full
2500            // content was outputed (the file used to store the ResponseDocument).
2501            // Then, rewind stdout to restart writing from the begining of the file.
2502            // This way, the data will be updated at the end of the process run.
2503            //
[654]2504            printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value,
2505                                  SERVICE_STARTED, request_input_real_format,
2506                                  request_output_real_format);
2507            fflush (stdout);
2508#ifdef RELY_ON_DB
2509            recordResponse(m,fbkp);
2510#endif
2511          }
2512
[917]2513          fflush (stderr);
[654]2514
[917]2515          fbkp1 =
2516            (char *)
2517            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
2518                     strlen (usid->value) + 13) * sizeof (char));
2519          sprintf (fbkp1, "%s/%s_final_%s.xml", r_inputs->value,
2520                   r_inputs1->value, usid->value);
[772]2521          setMapInMaps (m, "lenv", "file.responseFinal", fbkp1);
[631]2522
[917]2523          f1 = freopen (fbkp1, "w+", stdout);
[631]2524
[917]2525          map* serviceTypeMap=getMap(s1->content,"serviceType");
2526          if(serviceTypeMap!=NULL)
2527            setMapInMaps (m, "lenv", "serviceType", serviceTypeMap->value);
2528
2529          char *flenv =
2530            (char *)
2531            malloc ((strlen (r_inputs->value) + 
2532                     strlen (usid->value) + 12) * sizeof (char));
2533          sprintf (flenv, "%s/%s_lenv.cfg", r_inputs->value, usid->value);
2534          maps* lenvMaps=getMaps(m,"lenv");
2535          dumpMapsToFile(lenvMaps,flenv,0);
2536          free(flenv);
2537
2538#ifdef USE_CALLBACK
2539          invokeCallback(m,request_input_real_format,NULL,1,0);
2540#endif
[621]2541          if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
2542            freeService (&s1);
2543            free (s1);
[917]2544            fflush (stdout);
2545            fflush (stderr);
[765]2546            fclose (f0);
2547            fclose (f1);
2548            if(dumpBackFinalFile(m,fbkp,fbkp1)<0)
2549              return -1;
[917]2550#ifndef RELY_ON_DB
2551            dumpMapsToFile(bmap,fbkpres,1);
2552            removeShmLock (m, 1);
2553#else
2554            recordResponse(m,fbkp1);
2555#ifdef USE_CALLBACK
2556            invokeCallback(m,NULL,NULL,7,0);
2557#endif
2558#endif
2559            zUnlink (fbkpid);
[765]2560            unhandleStatus (m);
[917]2561#ifdef RELY_ON_DB
2562#ifdef META_DB
2563            cleanupCallbackThreads();
2564            close_sql(m,1);
2565#endif
2566            close_sql(m,0);
2567#endif
[621]2568            freeMaps (&m);
2569            free (m);
2570            free (REQUEST);
2571            free (SERVICE_URL);
2572            freeMaps (&request_input_real_format);
2573            free (request_input_real_format);
2574            freeMaps (&request_output_real_format);
2575            free (request_output_real_format);
2576            freeMaps (&tmpmaps);
2577            free (tmpmaps);
2578            return -1;
2579          }
[917]2580          if(getMapFromMaps(m,"lenv","mapError")!=NULL){
2581            setMapInMaps(m,"lenv","message",_("Issue with geographic data"));
2582#ifdef USE_CALLBACK
2583            invokeCallback(m,NULL,NULL,7,0);
2584#endif
2585            eres=-1;//SERVICE_FAILED;
2586          }else{
2587            map* testMap=getMapFromMaps(m,"main","memory");
2588            if(testMap==NULL || strcasecmp(testMap->value,"load")!=0)
2589              dumpMapsValuesToFiles(&m,&request_input_real_format);
2590            loadServiceAndRun (&m, s1, request_inputs,
2591                               &request_input_real_format,
2592                               &request_output_real_format, &eres);
2593          }
2594        }
[541]2595      else
2596        {
[652]2597          /**
2598           * error server don't accept the process need to output a valid
2599           * error response here !!!
2600           */
[917]2601          eres = -1;
2602          errorException (m, _("Unable to run the child process properly"),
2603                          "InternalError", NULL);
[541]2604        }
[1]2605    }
[917]2606       
[1]2607#ifdef DEBUG
[917]2608  fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
[541]2609  dumpMaps (request_output_real_format);
[917]2610  fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
[1]2611#endif
[917]2612  fflush(stdout);
2613  rewind(stdout);
[788]2614
[917]2615  //fprintf(stderr,"%s %d %d\n",__FILE__,__LINE__,eres); 
[541]2616  if (eres != -1)
2617    outputResponse (s1, request_input_real_format,
2618                    request_output_real_format, request_inputs,
2619                    cpid, m, eres);
2620  fflush (stdout);
[917]2621 
[105]2622  /**
2623   * Ensure that if error occurs when freeing memory, no signal will return
2624   * an ExceptionReport document as the result was already returned to the
2625   * client.
2626   */
2627#ifndef USE_GDB
[541]2628  signal (SIGSEGV, donothing);
2629  signal (SIGTERM, donothing);
2630  signal (SIGINT, donothing);
2631  signal (SIGILL, donothing);
2632  signal (SIGFPE, donothing);
2633  signal (SIGABRT, donothing);
[105]2634#endif
[917]2635
2636  if (((int) zGetpid ()) != cpid || cgiSid != NULL)
2637    {
2638      if (eres == SERVICE_SUCCEEDED)
2639#ifdef USE_CALLBACK
2640        invokeCallback(m,NULL,request_output_real_format,5,1);
2641#endif
2642      fflush(stderr);
2643      fflush(stdout);
2644
[541]2645      fclose (stdout);
[765]2646
[541]2647      fclose (f0);
2648      fclose (f1);
[917]2649
2650      if(dumpBackFinalFile(m,fbkp,fbkp1)<0)
2651        return -1;
2652      zUnlink (fbkpid);
[654]2653      switch(eres){
2654      default:
2655      case SERVICE_FAILED:
2656        setMapInMaps(bmap,"status","status",wpsStatus[1]);
2657        setMapInMaps(m,"lenv","fstate",wpsStatus[1]);
2658        break;
2659      case SERVICE_SUCCEEDED:
2660        setMapInMaps(bmap,"status","status",wpsStatus[0]);
2661        setMapInMaps(m,"lenv","fstate",wpsStatus[0]);
2662        break;
[788]2663      }     
[917]2664#ifndef RELY_ON_DB
[682]2665      dumpMapsToFile(bmap,fbkpres,1);
[652]2666      removeShmLock (m, 1);
2667#else
2668      recordResponse(m,fbkp1);
[917]2669      if (eres == SERVICE_SUCCEEDED)
2670#ifdef USE_CALLBACK
2671        invokeCallback(m,NULL,request_output_real_format,6,0);
[652]2672#endif
[917]2673#endif
[654]2674      freeMaps(&bmap);
2675      free(bmap);
[917]2676      zUnlink (fbkp1);
[652]2677      unhandleStatus (m);
[917]2678#ifdef RELY_ON_DB
2679#ifdef META_DB
2680      cleanupCallbackThreads();
2681      close_sql(m,1);
2682#endif
2683      close_sql(m,0);
2684      end_sql();
2685#endif
[652]2686      free(fbkpid);
[788]2687      free(fbkpres); 
[541]2688      free (fbkp1);
[682]2689      if(cgiSid!=NULL)
2690        free(cgiSid);
[917]2691      //InternetCloseHandle (&hInternet);
2692      fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
2693      fflush(stderr);
2694      fclose (stderr);
2695      zUnlink (flog);
2696      free (flog);
[541]2697    }
[917]2698  else{
2699    //InternetCloseHandle (&hInternet); 
2700#ifdef META_DB
2701    close_sql(m,0);
2702#endif
2703  }
2704 
[541]2705  freeService (&s1);
2706  free (s1);
2707  freeMaps (&m);
2708  free (m);
2709
2710  freeMaps (&request_input_real_format);
2711  free (request_input_real_format);
2712
2713  freeMaps (&request_output_real_format);
2714  free (request_output_real_format);
2715
2716  free (REQUEST);
2717  free (SERVICE_URL);
[1]2718#ifdef DEBUG
[541]2719  fprintf (stderr, "Processed response \n");
2720  fflush (stdout);
2721  fflush (stderr);
[1]2722#endif
2723
[917]2724  if (((int) zGetpid ()) != cpid || cgiSid != NULL)
[541]2725    {
2726      exit (0);
2727    }
[516]2728
[1]2729  return 0;
2730}
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