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

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

Add binary files support for DataInputs?. Managing this binary format for the Python language, this should resolve the GRASS support in case of binary DataInputs?.

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