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

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

Search

ZOO Sponsors

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

Become a sponsor !

Knowledge partners

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

Become a knowledge partner

Related links

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