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

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

Fixe previous commit...

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