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

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

Remove uneeded verbose debug messages.

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((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            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            }
1078          tmpc=strtok(NULL,"@");
1079        }
1080#ifdef DEBUG
1081        dumpMaps(tmpmaps);
1082        fflush(stderr);
1083#endif
1084        if(request_input_real_format==NULL)
1085          request_input_real_format=dupMaps(&tmpmaps);
1086        else
1087          addMapsToMaps(&request_input_real_format,tmpmaps);
1088        freeMaps(&tmpmaps);
1089        free(tmpmaps);
1090        tmpmaps=NULL;
1091        free(tmp);
1092      }
1093    }
1094    free(inputs_as_text);
1095  }
1096  else {
1097    /**
1098     * Parse XML request
1099     */ 
1100    xmlInitParser();
1101#ifdef DEBUG
1102    fflush(stderr);
1103    fprintf(stderr,"BEFORE %s\n",postRequest->value);
1104    fflush(stderr);
1105#endif
1106    xmlDocPtr doc =
1107      xmlParseMemory(postRequest->value,cgiContentLength);
1108#ifdef DEBUG
1109    fprintf(stderr,"AFTER\n");
1110    fflush(stderr);
1111#endif
1112    /**
1113     * Parse every Input in DataInputs node.
1114     */
1115    xmlXPathObjectPtr tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='Input']");
1116    xmlNodeSet* tmps=tmpsptr->nodesetval;
1117#ifdef DEBUG
1118    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1119#endif
1120    for(int k=0;k<tmps->nodeNr;k++){
1121      maps *tmpmaps=NULL;
1122      xmlNodePtr cur=tmps->nodeTab[k];
1123      if(tmps->nodeTab[k]->type == XML_ELEMENT_NODE) {
1124        /**
1125         * A specific Input node.
1126         */
1127#ifdef DEBUG
1128        fprintf(stderr, "= element 0 node \"%s\"\n", cur->name);
1129#endif
1130        xmlNodePtr cur2=cur->children;
1131        while(cur2!=NULL){
1132          while(cur2!=NULL && cur2->type!=XML_ELEMENT_NODE)
1133            cur2=cur2->next;
1134          if(cur2==NULL)
1135            break;
1136          /**
1137           * Indentifier
1138           */
1139          if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1140            xmlChar *val= xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1141            if(tmpmaps==NULL){
1142              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1143              if(tmpmaps == NULL){
1144                return errorException(m, _("Unable to allocate memory."), "InternalError");
1145              }
1146              tmpmaps->name=strdup((char*)val);
1147              tmpmaps->content=NULL;
1148              tmpmaps->next=NULL;
1149            }
1150            xmlFree(val);
1151          }
1152          /**
1153           * Title, Asbtract
1154           */
1155          if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1156             xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1157            xmlChar *val=
1158              xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1159            if(tmpmaps==NULL){
1160              tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1161              if(tmpmaps == NULL){
1162                return errorException(m, _("Unable to allocate memory."), "InternalError");
1163              }
1164              tmpmaps->name=strdup("missingIndetifier");
1165              tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1166              tmpmaps->next=NULL;
1167            }
1168            else{
1169              if(tmpmaps->content!=NULL)
1170                addToMap(tmpmaps->content,
1171                         (char*)cur2->name,(char*)val);
1172              else
1173                tmpmaps->content=
1174                  createMap((char*)cur2->name,(char*)val);
1175            }
1176#ifdef DEBUG
1177            dumpMaps(tmpmaps);
1178#endif
1179            xmlFree(val);
1180          }
1181          /**
1182           * InputDataFormChoice (Reference or Data ?)
1183           */
1184          if(xmlStrcasecmp(cur2->name,BAD_CAST "Reference")==0){
1185            /**
1186             * Get every attribute from a Reference node
1187             * mimeType, encoding, schema, href, method
1188             * Header and Body gesture should be added here
1189             */
1190#ifdef DEBUG
1191            fprintf(stderr,"REFERENCE\n");
1192#endif
1193            const char *refs[5];
1194            refs[0]="mimeType";
1195            refs[1]="encoding";
1196            refs[2]="schema";
1197            refs[3]="method";
1198            refs[4]="href";
1199            for(int l=0;l<5;l++){
1200#ifdef DEBUG
1201              fprintf(stderr,"*** %s ***",refs[l]);
1202#endif
1203              xmlChar *val=xmlGetProp(cur2,BAD_CAST refs[l]);
1204              if(val!=NULL && xmlStrlen(val)>0){
1205                if(tmpmaps->content!=NULL)
1206                  addToMap(tmpmaps->content,refs[l],(char*)val);
1207                else
1208                  tmpmaps->content=createMap(refs[l],(char*)val);
1209                map* ltmp=getMap(tmpmaps->content,"method");
1210                if(l==4){
1211                  if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0)
1212                     && CHECK_INET_HANDLE(hInternet)){
1213                    res=InternetOpenUrl(hInternet,(char*)val,NULL,0,
1214                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1215                    char* tmpContent=
1216                      (char*)calloc((res.nDataLen+1),sizeof(char));
1217                    if(tmpContent == NULL){
1218                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1219                    }
1220                    size_t dwRead;
1221                    InternetReadFile(res, (LPVOID)tmpContent,
1222                                     res.nDataLen, &dwRead);
1223                    tmpContent[res.nDataLen]=0;
1224                    addToMap(tmpmaps->content,"value",tmpContent);
1225                  }
1226                }
1227              }
1228#ifdef DEBUG
1229              fprintf(stderr,"%s\n",val);
1230#endif
1231              xmlFree(val);
1232            }
1233#ifdef POST_DEBUG
1234            fprintf(stderr,"Parse Header and Body from Reference \n");
1235#endif
1236            xmlNodePtr cur3=cur2->children;
1237            hInternet.header=NULL;
1238            while(cur3){
1239              while(cur3!=NULL && cur3->type!=XML_ELEMENT_NODE)
1240                cur2=cur3->next;
1241              if(xmlStrcasecmp(cur3->name,BAD_CAST "Header")==0 ){
1242                const char *ha[2];
1243                ha[0]="key";
1244                ha[1]="value";
1245                int hai;
1246                char *has;
1247                char *key;
1248                for(hai=0;hai<2;hai++){
1249                  xmlChar *val=xmlGetProp(cur3,BAD_CAST ha[hai]);
1250#ifdef POST_DEBUG
1251                  fprintf(stderr,"%s = %s\n",ha[hai],(char*)val);
1252#endif
1253                  if(hai==0){
1254                    key=(char*)calloc((1+strlen((char*)val)),sizeof(char));
1255                    snprintf(key,1+strlen((char*)val),"%s",(char*)val);
1256                  }else{
1257                    has=(char*)calloc((3+strlen((char*)val)+strlen(key)),sizeof(char));
1258                    if(has == NULL){
1259                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1260                    }
1261                    snprintf(has,(3+strlen((char*)val)+strlen(key)),"%s: %s",key,(char*)val);
1262#ifdef POST_DEBUG
1263                    fprintf(stderr,"%s\n",has);
1264#endif
1265                  }
1266                }
1267                hInternet.header=curl_slist_append(hInternet.header, has);
1268                free(has);
1269              }
1270              else{
1271#ifdef POST_DEBUG
1272                fprintf(stderr,"Try to fetch the body part of the request ...\n");
1273#endif
1274                if(xmlStrcasecmp(cur3->name,BAD_CAST "Body")==0 ){
1275#ifdef POST_DEBUG
1276                  fprintf(stderr,"Body part found !!!\n",(char*)cur3->content);
1277#endif
1278                  char *tmp=new char[cgiContentLength];
1279                  memset(tmp,0,cgiContentLength);
1280                  xmlNodePtr cur4=cur3->children;
1281                  while(cur4!=NULL){
1282                    while(cur4->type!=XML_ELEMENT_NODE)
1283                      cur4=cur4->next;
1284                    xmlDocPtr bdoc = xmlNewDoc(BAD_CAST "1.0");
1285                    bdoc->encoding = xmlCharStrdup ("UTF-8");
1286                    xmlDocSetRootElement(bdoc,cur4);
1287                    xmlChar* btmps;
1288                    int bsize;
1289                    xmlDocDumpMemory(bdoc,&btmps,&bsize);
1290#ifdef POST_DEBUG
1291                    fprintf(stderr,"Body part found !!! %s %s\n",tmp,(char*)btmps);
1292#endif
1293                    if(btmps!=NULL)
1294                      sprintf(tmp,"%s",(char*)btmps);
1295                    xmlFreeDoc(bdoc);
1296                    cur4=cur4->next;
1297                  }
1298                  map *btmp=getMap(tmpmaps->content,"href");
1299                  if(btmp!=NULL){
1300#ifdef POST_DEBUG
1301                    fprintf(stderr,"%s %s\n",btmp->value,tmp);
1302                    curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1303#endif
1304                    res=InternetOpenUrl(hInternet,btmp->value,tmp,strlen(tmp),
1305                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
1306                    char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1307                    if(tmpContent == NULL){
1308                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1309                    }
1310                    size_t dwRead;
1311                    InternetReadFile(res, (LPVOID)tmpContent,
1312                                     res.nDataLen, &dwRead);
1313                    tmpContent[res.nDataLen]=0;
1314                    if(hInternet.header!=NULL)
1315                      curl_slist_free_all(hInternet.header);
1316                    addToMap(tmpmaps->content,"value",tmpContent);
1317#ifdef POST_DEBUG
1318                    fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1319#endif
1320                  }
1321                }
1322                else
1323                  if(xmlStrcasecmp(cur3->name,BAD_CAST "BodyReference")==0 ){
1324                    xmlChar *val=xmlGetProp(cur3,BAD_CAST "href");
1325                    HINTERNET bInternet,res1;
1326                    bInternet=InternetOpen(
1327#ifndef WIN32
1328                                           (LPCTSTR)
1329#endif
1330                                           "ZooWPSClient\0",
1331                                           INTERNET_OPEN_TYPE_PRECONFIG,
1332                                           NULL,NULL, 0);
1333                    if(!CHECK_INET_HANDLE(bInternet))
1334                      fprintf(stderr,"WARNING : hInternet handle failed to initialize");
1335#ifdef POST_DEBUG
1336                    curl_easy_setopt(bInternet.handle, CURLOPT_VERBOSE, 1);
1337#endif
1338                    res1=InternetOpenUrl(bInternet,(char*)val,NULL,0,
1339                                         INTERNET_FLAG_NO_CACHE_WRITE,0);
1340                    char* tmp=
1341                      (char*)calloc((res1.nDataLen+1),sizeof(char));
1342                    if(tmp == NULL){
1343                      return errorException(m, _("Unable to allocate memory."), "InternalError");
1344                    }
1345                    size_t bRead;
1346                    InternetReadFile(res1, (LPVOID)tmp,
1347                                     res1.nDataLen, &bRead);
1348                    tmp[res1.nDataLen]=0;
1349                    InternetCloseHandle(bInternet);
1350                    map *btmp=getMap(tmpmaps->content,"href");
1351                    if(btmp!=NULL){
1352#ifdef POST_DEBUG
1353                      fprintf(stderr,"%s %s\n",btmp->value,tmp);
1354                      curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1);
1355#endif
1356                      res=InternetOpenUrl(hInternet,btmp->value,tmp,
1357                                          strlen(tmp),
1358                                          INTERNET_FLAG_NO_CACHE_WRITE,0);
1359                      char* tmpContent = (char*)calloc((res.nDataLen+1),sizeof(char));
1360                      if(tmpContent == NULL){
1361                        return errorException(m, _("Unable to allocate memory."), "InternalError");
1362                      }
1363                      size_t dwRead;
1364                      InternetReadFile(res, (LPVOID)tmpContent,
1365                                       res.nDataLen, &dwRead);
1366                      tmpContent[res.nDataLen]=0;
1367                      if(hInternet.header!=NULL)
1368                        curl_slist_free_all(hInternet.header);
1369                      addToMap(tmpmaps->content,"value",tmpContent);
1370#ifdef POST_DEBUG
1371                      fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1372#endif
1373                    }
1374                  }
1375              }
1376              cur3=cur3->next;
1377            }
1378#ifdef POST_DEBUG
1379            fprintf(stderr,"Header and Body was parsed from Reference \n");
1380#endif
1381#ifdef DEBUG
1382            dumpMap(tmpmaps->content);
1383            fprintf(stderr, "= element 2 node \"%s\" = (%s)\n", 
1384                    cur2->name,cur2->content);
1385#endif
1386          }
1387          else if(xmlStrcasecmp(cur2->name,BAD_CAST "Data")==0){
1388#ifdef DEBUG
1389            fprintf(stderr,"DATA\n");
1390#endif
1391            xmlNodePtr cur4=cur2->children;
1392            while(cur4!=NULL){
1393              while(cur4!=NULL &&cur4->type!=XML_ELEMENT_NODE)
1394                cur4=cur4->next;
1395              if(cur4==NULL)
1396                break;
1397              if(xmlStrcasecmp(cur4->name, BAD_CAST "LiteralData")==0){
1398                /**
1399                 * Get every attribute from a LiteralData node
1400                 * dataType , uom
1401                 */
1402                char *list[2];
1403                list[0]=strdup("dataType");
1404                list[1]=strdup("uom");
1405                for(int l=0;l<2;l++){
1406#ifdef DEBUG
1407                  fprintf(stderr,"*** LiteralData %s ***",list[l]);
1408#endif
1409                  xmlChar *val=xmlGetProp(cur4,BAD_CAST list[l]);
1410                  if(val!=NULL && strlen((char*)val)>0){
1411                    if(tmpmaps->content!=NULL)
1412                      addToMap(tmpmaps->content,list[l],(char*)val);
1413                    else
1414                      tmpmaps->content=createMap(list[l],(char*)val);
1415                  }
1416#ifdef DEBUG
1417                  fprintf(stderr,"%s\n",val);
1418#endif
1419                  xmlFree(val);
1420                  free(list[l]);
1421                }
1422              }
1423              else if(xmlStrcasecmp(cur4->name, BAD_CAST "ComplexData")==0){
1424                /**
1425                 * Get every attribute from a Reference node
1426                 * mimeType, encoding, schema
1427                 */
1428                const char *coms[3];
1429                coms[0]="mimeType";
1430                coms[1]="encoding";
1431                coms[2]="schema";
1432                for(int l=0;l<3;l++){
1433#ifdef DEBUG
1434                  fprintf(stderr,"*** ComplexData %s ***",coms[l]);
1435#endif
1436                  xmlChar *val=xmlGetProp(cur4,BAD_CAST coms[l]);
1437                  if(val!=NULL && strlen((char*)val)>0){
1438                    if(tmpmaps->content!=NULL)
1439                      addToMap(tmpmaps->content,coms[l],(char*)val);
1440                    else
1441                      tmpmaps->content=createMap(coms[l],(char*)val);
1442                  }
1443#ifdef DEBUG
1444                  fprintf(stderr,"%s\n",val);
1445#endif
1446                  xmlFree(val);
1447                }
1448              }
1449              map* test=getMap(tmpmaps->content,"encoding");
1450              if(test==NULL || strcasecmp(test->value,"base64")!=0){
1451                xmlChar* mv=xmlNodeListGetString(doc,cur4->xmlChildrenNode,1);
1452                if(mv==NULL){
1453                  xmlDocPtr doc1=xmlNewDoc(BAD_CAST "1.0");
1454                  int buffersize;
1455                  xmlDocSetRootElement(doc1,cur4->xmlChildrenNode);
1456                  xmlDocDumpFormatMemoryEnc(doc1, &mv, &buffersize, "utf-8", 1);
1457                  char size[1024];
1458                  sprintf(size,"%d",buffersize);
1459                  addToMap(tmpmaps->content,"size",size);
1460                }
1461                addToMap(tmpmaps->content,"value",(char*)mv);
1462                xmlFree(mv);
1463              }else{
1464                xmlChar* tmp=xmlNodeListGetRawString(doc,cur4->xmlChildrenNode,0);
1465                addToMap(tmpmaps->content,"value",(char*)tmp);
1466                map* tmpv=getMap(tmpmaps->content,"value");
1467                char *res=NULL;
1468                char *curs=tmpv->value;
1469                for(int i=0;i<=strlen(tmpv->value)/64;i++) {
1470                  if(res==NULL)
1471                    res=(char*)malloc(67*sizeof(char));
1472                  else
1473                    res=(char*)realloc(res,(((i+1)*65)+i)*sizeof(char));
1474                  int csize=i*65;
1475                  strncpy(res + csize,curs,64);
1476                  if(i==xmlStrlen(tmp)/64)
1477                    strcat(res,"\n\0");
1478                  else{
1479                    strncpy(res + (((i+1)*64)+i),"\n\0",2);
1480                    curs+=64;
1481                  }
1482                }
1483                free(tmpv->value);
1484                tmpv->value=strdup(res);
1485                free(res);
1486                xmlFree(tmp);
1487              }
1488              cur4=cur4->next;
1489            }
1490          }
1491#ifdef DEBUG
1492          fprintf(stderr,"cur2 next \n");
1493          fflush(stderr);
1494#endif
1495          cur2=cur2->next;
1496        }
1497#ifdef DEBUG
1498        fprintf(stderr,"ADD MAPS TO REQUEST MAPS !\n");
1499        fflush(stderr);
1500#endif
1501        addMapsToMaps(&request_input_real_format,tmpmaps);
1502       
1503#ifdef DEBUG
1504        fprintf(stderr,"******TMPMAPS*****\n");
1505        dumpMaps(tmpmaps);
1506        fprintf(stderr,"******REQUESTMAPS*****\n");
1507        dumpMaps(request_input_real_format);
1508#endif
1509        freeMaps(&tmpmaps);
1510        free(tmpmaps);
1511        tmpmaps=NULL;         
1512      }
1513#ifdef DEBUG
1514      dumpMaps(tmpmaps); 
1515#endif
1516    }
1517#ifdef DEBUG
1518    fprintf(stderr,"Search for response document node\n");
1519#endif
1520    xmlXPathFreeObject(tmpsptr);
1521   
1522    tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='ResponseDocument']");
1523    bool asRaw=false;
1524    tmps=tmpsptr->nodesetval;
1525    if(tmps->nodeNr==0){
1526      tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='RawDataOutput']");
1527      tmps=tmpsptr->nodesetval;
1528      asRaw=true;
1529    }
1530#ifdef DEBUG
1531    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1532#endif
1533    for(int k=0;k<tmps->nodeNr;k++){
1534      if(asRaw==true)
1535        addToMap(request_inputs,"RawDataOutput","");
1536      else
1537        addToMap(request_inputs,"ResponseDocument","");
1538      maps *tmpmaps=NULL;
1539      xmlNodePtr cur=tmps->nodeTab[k];
1540      if(cur->type == XML_ELEMENT_NODE) {
1541        /**
1542         * A specific responseDocument node.
1543         */
1544        if(tmpmaps==NULL){
1545          tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1546          if(tmpmaps == NULL){
1547            return errorException(m, _("Unable to allocate memory."), "InternalError");
1548          }
1549          tmpmaps->name=strdup("unknownIdentifier");
1550          tmpmaps->next=NULL;
1551        }
1552        /**
1553         * Get every attribute from a LiteralData node
1554         * storeExecuteResponse, lineage, status
1555         */
1556        const char *ress[3];
1557        ress[0]="storeExecuteResponse";
1558        ress[1]="lineage";
1559        ress[2]="status";
1560        xmlChar *val;
1561        for(int l=0;l<3;l++){
1562#ifdef DEBUG
1563          fprintf(stderr,"*** %s ***\t",ress[l]);
1564#endif
1565          val=xmlGetProp(cur,BAD_CAST ress[l]);
1566          if(val!=NULL && strlen((char*)val)>0){
1567            if(tmpmaps->content!=NULL)
1568              addToMap(tmpmaps->content,ress[l],(char*)val);
1569            else
1570              tmpmaps->content=createMap(ress[l],(char*)val);
1571            addToMap(request_inputs,ress[l],(char*)val);
1572          }
1573#ifdef DEBUG
1574          fprintf(stderr,"%s\n",val);
1575#endif
1576          xmlFree(val);
1577        }
1578        xmlNodePtr cur1=cur->children;
1579        while(cur1){
1580          if(xmlStrncasecmp(cur1->name,BAD_CAST "Output",xmlStrlen(cur1->name))==0){
1581            /**
1582             * Get every attribute from a Output node
1583             * mimeType, encoding, schema, uom, asReference
1584             */
1585            const char *outs[5];
1586            outs[0]="mimeType";
1587            outs[1]="encoding";
1588            outs[2]="schema";
1589            outs[3]="uom";
1590            outs[4]="asReference";
1591            for(int l=0;l<5;l++){
1592#ifdef DEBUG
1593              fprintf(stderr,"*** %s ***\t",outs[l]);
1594#endif
1595              val=xmlGetProp(cur1,BAD_CAST outs[l]);
1596              if(val!=NULL && strlen((char*)val)>0){
1597                if(tmpmaps->content!=NULL)
1598                  addToMap(tmpmaps->content,outs[l],(char*)val);
1599                else
1600                  tmpmaps->content=createMap(outs[l],(char*)val);
1601              }
1602#ifdef DEBUG
1603              fprintf(stderr,"%s\n",val);
1604#endif
1605              xmlFree(val);
1606            }
1607           
1608            xmlNodePtr cur2=cur1->children;
1609            while(cur2){
1610              /**
1611               * Indentifier
1612               */
1613              if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1614                xmlChar *val=
1615                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1616                if(tmpmaps==NULL){
1617                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1618                  if(tmpmaps == NULL){
1619                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1620                  }
1621                  tmpmaps->name=strdup((char*)val);
1622                  tmpmaps->content=NULL;
1623                  tmpmaps->next=NULL;
1624                }
1625                else
1626                  tmpmaps->name=strdup((char*)val);;
1627                xmlFree(val);
1628              }
1629              /**
1630               * Title, Asbtract
1631               */
1632              else if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1633                 xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1634                xmlChar *val=
1635                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1636                if(tmpmaps==NULL){
1637                  tmpmaps=(maps*)calloc(1,MAPS_SIZE);
1638                  if(tmpmaps == NULL){
1639                    return errorException(m, _("Unable to allocate memory."), "InternalError");
1640                  }
1641                  tmpmaps->name=strdup("missingIndetifier");
1642                  tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1643                  tmpmaps->next=NULL;
1644                }
1645                else{
1646                  if(tmpmaps->content!=NULL)
1647                    addToMap(tmpmaps->content,
1648                             (char*)cur2->name,(char*)val);
1649                  else
1650                    tmpmaps->content=
1651                      createMap((char*)cur2->name,(char*)val);
1652                }
1653                xmlFree(val);
1654              }
1655              cur2=cur2->next;
1656            }
1657          }
1658          cur1=cur1->next;
1659        }
1660      }
1661      if(request_output_real_format==NULL)
1662        request_output_real_format=dupMaps(&tmpmaps);
1663      else
1664        addMapsToMaps(&request_output_real_format,tmpmaps);
1665#ifdef DEBUG
1666      dumpMaps(tmpmaps);
1667#endif
1668      freeMaps(&tmpmaps);
1669      free(tmpmaps);
1670    }
1671
1672    xmlXPathFreeObject(tmpsptr);
1673    xmlCleanupParser();
1674  }
1675 
1676  //if(CHECK_INET_HANDLE(hInternet))
1677  InternetCloseHandle(hInternet);
1678
1679#ifdef DEBUG
1680  fprintf(stderr,"\n%i\n",i);
1681  dumpMaps(request_input_real_format);
1682  dumpMaps(request_output_real_format);
1683  dumpMap(request_inputs);
1684#endif
1685
1686  /**
1687   * Ensure that each requested arguments are present in the request
1688   * DataInputs and ResponseDocument / RawDataOutput
1689   */
1690  char *dfv=addDefaultValues(&request_input_real_format,s1->inputs,m,0);
1691  if(strcmp(dfv,"")!=0){
1692    char tmps[1024];
1693    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);
1694    map* tmpe=createMap("text",tmps);
1695    addToMap(tmpe,"code","MissingParameterValue");
1696    printExceptionReportResponse(m,tmpe);
1697    freeService(&s1);
1698    free(s1);
1699    freeMap(&tmpe);
1700    free(tmpe);
1701    freeMaps(&m);
1702    free(m);
1703    free(REQUEST);
1704    free(SERVICE_URL);
1705    freeMaps(&request_input_real_format);
1706    free(request_input_real_format);
1707    freeMaps(&request_output_real_format);
1708    free(request_output_real_format);
1709    freeMaps(&tmpmaps);
1710    free(tmpmaps);
1711    return 1;
1712  }
1713  addDefaultValues(&request_output_real_format,s1->outputs,m,1);
1714
1715  ensureDecodedBase64(&request_input_real_format);
1716
1717#ifdef DEBUG
1718  fprintf(stderr,"REQUEST_INPUTS\n");
1719  dumpMaps(request_input_real_format);
1720  fprintf(stderr,"REQUEST_OUTPUTS\n");
1721  dumpMaps(request_output_real_format);
1722#endif
1723
1724  maps* curs=getMaps(m,"env");
1725  if(curs!=NULL){
1726    map* mapcs=curs->content;
1727    while(mapcs!=NULLMAP){
1728#ifndef WIN32
1729      setenv(mapcs->name,mapcs->value,1);
1730#else
1731#ifdef DEBUG
1732      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1733#endif
1734      if(mapcs->value[strlen(mapcs->value)-2]=='\r'){
1735#ifdef DEBUG
1736        fprintf(stderr,"[ZOO: Env var finish with \r]\n");
1737#endif
1738        mapcs->value[strlen(mapcs->value)-1]=0;
1739      }
1740#ifdef DEBUG
1741      fflush(stderr);
1742      fprintf(stderr,"setting variable... %s\n",
1743#endif
1744              SetEnvironmentVariable(mapcs->name,mapcs->value)
1745#ifdef DEBUG
1746              ? "OK" : "FAILED");
1747#else
1748      ;
1749#endif
1750#ifdef DEBUG
1751      fflush(stderr);
1752#endif
1753#endif
1754#ifdef DEBUG
1755      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
1756      fflush(stderr);
1757#endif
1758      mapcs=mapcs->next;
1759    }
1760  }
1761 
1762#ifdef DEBUG
1763  dumpMap(request_inputs);
1764#endif
1765
1766  /**
1767   * Need to check if we need to fork to load a status enabled
1768   */
1769  r_inputs=NULL;
1770  map* store=getMap(request_inputs,"storeExecuteResponse");
1771  map* status=getMap(request_inputs,"status");
1772  /**
1773   * 05-007r7 WPS 1.0.0 page 57 :
1774   * 'If status="true" and storeExecuteResponse is "false" then the service
1775   * shall raise an exception.'
1776   */
1777  if(status!=NULL && strcmp(status->value,"true")==0 && 
1778     store!=NULL && strcmp(store->value,"false")==0){
1779    errorException(m, _("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."), "InvalidParameterValue");
1780    freeService(&s1);
1781    free(s1);
1782    freeMaps(&m);
1783    free(m);
1784   
1785    freeMaps(&request_input_real_format);
1786    free(request_input_real_format);
1787   
1788    freeMaps(&request_output_real_format);
1789    free(request_output_real_format);
1790   
1791    free(REQUEST);
1792    free(SERVICE_URL);
1793    return 1;
1794  }
1795  r_inputs=getMap(request_inputs,"storeExecuteResponse");
1796  int eres=SERVICE_STARTED;
1797  int cpid=getpid();
1798
1799  maps *_tmpMaps=(maps*)malloc(MAPS_SIZE);
1800  _tmpMaps->name=strdup("lenv");
1801  char tmpBuff[100];
1802  sprintf(tmpBuff,"%i",cpid);
1803  _tmpMaps->content=createMap("sid",tmpBuff);
1804  _tmpMaps->next=NULL;
1805  addToMap(_tmpMaps->content,"status","0");
1806  addToMap(_tmpMaps->content,"cwd",ntmp);
1807  if(cgiCookie!=NULL && strlen(cgiCookie)>0){
1808    addToMap(_tmpMaps->content,"sessid",strstr(cgiCookie,"=")+1);
1809    char session_file_path[1024];
1810    map *tmpPath=getMapFromMaps(m,"main","sessPath");
1811    if(tmpPath==NULL)
1812      tmpPath=getMapFromMaps(m,"main","tmpPath");
1813    char *tmp1=strtok(cgiCookie,";");
1814    if(tmp1!=NULL)
1815      sprintf(session_file_path,"%s/sess_%s.cfg",tmpPath->value,strstr(tmp1,"=")+1);
1816    else
1817      sprintf(session_file_path,"%s/sess_%s.cfg",tmpPath->value,strstr(cgiCookie,"=")+1);
1818
1819    maps *tmpSess=(maps*)calloc(1,MAPS_SIZE);
1820    struct stat file_status;
1821    int istat = stat(session_file_path, &file_status);
1822    if(istat==0 && file_status.st_size>0){
1823      conf_read(session_file_path,tmpSess);
1824      addMapsToMaps(&m,tmpSess);
1825      freeMaps(&tmpSess);
1826    }
1827    free(tmpSess);
1828  }
1829  addMapsToMaps(&m,_tmpMaps);
1830  freeMaps(&_tmpMaps);
1831  free(_tmpMaps);
1832
1833#ifdef DEBUG
1834  dumpMap(request_inputs);
1835#endif
1836#ifdef WIN32
1837  char *cgiSidL=NULL;
1838  if(getenv("CGISID")!=NULL)
1839        addToMap(request_inputs,"cgiSid",getenv("CGISID"));
1840  map* test1=getMap(request_inputs,"cgiSid");
1841  if(test1!=NULL){
1842    cgiSid=test1->value;
1843  }
1844  if(cgiSid!=NULL){
1845    addToMap(request_inputs,"storeExecuteResponse","true");
1846    addToMap(request_inputs,"status","true");
1847    status=getMap(request_inputs,"status");
1848    fprintf(stderr,"cgiSID : %s",cgiSid);
1849  }
1850#endif
1851  if(status!=NULL)
1852    if(strcasecmp(status->value,"false")==0)
1853      status=NULL;
1854  if(status==NULLMAP){
1855    loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1856  }
1857  else{
1858    pid_t   pid;
1859#ifdef DEBUG
1860    fprintf(stderr,"\nPID : %d\n",cpid);
1861#endif
1862
1863#ifndef WIN32
1864    pid = fork ();
1865#else
1866    if(cgiSid==NULL){
1867      addToMap(request_inputs,"cgSid",cgiSid);
1868      createProcess(m,request_inputs,s1,NULL,cpid,request_input_real_format,request_output_real_format);
1869      pid = cpid;
1870    }else{
1871      pid=0;
1872      cpid=atoi(cgiSid);
1873    }
1874    fflush(stderr);
1875#endif
1876    if (pid > 0) {
1877      /**
1878       * dady :
1879       * set status to SERVICE_ACCEPTED
1880       */
1881#ifdef DEBUG
1882      fprintf(stderr,"father pid continue (origin %d) %d ...\n",cpid,getpid());
1883#endif
1884      eres=SERVICE_ACCEPTED;
1885    }else if (pid == 0) {
1886      /**
1887       * son : have to close the stdout, stdin and stderr to let the parent
1888       * process answer to http client.
1889       */
1890      r_inputs=getMapFromMaps(m,"main","tmpPath");
1891      map* r_inputs1=getMap(s1->content,"ServiceProvider");
1892      char* fbkp=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1893      sprintf(fbkp,"%s/%s_%d.xml",r_inputs->value,r_inputs1->value,cpid);
1894      char* flog=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+100)*sizeof(char));
1895      sprintf(flog,"%s/%s_%d_error.log",r_inputs->value,r_inputs1->value,cpid);
1896#ifdef DEBUG
1897      fprintf(stderr,"RUN IN BACKGROUND MODE \n");
1898      fprintf(stderr,"son pid continue (origin %d) %d ...\n",cpid,getpid());
1899      fprintf(stderr,"\nFILE TO STORE DATA %s\n",r_inputs->value);
1900#endif
1901      freopen(flog,"w+",stderr);
1902      freopen(fbkp , "w+", stdout);
1903      fclose(stdin);
1904      free(fbkp);
1905      free(flog);
1906      /**
1907       * set status to SERVICE_STARTED and flush stdout to ensure full
1908       * content was outputed (the file used to store the ResponseDocument).
1909       * The rewind stdout to restart writing from the bgining of the file,
1910       * this way the data will be updated at the end of the process run.
1911       */
1912      updateStatus(m);
1913      printProcessResponse(m,request_inputs,cpid,
1914                           s1,r_inputs1->value,SERVICE_STARTED,
1915                           request_input_real_format,
1916                           request_output_real_format);
1917#ifndef WIN32
1918      fflush(stdout);
1919      rewind(stdout);
1920#endif
1921
1922      loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
1923
1924    } else {
1925      /**
1926       * error server don't accept the process need to output a valid
1927       * error response here !!!
1928       */
1929      eres=-1;
1930      errorException(m, _("Unable to run the child process properly"), "InternalError");
1931    }
1932  }
1933
1934#ifdef DEBUG
1935  dumpMaps(request_output_real_format);
1936  fprintf(stderr,"Function loaded and returned %d\n",*eres);
1937  fflush(stderr);
1938#endif
1939  if(eres!=-1)
1940    outputResponse(s1,request_input_real_format,
1941                   request_output_real_format,request_inputs,
1942                   cpid,m,eres);
1943  fflush(stdout);
1944  /**
1945   * Ensure that if error occurs when freeing memory, no signal will return
1946   * an ExceptionReport document as the result was already returned to the
1947   * client.
1948   */
1949#ifndef USE_GDB
1950  (void) signal(SIGSEGV,donothing);
1951  (void) signal(SIGTERM,donothing);
1952  (void) signal(SIGINT,donothing);
1953  (void) signal(SIGILL,donothing);
1954  (void) signal(SIGFPE,donothing);
1955  (void) signal(SIGABRT,donothing);
1956#endif
1957
1958  if(((int)getpid())!=cpid){
1959    fclose(stdout);
1960    fclose(stderr);
1961    unhandleStatus(m);
1962  }
1963
1964  freeService(&s1);
1965  free(s1);
1966  freeMaps(&m);
1967  free(m);
1968 
1969  freeMaps(&request_input_real_format);
1970  free(request_input_real_format);
1971 
1972  freeMaps(&request_output_real_format);
1973  free(request_output_real_format);
1974 
1975  free(REQUEST);
1976  free(SERVICE_URL);
1977#ifdef DEBUG
1978  fprintf(stderr,"Processed response \n");
1979  fflush(stdout);
1980  fflush(stderr);
1981#endif
1982
1983  return 0;
1984}
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