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

Last change on this file since 34 was 34, checked in by djay, 14 years ago

Make ZOO Kernel able to speak the natural language you teach him by using gettext tools for creating translation files. Add basic french translation .po files as current messages.po. Gettext Domains used are zoo-kernel and zoo-services.

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