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

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