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

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

Search

ZOO Sponsors

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

Become a sponsor !

Knowledge partners

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

Become a knowledge partner

Related links

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