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

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

Small fix in the XML parser.

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