source: branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c @ 851

Last change on this file since 851 was 851, checked in by djay, 7 years ago

Invoke callback asynchronously. Still the ZOO-Kernel has still to wait for every requests to finish before stoping its execution.

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