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

Last change on this file since 654 was 654, checked in by djay, 9 years ago

Initial support for WPS 2.0.0 including the Dismiss extension.

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