source: branches/branch-1.2/zoo-kernel/zoo_service_loader.c @ 268

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

Merge branch-1.2 to current rev. 267. Tag release-1.2.0-rc2.

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