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

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

Small fixs in freeElements and runRequest.

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