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

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