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

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

Fix FastCGI support. Small fix to store mapon disk and manage session. Fix typo in main.cfg.

File size: 54.9 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
496  /**
497   * Check for minimum inputs
498   */
499  r_inputs=getMap(request_inputs,"Request");
500  if(request_inputs==NULL || r_inputs==NULL){ 
501    errorException(m, _("Parameter <request> was not specified"),"MissingParameterValue");
502    freeMaps(&m);
503    free(m);
504    freeMap(&request_inputs);
505    free(request_inputs);
506    free(REQUEST);
507    return 1;
508  }
509  else{
510    REQUEST=strdup(r_inputs->value);
511    if(strncasecmp(r_inputs->value,"GetCapabilities",15)!=0
512       && strncasecmp(r_inputs->value,"DescribeProcess",15)!=0
513       && strncasecmp(r_inputs->value,"Execute",7)!=0){ 
514      errorException(m, _("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), "InvalidParameterValue");
515      freeMaps(&m);
516      free(m);
517      free(REQUEST);
518      return 1;
519    }
520  }
521  r_inputs=NULL;
522  r_inputs=getMap(request_inputs,"Service");
523  if(r_inputs==NULLMAP){
524    errorException(m, _("Parameter <service> was not specified"),"MissingParameterValue");
525    freeMaps(&m);
526    free(m);
527    free(REQUEST);
528    return 1;
529  }
530  if(strncasecmp(REQUEST,"GetCapabilities",15)!=0){
531    r_inputs=getMap(request_inputs,"Version");
532    if(r_inputs==NULL){ 
533      errorException(m, _("Parameter <version> was not specified"),"MissingParameterValue");
534      freeMaps(&m);
535      free(m);
536      free(REQUEST);
537      return 1;
538    }
539  }
540
541  r_inputs=getMap(request_inputs,"serviceprovider");
542  if(r_inputs==NULL){
543    addToMap(request_inputs,"serviceprovider","");
544  }
545
546  maps* request_output_real_format=NULL;
547  map* tmpm=getMapFromMaps(m,"main","serverAddress");
548  if(tmpm!=NULL)
549    SERVICE_URL=strdup(tmpm->value);
550  else
551    SERVICE_URL=strdup(DEFAULT_SERVICE_URL);
552
553  service* s1;
554  int scount=0;
555
556#ifdef DEBUG
557  dumpMap(r_inputs);
558#endif
559  char conf_dir[1024];
560  int t;
561  char tmps1[1024];
562
563  r_inputs=NULL;
564  r_inputs=getMap(request_inputs,"metapath");
565  if(r_inputs!=NULL)
566    snprintf(conf_dir,1024,"%s/%s",ntmp,r_inputs->value);
567  else
568    snprintf(conf_dir,1024,"%s",ntmp);
569
570  if(strncasecmp(REQUEST,"GetCapabilities",15)==0){
571    struct dirent *dp;
572#ifdef DEBUG
573    dumpMap(r_inputs);
574#endif
575    DIR *dirp = opendir(conf_dir);
576    if(dirp==NULL){
577      return errorException(m, _("The specified path doesn't exist."),"InvalidParameterValue");
578    }
579    xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
580    r_inputs=NULL;
581    r_inputs=getMap(request_inputs,"ServiceProvider");
582    xmlNodePtr n;
583    if(r_inputs!=NULL)
584      n = printGetCapabilitiesHeader(doc,r_inputs->value,m);
585    else
586      n = printGetCapabilitiesHeader(doc,"",m);
587    /**
588     * Here we need to close stdout to ensure that not supported chars
589     * has been found in the zcfg and then printed on stdout
590     */
591    int saved_stdout = dup(fileno(stdout));
592    dup2(fileno(stderr),fileno(stdout));
593    while ((dp = readdir(dirp)) != NULL)
594      if(strstr(dp->d_name,".zcfg")!=0){
595        memset(tmps1,0,1024);
596        snprintf(tmps1,1024,"%s/%s",conf_dir,dp->d_name);
597        s1=(service*)calloc(1,SERVICE_SIZE);
598        if(s1 == NULL){ 
599          return errorException(m, _("Unable to allocate memory."),"InternalError");
600        }
601#ifdef DEBUG
602        fprintf(stderr,"#################\n%s\n#################\n",tmps1);
603#endif
604        t=getServiceFromFile(tmps1,&s1);
605#ifdef DEBUG
606        dumpService(s1);
607        fflush(stdout);
608        fflush(stderr);
609#endif
610        printGetCapabilitiesForProcess(m,n,s1);
611        freeService(&s1);
612        free(s1);
613        scount++;
614      }
615    (void)closedir(dirp);
616    fflush(stdout);
617    dup2(saved_stdout,fileno(stdout));
618    printDocument(m,doc,getpid());
619    freeMaps(&m);
620    free(m);
621    free(REQUEST);
622    free(SERVICE_URL);
623    fflush(stdout);
624    return 0;
625  }
626  else{
627    r_inputs=getMap(request_inputs,"Identifier");
628    if(r_inputs==NULL 
629       || strlen(r_inputs->name)==0 || strlen(r_inputs->value)==0){ 
630      errorException(m, _("Mandatory <identifier> was not specified"),"MissingParameterValue");
631      freeMaps(&m);
632      free(m);
633      free(REQUEST);
634      free(SERVICE_URL);
635      return 0;
636    }
637
638    struct dirent *dp;
639    DIR *dirp = opendir(conf_dir);
640    if(dirp==NULL){
641      errorException(m, _("The specified path path doesn't exist."),"InvalidParameterValue");
642      freeMaps(&m);
643      free(m);
644      free(REQUEST);
645      free(SERVICE_URL);
646      return 0;
647    }
648    if(strncasecmp(REQUEST,"DescribeProcess",15)==0){
649      /**
650       * Loop over Identifier list
651       */
652      xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
653      r_inputs=NULL;
654      r_inputs=getMap(request_inputs,"ServiceProvider");
655
656      xmlNodePtr n;
657      if(r_inputs!=NULL)
658        n = printDescribeProcessHeader(doc,r_inputs->value,m);
659      else
660        n = printDescribeProcessHeader(doc,"",m);
661
662      r_inputs=getMap(request_inputs,"Identifier");
663      char *tmps=strtok(r_inputs->value,",");
664     
665      char buff[256];
666      char buff1[1024];
667      int saved_stdout = dup(fileno(stdout));
668      dup2(fileno(stderr),fileno(stdout));
669      while(tmps){
670        memset(buff,0,256);
671        snprintf(buff,256,"%s.zcfg",tmps);
672        memset(buff1,0,1024);
673#ifdef DEBUG
674        fprintf(stderr,"\n#######%s\n########\n",buff1);
675#endif
676        while ((dp = readdir(dirp)) != NULL)
677          if(strcmp(dp->d_name,buff)==0){
678            memset(buff1,0,1024);
679            snprintf(buff1,1024,"%s/%s",conf_dir,dp->d_name);
680            s1=(service*)calloc(1,SERVICE_SIZE);
681            if(s1 == NULL){
682              return errorException(m, _("Unable to allocate memory."),"InternalError");
683            }
684#ifdef DEBUG
685            fprintf(stderr,"#################\n%s\n#################\n",buff1);
686#endif
687            t=getServiceFromFile(buff1,&s1);
688#ifdef DEBUG
689            dumpService(s1);
690#endif
691            printDescribeProcessForProcess(m,n,s1,1);
692            freeService(&s1);
693            free(s1);
694            scount++;
695          }
696        rewinddir(dirp);
697        tmps=strtok(NULL,",");
698      }
699      closedir(dirp);
700      fflush(stdout);
701      dup2(saved_stdout,fileno(stdout));
702      printDocument(m,doc,getpid());
703      freeMaps(&m);
704      free(m);
705      free(REQUEST);
706      free(SERVICE_URL);
707      fflush(stdout);
708#ifndef LINUX_FREE_ISSUE
709      if(s1)
710        free(s1);
711#endif
712      return 0;
713    }
714    else
715      if(strncasecmp(REQUEST,"Execute",strlen(REQUEST))!=0){
716        errorException(m, _("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), "InvalidParameterValue");
717#ifdef DEBUG
718        fprintf(stderr,"No request found %s",REQUEST);
719#endif 
720        closedir(dirp);
721        return 0;
722      }
723    closedir(dirp);
724  }
725 
726  s1=NULL;
727  s1=(service*)calloc(1,SERVICE_SIZE);
728  if(s1 == NULL){
729    freeMaps(&m);
730    free(m);
731    free(REQUEST);
732    free(SERVICE_URL);
733    return errorException(m, _("Unable to allocate memory."),"InternalError");
734  }
735  r_inputs=getMap(request_inputs,"MetaPath");
736  if(r_inputs!=NULL)
737    snprintf(tmps1,1024,"%s/%s",ntmp,r_inputs->value);
738  else
739    snprintf(tmps1,1024,"%s/",ntmp);
740  r_inputs=getMap(request_inputs,"Identifier");
741  char *ttmp=strdup(tmps1);
742  snprintf(tmps1,1024,"%s/%s.zcfg",ttmp,r_inputs->value);
743  free(ttmp);
744#ifdef DEBUG
745  fprintf(stderr,"Trying to load %s\n", tmps1);
746#endif
747  int saved_stdout = dup(fileno(stdout));
748    dup2(fileno(stderr),fileno(stdout));
749  t=getServiceFromFile(tmps1,&s1);
750  fflush(stdout);
751  dup2(saved_stdout,fileno(stdout));
752  if(t<0){
753    char *tmpMsg=(char*)malloc(2048+strlen(r_inputs->value));
754   
755    sprintf(tmpMsg,_("The value for <indetifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."),r_inputs->value);
756    errorException(m, tmpMsg, "InvalidParameterValue");
757    free(tmpMsg);
758    freeService(&s1);
759    free(s1);
760    freeMaps(&m);
761    free(m);
762    free(REQUEST);
763    free(SERVICE_URL);
764    return 0;
765  }
766  //close(saved_stdout);
767
768#ifdef DEBUG
769  dumpService(s1);
770#endif
771  int j;
772 
773  /**
774   * Create the input maps data structure
775   */
776  int i=0;
777  HINTERNET hInternet;
778  HINTERNET res;
779  hInternet=InternetOpen(
780#ifndef WIN32
781                         (LPCTSTR)
782#endif
783                         "ZooWPSClient\0",
784                         INTERNET_OPEN_TYPE_PRECONFIG,
785                         NULL,NULL, 0);
786
787#ifndef WIN32
788  if(!CHECK_INET_HANDLE(hInternet))
789    fprintf(stderr,"WARNING : hInternet handle failed to initialize");
790#endif
791  maps* request_input_real_format=NULL;
792  maps* tmpmaps = request_input_real_format;
793  map* postRequest=NULL;
794  postRequest=getMap(request_inputs,"xrequest");
795  if(postRequest==NULLMAP){
796    /**
797     * Parsing outputs provided as KVP
798     */
799    r_inputs=NULL;
800#ifdef DEBUG
801    fprintf(stderr,"OUTPUT Parsing ... \n");
802#endif
803    r_inputs=getMap(request_inputs,"ResponseDocument"); 
804    if(r_inputs==NULL) r_inputs=getMap(request_inputs,"RawDataOutput");
805   
806#ifdef DEBUG
807    fprintf(stderr,"OUTPUT Parsing ... \n");
808#endif
809    if(r_inputs!=NULL){
810#ifdef DEBUG
811      fprintf(stderr,"OUTPUT Parsing start now ... \n");
812#endif
813      char cursor_output[10240];
814      char *cotmp=strdup(r_inputs->value);
815      snprintf(cursor_output,10240,"%s",cotmp);
816      free(cotmp);
817      j=0;
818       
819      /**
820       * Put each Output into the outputs_as_text array
821       */
822      char * pToken;
823      maps* tmp_output=NULL;
824#ifdef DEBUG
825      fprintf(stderr,"OUTPUT [%s]\n",cursor_output);
826#endif
827      pToken=strtok(cursor_output,";");
828      char** outputs_as_text=(char**)calloc(128,sizeof(char*));
829      if(outputs_as_text == NULL) {
830        return errorException(m, _("Unable to allocate memory"), "InternalError");
831      }
832      i=0;
833      while(pToken!=NULL){
834#ifdef DEBUG
835        fprintf(stderr,"***%s***\n",pToken);
836        fflush(stderr);
837        fprintf(stderr,"***%s***\n",pToken);
838#endif
839        outputs_as_text[i]=(char*)calloc(strlen(pToken)+1,sizeof(char));
840        if(outputs_as_text[i] == NULL) {
841          return errorException(m, _("Unable to allocate memory"), "InternalError");
842        }
843        snprintf(outputs_as_text[i],strlen(pToken)+1,"%s",pToken);
844        pToken = strtok(NULL,";");
845        i++;
846      }
847      for(j=0;j<i;j++){
848        char *tmp=strdup(outputs_as_text[j]);
849        free(outputs_as_text[j]);
850        char *tmpc;
851        tmpc=strtok(tmp,"@");
852        int k=0;
853        while(tmpc!=NULL){
854          if(k==0){
855            if(tmp_output==NULL){
856              tmp_output=(maps*)calloc(1,MAPS_SIZE);
857              if(tmp_output == NULL){
858                return errorException(m, _("Unable to allocate memory."), "InternalError");
859              }
860              tmp_output->name=strdup(tmpc);
861              tmp_output->content=NULL;
862              tmp_output->next=NULL;
863            }
864          }
865          else{
866            char *tmpv=strstr(tmpc,"=");
867            char tmpn[256];
868            memset(tmpn,0,256);
869            strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
870            tmpn[strlen(tmpc)-strlen(tmpv)]=0;
871#ifdef DEBUG
872            fprintf(stderr,"OUTPUT DEF [%s]=[%s]\n",tmpn,tmpv+1);
873#endif
874            if(tmp_output->content==NULL){
875              tmp_output->content=createMap(tmpn,tmpv+1);
876              tmp_output->content->next=NULL;
877            }
878            else
879              addToMap(tmp_output->content,tmpn,tmpv+1);
880          }
881          k++;
882#ifdef DEBUG
883          fprintf(stderr,"***%s***\n",tmpc);
884#endif
885          tmpc=strtok(NULL,"@");
886        }
887        if(request_output_real_format==NULL)
888          request_output_real_format=dupMaps(&tmp_output);
889        else
890          addMapsToMaps(&request_output_real_format,tmp_output);
891        freeMaps(&tmp_output);
892        free(tmp_output);
893        tmp_output=NULL;
894#ifdef DEBUG
895        dumpMaps(tmp_output);
896        fflush(stderr);
897#endif
898        free(tmp);
899      }
900      free(outputs_as_text);
901    }
902
903
904    /**
905     * Parsing inputs provided as KVP
906     */
907    r_inputs=getMap(request_inputs,"DataInputs");
908#ifdef DEBUG
909    fprintf(stderr,"DATA INPUTS [%s]\n",r_inputs->value);
910#endif
911    char cursor_input[40960];
912    if(r_inputs!=NULL)
913      snprintf(cursor_input,40960,"%s",r_inputs->value);
914    else{
915      errorException(m, _("Parameter <DataInputs> was not specified"),"MissingParameterValue");
916      freeMaps(&m);
917      free(m);
918      free(REQUEST);
919      free(SERVICE_URL);
920      InternetCloseHandle(hInternet);
921      freeService(&s1);
922      free(s1);
923      return 0;
924    }
925    j=0;
926 
927    /**
928     * Put each DataInputs into the inputs_as_text array
929     */
930    char * pToken;
931    pToken=strtok(cursor_input,";");
932    char** inputs_as_text=(char**)calloc(100,sizeof(char*));
933    if(inputs_as_text == NULL){
934      return errorException(m, _("Unable to allocate memory."), "InternalError");
935    }
936    i=0;
937    while(pToken!=NULL){
938#ifdef DEBUG
939      fprintf(stderr,"***%s***\n",pToken);
940#endif
941      fflush(stderr);
942#ifdef DEBUG
943      fprintf(stderr,"***%s***\n",pToken);
944#endif
945      inputs_as_text[i]=(char*)calloc(strlen(pToken)+1,sizeof(char));
946      snprintf(inputs_as_text[i],strlen(pToken)+1,"%s",pToken);
947      if(inputs_as_text[i] == NULL){
948        return errorException(m, _("Unable to allocate memory."), "InternalError");
949      }
950      pToken = strtok(NULL,";");
951      i++;
952    }
953
954    for(j=0;j<i;j++){
955      char *tmp=strdup(inputs_as_text[j]);
956      free(inputs_as_text[j]);
957      char *tmpc;
958      tmpc=strtok(tmp,"@");
959      while(tmpc!=NULL){
960#ifdef DEBUG
961        fprintf(stderr,"***\n***%s***\n",tmpc);
962#endif
963        char *tmpv=strstr(tmpc,"=");
964        char tmpn[256];
965        memset(tmpn,0,256);
966        if(tmpv!=NULL){
967          strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
968          tmpn[strlen(tmpc)-strlen(tmpv)]=0;
969        }
970        else{
971          strncpy(tmpn,tmpc,strlen(tmpc)*sizeof(char));
972          tmpn[strlen(tmpc)]=0;
973        }
974#ifdef DEBUG
975        fprintf(stderr,"***\n*** %s = %s ***\n",tmpn,tmpv+1);
976#endif
977        if(tmpmaps==NULL){
978          tmpmaps=(maps*)calloc(1,MAPS_SIZE);
979          if(tmpmaps == NULL){
980            return errorException(m, _("Unable to allocate memory."), "InternalError");
981          }
982          tmpmaps->name=strdup(tmpn);
983          if(tmpv!=NULL)
984            tmpmaps->content=createMap("value",tmpv+1);
985          else
986            tmpmaps->content=createMap("value","Reference");
987          tmpmaps->next=NULL;
988        }
989        tmpc=strtok(NULL,"@");
990        while(tmpc!=NULL){
991#ifdef DEBUG
992          fprintf(stderr,"*** KVP NON URL-ENCODED \n***%s***\n",tmpc);
993#endif
994          char *tmpv1=strstr(tmpc,"=");
995#ifdef DEBUG
996          fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1);
997#endif
998          char tmpn1[1024];
999          memset(tmpn1,0,1024);
1000          if(tmpv1!=NULL){
1001            strncpy(tmpn1,tmpc,strlen(tmpc)-strlen(tmpv1));
1002            tmpn1[strlen(tmpc)-strlen(tmpv1)]=0;
1003            addToMap(tmpmaps->content,tmpn1,tmpv1+1);
1004          }
1005          else{
1006            strncpy(tmpn1,tmpc,strlen(tmpc));
1007            tmpn1[strlen(tmpc)]=0;
1008            map* lmap=getLastMap(tmpmaps->content);
1009            char *tmpValue=(char*)calloc((strlen(lmap->value)+strlen(tmpc)+1),sizeof(char));
1010            sprintf(tmpValue,"%s@%s",lmap->value,tmpc);
1011            free(lmap->value);
1012            lmap->value=strdup(tmpValue);
1013            free(tmpValue);
1014            dumpMap(tmpmaps->content);
1015            tmpc=strtok(NULL,"@");
1016            continue;
1017          }
1018#ifdef DEBUG
1019          fprintf(stderr,"*** NAME NON URL-ENCODED \n***%s***\n",tmpn1);
1020          fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1);
1021#endif
1022          if(strcmp(tmpn1,"xlink:href")!=0)
1023            addToMap(tmpmaps->content,tmpn1,tmpv1+1);
1024          else
1025            if(tmpv1!=NULL){
1026              if(strncasecmp(tmpv1+1,"http://",7)!=0 &&
1027                 strncasecmp(tmpv1+1,"ftp://",6)!=0){
1028                char emsg[1024];
1029                sprintf(emsg,_("Unable to find a valid protocol to download the remote file %s"),tmpv1+1);
1030                errorException(m,emsg,"InternalError");
1031                freeMaps(&m);
1032                free(m);
1033                free(REQUEST);
1034                free(SERVICE_URL);
1035                InternetCloseHandle(hInternet);
1036                freeService(&s1);
1037                free(s1);
1038                return 0;
1039              }
1040#ifdef DEBUG
1041              fprintf(stderr,"REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",tmpv1+1);
1042#endif
1043#ifndef WIN32
1044              if(CHECK_INET_HANDLE(hInternet))
1045#endif
1046                {
1047                  res=InternetOpenUrl(hInternet,tmpv1+1,NULL,0,
1048                                      INTERNET_FLAG_NO_CACHE_WRITE,0);
1049#ifdef DEBUG
1050                  fprintf(stderr,"(%s) content-length : %d,,res.nDataAlloc %d \n",
1051                          tmpv1+1,res.nDataAlloc,res.nDataLen);
1052#endif
1053                  char* tmpContent=(char*)calloc((res.nDataLen+1),sizeof(char));
1054                  if(tmpContent == NULL){
1055                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1056                  }
1057                  size_t dwRead;
1058                  InternetReadFile(res, (LPVOID)tmpContent,res.nDataLen, &dwRead);
1059                  map* tmpMap=getMap(tmpmaps->content,"value");
1060                  if(tmpMap!=NULL){
1061                    free(tmpMap->value);
1062                    tmpMap->value=(char*)malloc((res.nDataLen+1)*sizeof(char));
1063                    memmove(tmpMap->value,tmpContent,(res.nDataLen)*sizeof(char));
1064                    tmpMap->value[res.nDataLen]=0;
1065                    if(strlen(tmpContent)!=res.nDataLen){
1066                      char tmp[256];
1067                      sprintf(tmp,"%d",res.nDataLen*sizeof(char));
1068                      addToMap(tmpmaps->content,"size",tmp);
1069                    }
1070                  }
1071                  free(tmpContent);
1072                }
1073              char *tmpx=url_encode(tmpv1+1);
1074              addToMap(tmpmaps->content,tmpn1,tmpx);
1075              free(tmpx);
1076              addToMap(tmpmaps->content,"Reference",tmpv1+1);
1077              dumpMap(tmpmaps->content);
1078            }
1079          tmpc=strtok(NULL,"@");
1080        }
1081#ifdef DEBUG
1082        dumpMaps(tmpmaps);
1083        fflush(stderr);
1084#endif
1085        if(request_input_real_format==NULL)
1086          request_input_real_format=dupMaps(&tmpmaps);
1087        else
1088          addMapsToMaps(&request_input_real_format,tmpmaps);
1089        freeMaps(&tmpmaps);
1090        free(tmpmaps);
1091        tmpmaps=NULL;
1092        free(tmp);
1093      }
1094    }
1095    free(inputs_as_text);
1096  }
1097  else {
1098    /**
1099     * Parse XML request
1100     */ 
1101    xmlInitParser();
1102#ifdef DEBUG
1103    fflush(stderr);
1104    fprintf(stderr,"BEFORE %s\n",postRequest->value);
1105    fflush(stderr);
1106#endif
1107    xmlDocPtr doc =
1108      xmlParseMemory(postRequest->value,cgiContentLength);
1109#ifdef DEBUG
1110    fprintf(stderr,"AFTER\n");
1111    fflush(stderr);
1112#endif
1113    /**
1114     * Parse every Input in DataInputs node.
1115     */
1116    xmlXPathObjectPtr tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='Input']");
1117    xmlNodeSet* tmps=tmpsptr->nodesetval;
1118#ifdef DEBUG
1119    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1120#endif
1121    for(int k=0;k<tmps->nodeNr;k++){
1122      maps *tmpmaps=NULL;
1123      xmlNodePtr cur=tmps->nodeTab[k];
1124      if(tmps->nodeTab[k]->type == XML_ELEMENT_NODE) {
1125        /**
1126         * A specific Input node.
1127         */
1128#ifdef DEBUG
1129        fprintf(stderr, "= element 0 node \"%s\"\n", cur->name);
1130#endif
1131        xmlNodePtr cur2=cur->children;
1132        while(cur2!=NULL){
1133          while(cur2!=NULL && cur2->type!=XML_ELEMENT_NODE)
1134            cur2=cur2->next;
1135          if(cur2==NULL)
1136            break;
1137          /**
1138           * Indentifier
1139           */
1140          if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1141            xmlChar *val= xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1142            if(tmpmaps==NULL){
1143              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1144              if(tmpmaps == NULL){
1145                return errorException(m, _("Unable to allocate memory."), "InternalError");
1146              }
1147              tmpmaps->name=strdup((char*)val);
1148              tmpmaps->content=NULL;
1149              tmpmaps->next=NULL;
1150            }
1151            xmlFree(val);
1152          }
1153          /**
1154           * Title, Asbtract
1155           */
1156          if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1157             xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1158            xmlChar *val=
1159              xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1160            if(tmpmaps==NULL){
1161              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1162              if(tmpmaps == NULL){
1163                return errorException(m, _("Unable to allocate memory."), "InternalError");
1164              }
1165              tmpmaps->name=strdup("missingIndetifier");
1166              tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1167              tmpmaps->next=NULL;
1168            }
1169            else{
1170              if(tmpmaps->content!=NULL)
1171                addToMap(tmpmaps->content,
1172                         (char*)cur2->name,(char*)val);
1173              else
1174                tmpmaps->content=
1175                  createMap((char*)cur2->name,(char*)val);
1176            }
1177#ifdef DEBUG
1178            dumpMaps(tmpmaps);
1179#endif
1180            xmlFree(val);
1181          }
1182          /**
1183           * InputDataFormChoice (Reference or Data ?)
1184           */
1185          if(xmlStrcasecmp(cur2->name,BAD_CAST "Reference")==0){
1186            /**
1187             * Get every attribute from a Reference node
1188             * mimeType, encoding, schema, href, method
1189             * Header and Body gesture should be added here
1190             */
1191#ifdef DEBUG
1192            fprintf(stderr,"REFERENCE\n");
1193#endif
1194            const char *refs[5];
1195            refs[0]="mimeType";
1196            refs[1]="encoding";
1197            refs[2]="schema";
1198            refs[3]="method";
1199            refs[4]="href";
1200            for(int l=0;l<5;l++){
1201#ifdef DEBUG
1202              fprintf(stderr,"*** %s ***",refs[l]);
1203#endif
1204              xmlChar *val=xmlGetProp(cur2,BAD_CAST refs[l]);
1205              if(val!=NULL && xmlStrlen(val)>0){
1206                if(tmpmaps->content!=NULL)
1207                  addToMap(tmpmaps->content,refs[l],(char*)val);
1208                else
1209                  tmpmaps->content=createMap(refs[l],(char*)val);
1210                map* ltmp=getMap(tmpmaps->content,"method");
1211                if(l==4){
1212                  if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0)
1213                     && CHECK_INET_HANDLE(hInternet)){
1214                    res=InternetOpenUrl(hInternet,(char*)val,NULL,0,
1215                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1216                    char* tmpContent=
1217                      (char*)calloc((res.nDataLen+1),sizeof(char));
1218                    if(tmpContent == NULL){
1219                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1220                    }
1221                    size_t dwRead;
1222                    InternetReadFile(res, (LPVOID)tmpContent,
1223                                     res.nDataLen, &dwRead);
1224                    tmpContent[res.nDataLen]=0;
1225                    addToMap(tmpmaps->content,"value",tmpContent);
1226                  }
1227                }
1228              }
1229#ifdef DEBUG
1230              fprintf(stderr,"%s\n",val);
1231#endif
1232              xmlFree(val);
1233            }
1234#ifdef POST_DEBUG
1235            fprintf(stderr,"Parse Header and Body from Reference \n");
1236#endif
1237            xmlNodePtr cur3=cur2->children;
1238            hInternet.header=NULL;
1239            while(cur3){
1240              while(cur3!=NULL && cur3->type!=XML_ELEMENT_NODE)
1241                cur2=cur3->next;
1242              if(xmlStrcasecmp(cur3->name,BAD_CAST "Header")==0 ){
1243                const char *ha[2];
1244                ha[0]="key";
1245                ha[1]="value";
1246                int hai;
1247                char *has;
1248                char *key;
1249                for(hai=0;hai<2;hai++){
1250                  xmlChar *val=xmlGetProp(cur3,BAD_CAST ha[hai]);
1251#ifdef POST_DEBUG
1252                  fprintf(stderr,"%s = %s\n",ha[hai],(char*)val);
1253#endif
1254                  if(hai==0){
1255                    key=(char*)calloc((1+strlen((char*)val)),sizeof(char));
1256                    snprintf(key,1+strlen((char*)val),"%s",(char*)val);
1257                  }else{
1258                    has=(char*)calloc((3+strlen((char*)val)+strlen(key)),sizeof(char));
1259                    if(has == NULL){
1260                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1261                    }
1262                    snprintf(has,(3+strlen((char*)val)+strlen(key)),"%s: %s",key,(char*)val);
1263#ifdef POST_DEBUG
1264                    fprintf(stderr,"%s\n",has);
1265#endif
1266                  }
1267                }
1268                hInternet.header=curl_slist_append(hInternet.header, has);
1269                free(has);
1270              }
1271              else{
1272#ifdef POST_DEBUG
1273                fprintf(stderr,"Try to fetch the body part of the request ...\n");
1274#endif
1275                if(xmlStrcasecmp(cur3->name,BAD_CAST "Body")==0 ){
1276#ifdef POST_DEBUG
1277                  fprintf(stderr,"Body part found !!!\n",(char*)cur3->content);
1278#endif
1279                  char *tmp=new char[cgiContentLength];
1280                  memset(tmp,0,cgiContentLength);
1281                  xmlNodePtr cur4=cur3->children;
1282                  while(cur4!=NULL){
1283                    while(cur4->type!=XML_ELEMENT_NODE)
1284                      cur4=cur4->next;
1285                    xmlDocPtr bdoc = xmlNewDoc(BAD_CAST "1.0");
1286                    bdoc->encoding = xmlCharStrdup ("UTF-8");
1287                    xmlDocSetRootElement(bdoc,cur4);
1288                    xmlChar* btmps;
1289                    int bsize;
1290                    xmlDocDumpMemory(bdoc,&btmps,&bsize);
1291#ifdef POST_DEBUG
1292                    fprintf(stderr,"Body part found !!! %s %s\n",tmp,(char*)btmps);
1293#endif
1294                    if(btmps!=NULL)
1295                      sprintf(tmp,"%s",(char*)btmps);
1296                    xmlFreeDoc(bdoc);
1297                    cur4=cur4->next;
1298                  }
1299                  map *btmp=getMap(tmpmaps->content,"href");
1300                  if(btmp!=NULL){
1301#ifdef POST_DEBUG
1302                    fprintf(stderr,"%s %s\n",btmp->value,tmp);
1303                    curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1304#endif
1305                    res=InternetOpenUrl(hInternet,btmp->value,tmp,strlen(tmp),
1306                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1307                    char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1308                    if(tmpContent == NULL){
1309                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1310                    }
1311                    size_t dwRead;
1312                    InternetReadFile(res, (LPVOID)tmpContent,
1313                                     res.nDataLen, &dwRead);
1314                    tmpContent[res.nDataLen]=0;
1315                    if(hInternet.header!=NULL)
1316                      curl_slist_free_all(hInternet.header);
1317                    addToMap(tmpmaps->content,"value",tmpContent);
1318#ifdef POST_DEBUG
1319                    fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1320#endif
1321                  }
1322                }
1323                else
1324                  if(xmlStrcasecmp(cur3->name,BAD_CAST "BodyReference")==0 ){
1325                    xmlChar *val=xmlGetProp(cur3,BAD_CAST "href");
1326                    HINTERNET bInternet,res1;
1327                    bInternet=InternetOpen(
1328#ifndef WIN32
1329                                           (LPCTSTR)
1330#endif
1331                                           "ZooWPSClient\0",
1332                                           INTERNET_OPEN_TYPE_PRECONFIG,
1333                                           NULL,NULL, 0);
1334                    if(!CHECK_INET_HANDLE(bInternet))
1335                      fprintf(stderr,"WARNING : hInternet handle failed to initialize");
1336#ifdef POST_DEBUG
1337                    curl_easy_setopt(bInternet.handle, CURLOPT_VERBOSE, 1);
1338#endif
1339                    res1=InternetOpenUrl(bInternet,(char*)val,NULL,0,
1340                                         INTERNET_FLAG_NO_CACHE_WRITE,0);
1341                    char* tmp=
1342                      (char*)calloc((res1.nDataLen+1),sizeof(char));
1343                    if(tmp == NULL){
1344                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1345                    }
1346                    size_t bRead;
1347                    InternetReadFile(res1, (LPVOID)tmp,
1348                                     res1.nDataLen, &bRead);
1349                    tmp[res1.nDataLen]=0;
1350                    InternetCloseHandle(bInternet);
1351                    map *btmp=getMap(tmpmaps->content,"href");
1352                    if(btmp!=NULL){
1353#ifdef POST_DEBUG
1354                      fprintf(stderr,"%s %s\n",btmp->value,tmp);
1355                      curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1356#endif
1357                      res=InternetOpenUrl(hInternet,btmp->value,tmp,
1358                                          strlen(tmp),
1359                                          INTERNET_FLAG_NO_CACHE_WRITE,0);
1360                      char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1361                      if(tmpContent == NULL){
1362                        return errorException(m, _("Unable to allocate memory."), "InternalError");
1363                      }
1364                      size_t dwRead;
1365                      InternetReadFile(res, (LPVOID)tmpContent,
1366                                       res.nDataLen, &dwRead);
1367                      tmpContent[res.nDataLen]=0;
1368                      if(hInternet.header!=NULL)
1369                        curl_slist_free_all(hInternet.header);
1370                      addToMap(tmpmaps->content,"value",tmpContent);
1371#ifdef POST_DEBUG
1372                      fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1373#endif
1374                    }
1375                  }
1376              }
1377              cur3=cur3->next;
1378            }
1379#ifdef POST_DEBUG
1380            fprintf(stderr,"Header and Body was parsed from Reference \n");
1381#endif
1382#ifdef DEBUG
1383            dumpMap(tmpmaps->content);
1384            fprintf(stderr, "= element 2 node \"%s\" = (%s)\n", 
1385                    cur2->name,cur2->content);
1386#endif
1387          }
1388          else if(xmlStrcasecmp(cur2->name,BAD_CAST "Data")==0){
1389#ifdef DEBUG
1390            fprintf(stderr,"DATA\n");
1391#endif
1392            xmlNodePtr cur4=cur2->children;
1393            while(cur4!=NULL){
1394              while(cur4!=NULL &&cur4->type!=XML_ELEMENT_NODE)
1395                cur4=cur4->next;
1396              if(cur4==NULL)
1397                break;
1398              if(xmlStrcasecmp(cur4->name, BAD_CAST "LiteralData")==0){
1399                /**
1400                 * Get every attribute from a LiteralData node
1401                 * dataType , uom
1402                 */
1403                char *list[2];
1404                list[0]=strdup("dataType");
1405                list[1]=strdup("uom");
1406                for(int l=0;l<2;l++){
1407#ifdef DEBUG
1408                  fprintf(stderr,"*** LiteralData %s ***",list[l]);
1409#endif
1410                  xmlChar *val=xmlGetProp(cur4,BAD_CAST list[l]);
1411                  if(val!=NULL && strlen((char*)val)>0){
1412                    if(tmpmaps->content!=NULL)
1413                      addToMap(tmpmaps->content,list[l],(char*)val);
1414                    else
1415                      tmpmaps->content=createMap(list[l],(char*)val);
1416                  }
1417#ifdef DEBUG
1418                  fprintf(stderr,"%s\n",val);
1419#endif
1420                  xmlFree(val);
1421                  free(list[l]);
1422                }
1423              }
1424              else if(xmlStrcasecmp(cur4->name, BAD_CAST "ComplexData")==0){
1425                /**
1426                 * Get every attribute from a Reference node
1427                 * mimeType, encoding, schema
1428                 */
1429                const char *coms[3];
1430                coms[0]="mimeType";
1431                coms[1]="encoding";
1432                coms[2]="schema";
1433                for(int l=0;l<3;l++){
1434#ifdef DEBUG
1435                  fprintf(stderr,"*** ComplexData %s ***",coms[l]);
1436#endif
1437                  xmlChar *val=xmlGetProp(cur4,BAD_CAST coms[l]);
1438                  if(val!=NULL && strlen((char*)val)>0){
1439                    if(tmpmaps->content!=NULL)
1440                      addToMap(tmpmaps->content,coms[l],(char*)val);
1441                    else
1442                      tmpmaps->content=createMap(coms[l],(char*)val);
1443                  }
1444#ifdef DEBUG
1445                  fprintf(stderr,"%s\n",val);
1446#endif
1447                  xmlFree(val);
1448                }
1449              }
1450              map* test=getMap(tmpmaps->content,"encoding");
1451              if(test==NULL || strcasecmp(test->value,"base64")!=0){
1452                xmlChar* mv=xmlNodeListGetString(doc,cur4->xmlChildrenNode,1);
1453                if(mv==NULL){
1454                  xmlDocPtr doc1=xmlNewDoc(BAD_CAST "1.0");
1455                  int buffersize;
1456                  xmlDocSetRootElement(doc1,cur4->xmlChildrenNode);
1457                  xmlDocDumpFormatMemoryEnc(doc1, &mv, &buffersize, "utf-8", 1);
1458                  char size[1024];
1459                  sprintf(size,"%d",buffersize);
1460                  addToMap(tmpmaps->content,"size",size);
1461                }
1462                addToMap(tmpmaps->content,"value",(char*)mv);
1463                xmlFree(mv);
1464              }else{
1465                xmlChar* tmp=xmlNodeListGetRawString(doc,cur4->xmlChildrenNode,0);
1466                addToMap(tmpmaps->content,"value",(char*)tmp);
1467                map* tmpv=getMap(tmpmaps->content,"value");
1468                char *res=NULL;
1469                char *curs=tmpv->value;
1470                for(int i=0;i<=strlen(tmpv->value)/64;i++) {
1471                  if(res==NULL)
1472                    res=(char*)malloc(67*sizeof(char));
1473                  else
1474                    res=(char*)realloc(res,(((i+1)*65)+i)*sizeof(char));
1475                  int csize=i*65;
1476                  strncpy(res + csize,curs,64);
1477                  if(i==xmlStrlen(tmp)/64)
1478                    strcat(res,"\n\0");
1479                  else{
1480                    strncpy(res + (((i+1)*64)+i),"\n\0",2);
1481                    curs+=64;
1482                  }
1483                }
1484                free(tmpv->value);
1485                tmpv->value=strdup(res);
1486                free(res);
1487                xmlFree(tmp);
1488              }
1489              cur4=cur4->next;
1490            }
1491          }
1492#ifdef DEBUG
1493          fprintf(stderr,"cur2 next \n");
1494          fflush(stderr);
1495#endif
1496          cur2=cur2->next;
1497        }
1498#ifdef DEBUG
1499        fprintf(stderr,"ADD MAPS TO REQUEST MAPS !\n");
1500        fflush(stderr);
1501#endif
1502        addMapsToMaps(&request_input_real_format,tmpmaps);
1503       
1504#ifdef DEBUG
1505        fprintf(stderr,"******TMPMAPS*****\n");
1506        dumpMaps(tmpmaps);
1507        fprintf(stderr,"******REQUESTMAPS*****\n");
1508        dumpMaps(request_input_real_format);
1509#endif
1510        freeMaps(&tmpmaps);
1511        free(tmpmaps);
1512        tmpmaps=NULL;         
1513      }
1514#ifdef DEBUG
1515      dumpMaps(tmpmaps); 
1516#endif
1517    }
1518#ifdef DEBUG
1519    fprintf(stderr,"Search for response document node\n");
1520#endif
1521    xmlXPathFreeObject(tmpsptr);
1522   
1523    tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='ResponseDocument']");
1524    bool asRaw=false;
1525    tmps=tmpsptr->nodesetval;
1526    if(tmps->nodeNr==0){
1527      tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='RawDataOutput']");
1528      tmps=tmpsptr->nodesetval;
1529      asRaw=true;
1530    }
1531#ifdef DEBUG
1532    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1533#endif
1534    for(int k=0;k<tmps->nodeNr;k++){
1535      if(asRaw==true)
1536        addToMap(request_inputs,"RawDataOutput","");
1537      else
1538        addToMap(request_inputs,"ResponseDocument","");
1539      maps *tmpmaps=NULL;
1540      xmlNodePtr cur=tmps->nodeTab[k];
1541      if(cur->type == XML_ELEMENT_NODE) {
1542        /**
1543         * A specific responseDocument node.
1544         */
1545        if(tmpmaps==NULL){
1546          tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1547          if(tmpmaps == NULL){
1548            return errorException(m, _("Unable to allocate memory."), "InternalError");
1549          }
1550          tmpmaps->name=strdup("unknownIdentifier");
1551          tmpmaps->next=NULL;
1552        }
1553        /**
1554         * Get every attribute from a LiteralData node
1555         * storeExecuteResponse, lineage, status
1556         */
1557        const char *ress[3];
1558        ress[0]="storeExecuteResponse";
1559        ress[1]="lineage";
1560        ress[2]="status";
1561        xmlChar *val;
1562        for(int l=0;l<3;l++){
1563#ifdef DEBUG
1564          fprintf(stderr,"*** %s ***\t",ress[l]);
1565#endif
1566          val=xmlGetProp(cur,BAD_CAST ress[l]);
1567          if(val!=NULL && strlen((char*)val)>0){
1568            if(tmpmaps->content!=NULL)
1569              addToMap(tmpmaps->content,ress[l],(char*)val);
1570            else
1571              tmpmaps->content=createMap(ress[l],(char*)val);
1572            addToMap(request_inputs,ress[l],(char*)val);
1573          }
1574#ifdef DEBUG
1575          fprintf(stderr,"%s\n",val);
1576#endif
1577          xmlFree(val);
1578        }
1579        xmlNodePtr cur1=cur->children;
1580        while(cur1){
1581          if(xmlStrncasecmp(cur1->name,BAD_CAST "Output",xmlStrlen(cur1->name))==0){
1582            /**
1583             * Get every attribute from a Output node
1584             * mimeType, encoding, schema, uom, asReference
1585             */
1586            const char *outs[5];
1587            outs[0]="mimeType";
1588            outs[1]="encoding";
1589            outs[2]="schema";
1590            outs[3]="uom";
1591            outs[4]="asReference";
1592            for(int l=0;l<5;l++){
1593#ifdef DEBUG
1594              fprintf(stderr,"*** %s ***\t",outs[l]);
1595#endif
1596              val=xmlGetProp(cur1,BAD_CAST outs[l]);
1597              if(val!=NULL && strlen((char*)val)>0){
1598                if(tmpmaps->content!=NULL)
1599                  addToMap(tmpmaps->content,outs[l],(char*)val);
1600                else
1601                  tmpmaps->content=createMap(outs[l],(char*)val);
1602              }
1603#ifdef DEBUG
1604              fprintf(stderr,"%s\n",val);
1605#endif
1606              xmlFree(val);
1607            }
1608           
1609            xmlNodePtr cur2=cur1->children;
1610            while(cur2){
1611              /**
1612               * Indentifier
1613               */
1614              if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1615                xmlChar *val=
1616                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1617                if(tmpmaps==NULL){
1618                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1619                  if(tmpmaps == NULL){
1620                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1621                  }
1622                  tmpmaps->name=strdup((char*)val);
1623                  tmpmaps->content=NULL;
1624                  tmpmaps->next=NULL;
1625                }
1626                else
1627                  tmpmaps->name=strdup((char*)val);;
1628                xmlFree(val);
1629              }
1630              /**
1631               * Title, Asbtract
1632               */
1633              if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1634                 xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1635                xmlChar *val=
1636                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1637                if(tmpmaps==NULL){
1638                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1639                  if(tmpmaps == NULL){
1640                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1641                  }
1642                  tmpmaps->name=strdup("missingIndetifier");
1643                  tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1644                  tmpmaps->next=NULL;
1645                }
1646                else{
1647                  if(tmpmaps->content!=NULL)
1648                    addToMap(tmpmaps->content,
1649                             (char*)cur2->name,(char*)val);
1650                  else
1651                    tmpmaps->content=
1652                      createMap((char*)cur2->name,(char*)val);
1653                }
1654                xmlFree(val);
1655              }
1656              cur2=cur2->next;
1657            }
1658          }
1659          cur1=cur1->next;
1660        }
1661      }
1662      if(request_output_real_format==NULL)
1663        request_output_real_format=dupMaps(&tmpmaps);
1664      else
1665        addMapsToMaps(&request_output_real_format,tmpmaps);
1666#ifdef DEBUG
1667      dumpMaps(tmpmaps);
1668#endif
1669      freeMaps(&tmpmaps);
1670      free(tmpmaps);
1671    }
1672
1673    xmlXPathFreeObject(tmpsptr);
1674    xmlCleanupParser();
1675  }
1676 
1677  //if(CHECK_INET_HANDLE(hInternet))
1678  InternetCloseHandle(hInternet);
1679
1680#ifdef DEBUG
1681  fprintf(stderr,"\n%i\n",i);
1682  dumpMaps(request_input_real_format);
1683  dumpMaps(request_output_real_format);
1684  dumpMap(request_inputs);
1685#endif
1686
1687  /**
1688   * Ensure that each requested arguments are present in the request
1689   * DataInputs and ResponseDocument / RawDataOutput
1690   */
1691  char *dfv=addDefaultValues(&request_input_real_format,s1->inputs,m,0);
1692  if(strcmp(dfv,"")!=0){
1693    char tmps[1024];
1694    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);
1695    map* tmpe=createMap("text",tmps);
1696    addToMap(tmpe,"code","MissingParameterValue");
1697    printExceptionReportResponse(m,tmpe);
1698    freeService(&s1);
1699    free(s1);
1700    freeMap(&tmpe);
1701    free(tmpe);
1702    freeMaps(&m);
1703    free(m);
1704    free(REQUEST);
1705    free(SERVICE_URL);
1706    freeMaps(&request_input_real_format);
1707    free(request_input_real_format);
1708    freeMaps(&request_output_real_format);
1709    free(request_output_real_format);
1710    freeMaps(&tmpmaps);
1711    free(tmpmaps);
1712    return 1;
1713  }
1714  addDefaultValues(&request_output_real_format,s1->outputs,m,1);
1715
1716  ensureDecodedBase64(&request_input_real_format);
1717
1718#ifdef DEBUG
1719  fprintf(stderr,"REQUEST_INPUTS\n");
1720  dumpMaps(request_input_real_format);
1721  fprintf(stderr,"REQUEST_OUTPUTS\n");
1722  dumpMaps(request_output_real_format);
1723#endif
1724
1725  maps* curs=getMaps(m,"env");
1726  if(curs!=NULL){
1727    map* mapcs=curs->content;
1728    while(mapcs!=NULLMAP){
1729#ifndef WIN32
1730      setenv(mapcs->name,mapcs->value,1);
1731#else
1732#ifdef DEBUG
1733      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1734#endif
1735      if(mapcs->value[strlen(mapcs->value)-2]=='\r'){
1736#ifdef DEBUG
1737        fprintf(stderr,"[ZOO: Env var finish with \r]\n");
1738#endif
1739        mapcs->value[strlen(mapcs->value)-1]=0;
1740      }
1741#ifdef DEBUG
1742      fflush(stderr);
1743      fprintf(stderr,"setting variable... %s\n",
1744#endif
1745              SetEnvironmentVariable(mapcs->name,mapcs->value)
1746#ifdef DEBUG
1747              ? "OK" : "FAILED");
1748#else
1749      ;
1750#endif
1751#ifdef DEBUG
1752      fflush(stderr);
1753#endif
1754#endif
1755#ifdef DEBUG
1756      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1757      fflush(stderr);
1758#endif
1759      mapcs=mapcs->next;
1760    }
1761  }
1762 
1763#ifdef DEBUG
1764  dumpMap(request_inputs);
1765#endif
1766
1767  /**
1768   * Need to check if we need to fork to load a status enabled
1769   */
1770  r_inputs=NULL;
1771  map* store=getMap(request_inputs,"storeExecuteResponse");
1772  map* status=getMap(request_inputs,"status");
1773  /**
1774   * 05-007r7 WPS 1.0.0 page 57 :
1775   * 'If status="true" and storeExecuteResponse is "false" then the service
1776   * shall raise an exception.'
1777   */
1778  if(status!=NULL && strcmp(status->value,"true")==0 && 
1779     store!=NULL && strcmp(store->value,"false")==0){
1780    errorException(m, _("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."), "InvalidParameterValue");
1781    freeService(&s1);
1782    free(s1);
1783    freeMaps(&m);
1784    free(m);
1785   
1786    freeMaps(&request_input_real_format);
1787    free(request_input_real_format);
1788   
1789    freeMaps(&request_output_real_format);
1790    free(request_output_real_format);
1791   
1792    free(REQUEST);
1793    free(SERVICE_URL);
1794    return 1;
1795  }
1796  r_inputs=getMap(request_inputs,"storeExecuteResponse");
1797  int eres=SERVICE_STARTED;
1798  int cpid=getpid();
1799
1800  maps *_tmpMaps=(maps*)malloc(MAPS_SIZE);
1801  _tmpMaps->name=strdup("lenv");
1802  char tmpBuff[100];
1803  sprintf(tmpBuff,"%i",cpid);
1804  _tmpMaps->content=createMap("sid",tmpBuff);
1805  _tmpMaps->next=NULL;
1806  addToMap(_tmpMaps->content,"status","0");
1807  addToMap(_tmpMaps->content,"cwd",ntmp);
1808  if(cgiCookie!=NULL && strlen(cgiCookie)>0){
1809    addToMap(_tmpMaps->content,"sessid",strstr(cgiCookie,"=")+1);
1810    char session_file_path[1024];
1811    map *tmpPath=getMapFromMaps(m,"main","sessPath");
1812    if(tmpPath==NULL)
1813      tmpPath=getMapFromMaps(m,"main","tmpPath");
1814    char *tmp1=strtok(cgiCookie,";");
1815    if(tmp1!=NULL)
1816      sprintf(session_file_path,"%s/sess_%s.cfg",tmpPath->value,strstr(tmp1,"=")+1);
1817    else
1818      sprintf(session_file_path,"%s/sess_%s.cfg",tmpPath->value,strstr(cgiCookie,"=")+1);
1819
1820    maps *tmpSess=(maps*)calloc(1,MAPS_SIZE);
1821    struct stat file_status;
1822    int istat = stat(session_file_path, &file_status);
1823    if(istat==0){
1824      conf_read(session_file_path,tmpSess);
1825      dumpMaps(tmpSess);
1826      addMapsToMaps(&m,tmpSess);
1827      freeMaps(&tmpSess);
1828    }
1829    free(tmpSess);
1830  }
1831  addMapsToMaps(&m,_tmpMaps);
1832  freeMaps(&_tmpMaps);
1833  free(_tmpMaps);
1834
1835#ifdef DEBUG
1836  dumpMap(request_inputs);
1837#endif
1838#ifdef WIN32
1839  char *cgiSidL=NULL;
1840  if(getenv("CGISID")!=NULL)
1841        addToMap(request_inputs,"cgiSid",getenv("CGISID"));
1842  map* test1=getMap(request_inputs,"cgiSid");
1843  if(test1!=NULL){
1844    cgiSid=test1->value;
1845  }
1846  if(cgiSid!=NULL){
1847    addToMap(request_inputs,"storeExecuteResponse","true");
1848    addToMap(request_inputs,"status","true");
1849    status=getMap(request_inputs,"status");
1850    dumpMap(request_inputs);
1851    fprintf(stderr,"cgiSID : %s",cgiSid);
1852  }
1853#endif
1854  if(status!=NULL)
1855    if(strcasecmp(status->value,"false")==0)
1856      status=NULL;
1857  if(status==NULLMAP){
1858    loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1859  }
1860  else{
1861    pid_t   pid;
1862#ifdef DEBUG
1863    fprintf(stderr,"\nPID : %d\n",cpid);
1864#endif
1865
1866#ifndef WIN32
1867    pid = fork ();
1868#else
1869    if(cgiSid==NULL){
1870      addToMap(request_inputs,"cgSid",cgiSid);
1871      createProcess(m,request_inputs,s1,NULL,cpid,request_input_real_format,request_output_real_format);
1872      pid = cpid;
1873    }else{
1874      pid=0;
1875      cpid=atoi(cgiSid);
1876    }
1877    fflush(stderr);
1878#endif
1879    if (pid > 0) {
1880      /**
1881       * dady :
1882       * set status to SERVICE_ACCEPTED
1883       */
1884#ifdef DEBUG
1885      fprintf(stderr,"father pid continue (origin %d) %d ...\n",cpid,getpid());
1886#endif
1887      eres=SERVICE_ACCEPTED;
1888    }else if (pid == 0) {
1889      /**
1890       * son : have to close the stdout, stdin and stderr to let the parent
1891       * process answer to http client.
1892       */
1893      r_inputs=getMapFromMaps(m,"main","tmpPath");
1894      map* r_inputs1=getMap(s1->content,"ServiceProvider");
1895      char* fbkp=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1896      sprintf(fbkp,"%s/%s_%d.xml",r_inputs->value,r_inputs1->value,cpid);
1897      char* flog=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1898      sprintf(flog,"%s/%s_%d_error.log",r_inputs->value,r_inputs1->value,cpid);
1899#ifdef DEBUG
1900      fprintf(stderr,"RUN IN BACKGROUND MODE \n");
1901      fprintf(stderr,"son pid continue (origin %d) %d ...\n",cpid,getpid());
1902      fprintf(stderr,"\nFILE TO STORE DATA %s\n",r_inputs->value);
1903#endif
1904      freopen(flog,"w+",stderr);
1905      freopen(fbkp , "w+", stdout);
1906      fclose(stdin);
1907      free(fbkp);
1908      free(flog);
1909      /**
1910       * set status to SERVICE_STARTED and flush stdout to ensure full
1911       * content was outputed (the file used to store the ResponseDocument).
1912       * The rewind stdout to restart writing from the bgining of the file,
1913       * this way the data will be updated at the end of the process run.
1914       */
1915      updateStatus(m);
1916      printProcessResponse(m,request_inputs,cpid,
1917                           s1,r_inputs1->value,SERVICE_STARTED,
1918                           request_input_real_format,
1919                           request_output_real_format);
1920#ifndef WIN32
1921      fflush(stdout);
1922      rewind(stdout);
1923#endif
1924
1925      loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1926
1927    } else {
1928      /**
1929       * error server don't accept the process need to output a valid
1930       * error response here !!!
1931       */
1932      eres=-1;
1933      errorException(m, _("Unable to run the child process properly"), "InternalError");
1934    }
1935  }
1936
1937#ifdef DEBUG
1938  dumpMaps(request_output_real_format);
1939  fprintf(stderr,"Function loaded and returned %d\n",*eres);
1940  fflush(stderr);
1941#endif
1942  if(eres!=-1)
1943    outputResponse(s1,request_input_real_format,
1944                   request_output_real_format,request_inputs,
1945                   cpid,m,eres);
1946  fflush(stdout);
1947  /**
1948   * Ensure that if error occurs when freeing memory, no signal will return
1949   * an ExceptionReport document as the result was already returned to the
1950   * client.
1951   */
1952#ifndef USE_GDB
1953  (void) signal(SIGSEGV,donothing);
1954  (void) signal(SIGTERM,donothing);
1955  (void) signal(SIGINT,donothing);
1956  (void) signal(SIGILL,donothing);
1957  (void) signal(SIGFPE,donothing);
1958  (void) signal(SIGABRT,donothing);
1959#endif
1960
1961  if(((int)getpid())!=cpid){
1962    fclose(stdout);
1963    fclose(stderr);
1964    unhandleStatus(m);
1965  }
1966
1967  freeService(&s1);
1968  free(s1);
1969  freeMaps(&m);
1970  free(m);
1971 
1972  freeMaps(&request_input_real_format);
1973  free(request_input_real_format);
1974 
1975  freeMaps(&request_output_real_format);
1976  free(request_output_real_format);
1977 
1978  free(REQUEST);
1979  free(SERVICE_URL);
1980#ifdef DEBUG
1981  fprintf(stderr,"Processed response \n");
1982  fflush(stdout);
1983  fflush(stderr);
1984#endif
1985
1986  return 0;
1987}
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