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

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

Small code cleanup, add loadRemoteFile in service_internal to download file and take care of cached files.

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