source: trunk/zoo-kernel/zoo_service_loader.c @ 53

Last change on this file since 53 was 53, checked in by djay, 13 years ago

Add the USE_GDB flag to enable compiling ZOO-Kernel without signal handling, useful when used from gdb for debuging purpose ...

File size: 48.3 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 *  Copyright 2008-2009 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
31extern "C" {
32#include <libxml/tree.h>
33#include <libxml/xmlmemory.h>
34#include <libxml/parser.h>
35#include <libxml/xpath.h>
36#include <libxml/xpathInternals.h>
37}
38
39#include "cgic.h"
40#include "ulinet.h"
41
42#include <libintl.h>
43#include <locale.h>
44#include <string.h>
45
46#include "service.h"
47
48#include "service_internal.h"
49
50#ifdef USE_PYTHON
51#include "service_internal_python.h"
52#endif
53
54#ifdef USE_JAVA
55#include "service_internal_java.h"
56#endif
57
58#ifdef USE_PHP
59#include "service_internal_php.h"
60#endif
61
62#ifdef USE_JS
63#include "service_internal_js.h"
64#endif
65
66#ifdef USE_PERL
67#include "service_internal_perl.h"
68#endif
69
70
71
72#include <dirent.h>
73#include <signal.h>
74#include <unistd.h>
75#ifndef WIN32
76#include <dlfcn.h>
77#include <libgen.h>
78#else
79#include <windows.h>
80#include <direct.h>
81#endif
82#include <fcntl.h>
83#include <time.h>
84#include <stdarg.h>
85
86#define _(String) dgettext ("zoo-kernel",String)
87
88
89void *translateChar(char* str,char toReplace,char toReplaceBy){
90  int i=0,len=strlen(str);
91  for(i=0;i<len;i++){
92    if(str[i]==toReplace)
93      str[i]=toReplaceBy;
94  }
95}
96
97xmlXPathObjectPtr extractFromDoc(xmlDocPtr doc,char* search){
98  xmlXPathContextPtr xpathCtx;
99  xmlXPathObjectPtr xpathObj;
100  xpathCtx = xmlXPathNewContext(doc);
101  xpathObj = xmlXPathEvalExpression(BAD_CAST search,xpathCtx);
102  xmlXPathFreeContext(xpathCtx);
103  return xpathObj;
104}
105
106void sig_handler(int sig){
107  char tmp[100];
108  char *ssig;
109  switch(sig){
110  case SIGSEGV:
111    ssig="SIGSEGV";
112    break;
113  case SIGTERM:
114    ssig="SIGTERM";
115    break;
116  case SIGINT:
117    ssig="SIGINT";
118    break;
119  case SIGILL:
120    ssig="SIGILL";
121    break;
122  case SIGFPE:
123    ssig="SIGFPE";
124    break;
125  case SIGABRT:
126    ssig="SIGABRT";
127    break;
128  default:
129    ssig="UNKNOWN";
130    break;
131  }
132  sprintf(tmp,_("ZOO Kernel failed to process your request receiving signal %d = %s"),sig,ssig);
133  errorException(NULL, tmp, "InternalError");
134#ifdef DEBUG
135  fprintf(stderr,"Not this time!\n");
136#endif
137  exit(0);
138}
139
140void *loadServiceAndRun(maps **myMap,service* s1,map* request_inputs,maps **inputs,maps** ioutputs,int* eres){
141  char tmps1[1024];
142  char ntmp[1024];
143  maps *m=*myMap;
144  maps *request_output_real_format=*ioutputs;
145  maps *request_input_real_format=*inputs;
146  /**
147   * Extract serviceType to know what kind of service should be loaded
148   */
149  map* r_inputs=NULL;
150#ifndef WIN32
151  getcwd(ntmp,1024);
152#else
153  _getcwd(ntmp,1024);
154#endif
155  r_inputs=getMap(s1->content,"serviceType");
156#ifdef DEBUG
157  fprintf(stderr,"LOAD A %s SERVICE PROVIDER \n",r_inputs->value);
158  fflush(stderr);
159#endif
160  if(strncasecmp(r_inputs->value,"C",1)==0){
161    r_inputs=getMap(request_inputs,"metapath");
162    if(r_inputs!=NULL)
163      sprintf(tmps1,"%s/%s",ntmp,r_inputs->value);
164    else
165      sprintf(tmps1,"%s/",ntmp);
166    char *altPath=strdup(tmps1);
167    r_inputs=getMap(s1->content,"ServiceProvider");
168    sprintf(tmps1,"%s/%s",altPath,r_inputs->value);
169    free(altPath);
170#ifdef DEBUG
171    fprintf(stderr,"Trying to load %s\n",tmps1);
172#endif
173#ifdef WIN32
174    HINSTANCE so = LoadLibraryEx(tmps1,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
175#else
176    void* so = dlopen(tmps1, RTLD_LAZY);
177#endif
178#ifdef DEBUG
179#ifdef WIN32<
180    DWORD errstr;
181    errstr = GetLastError();
182    fprintf(stderr,"%s loaded (%d) \n",tmps1,errstr);
183#else
184    char *errstr;
185    errstr = dlerror();
186#endif
187#endif
188
189    if( so != NULL ) {
190#ifdef DEBUG
191      fprintf(stderr,"Library loaded %s \n",errstr);
192      fprintf(stderr,"Service Shared Object = %s\n",r_inputs->value);
193#endif
194      r_inputs=getMap(s1->content,"serviceType");
195#ifdef DEBUG
196      dumpMap(r_inputs);
197      fprintf(stderr,"%s\n",r_inputs->value);
198      fflush(stderr);
199#endif
200      if(strncasecmp(r_inputs->value,"C-FORTRAN",9)==0){
201#ifdef WIN32
202        //Strange return value needed here !
203        return 1;
204#endif
205        r_inputs=getMap(request_inputs,"Identifier");
206        char fname[1024];
207        sprintf(fname,"%s_",r_inputs->value);
208#ifdef DEBUG
209        fprintf(stderr,"Try to load function %s\n",fname);
210#endif
211#ifdef WIN32
212        typedef int (CALLBACK* execute_t)(char***,char***,char***);
213        execute_t execute=(execute_t)GetProcAddress(so,fname);
214#else
215        typedef int (*execute_t)(char***,char***,char***);
216        execute_t execute=(execute_t)dlsym(so,fname);
217#endif
218#ifdef DEBUG
219#ifdef WIN32
220        errstr = GetLastError();
221#else
222        errstr = dlerror();
223#endif
224        fprintf(stderr,"Function loaded %s\n",errstr);
225#endif 
226
227        char main_conf[10][30][1024];
228        char inputs[10][30][1024];
229        char outputs[10][30][1024];
230        for(int i=0;i<10;i++){
231          for(int j=0;j<30;j++){
232            memset(main_conf[i][j],0,1024);
233            memset(inputs[i][j],0,1024);
234            memset(outputs[i][j],0,1024);
235          }
236        }
237        mapsToCharXXX(m,(char***)main_conf);
238        mapsToCharXXX(request_input_real_format,(char***)inputs);
239        mapsToCharXXX(request_output_real_format,(char***)outputs);
240        *eres=execute((char***)&main_conf[0],(char***)&inputs[0],(char***)&outputs[0]);
241#ifdef DEBUG
242        fprintf(stderr,"Function run successfully \n");
243#endif
244        charxxxToMaps((char***)&outputs[0],&request_output_real_format);
245      }else{
246#ifdef DEBUG
247#ifdef WIN32
248        errstr = GetLastError();
249        fprintf(stderr,"Function %s failed to load because of %d\n",r_inputs->value,errstr);
250#endif
251#endif
252        r_inputs=getMap(request_inputs,"Identifier");
253#ifdef DEBUG
254        fprintf(stderr,"Try to load function %s\n",r_inputs->value);
255#endif
256        typedef int (*execute_t)(maps**,maps**,maps**);
257#ifdef WIN32
258        execute_t execute=(execute_t)GetProcAddress(so,r_inputs->value); 
259#else
260        execute_t execute=(execute_t)dlsym(so,r_inputs->value);
261#endif
262
263#ifdef DEBUG
264#ifdef WIN32
265        errstr = GetLastError();
266#else
267        errstr = dlerror();
268#endif
269        fprintf(stderr,"Function loaded %s\n",errstr);
270#endif 
271
272#ifdef DEBUG
273        fprintf(stderr,"Now run the function \n");
274        fflush(stderr);
275#endif
276        *eres=execute(&m,&request_input_real_format,&request_output_real_format);
277#ifdef DEBUG
278        fprintf(stderr,"Function loaded and returned %d\n",eres);
279        fflush(stderr);
280#endif
281      }
282      dlclose(so);
283    } else {
284      /**
285       * Unable to load the specified shared library
286       */
287      char tmps[1024];
288#ifdef WIN32
289      DWORD errstr = GetLastError();
290#else
291      char* errstr = dlerror();
292#endif
293      sprintf(tmps,_("C Library can't be loaded %s \n"),errstr);
294      map* tmps1=createMap("text",tmps);
295      printExceptionReportResponse(m,tmps1);
296      *eres=-1;
297    }
298  }
299  else
300#ifdef USE_PYTHON
301    if(strncasecmp(r_inputs->value,"PYTHON",6)==0){
302      *eres=zoo_python_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
303    }
304    else
305#endif
306       
307#ifdef USE_JAVA
308      if(strncasecmp(r_inputs->value,"JAVA",4)==0){
309        *eres=zoo_java_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
310      }
311      else
312#endif
313
314#ifdef USE_PHP
315        if(strncasecmp(r_inputs->value,"PHP",3)==0){
316          *eres=zoo_php_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
317        }
318        else
319#endif
320           
321           
322#ifdef USE_PERL
323          if(strncasecmp(r_inputs->value,"PERL",4)==0){
324            *eres=zoo_perl_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
325          }
326          else
327#endif
328
329#ifdef USE_JS
330            if(strncasecmp(r_inputs->value,"JS",2)==0){
331              *eres=zoo_js_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
332            }
333            else
334#endif
335              {
336                char tmpv[1024];
337                sprintf(tmpv,_("Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n"),r_inputs->value);
338                map* tmps=createMap("text",tmpv);
339                printExceptionReportResponse(m,tmps);
340                *eres=-1;
341              }
342  *ioutputs=request_output_real_format;
343}
344
345int runRequest(map* request_inputs)
346{
347
348#ifndef USE_GDB
349  (void) signal(SIGSEGV,sig_handler);
350  (void) signal(SIGTERM,sig_handler);
351  (void) signal(SIGINT,sig_handler);
352  (void) signal(SIGILL,sig_handler);
353  (void) signal(SIGFPE,sig_handler);
354  (void) signal(SIGABRT,sig_handler);
355#endif
356
357  map* r_inputs=NULL,*tmps=NULL;
358  maps* m=NULL;
359  int argc=count(request_inputs);
360
361  char* REQUEST=NULL;
362  /**
363   * Parsing service specfic configuration file
364   */
365  m=(maps*)calloc(1,MAPS_SIZE);
366  if(m == NULL){
367    return errorException(m, _("Unable to allocate memory."), "InternalError");
368  }
369  char ntmp[1024];
370#ifndef WIN32
371  getcwd(ntmp,1024);
372#else
373  _getcwd(ntmp,1024);
374#endif
375  r_inputs=getMap(request_inputs,"metapath");
376  if(r_inputs==NULL){
377    if(request_inputs==NULL)
378      request_inputs=createMap("metapath","");
379    else
380      addToMap(request_inputs,"metapath","");
381#ifdef DEBUG
382    fprintf(stderr,"ADD METAPATH\n");
383    dumpMap(request_inputs);
384#endif
385    r_inputs=getMap(request_inputs,"metapath");
386  }
387  char conf_file[10240];
388  snprintf(conf_file,10240,"%s/%s/main.cfg",ntmp,r_inputs->value);
389  conf_read(conf_file,m);
390#ifdef DEBUG
391  fprintf(stderr, "***** BEGIN MAPS\n"); 
392  dumpMaps(m);
393  fprintf(stderr, "***** END MAPS\n");
394#endif
395
396  bindtextdomain ("zoo-kernel","/usr/share/locale/");
397  bindtextdomain ("zoo-services","/usr/share/locale/");
398 
399  if((r_inputs=getMap(request_inputs,"language"))!=NULL){
400    char *tmp=strdup(r_inputs->value);
401    translateChar(tmp,'-','_');
402    setlocale (LC_ALL, tmp);
403    free(tmp);
404    setMapInMaps(m,"main","language",r_inputs->value);
405  }
406  else{
407    setlocale (LC_ALL, "en_US");
408    setMapInMaps(m,"main","language","en-US");
409  }
410  setlocale (LC_NUMERIC, "en_US");
411  bind_textdomain_codeset("zoo-kernel","UTF-8");
412  textdomain("zoo-kernel");
413  bind_textdomain_codeset("zoo-services","UTF-8");
414  textdomain("zoo-services");
415
416
417  /**
418   * Check for minimum inputs
419   */
420  r_inputs=getMap(request_inputs,"Request");
421  if(request_inputs==NULL || r_inputs==NULL){ 
422    errorException(m, _("Parameter <request> was not specified"),"MissingParameterValue");
423    freeMaps(&m);
424    free(m);
425    return 1;
426  }
427  else{
428    REQUEST=strdup(r_inputs->value);
429    if(strncasecmp(r_inputs->value,"GetCapabilities",15)!=0
430       && strncasecmp(r_inputs->value,"DescribeProcess",15)!=0
431       && strncasecmp(r_inputs->value,"Execute",7)!=0){ 
432      errorException(m, _("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), "InvalidParameterValue");
433      freeMaps(&m);
434      free(m);
435      free(REQUEST);
436      return 1;
437    }
438  }
439  r_inputs=NULL;
440  r_inputs=getMap(request_inputs,"Service");
441  if(r_inputs==NULLMAP){
442    errorException(m, _("Parameter <service> was not specified"),"MissingParameterValue");
443    freeMaps(&m);
444    free(m);
445    free(REQUEST);
446    return 1;
447  }
448  if(strncasecmp(REQUEST,"GetCapabilities",15)!=0){
449    r_inputs=getMap(request_inputs,"Version");
450    if(r_inputs==NULL){ 
451      errorException(m, _("Parameter <version> was not specified"),"MissingParameterValue");
452      freeMaps(&m);
453      free(m);
454      free(REQUEST);
455      return 1;
456    }
457  }
458
459  r_inputs=getMap(request_inputs,"serviceprovider");
460  if(r_inputs==NULL){
461    addToMap(request_inputs,"serviceprovider","");
462  }
463
464  map* outputs=NULL;
465  maps* request_output_real_format=NULL;
466  map* tmpm=getMapFromMaps(m,"main","serverAddress");
467  if(tmpm!=NULL)
468    SERVICE_URL=strdup(tmpm->value);
469  else
470    SERVICE_URL=DEFAULT_SERVICE_URL;
471
472  service* s[100];
473  service* s1;
474  int scount=0;
475
476#ifdef DEBUG
477  dumpMap(r_inputs);
478#endif
479  char conf_dir[1024];
480  int t;
481  char tmps1[1024];
482
483  r_inputs=NULL;
484  r_inputs=getMap(request_inputs,"metapath");
485  if(r_inputs!=NULL)
486    snprintf(conf_dir,1024,"%s/%s",ntmp,r_inputs->value);
487  else
488    snprintf(conf_dir,1024,"%s",ntmp);
489
490  if(strncasecmp(REQUEST,"GetCapabilities",15)==0){
491    int i=0;
492    struct dirent *dp;
493#ifdef DEBUG
494    dumpMap(r_inputs);
495#endif
496    DIR *dirp = opendir(conf_dir);
497    if(dirp==NULL){
498      return errorException(m, _("The specified path doesn't exist."),"InvalidParameterValue");
499    }
500    xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
501    r_inputs=NULL;
502    r_inputs=getMap(request_inputs,"ServiceProvider");
503    xmlNodePtr n;
504    //dumpMap(request_inputs);
505    if(r_inputs!=NULL)
506      n = printGetCapabilitiesHeader(doc,r_inputs->value,m);
507    else
508      n = printGetCapabilitiesHeader(doc,"",m);
509    /**
510     * Strange, here we need to close stdout to ensure that no uneeded
511     * char will be printed (parser issue ?)
512     */
513    int saved_stdout = dup(fileno(stdout));
514    dup2(fileno(stderr),fileno(stdout));
515    while ((dp = readdir(dirp)) != NULL)
516      if(strstr(dp->d_name,".zcfg")!=0){
517        memset(tmps1,0,1024);
518        snprintf(tmps1,1024,"%s/%s",conf_dir,dp->d_name);
519        s1=(service*)calloc(1,SERVICE_SIZE);
520        if(s1 == NULL){ 
521          return errorException(m, _("Unable to allocate memory."),"InternalError");
522        }
523#ifdef DEBUG
524        fprintf(stderr,"#################\n%s\n#################\n",tmps1);
525#endif
526        t=getServiceFromFile(tmps1,&s1);
527#ifdef DEBUG
528        dumpService(s1);
529        fflush(stdout);
530        fflush(stderr);
531#endif
532        printGetCapabilitiesForProcess(m,n,s1);
533        freeService(&s1);
534        free(s1);
535        scount++;
536      }
537    (void)closedir(dirp);
538    fflush(stdout);
539    dup2(saved_stdout,fileno(stdout));
540    printDocument(m,doc,getpid());
541    freeMaps(&m);
542    free(m);
543    free(REQUEST);
544    free(SERVICE_URL);
545    fflush(stdout);
546    return 0;
547  }
548  else{
549    r_inputs=getMap(request_inputs,"Identifier");
550    if(r_inputs==NULL 
551       || strlen(r_inputs->name)==0 || strlen(r_inputs->value)==0){ 
552      errorException(m, _("Mandatory <identifier> was not specified"),"MissingParameterValue");
553      freeMaps(&m);
554      free(m);
555      free(REQUEST);
556      free(SERVICE_URL);
557      return 0;
558    }
559
560    struct dirent *dp;
561    DIR *dirp = opendir(conf_dir);
562    if(dirp==NULL){
563      errorException(m, _("The specified path path doesn't exist."),"InvalidParameterValue");
564      freeMaps(&m);
565      free(m);
566      free(REQUEST);
567      free(SERVICE_URL);
568      return 0;
569    }
570    if(strncasecmp(REQUEST,"DescribeProcess",15)==0){
571      /**
572       * Loop over Identifier list
573       */
574      xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
575      r_inputs=NULL;
576      r_inputs=getMap(request_inputs,"ServiceProvider");
577
578      xmlNodePtr n;
579      if(r_inputs!=NULL)
580        n = printDescribeProcessHeader(doc,r_inputs->value,m);
581      else
582        n = printDescribeProcessHeader(doc,"",m);
583
584      r_inputs=getMap(request_inputs,"Identifier");
585      char *tmps=strtok(r_inputs->value,",");
586     
587      char buff[256];
588      char buff1[1024];
589      int i=0;
590      int j=0;
591      int end=-1;
592      int saved_stdout = dup(fileno(stdout));
593      dup2(fileno(stderr),fileno(stdout));
594      while(tmps){
595        memset(buff,0,256);
596        snprintf(buff,256,"%s.zcfg",tmps);
597        memset(buff1,0,1024);
598#ifdef DEBUG
599        fprintf(stderr,"\n#######%s\n########\n",buff1);
600#endif
601        while ((dp = readdir(dirp)) != NULL)
602          if(strcmp(dp->d_name,buff)==0){
603            memset(buff1,0,1024);
604            snprintf(buff1,1024,"%s/%s",conf_dir,dp->d_name);
605            //s1=(service*)malloc(sizeof(service*));
606            s1=(service*)calloc(1,SERVICE_SIZE);
607            if(s1 == NULL){
608              return errorException(m, _("Unable to allocate memory."),"InternalError");
609            }
610#ifdef DEBUG
611            fprintf(stderr,"#################\n%s\n#################\n",buff1);
612#endif
613            t=getServiceFromFile(buff1,&s1);
614#ifdef DEBUG
615            dumpService(s1);
616#endif
617            printDescribeProcessForProcess(m,n,s1,1);
618            freeService(&s1);
619            free(s1);
620            scount++;
621          }
622        rewinddir(dirp);
623        tmps=strtok(NULL,",");
624      }
625      closedir(dirp);
626      fflush(stdout);
627      dup2(saved_stdout,fileno(stdout));
628      printDocument(m,doc,getpid());
629      freeMaps(&m);
630      free(m);
631      free(REQUEST);
632      free(SERVICE_URL);
633      fflush(stdout);
634      //xmlFree(n);
635#ifndef LINUX_FREE_ISSUE
636      if(s1)
637        free(s1);
638#endif
639      return 0;
640    }
641    else
642      if(strncasecmp(REQUEST,"Execute",strlen(REQUEST))!=0){
643        errorException(m, _("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), "InvalidParameterValue");
644#ifdef DEBUG
645        fprintf(stderr,"No request found %s",REQUEST);
646#endif 
647        closedir(dirp);
648        free(s);
649        return 0;
650      }
651    closedir(dirp);
652  }
653 
654  s1=NULL;
655  s1=(service*)calloc(1,SERVICE_SIZE);
656  if(s1 == NULL){
657    freeMaps(&m);
658    free(m);
659    free(REQUEST);
660    free(SERVICE_URL);
661    return errorException(m, _("Unable to allocate memory."),"InternalError");
662  }
663  r_inputs=getMap(request_inputs,"MetaPath");
664  if(r_inputs!=NULL)
665    snprintf(tmps1,1024,"%s/%s",ntmp,r_inputs->value);
666  else
667    snprintf(tmps1,1024,"%s/",ntmp);
668  r_inputs=getMap(request_inputs,"Identifier");
669  char *ttmp=strdup(tmps1);
670  snprintf(tmps1,1024,"%s/%s.zcfg",ttmp,r_inputs->value);
671  free(ttmp);
672#ifdef DEBUG
673  fprintf(stderr,"Trying to load %s\n", tmps1);
674#endif
675  int saved_stdout = dup(fileno(stdout));
676  dup2(fileno(stderr),fileno(stdout));
677  t=getServiceFromFile(tmps1,&s1);
678  fflush(stdout);
679  dup2(saved_stdout,fileno(stdout));
680  if(t<0){
681    char tmpMsg[2048+strlen(r_inputs->value)];
682    sprintf(tmpMsg,_("The value for <indetifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."),r_inputs->value);
683    errorException(m, tmpMsg, "InvalidParameterValue");
684    freeService(&s1);
685    free(s1);
686    freeMaps(&m);
687    free(m);
688    free(REQUEST);
689    free(SERVICE_URL);
690    return 0;
691  }
692  close(saved_stdout);
693
694#ifdef DEBUG
695  dumpService(s1);
696#endif
697  map* inputs=NULL;
698  elements* c_inputs=s1->inputs;
699  int j;
700 
701  /**
702   * Create the input maps data structure
703   */
704  int i=0;
705  HINTERNET hInternet;
706  HINTERNET res;
707  hInternet=InternetOpen(
708#ifndef WIN32
709                         (LPCTSTR)
710#endif
711                         "ZooWPSClient\0",
712                         INTERNET_OPEN_TYPE_PRECONFIG,
713                         NULL,NULL, 0);
714
715#ifndef WIN32
716  if(!CHECK_INET_HANDLE(hInternet))
717    fprintf(stderr,"WARNING : hInternet handle failed to initialize");
718#endif
719  maps* request_input_real_format=NULL;
720  maps* tmpmaps = request_input_real_format;
721  map* postRequest=NULL;
722  postRequest=getMap(request_inputs,"xrequest");
723  if(postRequest==NULLMAP){
724    /**
725     * Parsing outputs provided as KVP
726     */
727    r_inputs=NULL;
728#ifdef DEBUG
729    fprintf(stderr,"OUTPUT Parsing ... \n");
730#endif
731    r_inputs=getMap(request_inputs,"ResponseDocument"); 
732    if(r_inputs==NULL) r_inputs=getMap(request_inputs,"RawDataOutput");
733   
734#ifdef DEBUG
735    fprintf(stderr,"OUTPUT Parsing ... \n");
736#endif
737    if(r_inputs!=NULL){
738#ifdef DEBUG
739      fprintf(stderr,"OUTPUT Parsing start now ... \n");
740#endif
741      char current_output_as_string[10240];
742      char cursor_output[10240];
743      char *cotmp=strdup(r_inputs->value);
744      snprintf(cursor_output,10240,"%s",cotmp);
745      free(cotmp);
746      j=0;
747      map* request_kvp_outputs=NULL;
748       
749      /**
750       * Put each Output into the outputs_as_text array
751       */
752      char * pToken;
753      maps* tmp_output=NULL;
754#ifdef DEBUG
755      fprintf(stderr,"OUTPUT [%s]\n",cursor_output);
756#endif
757      pToken=strtok(cursor_output,";");
758      char** outputs_as_text=(char**)calloc(128,sizeof(char*));
759      if(outputs_as_text == NULL) {
760        return errorException(m, _("Unable to allocate memory"), "InternalError");
761      }
762      i=0;
763      while(pToken!=NULL){
764#ifdef DEBUG
765        fprintf(stderr,"***%s***\n",pToken);
766        fflush(stderr);
767        fprintf(stderr,"***%s***\n",pToken);
768#endif
769        outputs_as_text[i]=(char*)calloc(strlen(pToken)+1,sizeof(char));
770        if(outputs_as_text[i] == NULL) {
771          return errorException(m, _("Unable to allocate memory"), "InternalError");
772        }
773        snprintf(outputs_as_text[i],strlen(pToken)+1,"%s",pToken);
774        pToken = strtok(NULL,";");
775        i++;
776      }
777      for(j=0;j<i;j++){
778        char *tmp=strdup(outputs_as_text[j]);
779        free(outputs_as_text[j]);
780        char *tmpc;
781        tmpc=strtok(tmp,"@");
782        int k=0;
783        while(tmpc!=NULL){
784          if(k==0){
785            if(tmp_output==NULL){
786              tmp_output=(maps*)calloc(1,MAPS_SIZE);
787              if(tmp_output == NULL){
788                return errorException(m, _("Unable to allocate memory."), "InternalError");
789              }
790              tmp_output->name=strdup(tmpc);
791              tmp_output->content=NULL;
792              tmp_output->next=NULL;
793            }
794          }
795          else{
796            char *tmpv=strstr(tmpc,"=");
797            char tmpn[256];
798            memset(tmpn,0,256);
799            strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
800            tmpn[strlen(tmpc)-strlen(tmpv)]=0;
801#ifdef DEBUG
802            fprintf(stderr,"OUTPUT DEF [%s]=[%s]\n",tmpn,tmpv+1);
803#endif
804            if(tmp_output->content==NULL){
805              tmp_output->content=createMap(tmpn,tmpv+1);
806              tmp_output->content->next=NULL;
807            }
808            else
809              addToMap(tmp_output->content,tmpn,tmpv+1);
810          }
811          k++;
812#ifdef DEBUG
813          fprintf(stderr,"***%s***\n",tmpc);
814#endif
815          tmpc=strtok(NULL,"@");
816        }
817        if(request_output_real_format==NULL)
818          request_output_real_format=dupMaps(&tmp_output);
819        else
820          addMapsToMaps(&request_output_real_format,tmp_output);
821        freeMaps(&tmp_output);
822        free(tmp_output);
823        tmp_output=NULL;
824#ifdef DEBUG
825        dumpMaps(tmp_output);
826        fflush(stderr);
827#endif
828        //tmp_output=tmp_output->next;
829        free(tmp);
830      }
831      free(outputs_as_text);
832    }
833
834
835    /**
836     * Parsing inputs provided as KVP
837     */
838    r_inputs=getMap(request_inputs,"DataInputs");
839#ifdef DEBUG
840    fprintf(stderr,"DATA INPUTS [%s]\n",r_inputs->value);
841#endif
842    char current_input_as_string[40960];
843    char cursor_input[40960];
844    if(r_inputs!=NULL)
845      snprintf(cursor_input,40960,"%s",r_inputs->value);
846    else{
847      errorException(m, _("Parameter <DataInputs> was not specified"),"MissingParameterValue");
848      freeMaps(&m);
849      free(m);
850      free(REQUEST);
851      free(SERVICE_URL);
852      return 0;
853    }
854    j=0;
855    map* request_kvp_inputs=NULL;
856 
857    /**
858     * Put each DataInputs into the inputs_as_text array
859     */
860    char * pToken;
861    pToken=strtok(cursor_input,";");
862    char** inputs_as_text=(char**)calloc(100,sizeof(char*));
863    if(inputs_as_text == NULL){
864      return errorException(m, _("Unable to allocate memory."), "InternalError");
865    }
866    i=0;
867    while(pToken!=NULL){
868#ifdef DEBUG
869      fprintf(stderr,"***%s***\n",pToken);
870#endif
871      fflush(stderr);
872#ifdef DEBUG
873      fprintf(stderr,"***%s***\n",pToken);
874#endif
875      inputs_as_text[i]=(char*)calloc(strlen(pToken)+1,sizeof(char));
876      snprintf(inputs_as_text[i],strlen(pToken)+1,"%s",pToken);
877      if(inputs_as_text[i] == NULL){
878        return errorException(m, _("Unable to allocate memory."), "InternalError");
879      }
880      pToken = strtok(NULL,";");
881      i++;
882    }
883
884    for(j=0;j<i;j++){
885      char *tmp=strdup(inputs_as_text[j]);
886      free(inputs_as_text[j]);
887      char *tmpc;
888      tmpc=strtok(tmp,"@");
889      while(tmpc!=NULL){
890#ifdef DEBUG
891        fprintf(stderr,"***\n***%s***\n",tmpc);
892#endif
893        char *tmpv=strstr(tmpc,"=");
894        char tmpn[256];
895        memset(tmpn,0,256);
896        strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
897        tmpn[strlen(tmpc)-strlen(tmpv)]=0;
898        int cnt=0;
899#ifdef DEBUG
900        fprintf(stderr,"***\n*** %s = %s ***\n",tmpn,tmpv+1);
901#endif
902        if(tmpmaps==NULL){
903          tmpmaps=(maps*)calloc(1,MAPS_SIZE);
904          if(tmpmaps == NULL){
905            return errorException(m, _("Unable to allocate memory."), "InternalError");
906          }
907          tmpmaps->name=strdup(tmpn);
908          tmpmaps->content=createMap("value",tmpv+1);
909          tmpmaps->next=NULL;
910        }
911        tmpc=strtok(NULL,"@");
912        while(tmpc!=NULL){
913#ifdef DEBUG
914          fprintf(stderr,"*** KVP NON URL-ENCODED \n***%s***\n",tmpc);
915#endif
916          char *tmpv1=strstr(tmpc,"=");
917#ifdef DEBUG
918          fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1);
919#endif
920          char tmpn1[1024];
921          memset(tmpn1,0,1024);
922          strncpy(tmpn1,tmpc,strlen(tmpc)-strlen(tmpv1));
923          tmpn1[strlen(tmpc)-strlen(tmpv1)]=0;
924#ifdef DEBUG
925          fprintf(stderr,"*** NAME NON URL-ENCODED \n***%s***\n",tmpn1);
926          fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1);
927#endif
928          if(strcmp(tmpn1,"xlink:href")!=0)
929            addToMap(tmpmaps->content,tmpn1,tmpv1+1);
930          else{
931#ifdef DEBUG
932            fprintf(stderr,"REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",tmpv1+1);
933#endif
934#ifndef WIN32
935            if(CHECK_INET_HANDLE(hInternet))
936#endif
937              {
938                res=InternetOpenUrl(hInternet,tmpv1+1,NULL,0,
939                                    INTERNET_FLAG_NO_CACHE_WRITE,0);
940#ifdef DEBUG
941                fprintf(stderr,"(%s) content-length : %d,,res.nDataAlloc %d \n",
942                        tmpv1+1,res.nDataAlloc,res.nDataLen);
943#endif
944                char* tmpContent=(char*)calloc((res.nDataLen+1),sizeof(char));
945                if(tmpContent == NULL){
946                  return errorException(m, _("Unable to allocate memory."), "InternalError");
947                }
948                size_t dwRead;
949                InternetReadFile(res, (LPVOID)tmpContent,res.nDataLen, &dwRead);
950                map* tmpMap=getMap(tmpmaps->content,"value");
951                if(tmpMap!=NULL)
952                  tmpMap->value=strdup(tmpContent);
953                free(tmpContent);
954              }
955            addToMap(tmpmaps->content,tmpn1,tmpv1+1);
956          }
957          tmpc=strtok(NULL,"@");
958        }
959#ifdef DEBUG
960        dumpMaps(tmpmaps);
961        fflush(stderr);
962#endif
963        if(request_input_real_format==NULL)
964          request_input_real_format=dupMaps(&tmpmaps);
965        else
966          addMapsToMaps(&request_input_real_format,tmpmaps);
967        /*freeMap(&tmpmaps->content);
968        free(tmpmaps->content);
969        tmpmaps->content=NULL;*/
970        freeMaps(&tmpmaps);
971        free(tmpmaps);
972          //}
973        tmpmaps=NULL;
974        free(tmp);
975      }
976    }
977    free(inputs_as_text);
978  }
979  else {
980    /**
981     * Parse XML request
982     */ 
983    xmlInitParser();
984#ifdef DEBUG
985    fflush(stderr);
986    fprintf(stderr,"BEFORE %s\n",postRequest->value);
987    fflush(stderr);
988#endif
989    xmlDocPtr doc =
990      xmlParseMemory(postRequest->value,cgiContentLength);
991#ifdef DEBUG
992    fprintf(stderr,"AFTER\n");
993    fflush(stderr);
994#endif
995    xmlNodePtr cur = xmlDocGetRootElement(doc);
996    /**
997     * Parse every Input in DataInputs node.
998     */
999    maps* tempMaps=NULL;
1000    xmlXPathObjectPtr tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='Input']");
1001    xmlNodeSet* tmps=tmpsptr->nodesetval;
1002#ifdef DEBUG
1003    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1004#endif
1005    for(int k=0;k<tmps->nodeNr;k++){
1006      maps *tmpmaps=NULL;
1007      xmlNodePtr cur=tmps->nodeTab[k];
1008      if(tmps->nodeTab[k]->type == XML_ELEMENT_NODE) {
1009        /**
1010         * A specific Input node.
1011         */
1012#ifdef DEBUG
1013        fprintf(stderr, "= element 0 node \"%s\"\n", cur->name);
1014#endif
1015        xmlNodePtr cur2=cur->children;
1016        while(cur2!=NULL){
1017          while(cur2!=NULL && cur2->type!=XML_ELEMENT_NODE)
1018            cur2=cur2->next;
1019          if(cur2==NULL)
1020            break;
1021          /**
1022           * Indentifier
1023           */
1024          if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1025            xmlChar *val= xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1026            if(tmpmaps==NULL){
1027              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1028              if(tmpmaps == NULL){
1029                return errorException(m, _("Unable to allocate memory."), "InternalError");
1030              }
1031              tmpmaps->name=strdup((char*)val);
1032              tmpmaps->content=NULL;
1033              tmpmaps->next=NULL;
1034            }
1035            xmlFree(val);
1036          }
1037          /**
1038           * Title, Asbtract
1039           */
1040          if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1041             xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1042            xmlChar *val=
1043              xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1044            if(tmpmaps==NULL){
1045              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1046              if(tmpmaps == NULL){
1047                return errorException(m, _("Unable to allocate memory."), "InternalError");
1048              }
1049              tmpmaps->name="missingIndetifier";
1050              tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1051              tmpmaps->next=NULL;
1052            }
1053            else{
1054              if(tmpmaps->content!=NULL)
1055                addToMap(tmpmaps->content,
1056                         (char*)cur2->name,(char*)val);
1057              else
1058                tmpmaps->content=
1059                  createMap((char*)cur2->name,(char*)val);
1060            }
1061#ifdef DEBUG
1062            dumpMaps(tmpmaps);
1063#endif
1064            xmlFree(val);
1065          }
1066          /**
1067           * InputDataFormChoice (Reference or Data ?)
1068           */
1069          if(xmlStrcasecmp(cur2->name,BAD_CAST "Reference")==0){
1070            /**
1071             * Get every attribute from a Reference node
1072             * mimeType, encoding, schema, href, method
1073             * Header and Body gesture should be added here
1074             */
1075#ifdef DEBUG
1076            fprintf(stderr,"REFERENCE\n");
1077#endif
1078            map* referenceMap=NULL;
1079            char *refs[5];
1080            refs[0]="mimeType";
1081            refs[1]="encoding";
1082            refs[2]="schema";
1083            refs[3]="method";
1084            refs[4]="href";
1085            char*url;
1086            for(int l=0;l<5;l++){
1087#ifdef DEBUG
1088              fprintf(stderr,"*** %s ***",refs[l]);
1089#endif
1090              xmlChar *val=xmlGetProp(cur2,BAD_CAST refs[l]);
1091              if(val!=NULL && xmlStrlen(val)>0){
1092                if(tmpmaps->content!=NULL)
1093                  addToMap(tmpmaps->content,refs[l],(char*)val);
1094                else
1095                  tmpmaps->content=createMap(refs[l],(char*)val);
1096                map* ltmp=getMap(tmpmaps->content,"method");
1097                if(l==4){
1098                  if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0)
1099                     && CHECK_INET_HANDLE(hInternet)){
1100                    res=InternetOpenUrl(hInternet,(char*)val,NULL,0,
1101                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1102                    char* tmpContent=
1103                      (char*)calloc((res.nDataLen+1),sizeof(char));
1104                    if(tmpContent == NULL){
1105                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1106                    }
1107                    size_t dwRead;
1108                    InternetReadFile(res, (LPVOID)tmpContent,
1109                                     res.nDataLen, &dwRead);
1110                    tmpContent[res.nDataLen]=0;
1111                    addToMap(tmpmaps->content,"value",tmpContent);
1112                  }
1113                }
1114              }
1115#ifdef DEBUG
1116              fprintf(stderr,"%s\n",val);
1117#endif
1118              xmlFree(val);
1119            }
1120#ifdef POST_DEBUG
1121            fprintf(stderr,"Parse Header and Body from Reference \n");
1122#endif
1123            xmlNodePtr cur3=cur2->children;
1124            hInternet.header=NULL;
1125            while(cur3){
1126              if(xmlStrcasecmp(cur3->name,BAD_CAST "Header")==0 ){
1127                xmlNodePtr cur4=cur3;
1128                char *tmp=new char[cgiContentLength];
1129                char *ha[2];
1130                ha[0]="key";
1131                ha[1]="value";
1132                int hai;
1133                char *has;
1134                char *key;
1135                for(hai=0;hai<2;hai++){
1136                  xmlChar *val=xmlGetProp(cur3,BAD_CAST ha[hai]);
1137#ifdef POST_DEBUG
1138                  fprintf(stderr,"%s = %s\n",ha[hai],(char*)val);
1139#endif
1140                  if(hai==0){
1141                    key=(char*)calloc((1+strlen((char*)val)),sizeof(char));
1142                    snprintf(key,1+strlen((char*)val),"%s",(char*)val);
1143                  }else{
1144                    has=(char*)calloc((3+strlen((char*)val)+strlen(key)),sizeof(char));
1145                    if(has == NULL){
1146                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1147                    }
1148                    snprintf(has,(3+strlen((char*)val)+strlen(key)),"%s: %s",key,(char*)val);
1149#ifdef POST_DEBUG
1150                    fprintf(stderr,"%s\n",has);
1151#endif
1152                  }
1153                }
1154                hInternet.header=curl_slist_append(hInternet.header, has);
1155                //free(has);
1156              }
1157              else{
1158#ifdef POST_DEBUG
1159                fprintf(stderr,"Try to fetch the body part of the request ...\n");
1160#endif
1161                if(xmlStrcasecmp(cur3->name,BAD_CAST "Body")==0 ){
1162#ifdef POST_DEBUG
1163                  fprintf(stderr,"Body part found !!!\n",(char*)cur3->content);
1164#endif
1165                  char *tmp=new char[cgiContentLength];
1166                  memset(tmp,0,cgiContentLength);
1167                  xmlNodePtr cur4=cur3->children;
1168                  while(cur4!=NULL){
1169                    xmlDocPtr bdoc = xmlNewDoc(BAD_CAST "1.0");
1170                    bdoc->encoding = xmlCharStrdup ("UTF-8");
1171                    xmlDocSetRootElement(bdoc,cur4);
1172                    xmlChar* btmps;
1173                    int bsize;
1174                    xmlDocDumpMemory(bdoc,&btmps,&bsize);
1175#ifdef POST_DEBUG
1176                    fprintf(stderr,"Body part found !!! %s %s\n",tmp,(char*)btmps);
1177#endif
1178                    if(btmps!=NULL)
1179                      sprintf(tmp,"%s",(char*)btmps);
1180                    xmlFreeDoc(bdoc);
1181                    cur4=cur4->next;
1182                  }
1183                  map *btmp=getMap(tmpmaps->content,"href");
1184                  if(btmp!=NULL){
1185#ifdef POST_DEBUG
1186                    fprintf(stderr,"%s %s\n",btmp->value,tmp);
1187                    curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1188#endif
1189                    res=InternetOpenUrl(hInternet,btmp->value,tmp,strlen(tmp),
1190                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1191                    char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1192                    if(tmpContent == NULL){
1193                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1194                    }
1195                    size_t dwRead;
1196                    InternetReadFile(res, (LPVOID)tmpContent,
1197                                     res.nDataLen, &dwRead);
1198                    tmpContent[res.nDataLen]=0;
1199                    if(hInternet.header!=NULL)
1200                      curl_slist_free_all(hInternet.header);
1201                    addToMap(tmpmaps->content,"value",tmpContent);
1202#ifdef POST_DEBUG
1203                    fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1204#endif
1205                  }
1206                }
1207                else
1208                  if(xmlStrcasecmp(cur3->name,BAD_CAST "BodyReference")==0 ){
1209                    xmlChar *val=xmlGetProp(cur3,BAD_CAST "href");
1210                    HINTERNET bInternet,res1;
1211                    bInternet=InternetOpen(
1212#ifndef WIN32
1213                                           (LPCTSTR)
1214#endif
1215                                           "ZooWPSClient\0",
1216                                           INTERNET_OPEN_TYPE_PRECONFIG,
1217                                           NULL,NULL, 0);
1218                    if(!CHECK_INET_HANDLE(bInternet))
1219                      fprintf(stderr,"WARNING : hInternet handle failed to initialize");
1220#ifdef POST_DEBUG
1221                    curl_easy_setopt(bInternet.handle, CURLOPT_VERBOSE, 1);
1222#endif
1223                    res1=InternetOpenUrl(bInternet,(char*)val,NULL,0,
1224                                         INTERNET_FLAG_NO_CACHE_WRITE,0);
1225                    char* tmp=
1226                      (char*)calloc((res1.nDataLen+1),sizeof(char));
1227                    if(tmp == NULL){
1228                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1229                    }
1230                    size_t bRead;
1231                    InternetReadFile(res1, (LPVOID)tmp,
1232                                     res1.nDataLen, &bRead);
1233                    tmp[res1.nDataLen]=0;
1234                    InternetCloseHandle(bInternet);
1235                    map *btmp=getMap(tmpmaps->content,"href");
1236                    if(btmp!=NULL){
1237#ifdef POST_DEBUG
1238                      fprintf(stderr,"%s %s\n",btmp->value,tmp);
1239                      curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1240#endif
1241                      res=InternetOpenUrl(hInternet,btmp->value,tmp,
1242                                          strlen(tmp),
1243                                          INTERNET_FLAG_NO_CACHE_WRITE,0);
1244                      char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1245                      if(tmpContent == NULL){
1246                        return errorException(m, _("Unable to allocate memory."), "InternalError");
1247                      }
1248                      size_t dwRead;
1249                      InternetReadFile(res, (LPVOID)tmpContent,
1250                                       res.nDataLen, &dwRead);
1251                      tmpContent[res.nDataLen]=0;
1252                      if(hInternet.header!=NULL)
1253                        curl_slist_free_all(hInternet.header);
1254                      addToMap(tmpmaps->content,"value",tmpContent);
1255#ifdef POST_DEBUG
1256                      fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1257#endif
1258                    }
1259                  }
1260              }
1261              cur3=cur3->next;
1262            }
1263#ifdef POST_DEBUG
1264            fprintf(stderr,"Header and Body was parsed from Reference \n");
1265#endif
1266#ifdef DEBUG
1267            dumpMap(tmpmaps->content);
1268            fprintf(stderr, "= element 2 node \"%s\" = (%s)\n", 
1269                    cur2->name,cur2->content);
1270#endif
1271          }
1272          else if(xmlStrcasecmp(cur2->name,BAD_CAST "Data")==0){
1273#ifdef DEBUG
1274            fprintf(stderr,"DATA\n");
1275#endif
1276            xmlNodePtr cur4=cur2->children;
1277            while(cur4!=NULL){
1278              while(cur4!=NULL &&cur4->type!=XML_ELEMENT_NODE)
1279                cur4=cur4->next;
1280              if(cur4==NULL)
1281                break;
1282              if(xmlStrcasecmp(cur4->name, BAD_CAST "LiteralData")==0){
1283                /**
1284                 * Get every attribute from a LiteralData node
1285                 * dataType , uom
1286                 */
1287                char *lits[2];
1288                lits[0]="dataType";
1289                lits[1]="uom";
1290                for(int l=0;l<2;l++){
1291#ifdef DEBUG
1292                  fprintf(stderr,"*** LiteralData %s ***",lits[l]);
1293#endif
1294                  xmlChar *val=xmlGetProp(cur4,BAD_CAST lits[l]);
1295                  if(val!=NULL && strlen((char*)val)>0){
1296                    if(tmpmaps->content!=NULL)
1297                      addToMap(tmpmaps->content,lits[l],(char*)val);
1298                    else
1299                      tmpmaps->content=createMap(lits[l],(char*)val);
1300                  }
1301#ifdef DEBUG
1302                  fprintf(stderr,"%s\n",val);
1303#endif
1304                  xmlFree(val);
1305                }
1306              }
1307              else if(xmlStrcasecmp(cur4->name, BAD_CAST "ComplexData")==0){
1308                /**
1309                 * Get every attribute from a Reference node
1310                 * mimeType, encoding, schema
1311                 */
1312                char *coms[3];
1313                coms[0]="mimeType";
1314                coms[1]="encoding";
1315                coms[2]="schema";
1316                for(int l=0;l<3;l++){
1317#ifdef DEBUG
1318                  fprintf(stderr,"*** ComplexData %s ***",coms[l]);
1319#endif
1320                  xmlChar *val=xmlGetProp(cur4,BAD_CAST coms[l]);
1321                  if(val!=NULL && strlen((char*)val)>0){
1322                    if(tmpmaps->content!=NULL)
1323                      addToMap(tmpmaps->content,coms[l],(char*)val);
1324                    else
1325                      tmpmaps->content=createMap(coms[l],(char*)val);
1326                  }
1327#ifdef DEBUG
1328                  fprintf(stderr,"%s\n",val);
1329#endif
1330                  xmlFree(val);
1331                }
1332              }
1333              xmlChar* mv=xmlNodeListGetString(doc,cur4->xmlChildrenNode,1);
1334              addToMap(tmpmaps->content,"value",(char*)mv);
1335              xmlFree(mv);
1336              cur4=cur4->next;
1337            }
1338          }
1339#ifdef DEBUG
1340          fprintf(stderr,"cur2 next \n");
1341          fflush(stderr);
1342#endif
1343          cur2=cur2->next;
1344        }
1345#ifdef DEBUG
1346        fprintf(stderr,"ADD MAPS TO REQUEST MAPS !\n");
1347        fflush(stderr);
1348#endif
1349        addMapsToMaps(&request_input_real_format,tmpmaps);
1350       
1351#ifdef DEBUG
1352        fprintf(stderr,"******TMPMAPS*****\n");
1353        dumpMaps(tmpmaps);
1354        fprintf(stderr,"******REQUESTMAPS*****\n");
1355        dumpMaps(request_input_real_format);
1356#endif
1357        freeMaps(&tmpmaps);
1358        free(tmpmaps);
1359        tmpmaps=NULL;         
1360      }
1361#ifdef DEBUG
1362      dumpMaps(tmpmaps); 
1363#endif
1364    }
1365#ifdef DEBUG
1366    fprintf(stderr,"Search for response document node\n");
1367#endif
1368    xmlXPathFreeObject(tmpsptr);
1369    //xmlFree(tmps);
1370    tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='ResponseDocument']");
1371    tmps=tmpsptr->nodesetval;
1372#ifdef DEBUG
1373    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1374#endif
1375    for(int k=0;k<tmps->nodeNr;k++){
1376      addToMap(request_inputs,"ResponseDocument","");
1377      request_output_real_format;
1378      maps *tmpmaps=NULL;
1379      xmlNodePtr cur=tmps->nodeTab[k];
1380      if(cur->type == XML_ELEMENT_NODE) {
1381        /**
1382         * A specific responseDocument node.
1383         */
1384        if(tmpmaps==NULL){
1385          tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1386          if(tmpmaps == NULL){
1387            return errorException(m, _("Unable to allocate memory."), "InternalError");
1388          }
1389          tmpmaps->name="unknownIdentifier";
1390          tmpmaps->next=NULL;
1391        }
1392        /**
1393         * Get every attribute from a LiteralData node
1394         * storeExecuteResponse, lineage, status
1395         */
1396        char *ress[3];
1397        ress[0]="storeExecuteResponse";
1398        ress[1]="lineage";
1399        ress[2]="status";
1400        xmlChar *val;
1401        for(int l=0;l<3;l++){
1402#ifdef DEBUG
1403          fprintf(stderr,"*** %s ***\t",ress[l]);
1404#endif
1405          val=xmlGetProp(cur,BAD_CAST ress[l]);
1406          if(val!=NULL && strlen((char*)val)>0){
1407            if(tmpmaps->content!=NULL)
1408              addToMap(tmpmaps->content,ress[l],(char*)val);
1409            else
1410              tmpmaps->content=createMap(ress[l],(char*)val);
1411            addToMap(request_inputs,ress[l],(char*)val);
1412          }
1413#ifdef DEBUG
1414          fprintf(stderr,"%s\n",val);
1415#endif
1416          xmlFree(val);
1417        }
1418        xmlNodePtr cur1=cur->children;
1419        while(cur1){
1420          if(xmlStrncasecmp(cur1->name,BAD_CAST "Output",xmlStrlen(cur1->name))==0){
1421            /**
1422             * Get every attribute from a Output node
1423             * mimeType, encoding, schema, uom, asReference
1424             */
1425            char *outs[5];
1426            outs[0]="mimeType";
1427            outs[1]="encoding";
1428            outs[2]="schema";
1429            outs[3]="uom";
1430            outs[4]="asReference";
1431            for(int l=0;l<5;l++){
1432#ifdef DEBUG
1433              fprintf(stderr,"*** %s ***\t",outs[l]);
1434#endif
1435              val=xmlGetProp(cur1,BAD_CAST outs[l]);
1436              if(val!=NULL && strlen((char*)val)>0){
1437                if(tmpmaps->content!=NULL)
1438                  addToMap(tmpmaps->content,outs[l],(char*)val);
1439                else
1440                  tmpmaps->content=createMap(outs[l],(char*)val);
1441              }
1442#ifdef DEBUG
1443              fprintf(stderr,"%s\n",val);
1444#endif
1445              xmlFree(val);
1446            }
1447           
1448            xmlNodePtr cur2=cur1->children;
1449            while(cur2){
1450              /**
1451               * Indentifier
1452               */
1453              if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1454                xmlChar *val=
1455                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1456                if(tmpmaps==NULL){
1457                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1458                  if(tmpmaps == NULL){
1459                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1460                  }
1461                  tmpmaps->name=strdup((char*)val);
1462                  tmpmaps->content=NULL;
1463                  tmpmaps->next=NULL;
1464                }
1465                else
1466                  tmpmaps->name=strdup((char*)val);;
1467                xmlFree(val);
1468              }
1469              /**
1470               * Title, Asbtract
1471               */
1472              if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1473                 xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1474                xmlChar *val=
1475                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1476                if(tmpmaps==NULL){
1477                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1478                  if(tmpmaps == NULL){
1479                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1480                  }
1481                  tmpmaps->name="missingIndetifier";
1482                  tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1483                  tmpmaps->next=NULL;
1484                }
1485                else{
1486                  if(tmpmaps->content!=NULL)
1487                    addToMap(tmpmaps->content,
1488                             (char*)cur2->name,(char*)val);
1489                  else
1490                    tmpmaps->content=
1491                      createMap((char*)cur2->name,(char*)val);
1492                }
1493                xmlFree(val);
1494              }
1495              cur2=cur2->next;
1496            }
1497          }
1498          cur1=cur1->next;
1499        }
1500      }
1501      //xmlFree(cur);
1502      if(request_output_real_format==NULL)
1503        request_output_real_format=tmpmaps;
1504      else
1505        addMapsToMaps(&request_output_real_format,tmpmaps);
1506#ifdef DEBUG
1507      dumpMaps(tmpmaps);
1508#endif
1509    }
1510    xmlXPathFreeObject(tmpsptr);
1511    //xmlFree(tmps);
1512    tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='RawDataOutput']");
1513    tmps=tmpsptr->nodesetval;
1514#ifdef DEBUG
1515    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1516#endif
1517    for(int k=0;k<tmps->nodeNr;k++){
1518      addToMap(request_inputs,"RawDataOutput","");
1519      xmlNodePtr cur1=tmps->nodeTab[k];
1520      xmlChar *val;
1521      /**
1522       * Get every attribute from a Output node
1523       * mimeType, encoding, schema, uom, asReference
1524       */
1525      char *outs[4];
1526      outs[0]="mimeType";
1527      outs[1]="encoding";
1528      outs[2]="schema";
1529      outs[3]="uom";
1530      for(int l=0;l<4;l++){
1531#ifdef DEBUG
1532        fprintf(stderr,"*** %s ***\t",outs[l]);
1533#endif
1534        val=xmlGetProp(cur1,BAD_CAST outs[l]);
1535        if(val!=NULL && strlen((char*)val)>0){
1536          if(tmpmaps==NULL){
1537            tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1538            if(tmpmaps == NULL){
1539              return errorException(m, _("Unable to allocate memory."), "InternalError");
1540            }
1541            tmpmaps->name="unknownIdentifier";
1542            tmpmaps->content=createMap(outs[l],(char*)val);
1543            tmpmaps->next=NULL;
1544          }
1545          else
1546            addToMap(tmpmaps->content,outs[l],(char*)val);
1547        }
1548#ifdef DEBUG
1549        fprintf(stderr,"%s\n",val);
1550#endif
1551        xmlFree(val);
1552      }
1553           
1554      xmlNodePtr cur2=cur1->children;
1555      while(cur2){
1556        /**
1557         * Indentifier
1558         */
1559        if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1560          val=
1561            xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1562          if(tmpmaps==NULL){
1563            tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1564            if(tmpmaps == NULL){
1565              return errorException(m, _("Unable to allocate memory."), "InternalError");
1566            }
1567            tmpmaps->name=strdup((char*)val);
1568            tmpmaps->content=NULL;
1569            tmpmaps->next=NULL;
1570          }
1571          else
1572            tmpmaps->name=strdup((char*)val);;
1573          xmlFree(val);
1574        }
1575        cur2=cur2->next;
1576      }
1577      if(request_output_real_format==NULL)
1578        request_output_real_format=tmpmaps;
1579      else
1580        addMapsToMaps(&request_output_real_format,tmpmaps);
1581#ifdef DEBUG
1582      dumpMaps(tmpmaps);
1583#endif
1584    }
1585    xmlXPathFreeObject(tmpsptr);
1586    //xmlFree(tmps);
1587    xmlCleanupParser();
1588  }
1589
1590  //if(CHECK_INET_HANDLE(hInternet))
1591  InternetCloseHandle(hInternet);
1592
1593#ifdef DEBUG
1594  fprintf(stderr,"\n%i\n",i);
1595  dumpMaps(request_input_real_format);
1596  dumpMaps(request_output_real_format);
1597  dumpMap(request_inputs);
1598#endif
1599
1600  /**
1601   * Ensure that each requested arguments are present in the request
1602   * DataInputs and ResponseDocument / RawDataOutput
1603   */ 
1604  char *dfv=addDefaultValues(&request_input_real_format,s1->inputs,m,"inputs");
1605  if(strcmp(dfv,"")!=0){
1606    char tmps[1024];
1607    snprintf(tmps,1024,_("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."),dfv);
1608    map* tmpe=createMap("text",tmps);
1609    addToMap(tmpe,"code","MissingParameterValue");
1610    printExceptionReportResponse(m,tmpe);
1611    freeMap(&tmpe);
1612    free(tmpe);
1613    freeMaps(&m);
1614    free(m);
1615    free(REQUEST);
1616    freeMaps(&request_input_real_format);
1617    free(request_input_real_format);
1618    freeMaps(&request_output_real_format);
1619    free(request_output_real_format);
1620    freeMaps(&tmpmaps);
1621    free(tmpmaps);
1622    return 1;
1623  }
1624  addDefaultValues(&request_output_real_format,s1->outputs,m,"outputs");
1625
1626#ifdef DEBUG
1627  fprintf(stderr,"REQUEST_INPUTS\n");
1628  dumpMaps(request_input_real_format);
1629  fprintf(stderr,"REQUEST_OUTPUTS\n");
1630  dumpMaps(request_output_real_format);
1631#endif
1632
1633  maps* curs=getMaps(m,"env");
1634  if(curs!=NULL){
1635    map* mapcs=curs->content;
1636    while(mapcs!=NULLMAP){
1637#ifndef WIN32
1638      setenv(mapcs->name,mapcs->value,1);
1639#else
1640#ifdef DEBUG
1641      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1642#endif
1643      if(mapcs->value[strlen(mapcs->value)-2]=='\r'){
1644#ifdef DEBUG
1645        fprintf(stderr,"[ZOO: Env var finish with \r]\n");
1646#endif
1647        mapcs->value[strlen(mapcs->value)-1]=0;
1648      }
1649#ifdef DEBUG
1650      fflush(stderr);
1651      fprintf(stderr,"setting variable... %s\n",
1652#endif
1653              SetEnvironmentVariable(mapcs->name,mapcs->value)
1654#ifdef DEBUG
1655              ? "OK" : "FAILED");
1656#else
1657      ;
1658#endif
1659#ifdef DEBUG
1660      fflush(stderr);
1661#endif
1662#endif
1663#ifdef DEBUG
1664      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1665      fflush(stderr);
1666#endif
1667      mapcs=mapcs->next;
1668    }
1669  }
1670 
1671#ifdef DEBUG
1672  dumpMap(request_inputs);
1673#endif
1674
1675  /**
1676   * Need to check if we need to fork to load a status enabled
1677   */
1678  r_inputs=NULL;
1679  r_inputs=getMap(request_inputs,"storeExecuteResponse");
1680  int eres=SERVICE_STARTED;
1681  int cpid=getpid();
1682 
1683  maps *_tmpMaps=(maps*)malloc(MAPS_SIZE);
1684  _tmpMaps->name=strdup("lenv");
1685  char tmpBuff[100];
1686  sprintf(tmpBuff,"%i",cpid);
1687  _tmpMaps->content=createMap("sid",tmpBuff);
1688  _tmpMaps->next=NULL;
1689  addToMap(_tmpMaps->content,"status","0");
1690  addMapsToMaps(&m,_tmpMaps);
1691  freeMaps(&_tmpMaps);
1692  free(_tmpMaps);
1693
1694#ifdef DEBUG
1695  dumpMap(request_inputs);
1696#endif
1697
1698  if(r_inputs!=NULL)
1699    if(strcasecmp(r_inputs->value,"false")==0)
1700      r_inputs=NULL;
1701  if(r_inputs==NULLMAP){
1702    loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1703  }
1704  else{
1705    pid_t   pid;
1706#ifdef DEBUG
1707    fprintf(stderr,"\nPID : %d\n",cpid);
1708#endif
1709
1710#ifndef WIN32
1711    pid = fork ();
1712#else
1713    pid = 0;
1714#endif
1715    if (pid > 0) {
1716      /**
1717       * dady :
1718       * set status to SERVICE_ACCEPTED
1719       */
1720#ifdef DEBUG
1721      fprintf(stderr,"father pid continue (origin %d) %d ...\n",cpid,getpid());
1722#endif
1723      eres=SERVICE_ACCEPTED;
1724    }else if (pid == 0) {
1725      /**
1726       * son : have to close the stdout, stdin and stderr to let the parent
1727       * process answer to http client.
1728       */
1729      r_inputs=getMapFromMaps(m,"main","tmpPath");
1730      map* r_inputs1=getMap(s1->content,"ServiceProvider");
1731      char* fbkp=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1732      sprintf(fbkp,"%s/%s_%d.xml",r_inputs->value,r_inputs1->value,cpid);
1733      char* flog=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1734      sprintf(flog,"%s/%s_%d_error.log",r_inputs->value,r_inputs1->value,cpid);
1735#ifdef DEBUG
1736      fprintf(stderr,"RUN IN BACKGROUND MODE \n");
1737      fprintf(stderr,"son pid continue (origin %d) %d ...\n",cpid,getpid());
1738      fprintf(stderr,"\nFILE TO STORE DATA %s\n",r_inputs->value);
1739#endif
1740      freopen(fbkp , "w+", stdout);
1741      fclose(stdin);
1742      freopen(flog,"w+",stderr);
1743      free(fbkp);
1744      free(flog);
1745      /**
1746       * set status to SERVICE_STARTED and flush stdout to ensure full
1747       * content was outputed (the file used to store the ResponseDocument).
1748       * The rewind stdout to restart writing from the bgining of the file,
1749       * this way the data will be updated at the end of the process run.
1750       */
1751      updateStatus(m);
1752      printProcessResponse(m,request_inputs,cpid,
1753                            s1,r_inputs1->value,SERVICE_STARTED,
1754                            request_input_real_format,
1755                            request_output_real_format);
1756      fflush(stdout);
1757      rewind(stdout);
1758
1759      loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1760
1761    } else {
1762      /**
1763       * error server don't accept the process need to output a valid
1764       * error response here !!!
1765       */
1766      eres=-1;
1767      errorException(m, _("Unable to run the child process properly"), "InternalError");
1768    }
1769       
1770  }
1771
1772#ifdef DEBUG
1773  dumpMaps(request_output_real_format);
1774  fprintf(stderr,"Function loaded and returned %d\n",eres);
1775  fflush(stderr);
1776#endif
1777  if(eres!=-1)
1778    outputResponse(s1,request_input_real_format,
1779                   request_output_real_format,request_inputs,
1780                   cpid,m,eres);
1781
1782  if(((int)getpid())!=cpid){
1783    fclose(stdout);
1784    fclose(stderr);
1785    unhandleStatus(m);
1786  }
1787
1788  freeService(&s1);
1789  free(s1);
1790  freeMaps(&m);
1791  free(m);
1792 
1793  freeMaps(&request_input_real_format);
1794  free(request_input_real_format);
1795 
1796  /* The following is requested but get issue using with Python support :/ */
1797  /* freeMaps(&request_output_real_format);
1798     free(request_output_real_format);*/
1799 
1800  free(REQUEST);
1801  free(SERVICE_URL);
1802#ifdef DEBUG
1803  fprintf(stderr,"Processed response \n");
1804  fflush(stdout);
1805  fflush(stderr);
1806#endif
1807
1808  return 0;
1809}
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