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

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