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

Last change on this file since 513 was 513, checked in by david, 9 years ago

Fix syntax

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