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

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

Adding contains and getIoTypeFromElement functions in service.h. Remove memory leaks from the Service Configuration Parser, add the support for multiple supported formats as before. Small modification of the ZOO-Kernel Java Support, adding the capability to access the modified main_conf HashMap? from the Kernel to let lenv message map pass when an error occurs and it is handled in the Service code. Adding same capability for the ZOO-Kernel Python Support. Use strcasecmp in service.h rather than strlen+strncasecmp. Ensure that only OWS compliant informations are available for Contact.Phone and Contact.Adress. Remove memory leak in createExceptionReportNode. Correction of the addDefaultValues function to add the default format using the informations from DataInputs? if present, this should correct the behavior of the ZOO-Kernel when choosing the extension value which should now point to the corresponding zcfg value if present. Don't set the NULL value for inputs not provided in the DataInputs?, still set NULL as default value for outputs. Avoid segfault in freeElements when some zcfg values was not set correctly. Link against the client libjvm.so file rather than the server one.

File size: 48.4 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=strdup(tmpContent);
957                free(tmpContent);
958              }
959            addToMap(tmpmaps->content,tmpn1,tmpv1+1);
960          }
961          tmpc=strtok(NULL,"@");
962        }
963#ifdef DEBUG
964        dumpMaps(tmpmaps);
965        fflush(stderr);
966#endif
967        if(request_input_real_format==NULL)
968          request_input_real_format=dupMaps(&tmpmaps);
969        else
970          addMapsToMaps(&request_input_real_format,tmpmaps);
971        /*freeMap(&tmpmaps->content);
972        free(tmpmaps->content);
973        tmpmaps->content=NULL;*/
974        freeMaps(&tmpmaps);
975        free(tmpmaps);
976          //}
977        tmpmaps=NULL;
978        free(tmp);
979      }
980    }
981    free(inputs_as_text);
982  }
983  else {
984    /**
985     * Parse XML request
986     */ 
987    xmlInitParser();
988#ifdef DEBUG
989    fflush(stderr);
990    fprintf(stderr,"BEFORE %s\n",postRequest->value);
991    fflush(stderr);
992#endif
993    xmlDocPtr doc =
994      xmlParseMemory(postRequest->value,cgiContentLength);
995#ifdef DEBUG
996    fprintf(stderr,"AFTER\n");
997    fflush(stderr);
998#endif
999    xmlNodePtr cur = xmlDocGetRootElement(doc);
1000    /**
1001     * Parse every Input in DataInputs node.
1002     */
1003    maps* tempMaps=NULL;
1004    xmlXPathObjectPtr tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='Input']");
1005    xmlNodeSet* tmps=tmpsptr->nodesetval;
1006#ifdef DEBUG
1007    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1008#endif
1009    for(int k=0;k<tmps->nodeNr;k++){
1010      maps *tmpmaps=NULL;
1011      xmlNodePtr cur=tmps->nodeTab[k];
1012      if(tmps->nodeTab[k]->type == XML_ELEMENT_NODE) {
1013        /**
1014         * A specific Input node.
1015         */
1016#ifdef DEBUG
1017        fprintf(stderr, "= element 0 node \"%s\"\n", cur->name);
1018#endif
1019        xmlNodePtr cur2=cur->children;
1020        while(cur2!=NULL){
1021          while(cur2!=NULL && cur2->type!=XML_ELEMENT_NODE)
1022            cur2=cur2->next;
1023          if(cur2==NULL)
1024            break;
1025          /**
1026           * Indentifier
1027           */
1028          if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1029            xmlChar *val= xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1030            if(tmpmaps==NULL){
1031              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1032              if(tmpmaps == NULL){
1033                return errorException(m, _("Unable to allocate memory."), "InternalError");
1034              }
1035              tmpmaps->name=strdup((char*)val);
1036              tmpmaps->content=NULL;
1037              tmpmaps->next=NULL;
1038            }
1039            xmlFree(val);
1040          }
1041          /**
1042           * Title, Asbtract
1043           */
1044          if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1045             xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1046            xmlChar *val=
1047              xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1048            if(tmpmaps==NULL){
1049              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1050              if(tmpmaps == NULL){
1051                return errorException(m, _("Unable to allocate memory."), "InternalError");
1052              }
1053              tmpmaps->name="missingIndetifier";
1054              tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1055              tmpmaps->next=NULL;
1056            }
1057            else{
1058              if(tmpmaps->content!=NULL)
1059                addToMap(tmpmaps->content,
1060                         (char*)cur2->name,(char*)val);
1061              else
1062                tmpmaps->content=
1063                  createMap((char*)cur2->name,(char*)val);
1064            }
1065#ifdef DEBUG
1066            dumpMaps(tmpmaps);
1067#endif
1068            xmlFree(val);
1069          }
1070          /**
1071           * InputDataFormChoice (Reference or Data ?)
1072           */
1073          if(xmlStrcasecmp(cur2->name,BAD_CAST "Reference")==0){
1074            /**
1075             * Get every attribute from a Reference node
1076             * mimeType, encoding, schema, href, method
1077             * Header and Body gesture should be added here
1078             */
1079#ifdef DEBUG
1080            fprintf(stderr,"REFERENCE\n");
1081#endif
1082            map* referenceMap=NULL;
1083            char *refs[5];
1084            refs[0]="mimeType";
1085            refs[1]="encoding";
1086            refs[2]="schema";
1087            refs[3]="method";
1088            refs[4]="href";
1089            char*url;
1090            for(int l=0;l<5;l++){
1091#ifdef DEBUG
1092              fprintf(stderr,"*** %s ***",refs[l]);
1093#endif
1094              xmlChar *val=xmlGetProp(cur2,BAD_CAST refs[l]);
1095              if(val!=NULL && xmlStrlen(val)>0){
1096                if(tmpmaps->content!=NULL)
1097                  addToMap(tmpmaps->content,refs[l],(char*)val);
1098                else
1099                  tmpmaps->content=createMap(refs[l],(char*)val);
1100                map* ltmp=getMap(tmpmaps->content,"method");
1101                if(l==4){
1102                  if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0)
1103                     && CHECK_INET_HANDLE(hInternet)){
1104                    res=InternetOpenUrl(hInternet,(char*)val,NULL,0,
1105                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1106                    char* tmpContent=
1107                      (char*)calloc((res.nDataLen+1),sizeof(char));
1108                    if(tmpContent == NULL){
1109                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1110                    }
1111                    size_t dwRead;
1112                    InternetReadFile(res, (LPVOID)tmpContent,
1113                                     res.nDataLen, &dwRead);
1114                    tmpContent[res.nDataLen]=0;
1115                    addToMap(tmpmaps->content,"value",tmpContent);
1116                  }
1117                }
1118              }
1119#ifdef DEBUG
1120              fprintf(stderr,"%s\n",val);
1121#endif
1122              xmlFree(val);
1123            }
1124#ifdef POST_DEBUG
1125            fprintf(stderr,"Parse Header and Body from Reference \n");
1126#endif
1127            xmlNodePtr cur3=cur2->children;
1128            hInternet.header=NULL;
1129            while(cur3){
1130              if(xmlStrcasecmp(cur3->name,BAD_CAST "Header")==0 ){
1131                xmlNodePtr cur4=cur3;
1132                char *tmp=new char[cgiContentLength];
1133                char *ha[2];
1134                ha[0]="key";
1135                ha[1]="value";
1136                int hai;
1137                char *has;
1138                char *key;
1139                for(hai=0;hai<2;hai++){
1140                  xmlChar *val=xmlGetProp(cur3,BAD_CAST ha[hai]);
1141#ifdef POST_DEBUG
1142                  fprintf(stderr,"%s = %s\n",ha[hai],(char*)val);
1143#endif
1144                  if(hai==0){
1145                    key=(char*)calloc((1+strlen((char*)val)),sizeof(char));
1146                    snprintf(key,1+strlen((char*)val),"%s",(char*)val);
1147                  }else{
1148                    has=(char*)calloc((3+strlen((char*)val)+strlen(key)),sizeof(char));
1149                    if(has == NULL){
1150                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1151                    }
1152                    snprintf(has,(3+strlen((char*)val)+strlen(key)),"%s: %s",key,(char*)val);
1153#ifdef POST_DEBUG
1154                    fprintf(stderr,"%s\n",has);
1155#endif
1156                  }
1157                }
1158                hInternet.header=curl_slist_append(hInternet.header, has);
1159                //free(has);
1160              }
1161              else{
1162#ifdef POST_DEBUG
1163                fprintf(stderr,"Try to fetch the body part of the request ...\n");
1164#endif
1165                if(xmlStrcasecmp(cur3->name,BAD_CAST "Body")==0 ){
1166#ifdef POST_DEBUG
1167                  fprintf(stderr,"Body part found !!!\n",(char*)cur3->content);
1168#endif
1169                  char *tmp=new char[cgiContentLength];
1170                  memset(tmp,0,cgiContentLength);
1171                  xmlNodePtr cur4=cur3->children;
1172                  while(cur4!=NULL){
1173                    xmlDocPtr bdoc = xmlNewDoc(BAD_CAST "1.0");
1174                    bdoc->encoding = xmlCharStrdup ("UTF-8");
1175                    xmlDocSetRootElement(bdoc,cur4);
1176                    xmlChar* btmps;
1177                    int bsize;
1178                    xmlDocDumpMemory(bdoc,&btmps,&bsize);
1179#ifdef POST_DEBUG
1180                    fprintf(stderr,"Body part found !!! %s %s\n",tmp,(char*)btmps);
1181#endif
1182                    if(btmps!=NULL)
1183                      sprintf(tmp,"%s",(char*)btmps);
1184                    xmlFreeDoc(bdoc);
1185                    cur4=cur4->next;
1186                  }
1187                  map *btmp=getMap(tmpmaps->content,"href");
1188                  if(btmp!=NULL){
1189#ifdef POST_DEBUG
1190                    fprintf(stderr,"%s %s\n",btmp->value,tmp);
1191                    curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1192#endif
1193                    res=InternetOpenUrl(hInternet,btmp->value,tmp,strlen(tmp),
1194                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1195                    char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1196                    if(tmpContent == NULL){
1197                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1198                    }
1199                    size_t dwRead;
1200                    InternetReadFile(res, (LPVOID)tmpContent,
1201                                     res.nDataLen, &dwRead);
1202                    tmpContent[res.nDataLen]=0;
1203                    if(hInternet.header!=NULL)
1204                      curl_slist_free_all(hInternet.header);
1205                    addToMap(tmpmaps->content,"value",tmpContent);
1206#ifdef POST_DEBUG
1207                    fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1208#endif
1209                  }
1210                }
1211                else
1212                  if(xmlStrcasecmp(cur3->name,BAD_CAST "BodyReference")==0 ){
1213                    xmlChar *val=xmlGetProp(cur3,BAD_CAST "href");
1214                    HINTERNET bInternet,res1;
1215                    bInternet=InternetOpen(
1216#ifndef WIN32
1217                                           (LPCTSTR)
1218#endif
1219                                           "ZooWPSClient\0",
1220                                           INTERNET_OPEN_TYPE_PRECONFIG,
1221                                           NULL,NULL, 0);
1222                    if(!CHECK_INET_HANDLE(bInternet))
1223                      fprintf(stderr,"WARNING : hInternet handle failed to initialize");
1224#ifdef POST_DEBUG
1225                    curl_easy_setopt(bInternet.handle, CURLOPT_VERBOSE, 1);
1226#endif
1227                    res1=InternetOpenUrl(bInternet,(char*)val,NULL,0,
1228                                         INTERNET_FLAG_NO_CACHE_WRITE,0);
1229                    char* tmp=
1230                      (char*)calloc((res1.nDataLen+1),sizeof(char));
1231                    if(tmp == NULL){
1232                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1233                    }
1234                    size_t bRead;
1235                    InternetReadFile(res1, (LPVOID)tmp,
1236                                     res1.nDataLen, &bRead);
1237                    tmp[res1.nDataLen]=0;
1238                    InternetCloseHandle(bInternet);
1239                    map *btmp=getMap(tmpmaps->content,"href");
1240                    if(btmp!=NULL){
1241#ifdef POST_DEBUG
1242                      fprintf(stderr,"%s %s\n",btmp->value,tmp);
1243                      curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1244#endif
1245                      res=InternetOpenUrl(hInternet,btmp->value,tmp,
1246                                          strlen(tmp),
1247                                          INTERNET_FLAG_NO_CACHE_WRITE,0);
1248                      char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1249                      if(tmpContent == NULL){
1250                        return errorException(m, _("Unable to allocate memory."), "InternalError");
1251                      }
1252                      size_t dwRead;
1253                      InternetReadFile(res, (LPVOID)tmpContent,
1254                                       res.nDataLen, &dwRead);
1255                      tmpContent[res.nDataLen]=0;
1256                      if(hInternet.header!=NULL)
1257                        curl_slist_free_all(hInternet.header);
1258                      addToMap(tmpmaps->content,"value",tmpContent);
1259#ifdef POST_DEBUG
1260                      fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1261#endif
1262                    }
1263                  }
1264              }
1265              cur3=cur3->next;
1266            }
1267#ifdef POST_DEBUG
1268            fprintf(stderr,"Header and Body was parsed from Reference \n");
1269#endif
1270#ifdef DEBUG
1271            dumpMap(tmpmaps->content);
1272            fprintf(stderr, "= element 2 node \"%s\" = (%s)\n", 
1273                    cur2->name,cur2->content);
1274#endif
1275          }
1276          else if(xmlStrcasecmp(cur2->name,BAD_CAST "Data")==0){
1277#ifdef DEBUG
1278            fprintf(stderr,"DATA\n");
1279#endif
1280            xmlNodePtr cur4=cur2->children;
1281            while(cur4!=NULL){
1282              while(cur4!=NULL &&cur4->type!=XML_ELEMENT_NODE)
1283                cur4=cur4->next;
1284              if(cur4==NULL)
1285                break;
1286              if(xmlStrcasecmp(cur4->name, BAD_CAST "LiteralData")==0){
1287                /**
1288                 * Get every attribute from a LiteralData node
1289                 * dataType , uom
1290                 */
1291                char *lits[2];
1292                lits[0]="dataType";
1293                lits[1]="uom";
1294                for(int l=0;l<2;l++){
1295#ifdef DEBUG
1296                  fprintf(stderr,"*** LiteralData %s ***",lits[l]);
1297#endif
1298                  xmlChar *val=xmlGetProp(cur4,BAD_CAST lits[l]);
1299                  if(val!=NULL && strlen((char*)val)>0){
1300                    if(tmpmaps->content!=NULL)
1301                      addToMap(tmpmaps->content,lits[l],(char*)val);
1302                    else
1303                      tmpmaps->content=createMap(lits[l],(char*)val);
1304                  }
1305#ifdef DEBUG
1306                  fprintf(stderr,"%s\n",val);
1307#endif
1308                  xmlFree(val);
1309                }
1310              }
1311              else if(xmlStrcasecmp(cur4->name, BAD_CAST "ComplexData")==0){
1312                /**
1313                 * Get every attribute from a Reference node
1314                 * mimeType, encoding, schema
1315                 */
1316                char *coms[3];
1317                coms[0]="mimeType";
1318                coms[1]="encoding";
1319                coms[2]="schema";
1320                for(int l=0;l<3;l++){
1321#ifdef DEBUG
1322                  fprintf(stderr,"*** ComplexData %s ***",coms[l]);
1323#endif
1324                  xmlChar *val=xmlGetProp(cur4,BAD_CAST coms[l]);
1325                  if(val!=NULL && strlen((char*)val)>0){
1326                    if(tmpmaps->content!=NULL)
1327                      addToMap(tmpmaps->content,coms[l],(char*)val);
1328                    else
1329                      tmpmaps->content=createMap(coms[l],(char*)val);
1330                  }
1331#ifdef DEBUG
1332                  fprintf(stderr,"%s\n",val);
1333#endif
1334                  xmlFree(val);
1335                }
1336              }
1337              xmlChar* mv=xmlNodeListGetString(doc,cur4->xmlChildrenNode,1);
1338              addToMap(tmpmaps->content,"value",(char*)mv);
1339              xmlFree(mv);
1340              cur4=cur4->next;
1341            }
1342          }
1343#ifdef DEBUG
1344          fprintf(stderr,"cur2 next \n");
1345          fflush(stderr);
1346#endif
1347          cur2=cur2->next;
1348        }
1349#ifdef DEBUG
1350        fprintf(stderr,"ADD MAPS TO REQUEST MAPS !\n");
1351        fflush(stderr);
1352#endif
1353        addMapsToMaps(&request_input_real_format,tmpmaps);
1354       
1355#ifdef DEBUG
1356        fprintf(stderr,"******TMPMAPS*****\n");
1357        dumpMaps(tmpmaps);
1358        fprintf(stderr,"******REQUESTMAPS*****\n");
1359        dumpMaps(request_input_real_format);
1360#endif
1361        freeMaps(&tmpmaps);
1362        free(tmpmaps);
1363        tmpmaps=NULL;         
1364      }
1365#ifdef DEBUG
1366      dumpMaps(tmpmaps); 
1367#endif
1368    }
1369#ifdef DEBUG
1370    fprintf(stderr,"Search for response document node\n");
1371#endif
1372    xmlXPathFreeObject(tmpsptr);
1373    //xmlFree(tmps);
1374    tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='ResponseDocument']");
1375    tmps=tmpsptr->nodesetval;
1376#ifdef DEBUG
1377    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1378#endif
1379    for(int k=0;k<tmps->nodeNr;k++){
1380      addToMap(request_inputs,"ResponseDocument","");
1381      request_output_real_format;
1382      maps *tmpmaps=NULL;
1383      xmlNodePtr cur=tmps->nodeTab[k];
1384      if(cur->type == XML_ELEMENT_NODE) {
1385        /**
1386         * A specific responseDocument node.
1387         */
1388        if(tmpmaps==NULL){
1389          tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1390          if(tmpmaps == NULL){
1391            return errorException(m, _("Unable to allocate memory."), "InternalError");
1392          }
1393          tmpmaps->name="unknownIdentifier";
1394          tmpmaps->next=NULL;
1395        }
1396        /**
1397         * Get every attribute from a LiteralData node
1398         * storeExecuteResponse, lineage, status
1399         */
1400        char *ress[3];
1401        ress[0]="storeExecuteResponse";
1402        ress[1]="lineage";
1403        ress[2]="status";
1404        xmlChar *val;
1405        for(int l=0;l<3;l++){
1406#ifdef DEBUG
1407          fprintf(stderr,"*** %s ***\t",ress[l]);
1408#endif
1409          val=xmlGetProp(cur,BAD_CAST ress[l]);
1410          if(val!=NULL && strlen((char*)val)>0){
1411            if(tmpmaps->content!=NULL)
1412              addToMap(tmpmaps->content,ress[l],(char*)val);
1413            else
1414              tmpmaps->content=createMap(ress[l],(char*)val);
1415            addToMap(request_inputs,ress[l],(char*)val);
1416          }
1417#ifdef DEBUG
1418          fprintf(stderr,"%s\n",val);
1419#endif
1420          xmlFree(val);
1421        }
1422        xmlNodePtr cur1=cur->children;
1423        while(cur1){
1424          if(xmlStrncasecmp(cur1->name,BAD_CAST "Output",xmlStrlen(cur1->name))==0){
1425            /**
1426             * Get every attribute from a Output node
1427             * mimeType, encoding, schema, uom, asReference
1428             */
1429            char *outs[5];
1430            outs[0]="mimeType";
1431            outs[1]="encoding";
1432            outs[2]="schema";
1433            outs[3]="uom";
1434            outs[4]="asReference";
1435            for(int l=0;l<5;l++){
1436#ifdef DEBUG
1437              fprintf(stderr,"*** %s ***\t",outs[l]);
1438#endif
1439              val=xmlGetProp(cur1,BAD_CAST outs[l]);
1440              if(val!=NULL && strlen((char*)val)>0){
1441                if(tmpmaps->content!=NULL)
1442                  addToMap(tmpmaps->content,outs[l],(char*)val);
1443                else
1444                  tmpmaps->content=createMap(outs[l],(char*)val);
1445              }
1446#ifdef DEBUG
1447              fprintf(stderr,"%s\n",val);
1448#endif
1449              xmlFree(val);
1450            }
1451           
1452            xmlNodePtr cur2=cur1->children;
1453            while(cur2){
1454              /**
1455               * Indentifier
1456               */
1457              if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1458                xmlChar *val=
1459                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1460                if(tmpmaps==NULL){
1461                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1462                  if(tmpmaps == NULL){
1463                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1464                  }
1465                  tmpmaps->name=strdup((char*)val);
1466                  tmpmaps->content=NULL;
1467                  tmpmaps->next=NULL;
1468                }
1469                else
1470                  tmpmaps->name=strdup((char*)val);;
1471                xmlFree(val);
1472              }
1473              /**
1474               * Title, Asbtract
1475               */
1476              if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1477                 xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1478                xmlChar *val=
1479                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1480                if(tmpmaps==NULL){
1481                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1482                  if(tmpmaps == NULL){
1483                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1484                  }
1485                  tmpmaps->name="missingIndetifier";
1486                  tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1487                  tmpmaps->next=NULL;
1488                }
1489                else{
1490                  if(tmpmaps->content!=NULL)
1491                    addToMap(tmpmaps->content,
1492                             (char*)cur2->name,(char*)val);
1493                  else
1494                    tmpmaps->content=
1495                      createMap((char*)cur2->name,(char*)val);
1496                }
1497                xmlFree(val);
1498              }
1499              cur2=cur2->next;
1500            }
1501          }
1502          cur1=cur1->next;
1503        }
1504      }
1505      //xmlFree(cur);
1506      if(request_output_real_format==NULL)
1507        request_output_real_format=tmpmaps;
1508      else
1509        addMapsToMaps(&request_output_real_format,tmpmaps);
1510#ifdef DEBUG
1511      dumpMaps(tmpmaps);
1512#endif
1513    }
1514    xmlXPathFreeObject(tmpsptr);
1515    //xmlFree(tmps);
1516    tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='RawDataOutput']");
1517    tmps=tmpsptr->nodesetval;
1518#ifdef DEBUG
1519    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1520#endif
1521    for(int k=0;k<tmps->nodeNr;k++){
1522      addToMap(request_inputs,"RawDataOutput","");
1523      xmlNodePtr cur1=tmps->nodeTab[k];
1524      xmlChar *val;
1525      /**
1526       * Get every attribute from a Output node
1527       * mimeType, encoding, schema, uom, asReference
1528       */
1529      char *outs[4];
1530      outs[0]="mimeType";
1531      outs[1]="encoding";
1532      outs[2]="schema";
1533      outs[3]="uom";
1534      for(int l=0;l<4;l++){
1535#ifdef DEBUG
1536        fprintf(stderr,"*** %s ***\t",outs[l]);
1537#endif
1538        val=xmlGetProp(cur1,BAD_CAST outs[l]);
1539        if(val!=NULL && strlen((char*)val)>0){
1540          if(tmpmaps==NULL){
1541            tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1542            if(tmpmaps == NULL){
1543              return errorException(m, _("Unable to allocate memory."), "InternalError");
1544            }
1545            tmpmaps->name="unknownIdentifier";
1546            tmpmaps->content=createMap(outs[l],(char*)val);
1547            tmpmaps->next=NULL;
1548          }
1549          else
1550            addToMap(tmpmaps->content,outs[l],(char*)val);
1551        }
1552#ifdef DEBUG
1553        fprintf(stderr,"%s\n",val);
1554#endif
1555        xmlFree(val);
1556      }
1557           
1558      xmlNodePtr cur2=cur1->children;
1559      while(cur2){
1560        /**
1561         * Indentifier
1562         */
1563        if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1564          val=
1565            xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1566          if(tmpmaps==NULL){
1567            tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1568            if(tmpmaps == NULL){
1569              return errorException(m, _("Unable to allocate memory."), "InternalError");
1570            }
1571            tmpmaps->name=strdup((char*)val);
1572            tmpmaps->content=NULL;
1573            tmpmaps->next=NULL;
1574          }
1575          else
1576            tmpmaps->name=strdup((char*)val);;
1577          xmlFree(val);
1578        }
1579        cur2=cur2->next;
1580      }
1581      if(request_output_real_format==NULL)
1582        request_output_real_format=tmpmaps;
1583      else
1584        addMapsToMaps(&request_output_real_format,tmpmaps);
1585#ifdef DEBUG
1586      dumpMaps(tmpmaps);
1587#endif
1588    }
1589    xmlXPathFreeObject(tmpsptr);
1590    //xmlFree(tmps);
1591    xmlCleanupParser();
1592  }
1593
1594  //if(CHECK_INET_HANDLE(hInternet))
1595  InternetCloseHandle(hInternet);
1596
1597#ifdef DEBUG
1598  fprintf(stderr,"\n%i\n",i);
1599  dumpMaps(request_input_real_format);
1600  dumpMaps(request_output_real_format);
1601  dumpMap(request_inputs);
1602#endif
1603
1604  /**
1605   * Ensure that each requested arguments are present in the request
1606   * DataInputs and ResponseDocument / RawDataOutput
1607   */ 
1608  char *dfv=addDefaultValues(&request_input_real_format,s1->inputs,m,"inputs");
1609  if(strcmp(dfv,"")!=0){
1610    char tmps[1024];
1611    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);
1612    map* tmpe=createMap("text",tmps);
1613    addToMap(tmpe,"code","MissingParameterValue");
1614    printExceptionReportResponse(m,tmpe);
1615    freeMap(&tmpe);
1616    free(tmpe);
1617    freeMaps(&m);
1618    free(m);
1619    free(REQUEST);
1620    freeMaps(&request_input_real_format);
1621    free(request_input_real_format);
1622    freeMaps(&request_output_real_format);
1623    free(request_output_real_format);
1624    freeMaps(&tmpmaps);
1625    free(tmpmaps);
1626    return 1;
1627  }
1628  addDefaultValues(&request_output_real_format,s1->outputs,m,"outputs");
1629
1630#ifdef DEBUG
1631  fprintf(stderr,"REQUEST_INPUTS\n");
1632  dumpMaps(request_input_real_format);
1633  fprintf(stderr,"REQUEST_OUTPUTS\n");
1634  dumpMaps(request_output_real_format);
1635#endif
1636
1637  maps* curs=getMaps(m,"env");
1638  if(curs!=NULL){
1639    map* mapcs=curs->content;
1640    while(mapcs!=NULLMAP){
1641#ifndef WIN32
1642      setenv(mapcs->name,mapcs->value,1);
1643#else
1644#ifdef DEBUG
1645      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1646#endif
1647      if(mapcs->value[strlen(mapcs->value)-2]=='\r'){
1648#ifdef DEBUG
1649        fprintf(stderr,"[ZOO: Env var finish with \r]\n");
1650#endif
1651        mapcs->value[strlen(mapcs->value)-1]=0;
1652      }
1653#ifdef DEBUG
1654      fflush(stderr);
1655      fprintf(stderr,"setting variable... %s\n",
1656#endif
1657              SetEnvironmentVariable(mapcs->name,mapcs->value)
1658#ifdef DEBUG
1659              ? "OK" : "FAILED");
1660#else
1661      ;
1662#endif
1663#ifdef DEBUG
1664      fflush(stderr);
1665#endif
1666#endif
1667#ifdef DEBUG
1668      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1669      fflush(stderr);
1670#endif
1671      mapcs=mapcs->next;
1672    }
1673  }
1674 
1675#ifdef DEBUG
1676  dumpMap(request_inputs);
1677#endif
1678
1679  /**
1680   * Need to check if we need to fork to load a status enabled
1681   */
1682  r_inputs=NULL;
1683  r_inputs=getMap(request_inputs,"storeExecuteResponse");
1684  int eres=SERVICE_STARTED;
1685  int cpid=getpid();
1686 
1687  maps *_tmpMaps=(maps*)malloc(MAPS_SIZE);
1688  _tmpMaps->name=strdup("lenv");
1689  char tmpBuff[100];
1690  sprintf(tmpBuff,"%i",cpid);
1691  _tmpMaps->content=createMap("sid",tmpBuff);
1692  _tmpMaps->next=NULL;
1693  addToMap(_tmpMaps->content,"status","0");
1694  addMapsToMaps(&m,_tmpMaps);
1695  freeMaps(&_tmpMaps);
1696  free(_tmpMaps);
1697
1698#ifdef DEBUG
1699  dumpMap(request_inputs);
1700#endif
1701
1702  if(r_inputs!=NULL)
1703    if(strcasecmp(r_inputs->value,"false")==0)
1704      r_inputs=NULL;
1705  if(r_inputs==NULLMAP){
1706    loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1707  }
1708  else{
1709    pid_t   pid;
1710#ifdef DEBUG
1711    fprintf(stderr,"\nPID : %d\n",cpid);
1712#endif
1713
1714#ifndef WIN32
1715    pid = fork ();
1716#else
1717    pid = 0;
1718#endif
1719    if (pid > 0) {
1720      /**
1721       * dady :
1722       * set status to SERVICE_ACCEPTED
1723       */
1724#ifdef DEBUG
1725      fprintf(stderr,"father pid continue (origin %d) %d ...\n",cpid,getpid());
1726#endif
1727      eres=SERVICE_ACCEPTED;
1728    }else if (pid == 0) {
1729      /**
1730       * son : have to close the stdout, stdin and stderr to let the parent
1731       * process answer to http client.
1732       */
1733      r_inputs=getMapFromMaps(m,"main","tmpPath");
1734      map* r_inputs1=getMap(s1->content,"ServiceProvider");
1735      char* fbkp=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1736      sprintf(fbkp,"%s/%s_%d.xml",r_inputs->value,r_inputs1->value,cpid);
1737      char* flog=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1738      sprintf(flog,"%s/%s_%d_error.log",r_inputs->value,r_inputs1->value,cpid);
1739#ifdef DEBUG
1740      fprintf(stderr,"RUN IN BACKGROUND MODE \n");
1741      fprintf(stderr,"son pid continue (origin %d) %d ...\n",cpid,getpid());
1742      fprintf(stderr,"\nFILE TO STORE DATA %s\n",r_inputs->value);
1743#endif
1744      freopen(fbkp , "w+", stdout);
1745      fclose(stdin);
1746      freopen(flog,"w+",stderr);
1747      free(fbkp);
1748      free(flog);
1749      /**
1750       * set status to SERVICE_STARTED and flush stdout to ensure full
1751       * content was outputed (the file used to store the ResponseDocument).
1752       * The rewind stdout to restart writing from the bgining of the file,
1753       * this way the data will be updated at the end of the process run.
1754       */
1755      updateStatus(m);
1756      printProcessResponse(m,request_inputs,cpid,
1757                            s1,r_inputs1->value,SERVICE_STARTED,
1758                            request_input_real_format,
1759                            request_output_real_format);
1760      fflush(stdout);
1761      rewind(stdout);
1762
1763      loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1764
1765    } else {
1766      /**
1767       * error server don't accept the process need to output a valid
1768       * error response here !!!
1769       */
1770      eres=-1;
1771      errorException(m, _("Unable to run the child process properly"), "InternalError");
1772    }
1773       
1774  }
1775
1776#ifdef DEBUG
1777  dumpMaps(request_output_real_format);
1778  fprintf(stderr,"Function loaded and returned %d\n",eres);
1779  fflush(stderr);
1780#endif
1781  if(eres!=-1)
1782    outputResponse(s1,request_input_real_format,
1783                   request_output_real_format,request_inputs,
1784                   cpid,m,eres);
1785
1786  if(((int)getpid())!=cpid){
1787    fclose(stdout);
1788    fclose(stderr);
1789    unhandleStatus(m);
1790  }
1791
1792  freeService(&s1);
1793  free(s1);
1794  //For Python language support only
1795  //freeMaps(&m);
1796  free(m);
1797 
1798  freeMaps(&request_input_real_format);
1799  free(request_input_real_format);
1800 
1801  /* The following is requested but get issue using with Python support :/ */
1802  /* freeMaps(&request_output_real_format);
1803     free(request_output_real_format);*/
1804 
1805  free(REQUEST);
1806  free(SERVICE_URL);
1807#ifdef DEBUG
1808  fprintf(stderr,"Processed response \n");
1809  fflush(stdout);
1810  fflush(stderr);
1811#endif
1812
1813  return 0;
1814}
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