source: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c @ 549

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