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

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

Small fix to check if only one parameter was passed and if the first one is XML string (starting by the '<' character).

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