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

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

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png