source: trunk/zoo-project/zoo-kernel/zoo_service_loader.c @ 471

Last change on this file since 471 was 471, checked in by djay, 10 years ago

Remove leaks from DescribeProcess? and JavaScript? support. Fix wrong ulinet update on previous commit. Use the new updateStatus/setOutputValue functions as described in #88 from longProcess. Add default value for QREncode service in ZCFG. Fix name of profile service in the ZCFG file.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 72.2 KB
RevLine 
[1]1/**
2 * Author : Gérald FENOY
3 *
[392]4 *  Copyright 2008-2013 GeoLabs SARL. All rights reserved.
[1]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();
[9]29
[376]30#include "cgic.h"
31
[1]32extern "C" {
33#include <libxml/tree.h>
34#include <libxml/xmlmemory.h>
35#include <libxml/parser.h>
36#include <libxml/xpath.h>
37#include <libxml/xpathInternals.h>
38}
39
40#include "ulinet.h"
41
[34]42#include <libintl.h>
43#include <locale.h>
[1]44#include <string.h>
45
46#include "service.h"
[34]47
[1]48#include "service_internal.h"
[33]49
50#ifdef USE_PYTHON
[1]51#include "service_internal_python.h"
[33]52#endif
[1]53
54#ifdef USE_JAVA
55#include "service_internal_java.h"
56#endif
57
58#ifdef USE_PHP
59#include "service_internal_php.h"
60#endif
61
62#ifdef USE_JS
63#include "service_internal_js.h"
64#endif
65
[453]66#ifdef USE_RUBY
67#include "service_internal_ruby.h"
68#endif
69
[25]70#ifdef USE_PERL
71#include "service_internal_perl.h"
72#endif
[1]73
74#include <dirent.h>
75#include <signal.h>
76#include <unistd.h>
77#ifndef WIN32
78#include <dlfcn.h>
79#include <libgen.h>
80#else
81#include <windows.h>
82#include <direct.h>
[364]83#include <sys/types.h>
84#include <sys/stat.h>
85#include <unistd.h>
86#define pid_t int;
[1]87#endif
88#include <fcntl.h>
89#include <time.h>
90#include <stdarg.h>
91
[364]92#ifdef WIN32
93extern "C" {
[370]94  __declspec(dllexport) char *strcasestr(char const *a, char const *b)
95#ifndef USE_MS
96 { 
[467]97  char *x=zStrdup(a); 
98  char *y=zStrdup(b); 
[370]99 
100  x=_strlwr(x); 
101  y=_strlwr(y); 
102  char *pos = strstr(x, y); 
103  char *ret = pos == NULL ? NULL : (char *)(a + (pos-x)); 
104  free(x); 
105  free(y); 
106  return ret; 
107 };
108#else
109  ;
110#endif
[364]111}
112#endif
113
[34]114#define _(String) dgettext ("zoo-kernel",String)
[376]115#define __(String) dgettext ("zoo-service",String)
[34]116
[467]117extern   int getServiceFromFile(maps*,const char*,service**);
[34]118
[467]119int readServiceFile(maps* conf, char* file,service** service,char *name){
120  int t=getServiceFromFile(conf,file,service);
121#ifdef YAML
122  if(t<0){
123    t=getServiceFromYAML(conf,file,service,name);
124  }
125#endif
126  return t;
127}
128
[109]129void translateChar(char* str,char toReplace,char toReplaceBy){
[34]130  int i=0,len=strlen(str);
131  for(i=0;i<len;i++){
132    if(str[i]==toReplace)
133      str[i]=toReplaceBy;
134  }
135}
136
[360]137/**
138 * Create (or append to) an array valued maps
139 * value = "["",""]"
140 */
141int appendMapsToMaps(maps* m,maps* mo,maps* mi,elements* elem){
[362]142  maps* tmpMaps=getMaps(mo,mi->name);
143  map* tmap=getMapType(tmpMaps->content);
144  elements* el=getElements(elem,mi->name);
145  int hasEl=1;
146  if(el==NULL)
147    hasEl=-1;
[360]148  if(tmap==NULL){
[362]149    if(hasEl>0)
150      tmap=getMapType(el->defaults->content);     
[360]151  }
152
[362]153  map* testMap=NULL;
154  if(hasEl>0){
155    testMap=getMap(el->content,"maxOccurs");
156  }else{
157    testMap=createMap("maxOccurs","unbounded");
158  }
159
[360]160  if(testMap!=NULL){
161    if(strncasecmp(testMap->value,"unbounded",9)!=0 && atoi(testMap->value)>1){
162      if(addMapsArrayToMaps(&mo,mi,tmap->name)<0){
163        char emsg[1024];
164        sprintf(emsg,_("You set maximum occurences for <%s> as %i but you tried to use it more than the limit you set. Please correct your ZCFG file or your request."),mi->name,atoi(testMap->value));
[459]165        errorException(m,emsg,"InternalError",NULL);
[360]166        return -1;
167      }
168    }else{
169      if(strncasecmp(testMap->value,"unbounded",9)==0){
[362]170        if(hasEl<0){
171          freeMap(&testMap);
172          free(testMap);
173        }
[360]174        if(addMapsArrayToMaps(&mo,mi,tmap->name)<0){
175          char emsg[1024];
176          map* tmpMap=getMap(mi->content,"length");
177          sprintf(emsg,_("ZOO-Kernel was unable to load your data for %s position %s."),mi->name,tmpMap->value);
[459]178          errorException(m,emsg,"InternalError",NULL);
[360]179          return -1;
180        }
181      }
182      else{
183        char emsg[1024];
184        sprintf(emsg,_("You set maximum occurences for <%s> to one but you tried to use it more than once. Please correct your ZCFG file or your request."),mi->name);
[459]185        errorException(m,emsg,"InternalError",NULL);
[360]186        return -1;
187      }
188    }
189  }
190  return 0;
191}
192
[469]193int recursReaddirF(maps* m,xmlNodePtr n,char *conf_dir,char *prefix,int saved_stdout,int level,void (func) (maps*,xmlNodePtr,service*)){
194  struct dirent *dp;
195  int scount=0;
196
197  if(conf_dir==NULL)
198    return 1;
199  DIR *dirp = opendir(conf_dir);
200  if(dirp==NULL){ 
201    dup2(saved_stdout,fileno(stdout));
202    errorException(m, _("The specified path doesn't exist."),"InvalidParameterValue","metapath");
203    return -1;
204  }
205  char tmp1[25];
206  sprintf(tmp1,"sprefix_%d",level);
207  char levels[17];
208  sprintf(levels,"%d",level);
209  setMapInMaps(m,"lenv","level",levels);
210  while ((dp = readdir(dirp)) != NULL)
211    if((dp->d_type==DT_DIR || dp->d_type==DT_LNK) && dp->d_name[0]!='.' && strstr(dp->d_name,".")==NULL){
212
213      char *tmp=(char*)malloc((strlen(conf_dir)+strlen(dp->d_name)+2)*sizeof(char));
214      sprintf(tmp,"%s/%s",conf_dir,dp->d_name);
215
216      if(prefix!=NULL){
217        free(prefix);
218        prefix=NULL;
219      }
220      prefix=(char*)malloc((strlen(dp->d_name)+2)*sizeof(char));
221      sprintf(prefix,"%s.",dp->d_name);
222     
223      map* tmpMap=getMapFromMaps(m,"lenv",tmp1);
224     
225      int res;
226      if(prefix!=NULL){
227        setMapInMaps(m,"lenv",tmp1,prefix);
228        char *cprefix=zStrdup(prefix);
229        char levels1[17];
230        sprintf(levels1,"%d",level+1);
231        setMapInMaps(m,"lenv","level",levels1);
232        res=recursReaddirF(m,n,tmp,cprefix,saved_stdout,level+1,func);
233        sprintf(levels1,"%d",level);
234        setMapInMaps(m,"lenv","level",levels1);
235        free(prefix);
236        prefix=NULL;
237      }
238      free(tmp);
239      if(res<0){
240        return res;
241      }
242    }
243    else{
244      char* tmp0=strdup(dp->d_name);
245      if(dp->d_name[0]!='.' && strstr(dp->d_name,".zcfg")!=0){
246        int t;
247        char tmps1[1024];
248        memset(tmps1,0,1024);
249        snprintf(tmps1,1024,"%s/%s",conf_dir,dp->d_name);
250        service* s1=(service*)malloc(SERVICE_SIZE);
251        if(s1 == NULL){ 
252          dup2(saved_stdout,fileno(stdout));
253          errorException(m, _("Unable to allocate memory."),"InternalError",NULL);
254          return -1;
255        }
256#ifdef DEBUG
257        fprintf(stderr,"#################\n%s\n#################\n",tmps1);
258#endif
259        t=readServiceFile(m,tmps1,&s1,dp->d_name);
260        if(t<0){
261          dumpMaps(m);
262          map* tmp00=getMapFromMaps(m,"lenv","message");
263          char tmp01[1024];
264          if(tmp00!=NULL)
265            sprintf(tmp01,_("Unable to parse the ZCFG file: %s (%s)"),dp->d_name,tmp00->value);
266          else
267            sprintf(tmp01,_("Unable to parse the ZCFG file: %s."),dp->d_name);
268          dup2(saved_stdout,fileno(stdout));
269          errorException(m, tmp01,"InternalError",NULL);
270          freeMaps(&m);
271          free(m);
272          return -1;
273        }
274#ifdef DEBUG
275        dumpService(s1);
276        fflush(stdout);
277        fflush(stderr);
278#endif
279        func(m,n,s1);
280        freeService(&s1);
281        free(s1);
282        scount++;
283      }
284    }
285  (void)closedir(dirp);
286  return 1;
287}
288
[114]289xmlXPathObjectPtr extractFromDoc(xmlDocPtr doc,const char* search){
[1]290  xmlXPathContextPtr xpathCtx;
291  xmlXPathObjectPtr xpathObj;
292  xpathCtx = xmlXPathNewContext(doc);
293  xpathObj = xmlXPathEvalExpression(BAD_CAST search,xpathCtx);
[9]294  xmlXPathFreeContext(xpathCtx);
295  return xpathObj;
[1]296}
297
[105]298void donothing(int sig){
299  fprintf(stderr,"Signal %d after the ZOO-Kernel returned result !\n",sig);
300  exit(0);
301}
302
[9]303void sig_handler(int sig){
304  char tmp[100];
[114]305  const char *ssig;
[9]306  switch(sig){
307  case SIGSEGV:
308    ssig="SIGSEGV";
309    break;
310  case SIGTERM:
311    ssig="SIGTERM";
312    break;
313  case SIGINT:
314    ssig="SIGINT";
315    break;
316  case SIGILL:
317    ssig="SIGILL";
318    break;
319  case SIGFPE:
320    ssig="SIGFPE";
321    break;
322  case SIGABRT:
323    ssig="SIGABRT";
324    break;
325  default:
326    ssig="UNKNOWN";
327    break;
328  }
[34]329  sprintf(tmp,_("ZOO Kernel failed to process your request receiving signal %d = %s"),sig,ssig);
[459]330  errorException(NULL, tmp, "InternalError",NULL);
[10]331#ifdef DEBUG
[1]332  fprintf(stderr,"Not this time!\n");
[10]333#endif
[9]334  exit(0);
[1]335}
336
[109]337void loadServiceAndRun(maps **myMap,service* s1,map* request_inputs,maps **inputs,maps** ioutputs,int* eres){
[34]338  char tmps1[1024];
339  char ntmp[1024];
340  maps *m=*myMap;
341  maps *request_output_real_format=*ioutputs;
342  maps *request_input_real_format=*inputs;
343  /**
344   * Extract serviceType to know what kind of service should be loaded
345   */
346  map* r_inputs=NULL;
347#ifndef WIN32
[465]348  getcwd(ntmp,1024);
[34]349#else
350  _getcwd(ntmp,1024);
351#endif
352  r_inputs=getMap(s1->content,"serviceType");
353#ifdef DEBUG
354  fprintf(stderr,"LOAD A %s SERVICE PROVIDER \n",r_inputs->value);
355  fflush(stderr);
356#endif
[364]357  if(strlen(r_inputs->value)==1 && strncasecmp(r_inputs->value,"C",1)==0){
[34]358    r_inputs=getMap(request_inputs,"metapath");
359    if(r_inputs!=NULL)
360      sprintf(tmps1,"%s/%s",ntmp,r_inputs->value);
361    else
362      sprintf(tmps1,"%s/",ntmp);
[453]363    char *altPath=zStrdup(tmps1);
[34]364    r_inputs=getMap(s1->content,"ServiceProvider");
365    sprintf(tmps1,"%s/%s",altPath,r_inputs->value);
366    free(altPath);
367#ifdef DEBUG
368    fprintf(stderr,"Trying to load %s\n",tmps1);
369#endif
370#ifdef WIN32
371    HINSTANCE so = LoadLibraryEx(tmps1,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
372#else
373    void* so = dlopen(tmps1, RTLD_LAZY);
374#endif
[57]375#ifdef WIN32
[34]376    DWORD errstr;
377    errstr = GetLastError();
378    fprintf(stderr,"%s loaded (%d) \n",tmps1,errstr);
379#else
380    char *errstr;
381    errstr = dlerror();
382#endif
383    if( so != NULL ) {
384#ifdef DEBUG
385      fprintf(stderr,"Library loaded %s \n",errstr);
386      fprintf(stderr,"Service Shared Object = %s\n",r_inputs->value);
387#endif
388      r_inputs=getMap(s1->content,"serviceType");
389#ifdef DEBUG
390      dumpMap(r_inputs);
391      fprintf(stderr,"%s\n",r_inputs->value);
392      fflush(stderr);
393#endif
394      if(strncasecmp(r_inputs->value,"C-FORTRAN",9)==0){
395        r_inputs=getMap(request_inputs,"Identifier");
396        char fname[1024];
397        sprintf(fname,"%s_",r_inputs->value);
398#ifdef DEBUG
399        fprintf(stderr,"Try to load function %s\n",fname);
400#endif
401#ifdef WIN32
402        typedef int (CALLBACK* execute_t)(char***,char***,char***);
403        execute_t execute=(execute_t)GetProcAddress(so,fname);
404#else
405        typedef int (*execute_t)(char***,char***,char***);
406        execute_t execute=(execute_t)dlsym(so,fname);
407#endif
408#ifdef DEBUG
409#ifdef WIN32
410        errstr = GetLastError();
411#else
412        errstr = dlerror();
413#endif
414        fprintf(stderr,"Function loaded %s\n",errstr);
415#endif 
416
417        char main_conf[10][30][1024];
418        char inputs[10][30][1024];
419        char outputs[10][30][1024];
420        for(int i=0;i<10;i++){
421          for(int j=0;j<30;j++){
422            memset(main_conf[i][j],0,1024);
423            memset(inputs[i][j],0,1024);
424            memset(outputs[i][j],0,1024);
425          }
426        }
427        mapsToCharXXX(m,(char***)main_conf);
428        mapsToCharXXX(request_input_real_format,(char***)inputs);
429        mapsToCharXXX(request_output_real_format,(char***)outputs);
430        *eres=execute((char***)&main_conf[0],(char***)&inputs[0],(char***)&outputs[0]);
431#ifdef DEBUG
432        fprintf(stderr,"Function run successfully \n");
433#endif
434        charxxxToMaps((char***)&outputs[0],&request_output_real_format);
435      }else{
436#ifdef DEBUG
437#ifdef WIN32
438        errstr = GetLastError();
439        fprintf(stderr,"Function %s failed to load because of %d\n",r_inputs->value,errstr);
440#endif
441#endif
[469]442        r_inputs=getMapFromMaps(m,"lenv","Identifier");
[34]443#ifdef DEBUG
444        fprintf(stderr,"Try to load function %s\n",r_inputs->value);
445#endif
446        typedef int (*execute_t)(maps**,maps**,maps**);
447#ifdef WIN32
448        execute_t execute=(execute_t)GetProcAddress(so,r_inputs->value); 
449#else
450        execute_t execute=(execute_t)dlsym(so,r_inputs->value);
451#endif
452
[469]453        if(execute==NULL){
454#ifdef WIN32
455          errstr = GetLastError();
456#else
457          errstr = dlerror();
458#endif
459          char *tmpMsg=(char*)malloc(2048+strlen(r_inputs->value));
460          sprintf(tmpMsg,_("Error occured while running the %s function: %s"),r_inputs->value,errstr);
461          errorException(m, tmpMsg, "InternalError",NULL);
462          free(tmpMsg);
463          fprintf(stderr,"Function %s error %s\n",r_inputs->value,errstr);
464          return;
465        }
466
[34]467#ifdef DEBUG
468#ifdef WIN32
469        errstr = GetLastError();
470#else
471        errstr = dlerror();
472#endif
473        fprintf(stderr,"Function loaded %s\n",errstr);
474#endif 
475
476#ifdef DEBUG
477        fprintf(stderr,"Now run the function \n");
478        fflush(stderr);
479#endif
480        *eres=execute(&m,&request_input_real_format,&request_output_real_format);
481#ifdef DEBUG
482        fprintf(stderr,"Function loaded and returned %d\n",eres);
483        fflush(stderr);
484#endif
485      }
[216]486#ifdef WIN32
487      *ioutputs=dupMaps(&request_output_real_format);
488      FreeLibrary(so);
489#else
[34]490      dlclose(so);
[216]491#endif
[34]492    } else {
493      /**
494       * Unable to load the specified shared library
495       */
496      char tmps[1024];
497#ifdef WIN32
498      DWORD errstr = GetLastError();
499#else
500      char* errstr = dlerror();
501#endif
[392]502      sprintf(tmps,_("C Library can't be loaded %s"),errstr);
[34]503      map* tmps1=createMap("text",tmps);
504      printExceptionReportResponse(m,tmps1);
505      *eres=-1;
[471]506      freeMap(&tmps1);
507      free(tmps1);
[34]508    }
509  }
510  else
511#ifdef USE_PYTHON
512    if(strncasecmp(r_inputs->value,"PYTHON",6)==0){
513      *eres=zoo_python_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
514    }
515    else
516#endif
517       
518#ifdef USE_JAVA
519      if(strncasecmp(r_inputs->value,"JAVA",4)==0){
520        *eres=zoo_java_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
521      }
522      else
523#endif
524
525#ifdef USE_PHP
526        if(strncasecmp(r_inputs->value,"PHP",3)==0){
527          *eres=zoo_php_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
528        }
529        else
530#endif
531           
532           
533#ifdef USE_PERL
534          if(strncasecmp(r_inputs->value,"PERL",4)==0){
535            *eres=zoo_perl_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
536          }
537          else
538#endif
539
540#ifdef USE_JS
541            if(strncasecmp(r_inputs->value,"JS",2)==0){
542              *eres=zoo_js_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
543            }
544            else
545#endif
[453]546
547#ifdef USE_RUBY
548          if(strncasecmp(r_inputs->value,"Ruby",4)==0){
549            *eres=zoo_ruby_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format);
550          }
551          else
552#endif
553
[34]554              {
555                char tmpv[1024];
556                sprintf(tmpv,_("Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n"),r_inputs->value);
557                map* tmps=createMap("text",tmpv);
558                printExceptionReportResponse(m,tmps);
559                *eres=-1;
560              }
[57]561  *myMap=m;
[34]562  *ioutputs=request_output_real_format;
563}
564
[384]565
[216]566#ifdef WIN32
567/**
568 * createProcess function: create a new process after setting some env variables
569 */
570void createProcess(maps* m,map* request_inputs,service* s1,char* opts,int cpid, maps* inputs,maps* outputs){
571  STARTUPINFO si;
572  PROCESS_INFORMATION pi;
573  ZeroMemory( &si, sizeof(si) );
574  si.cb = sizeof(si);
575  ZeroMemory( &pi, sizeof(pi) );
576  char *tmp=(char *)malloc((1024+cgiContentLength)*sizeof(char));
577  char *tmpq=(char *)malloc((1024+cgiContentLength)*sizeof(char));
578  map *req=getMap(request_inputs,"request");
579  map *id=getMap(request_inputs,"identifier");
580  map *di=getMap(request_inputs,"DataInputs");
581
582  char *dataInputsKVP=getMapsAsKVP(inputs,cgiContentLength,0);
583  char *dataOutputsKVP=getMapsAsKVP(outputs,cgiContentLength,1);
[384]584#ifdef DEBUG
[216]585  fprintf(stderr,"DATAINPUTSKVP %s\n",dataInputsKVP);
586  fprintf(stderr,"DATAOUTPUTSKVP %s\n",dataOutputsKVP);
[384]587#endif
[216]588  map *sid=getMapFromMaps(m,"lenv","sid");
589  map* r_inputs=getMapFromMaps(m,"main","tmpPath");
[384]590  map* r_inputs1=getMap(request_inputs,"metapath");
591  int hasIn=-1;
592  if(r_inputs1==NULL){
593    r_inputs1=createMap("metapath","");
594    hasIn=1;
595  }
[452]596  map* r_inputs2=getMap(request_inputs,"ResponseDocument");
[216]597  if(r_inputs2==NULL)
[452]598    r_inputs2=getMap(request_inputs,"RawDataOutput");
[216]599  map *tmpPath=getMapFromMaps(m,"lenv","cwd");
600
[458]601  map *tmpReq=getMap(request_inputs,"xrequest");
[216]602  if(r_inputs2!=NULL){
[452]603    sprintf(tmp,"\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s&cgiSid=%s\"",r_inputs1->value,req->value,id->value,dataInputsKVP,r_inputs2->name,dataOutputsKVP,sid->value);
[384]604    sprintf(tmpq,"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s",r_inputs1->value,req->value,id->value,dataInputsKVP,r_inputs2->name,dataOutputsKVP);
[458]605  }
[216]606  else{
[384]607    sprintf(tmp,"\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&cgiSid=%s\"",r_inputs1->value,req->value,id->value,dataInputsKVP,sid->value);
608    sprintf(tmpq,"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s",r_inputs1->value,req->value,id->value,dataInputsKVP,sid->value);
[216]609  }
[384]610 
611  if(hasIn>0){
612    freeMap(&r_inputs1);
613    free(r_inputs1);
614  }
[453]615  char *tmp1=zStrdup(tmp);
[458]616  sprintf(tmp,"\"zoo_loader.cgi\" %s \"%s\"",tmp1,sid->value);
[384]617 
[216]618  free(dataInputsKVP);
619  free(dataOutputsKVP);
[384]620#ifdef DEBUG
[216]621  fprintf(stderr,"REQUEST IS : %s \n",tmp);
[384]622#endif
[216]623  SetEnvironmentVariable("CGISID",TEXT(sid->value));
624  SetEnvironmentVariable("QUERY_STRING",TEXT(tmpq));
625  char clen[1000];
626  sprintf(clen,"%d",strlen(tmpq));
627  SetEnvironmentVariable("CONTENT_LENGTH",TEXT(clen));
[458]628 
[216]629  if( !CreateProcess( NULL,             // No module name (use command line)
630                      TEXT(tmp),        // Command line
631                      NULL,             // Process handle not inheritable
632                      NULL,             // Thread handle not inheritable
633                      FALSE,            // Set handle inheritance to FALSE
634                      CREATE_NO_WINDOW, // Apache won't wait until the end
635                      NULL,             // Use parent's environment block
636                      NULL,             // Use parent's starting directory
637                      &si,              // Pointer to STARTUPINFO struct
638                      &pi )             // Pointer to PROCESS_INFORMATION struct
639      ) 
640    { 
[384]641#ifdef DEBUG
[216]642      fprintf( stderr, "CreateProcess failed (%d).\n", GetLastError() );
[384]643#endif
[216]644      return ;
645    }else{
[384]646#ifdef DEBUG
[216]647    fprintf( stderr, "CreateProcess successfull (%d).\n\n\n\n", GetLastError() );
[384]648#endif
[216]649  }
650  CloseHandle( pi.hProcess );
651  CloseHandle( pi.hThread );
[384]652#ifdef DEBUG
[216]653  fprintf(stderr,"CreateProcess finished !\n");
[384]654#endif
[216]655}
656#endif
657
[1]658int runRequest(map* request_inputs)
659{
660
[53]661#ifndef USE_GDB
[9]662  (void) signal(SIGSEGV,sig_handler);
663  (void) signal(SIGTERM,sig_handler);
664  (void) signal(SIGINT,sig_handler);
665  (void) signal(SIGILL,sig_handler);
666  (void) signal(SIGFPE,sig_handler);
667  (void) signal(SIGABRT,sig_handler);
[53]668#endif
[9]669
[114]670  map* r_inputs=NULL;
[1]671  maps* m=NULL;
672
673  char* REQUEST=NULL;
674  /**
675   * Parsing service specfic configuration file
676   */
[381]677  m=(maps*)malloc(MAPS_SIZE);
[9]678  if(m == NULL){
[459]679    return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]680  }
[1]681  char ntmp[1024];
682#ifndef WIN32
[465]683  getcwd(ntmp,1024);
[1]684#else
685  _getcwd(ntmp,1024);
686#endif
[471]687  r_inputs=getMapOrFill(&request_inputs,"metapath","");
[282]688
[381]689
[9]690  char conf_file[10240];
691  snprintf(conf_file,10240,"%s/%s/main.cfg",ntmp,r_inputs->value);
[385]692  if(conf_read(conf_file,m)==2){
[459]693    errorException(NULL, _("Unable to load the main.cfg file."),"InternalError",NULL);
[385]694    free(m);
695    return 1;
696  }
[9]697#ifdef DEBUG
698  fprintf(stderr, "***** BEGIN MAPS\n"); 
699  dumpMaps(m);
700  fprintf(stderr, "***** END MAPS\n");
701#endif
702
[376]703  map *getPath=getMapFromMaps(m,"main","gettextPath");
704  if(getPath!=NULL){
705    bindtextdomain ("zoo-kernel",getPath->value);
706    bindtextdomain ("zoo-services",getPath->value);   
707  }else{
708    bindtextdomain ("zoo-kernel","/usr/share/locale/");
709    bindtextdomain ("zoo-services","/usr/share/locale/");
710  }
[364]711
[381]712
[364]713  /**
714   * Manage our own error log file (usefull to separate standard apache debug
715   * messages from the ZOO-Kernel ones but also for IIS users to avoid wrong
716   * headers messages returned by the CGI due to wrong redirection of stderr)
717   */
718  FILE * fstde=NULL;
719  map* fstdem=getMapFromMaps(m,"main","logPath");
720  if(fstdem!=NULL)
[458]721    fstde = freopen(fstdem->value, "a+", stderr) ;
[364]722
[376]723  r_inputs=getMap(request_inputs,"language");
724  if(r_inputs==NULL)
725    r_inputs=getMapFromMaps(m,"main","language");
726  if(r_inputs!=NULL){
[453]727    char *tmp=zStrdup(r_inputs->value);
[376]728    setMapInMaps(m,"main","language",tmp);
[466]729#ifdef DEB
730    char tmp2[12];
731    sprintf(tmp2,"%s.utf-8",tmp);
732    translateChar(tmp2,'-','_');
733    setlocale (LC_ALL, tmp2);
734#else
[34]735    translateChar(tmp,'-','_');
736    setlocale (LC_ALL, tmp);
[466]737#endif
[444]738#ifndef WIN32
739    setenv("LC_ALL",tmp,1);
740#else
[376]741    char tmp1[12];
742    sprintf(tmp1,"LC_ALL=%s",tmp);
743    putenv(tmp1);
744#endif
[34]745    free(tmp);
746  }
747  else{
748    setlocale (LC_ALL, "en_US");
[444]749#ifndef WIN32
750    setenv("LC_ALL","en_US",1);
751#else
[376]752    char tmp1[12];
753    sprintf(tmp1,"LC_ALL=en_US");
754    putenv(tmp1);
755#endif
[34]756    setMapInMaps(m,"main","language","en-US");
757  }
758  setlocale (LC_NUMERIC, "en_US");
759  bind_textdomain_codeset("zoo-kernel","UTF-8");
760  textdomain("zoo-kernel");
761  bind_textdomain_codeset("zoo-services","UTF-8");
762  textdomain("zoo-services");
763
[280]764  map* lsoap=getMap(request_inputs,"soap");
765  if(lsoap!=NULL && strcasecmp(lsoap->value,"true")==0)
766    setMapInMaps(m,"main","isSoap","true");
767  else
768    setMapInMaps(m,"main","isSoap","false");
[34]769
[445]770  if(strlen(cgiServerName)>0){
771    char tmpUrl[1024];
772    sprintf(tmpUrl,"http://%s%s",cgiServerName,cgiScriptName);
773#ifdef DEBUG
774    fprintf(stderr,"*** %s ***\n",tmpUrl);
775#endif
776    setMapInMaps(m,"main","serverAddress",tmpUrl);
777  }
[381]778
[1]779  /**
780   * Check for minimum inputs
781   */
782  r_inputs=getMap(request_inputs,"Request");
[9]783  if(request_inputs==NULL || r_inputs==NULL){ 
[459]784    errorException(m, _("Parameter <request> was not specified"),"MissingParameterValue","request");
[9]785    freeMaps(&m);
786    free(m);
[471]787    if(request_inputs!=NULL){
788      freeMap(&request_inputs);
789      free(request_inputs);
790    }
[1]791    return 1;
792  }
[9]793  else{
[453]794    REQUEST=zStrdup(r_inputs->value);
[9]795    if(strncasecmp(r_inputs->value,"GetCapabilities",15)!=0
796       && strncasecmp(r_inputs->value,"DescribeProcess",15)!=0
797       && strncasecmp(r_inputs->value,"Execute",7)!=0){ 
[459]798      errorException(m, _("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), "InvalidParameterValue","request");
[9]799      freeMaps(&m);
800      free(m);
801      free(REQUEST);
802      return 1;
803    }
804  }
[1]805  r_inputs=NULL;
806  r_inputs=getMap(request_inputs,"Service");
[9]807  if(r_inputs==NULLMAP){
[459]808    errorException(m, _("Parameter <service> was not specified"),"MissingParameterValue","service");
[9]809    freeMaps(&m);
810    free(m);
811    free(REQUEST);
[1]812    return 1;
[459]813  }else{
814    if(strcasecmp(r_inputs->value,"WPS")!=0){
815      errorException(m, _("Unenderstood <service> value, WPS is the only acceptable value."), "InvalidParameterValue","service");
816      freeMaps(&m);
817      free(m);
818      free(REQUEST);
819      return 1;
820    }
[1]821  }
[9]822  if(strncasecmp(REQUEST,"GetCapabilities",15)!=0){
[1]823    r_inputs=getMap(request_inputs,"Version");
824    if(r_inputs==NULL){ 
[459]825      errorException(m, _("Parameter <version> was not specified"),"MissingParameterValue","version");
[9]826      freeMaps(&m);
827      free(m);
828      free(REQUEST);
[1]829      return 1;
[459]830    }else{
831      if(strcasecmp(r_inputs->value,"1.0.0")!=0){
832        errorException(m, _("Unenderstood <version> value, 1.0.0 is the only acceptable value."), "InvalidParameterValue","service");
833        freeMaps(&m);
834        free(m);
835        free(REQUEST);
836        return 1;
837      }
838    } 
[1]839  }
840
[9]841  r_inputs=getMap(request_inputs,"serviceprovider");
842  if(r_inputs==NULL){
843    addToMap(request_inputs,"serviceprovider","");
[1]844  }
845
846  maps* request_output_real_format=NULL;
847  map* tmpm=getMapFromMaps(m,"main","serverAddress");
848  if(tmpm!=NULL)
[453]849    SERVICE_URL=zStrdup(tmpm->value);
[1]850  else
[453]851    SERVICE_URL=zStrdup(DEFAULT_SERVICE_URL);
[1]852
853  service* s1;
854  int scount=0;
855#ifdef DEBUG
856  dumpMap(r_inputs);
857#endif
858  char conf_dir[1024];
859  int t;
860  char tmps1[1024];
861
[9]862  r_inputs=NULL;
863  r_inputs=getMap(request_inputs,"metapath");
864  if(r_inputs!=NULL)
865    snprintf(conf_dir,1024,"%s/%s",ntmp,r_inputs->value);
866  else
867    snprintf(conf_dir,1024,"%s",ntmp);
868
869  if(strncasecmp(REQUEST,"GetCapabilities",15)==0){
[1]870    struct dirent *dp;
871#ifdef DEBUG
872    dumpMap(r_inputs);
873#endif
874    xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
875    r_inputs=NULL;
876    r_inputs=getMap(request_inputs,"ServiceProvider");
[9]877    xmlNodePtr n;
878    if(r_inputs!=NULL)
879      n = printGetCapabilitiesHeader(doc,r_inputs->value,m);
880    else
881      n = printGetCapabilitiesHeader(doc,"",m);
[1]882    /**
[214]883     * Here we need to close stdout to ensure that not supported chars
884     * has been found in the zcfg and then printed on stdout
[1]885     */
886    int saved_stdout = dup(fileno(stdout));
887    dup2(fileno(stderr),fileno(stdout));
[469]888    if(int res=recursReaddirF(m,n,conf_dir,NULL,saved_stdout,0,printGetCapabilitiesForProcess)<0)
889      return res;
[1]890    dup2(saved_stdout,fileno(stdout));
[9]891    printDocument(m,doc,getpid());
892    freeMaps(&m);
893    free(m);
894    free(REQUEST);
895    free(SERVICE_URL);
[1]896    fflush(stdout);
897    return 0;
898  }
899  else{
900    r_inputs=getMap(request_inputs,"Identifier");
901    if(r_inputs==NULL 
902       || strlen(r_inputs->name)==0 || strlen(r_inputs->value)==0){ 
[459]903      errorException(m, _("Mandatory <identifier> was not specified"),"MissingParameterValue","identifier");
[9]904      freeMaps(&m);
905      free(m);
906      free(REQUEST);
907      free(SERVICE_URL);
908      return 0;
[1]909    }
910
911    struct dirent *dp;
912    DIR *dirp = opendir(conf_dir);
913    if(dirp==NULL){
[459]914      errorException(m, _("The specified path path doesn't exist."),"InvalidParameterValue",conf_dir);
[9]915      freeMaps(&m);
916      free(m);
917      free(REQUEST);
918      free(SERVICE_URL);
919      return 0;
[1]920    }
[9]921    if(strncasecmp(REQUEST,"DescribeProcess",15)==0){
[1]922      /**
923       * Loop over Identifier list
924       */
925      xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
926      r_inputs=NULL;
927      r_inputs=getMap(request_inputs,"ServiceProvider");
[9]928
[1]929      xmlNodePtr n;
930      if(r_inputs!=NULL)
931        n = printDescribeProcessHeader(doc,r_inputs->value,m);
[9]932      else
933        n = printDescribeProcessHeader(doc,"",m);
[1]934
935      r_inputs=getMap(request_inputs,"Identifier");
936     
[465]937      char *orig=zStrdup(r_inputs->value);
[469]938
[1]939      int saved_stdout = dup(fileno(stdout));
940      dup2(fileno(stderr),fileno(stdout));
[469]941      if(strcasecmp("all",orig)==0){
942        if(int res=recursReaddirF(m,n,conf_dir,NULL,saved_stdout,0,printDescribeProcessForProcess)<0)
943          return res;
944      }
945      else{
946        char *saveptr;
947        char *tmps=strtok_r(orig,",",&saveptr);
948       
949        char buff[256];
950        char buff1[1024];
951        while(tmps!=NULL){
952          char *corig=strdup(tmps);
953          if(strstr(corig,".")!=NULL){
954            parseIdentifier(m,conf_dir,corig,buff1);
[381]955            s1=(service*)malloc(SERVICE_SIZE);
[469]956            t=readServiceFile(m,buff1,&s1,corig);
[465]957            if(t<0){
958              map* tmp00=getMapFromMaps(m,"lenv","message");
959              char tmp01[1024];
960              if(tmp00!=NULL)
961                sprintf(tmp01,_("Unable to parse the ZCFG file: %s (%s)"),dp->d_name,tmp00->value);
962              else
963                sprintf(tmp01,_("Unable to parse the ZCFG file: %s."),dp->d_name);
964              dup2(saved_stdout,fileno(stdout));
965              errorException(m, tmp01,"InternalError",NULL);
966              freeMaps(&m);
967              free(m);
968              return 1;
969            }
[9]970#ifdef DEBUG
971            dumpService(s1);
972#endif
[469]973            printDescribeProcessForProcess(m,n,s1);
[9]974            freeService(&s1);
975            free(s1);
[465]976            s1=NULL;
[1]977            scount++;
[469]978            setMapInMaps(m,"lenv","level","0");
[1]979          }
[471]980          free(corig);
[469]981         
982          memset(buff,0,256);
983          snprintf(buff,256,"%s.zcfg",tmps);
984          memset(buff1,0,1024);
985#ifdef DEBUG
986          printf("\n#######%s\n########\n",buff);
987#endif
988          while ((dp = readdir(dirp)) != NULL)
989            if( (strcasecmp("all.zcfg",buff)==0 && strstr(dp->d_name,".zcfg")>0)
990                || strcasecmp(dp->d_name,buff)==0 ){
991              memset(buff1,0,1024);
992              snprintf(buff1,1024,"%s/%s",conf_dir,dp->d_name);
993              s1=(service*)malloc(SERVICE_SIZE);
994              if(s1 == NULL){
995                dup2(saved_stdout,fileno(stdout));
996                return errorException(m, _("Unable to allocate memory."),"InternalError",NULL);
997              }
998#ifdef DEBUG
999              printf("#################\n(%s) %s\n#################\n",r_inputs->value,buff1);
1000#endif
1001              char *tmp0=zStrdup(dp->d_name);
1002              tmp0[strlen(tmp0)-5]=0;
1003              t=readServiceFile(m,buff1,&s1,tmp0);
1004              free(tmp0);
1005              if(t<0){
1006                map* tmp00=getMapFromMaps(m,"lenv","message");
1007                char tmp01[1024];
1008                if(tmp00!=NULL)
1009                  sprintf(tmp01,_("Unable to parse the ZCFG file: %s (%s)"),dp->d_name,tmp00->value);
1010                else
1011                  sprintf(tmp01,_("Unable to parse the ZCFG file: %s."),dp->d_name);
1012                dup2(saved_stdout,fileno(stdout));
1013                errorException(m, tmp01,"InternalError",NULL);
1014                freeMaps(&m);
1015                free(m);
1016                return 1;
1017              }
1018#ifdef DEBUG
1019              dumpService(s1);
1020#endif
1021              printDescribeProcessForProcess(m,n,s1);
1022              freeService(&s1);
1023              free(s1);
1024              s1=NULL;
1025              scount++;
1026            }
1027          rewinddir(dirp);
1028          tmps=strtok_r(NULL,",",&saveptr);
1029        }
[1]1030      }
[9]1031      closedir(dirp);
[1]1032      fflush(stdout);
1033      dup2(saved_stdout,fileno(stdout));
[465]1034      free(orig);
[9]1035      printDocument(m,doc,getpid());
1036      freeMaps(&m);
1037      free(m);
1038      free(REQUEST);
1039      free(SERVICE_URL);
[1]1040      fflush(stdout);
1041      return 0;
1042    }
1043    else
[9]1044      if(strncasecmp(REQUEST,"Execute",strlen(REQUEST))!=0){
[459]1045        errorException(m, _("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), "InvalidParameterValue","request");
[1]1046#ifdef DEBUG
1047        fprintf(stderr,"No request found %s",REQUEST);
1048#endif 
[9]1049        closedir(dirp);
[385]1050        freeMaps(&m);
1051        free(m);
1052        free(REQUEST);
1053        free(SERVICE_URL);
1054        fflush(stdout);
[1]1055        return 0;
1056      }
[9]1057    closedir(dirp);
[1]1058  }
1059 
1060  s1=NULL;
[381]1061  s1=(service*)malloc(SERVICE_SIZE);
[9]1062  if(s1 == NULL){
[32]1063    freeMaps(&m);
1064    free(m);
1065    free(REQUEST);
1066    free(SERVICE_URL);
[459]1067    return errorException(m, _("Unable to allocate memory."),"InternalError",NULL);
[9]1068  }
[1]1069  r_inputs=getMap(request_inputs,"MetaPath");
[9]1070  if(r_inputs!=NULL)
1071    snprintf(tmps1,1024,"%s/%s",ntmp,r_inputs->value);
1072  else
1073    snprintf(tmps1,1024,"%s/",ntmp);
[1]1074  r_inputs=getMap(request_inputs,"Identifier");
[453]1075  char *ttmp=zStrdup(tmps1);
[9]1076  snprintf(tmps1,1024,"%s/%s.zcfg",ttmp,r_inputs->value);
1077  free(ttmp);
[1]1078#ifdef DEBUG
1079  fprintf(stderr,"Trying to load %s\n", tmps1);
1080#endif
[469]1081  if(strstr(r_inputs->value,".")!=NULL){
1082    char *identifier=zStrdup(r_inputs->value);
1083    parseIdentifier(m,conf_dir,identifier,tmps1);
1084    map* tmpMap=getMapFromMaps(m,"lenv","metapath");
1085    if(tmpMap!=NULL)
1086      addToMap(request_inputs,"metapath",tmpMap->value);
1087    free(identifier);
1088  }else
1089    setMapInMaps(m,"lenv","Identifier",r_inputs->value);
[1]1090  int saved_stdout = dup(fileno(stdout));
[331]1091  dup2(fileno(stderr),fileno(stdout));
[465]1092  t=readServiceFile(m,tmps1,&s1,r_inputs->value);
[1]1093  fflush(stdout);
1094  dup2(saved_stdout,fileno(stdout));
[32]1095  if(t<0){
[216]1096    char *tmpMsg=(char*)malloc(2048+strlen(r_inputs->value));
[34]1097    sprintf(tmpMsg,_("The value for <indetifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."),r_inputs->value);
[459]1098    errorException(m, tmpMsg, "InvalidParameterValue","identifier");
[216]1099    free(tmpMsg);
[32]1100    free(s1);
1101    freeMaps(&m);
1102    free(m);
1103    free(REQUEST);
1104    free(SERVICE_URL);
1105    return 0;
[1]1106  }
[258]1107  close(saved_stdout);
[1]1108
1109#ifdef DEBUG
1110  dumpService(s1);
1111#endif
1112  int j;
1113 
[381]1114
[1]1115  /**
[344]1116   * Create the input and output maps data structure
[1]1117   */
1118  int i=0;
1119  HINTERNET hInternet;
1120  HINTERNET res;
1121  hInternet=InternetOpen(
1122#ifndef WIN32
1123                         (LPCTSTR)
1124#endif
1125                         "ZooWPSClient\0",
1126                         INTERNET_OPEN_TYPE_PRECONFIG,
1127                         NULL,NULL, 0);
1128
1129#ifndef WIN32
1130  if(!CHECK_INET_HANDLE(hInternet))
1131    fprintf(stderr,"WARNING : hInternet handle failed to initialize");
1132#endif
1133  maps* request_input_real_format=NULL;
1134  maps* tmpmaps = request_input_real_format;
1135  map* postRequest=NULL;
1136  postRequest=getMap(request_inputs,"xrequest");
1137  if(postRequest==NULLMAP){
1138    /**
1139     * Parsing outputs provided as KVP
1140     */
1141    r_inputs=NULL;
1142#ifdef DEBUG
1143    fprintf(stderr,"OUTPUT Parsing ... \n");
1144#endif
1145    r_inputs=getMap(request_inputs,"ResponseDocument"); 
[9]1146    if(r_inputs==NULL) r_inputs=getMap(request_inputs,"RawDataOutput");
1147   
[32]1148#ifdef DEBUG
[1]1149    fprintf(stderr,"OUTPUT Parsing ... \n");
[32]1150#endif
[9]1151    if(r_inputs!=NULL){
[32]1152#ifdef DEBUG
[1]1153      fprintf(stderr,"OUTPUT Parsing start now ... \n");
[32]1154#endif
[1]1155      char cursor_output[10240];
[453]1156      char *cotmp=zStrdup(r_inputs->value);
[9]1157      snprintf(cursor_output,10240,"%s",cotmp);
1158      free(cotmp);
[1]1159      j=0;
1160       
1161      /**
1162       * Put each Output into the outputs_as_text array
1163       */
1164      char * pToken;
1165      maps* tmp_output=NULL;
1166#ifdef DEBUG
1167      fprintf(stderr,"OUTPUT [%s]\n",cursor_output);
1168#endif
1169      pToken=strtok(cursor_output,";");
[381]1170      char** outputs_as_text=(char**)malloc(128*sizeof(char*));
[9]1171      if(outputs_as_text == NULL) {
[459]1172        return errorException(m, _("Unable to allocate memory"), "InternalError",NULL);
[9]1173      }
[1]1174      i=0;
1175      while(pToken!=NULL){
1176#ifdef DEBUG
1177        fprintf(stderr,"***%s***\n",pToken);
1178        fflush(stderr);
1179        fprintf(stderr,"***%s***\n",pToken);
1180#endif
[381]1181        outputs_as_text[i]=(char*)malloc((strlen(pToken)+1)*sizeof(char));
[9]1182        if(outputs_as_text[i] == NULL) {
[459]1183          return errorException(m, _("Unable to allocate memory"), "InternalError",NULL);
[9]1184        }
1185        snprintf(outputs_as_text[i],strlen(pToken)+1,"%s",pToken);
[1]1186        pToken = strtok(NULL,";");
1187        i++;
1188      }
1189      for(j=0;j<i;j++){
[453]1190        char *tmp=zStrdup(outputs_as_text[j]);
[9]1191        free(outputs_as_text[j]);
[1]1192        char *tmpc;
1193        tmpc=strtok(tmp,"@");
1194        int k=0;
1195        while(tmpc!=NULL){
1196          if(k==0){
1197            if(tmp_output==NULL){
[381]1198              tmp_output=(maps*)malloc(MAPS_SIZE);
[9]1199              if(tmp_output == NULL){
[459]1200                return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1201              }
[453]1202              tmp_output->name=zStrdup(tmpc);
[1]1203              tmp_output->content=NULL;
1204              tmp_output->next=NULL;
1205            }
1206          }
1207          else{
1208            char *tmpv=strstr(tmpc,"=");
1209            char tmpn[256];
1210            memset(tmpn,0,256);
1211            strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
1212            tmpn[strlen(tmpc)-strlen(tmpv)]=0;
1213#ifdef DEBUG
1214            fprintf(stderr,"OUTPUT DEF [%s]=[%s]\n",tmpn,tmpv+1);
1215#endif
1216            if(tmp_output->content==NULL){
1217              tmp_output->content=createMap(tmpn,tmpv+1);
1218              tmp_output->content->next=NULL;
1219            }
1220            else
1221              addToMap(tmp_output->content,tmpn,tmpv+1);
1222          }
1223          k++;
1224#ifdef DEBUG
1225          fprintf(stderr,"***%s***\n",tmpc);
1226#endif
1227          tmpc=strtok(NULL,"@");
1228        }
1229        if(request_output_real_format==NULL)
[9]1230          request_output_real_format=dupMaps(&tmp_output);
[1]1231        else
1232          addMapsToMaps(&request_output_real_format,tmp_output);
[9]1233        freeMaps(&tmp_output);
1234        free(tmp_output);
1235        tmp_output=NULL;
[1]1236#ifdef DEBUG
1237        dumpMaps(tmp_output);
1238        fflush(stderr);
1239#endif
[9]1240        free(tmp);
[1]1241      }
[9]1242      free(outputs_as_text);
[1]1243    }
1244
1245
1246    /**
1247     * Parsing inputs provided as KVP
1248     */
1249    r_inputs=getMap(request_inputs,"DataInputs");
1250#ifdef DEBUG
1251    fprintf(stderr,"DATA INPUTS [%s]\n",r_inputs->value);
1252#endif
1253    char cursor_input[40960];
[9]1254    if(r_inputs!=NULL)
1255      snprintf(cursor_input,40960,"%s",r_inputs->value);
1256    else{
[459]1257      errorException(m, _("Parameter <DataInputs> was not specified"),"MissingParameterValue","DataInputs");
[9]1258      freeMaps(&m);
1259      free(m);
1260      free(REQUEST);
1261      free(SERVICE_URL);
[57]1262      InternetCloseHandle(hInternet);
1263      freeService(&s1);
1264      free(s1);
[9]1265      return 0;
1266    }
[1]1267    j=0;
1268 
1269    /**
1270     * Put each DataInputs into the inputs_as_text array
1271     */
[453]1272    char *tmp1=zStrdup(cursor_input);
[1]1273    char * pToken;
1274    pToken=strtok(cursor_input,";");
[328]1275    if(pToken!=NULL && strncasecmp(pToken,tmp1,strlen(tmp1))==0){
1276      char* tmp2=url_decode(tmp1);
1277      snprintf(cursor_input,(strlen(tmp2)+1)*sizeof(char),"%s",tmp2);
1278      free(tmp2);
1279      pToken=strtok(cursor_input,";");
1280    }
1281    free(tmp1);
1282
[381]1283    char** inputs_as_text=(char**)malloc(100*sizeof(char*));
[9]1284    if(inputs_as_text == NULL){
[459]1285      return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1286    }
[1]1287    i=0;
1288    while(pToken!=NULL){
1289#ifdef DEBUG
1290      fprintf(stderr,"***%s***\n",pToken);
1291#endif
1292      fflush(stderr);
1293#ifdef DEBUG
1294      fprintf(stderr,"***%s***\n",pToken);
1295#endif
[381]1296      inputs_as_text[i]=(char*)malloc((strlen(pToken)+1)*sizeof(char));
[9]1297      snprintf(inputs_as_text[i],strlen(pToken)+1,"%s",pToken);
1298      if(inputs_as_text[i] == NULL){
[459]1299        return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1300      }
[1]1301      pToken = strtok(NULL,";");
1302      i++;
1303    }
1304
1305    for(j=0;j<i;j++){
[453]1306      char *tmp=zStrdup(inputs_as_text[j]);
[9]1307      free(inputs_as_text[j]);
[1]1308      char *tmpc;
1309      tmpc=strtok(tmp,"@");
1310      while(tmpc!=NULL){
1311#ifdef DEBUG
1312        fprintf(stderr,"***\n***%s***\n",tmpc);
1313#endif
1314        char *tmpv=strstr(tmpc,"=");
1315        char tmpn[256];
1316        memset(tmpn,0,256);
[216]1317        if(tmpv!=NULL){
1318          strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
1319          tmpn[strlen(tmpc)-strlen(tmpv)]=0;
1320        }
1321        else{
1322          strncpy(tmpn,tmpc,strlen(tmpc)*sizeof(char));
1323          tmpn[strlen(tmpc)]=0;
1324        }
[1]1325#ifdef DEBUG
1326        fprintf(stderr,"***\n*** %s = %s ***\n",tmpn,tmpv+1);
1327#endif
1328        if(tmpmaps==NULL){
[381]1329          tmpmaps=(maps*)malloc(MAPS_SIZE);
[9]1330          if(tmpmaps == NULL){
[459]1331            return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1332          }
[453]1333          tmpmaps->name=zStrdup(tmpn);
[344]1334          if(tmpv!=NULL){
1335            char *tmpvf=url_decode(tmpv+1);
1336            tmpmaps->content=createMap("value",tmpvf);
1337            free(tmpvf);
1338          }
[216]1339          else
1340            tmpmaps->content=createMap("value","Reference");
[1]1341          tmpmaps->next=NULL;
1342        }
1343        tmpc=strtok(NULL,"@");
1344        while(tmpc!=NULL){
1345#ifdef DEBUG
1346          fprintf(stderr,"*** KVP NON URL-ENCODED \n***%s***\n",tmpc);
1347#endif
1348          char *tmpv1=strstr(tmpc,"=");
1349#ifdef DEBUG
1350          fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1);
1351#endif
1352          char tmpn1[1024];
1353          memset(tmpn1,0,1024);
[216]1354          if(tmpv1!=NULL){
1355            strncpy(tmpn1,tmpc,strlen(tmpc)-strlen(tmpv1));
1356            tmpn1[strlen(tmpc)-strlen(tmpv1)]=0;
1357            addToMap(tmpmaps->content,tmpn1,tmpv1+1);
1358          }
1359          else{
1360            strncpy(tmpn1,tmpc,strlen(tmpc));
1361            tmpn1[strlen(tmpc)]=0;
1362            map* lmap=getLastMap(tmpmaps->content);
[381]1363            char *tmpValue=(char*)malloc((strlen(tmpv)+strlen(tmpc)+1)*sizeof(char));
[365]1364            sprintf(tmpValue,"%s@%s",tmpv+1,tmpc);
[216]1365            free(lmap->value);
[453]1366            lmap->value=zStrdup(tmpValue);
[216]1367            free(tmpValue);
1368            tmpc=strtok(NULL,"@");
1369            continue;
1370          }
[1]1371#ifdef DEBUG
1372          fprintf(stderr,"*** NAME NON URL-ENCODED \n***%s***\n",tmpn1);
1373          fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1);
1374#endif
1375          if(strcmp(tmpn1,"xlink:href")!=0)
1376            addToMap(tmpmaps->content,tmpn1,tmpv1+1);
[216]1377          else
1378            if(tmpv1!=NULL){
[328]1379              char *tmpx2=url_decode(tmpv1+1);
1380              if(strncasecmp(tmpx2,"http://",7)!=0 &&
[453]1381                 strncasecmp(tmpx2,"ftp://",6)!=0 &&
1382                 strncasecmp(tmpx2,"https://",8)!=0){
[216]1383                char emsg[1024];
1384                sprintf(emsg,_("Unable to find a valid protocol to download the remote file %s"),tmpv1+1);
[459]1385                errorException(m,emsg,"InternalError",NULL);
[216]1386                freeMaps(&m);
1387                free(m);
1388                free(REQUEST);
1389                free(SERVICE_URL);
1390                InternetCloseHandle(hInternet);
1391                freeService(&s1);
1392                free(s1);
1393                return 0;
1394              }
[1]1395#ifdef DEBUG
[216]1396              fprintf(stderr,"REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",tmpv1+1);
[1]1397#endif
[328]1398              addToMap(tmpmaps->content,tmpn1,tmpx2);
[280]1399             
[1]1400#ifndef WIN32
[216]1401              if(CHECK_INET_HANDLE(hInternet))
[1]1402#endif
[216]1403                {
[471]1404                  if(loadRemoteFile(&m,&tmpmaps->content,hInternet,tmpx2)<0){
[368]1405                    freeMaps(&m);
1406                    free(m);
1407                    free(REQUEST);
1408                    free(SERVICE_URL);
1409                    InternetCloseHandle(hInternet);
1410                    freeService(&s1);
1411                    free(s1);
1412                    return 0;
1413                  }
[58]1414                }
[328]1415              free(tmpx2);
[216]1416              addToMap(tmpmaps->content,"Reference",tmpv1+1);
1417            }
[1]1418          tmpc=strtok(NULL,"@");
1419        }
1420#ifdef DEBUG
1421        dumpMaps(tmpmaps);
1422        fflush(stderr);
1423#endif
[9]1424        if(request_input_real_format==NULL)
1425          request_input_real_format=dupMaps(&tmpmaps);
[360]1426        else{
1427          maps* testPresence=getMaps(request_input_real_format,tmpmaps->name);
1428          if(testPresence!=NULL){
1429            elements* elem=getElements(s1->inputs,tmpmaps->name);
1430            if(elem!=NULL){
1431              if(appendMapsToMaps(m,request_input_real_format,tmpmaps,elem)<0){
1432                freeMaps(&m);
1433                free(m);
1434                free(REQUEST);
1435                free(SERVICE_URL);
1436                InternetCloseHandle(hInternet);
1437                freeService(&s1);
1438                free(s1);
1439                return 0;
1440              }
1441            }
1442          }
1443          else
1444            addMapsToMaps(&request_input_real_format,tmpmaps);
1445        }
[9]1446        freeMaps(&tmpmaps);
1447        free(tmpmaps);
1448        tmpmaps=NULL;
1449        free(tmp);
[1]1450      }
1451    }
[9]1452    free(inputs_as_text);
1453  }
[1]1454  else {
[9]1455    /**
1456     * Parse XML request
1457     */ 
[1]1458    xmlInitParser();
1459#ifdef DEBUG
1460    fflush(stderr);
1461    fprintf(stderr,"BEFORE %s\n",postRequest->value);
1462    fflush(stderr);
1463#endif
1464    xmlDocPtr doc =
[5]1465      xmlParseMemory(postRequest->value,cgiContentLength);
[1]1466#ifdef DEBUG
1467    fprintf(stderr,"AFTER\n");
1468    fflush(stderr);
1469#endif
1470    /**
1471     * Parse every Input in DataInputs node.
1472     */
[9]1473    xmlXPathObjectPtr tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='Input']");
1474    xmlNodeSet* tmps=tmpsptr->nodesetval;
[1]1475#ifdef DEBUG
1476    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1477#endif
1478    for(int k=0;k<tmps->nodeNr;k++){
1479      maps *tmpmaps=NULL;
1480      xmlNodePtr cur=tmps->nodeTab[k];
1481      if(tmps->nodeTab[k]->type == XML_ELEMENT_NODE) {
1482        /**
1483         * A specific Input node.
1484         */
1485#ifdef DEBUG
1486        fprintf(stderr, "= element 0 node \"%s\"\n", cur->name);
1487#endif
[9]1488        xmlNodePtr cur2=cur->children;
[32]1489        while(cur2!=NULL){
1490          while(cur2!=NULL && cur2->type!=XML_ELEMENT_NODE)
[9]1491            cur2=cur2->next;
[32]1492          if(cur2==NULL)
1493            break;
[9]1494          /**
1495           * Indentifier
1496           */
1497          if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
1498            xmlChar *val= xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1499            if(tmpmaps==NULL){
[381]1500              tmpmaps=(maps*)malloc(MAPS_SIZE);
[9]1501              if(tmpmaps == NULL){
[459]1502                return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[1]1503              }
[453]1504              tmpmaps->name=zStrdup((char*)val);
[9]1505              tmpmaps->content=NULL;
1506              tmpmaps->next=NULL;
[1]1507            }
[9]1508            xmlFree(val);
1509          }
1510          /**
1511           * Title, Asbtract
1512           */
1513          if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
1514             xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
1515            xmlChar *val=
1516              xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
1517            if(tmpmaps==NULL){
[381]1518              tmpmaps=(maps*)malloc(MAPS_SIZE);
[9]1519              if(tmpmaps == NULL){
[459]1520                return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[1]1521              }
[453]1522              tmpmaps->name=zStrdup("missingIndetifier");
[9]1523              tmpmaps->content=createMap((char*)cur2->name,(char*)val);
1524              tmpmaps->next=NULL;
1525            }
1526            else{
1527              if(tmpmaps->content!=NULL)
1528                addToMap(tmpmaps->content,
1529                         (char*)cur2->name,(char*)val);
1530              else
1531                tmpmaps->content=
1532                  createMap((char*)cur2->name,(char*)val);
1533            }
[1]1534#ifdef DEBUG
[9]1535            dumpMaps(tmpmaps);
[1]1536#endif
[9]1537            xmlFree(val);
1538          }
1539          /**
1540           * InputDataFormChoice (Reference or Data ?)
1541           */
1542          if(xmlStrcasecmp(cur2->name,BAD_CAST "Reference")==0){
[1]1543            /**
[9]1544             * Get every attribute from a Reference node
1545             * mimeType, encoding, schema, href, method
1546             * Header and Body gesture should be added here
[1]1547             */
1548#ifdef DEBUG
[9]1549            fprintf(stderr,"REFERENCE\n");
[1]1550#endif
[283]1551            const char *refs[5]={"mimeType","encoding","schema","method","href"};
[9]1552            for(int l=0;l<5;l++){
[1]1553#ifdef DEBUG
[9]1554              fprintf(stderr,"*** %s ***",refs[l]);
[1]1555#endif
[9]1556              xmlChar *val=xmlGetProp(cur2,BAD_CAST refs[l]);
1557              if(val!=NULL && xmlStrlen(val)>0){
1558                if(tmpmaps->content!=NULL)
1559                  addToMap(tmpmaps->content,refs[l],(char*)val);
1560                else
1561                  tmpmaps->content=createMap(refs[l],(char*)val);
1562                map* ltmp=getMap(tmpmaps->content,"method");
1563                if(l==4){
[440]1564                  if(!(ltmp!=NULL && strncmp(ltmp->value,"POST",4)==0)
[9]1565                     && CHECK_INET_HANDLE(hInternet)){
[471]1566                    if(loadRemoteFile(&m,&tmpmaps->content,hInternet,(char*)val)!=0){
[368]1567                      freeMaps(&m);
1568                      free(m);
1569                      free(REQUEST);
1570                      free(SERVICE_URL);
1571                      InternetCloseHandle(hInternet);
1572                      freeService(&s1);
1573                      free(s1);
1574                      return 0;
1575                    }
[1]1576                  }
1577                }
[9]1578              }
[1]1579#ifdef DEBUG
[9]1580              fprintf(stderr,"%s\n",val);
[1]1581#endif
[9]1582              xmlFree(val);
1583            }
[1]1584#ifdef POST_DEBUG
[9]1585            fprintf(stderr,"Parse Header and Body from Reference \n");
[1]1586#endif
[9]1587            xmlNodePtr cur3=cur2->children;
[441]1588            HINTERNET hInternetP;
1589            hInternetP=InternetOpen(
1590#ifndef WIN32
1591                                   (LPCTSTR)
1592#endif
1593                                   "ZooWPSClient\0",
1594                                   INTERNET_OPEN_TYPE_PRECONFIG,
1595                                   NULL,NULL, 0);
1596            hInternetP.header=NULL;
[9]1597            while(cur3){
[216]1598              while(cur3!=NULL && cur3->type!=XML_ELEMENT_NODE)
1599                cur2=cur3->next;
[9]1600              if(xmlStrcasecmp(cur3->name,BAD_CAST "Header")==0 ){
[114]1601                const char *ha[2];
[9]1602                ha[0]="key";
1603                ha[1]="value";
1604                int hai;
1605                char *has;
1606                char *key;
1607                for(hai=0;hai<2;hai++){
1608                  xmlChar *val=xmlGetProp(cur3,BAD_CAST ha[hai]);
[1]1609#ifdef POST_DEBUG
[9]1610                  fprintf(stderr,"%s = %s\n",ha[hai],(char*)val);
[1]1611#endif
[9]1612                  if(hai==0){
[381]1613                    key=(char*)malloc((1+strlen((char*)val))*sizeof(char));
[9]1614                    snprintf(key,1+strlen((char*)val),"%s",(char*)val);
1615                  }else{
[381]1616                    has=(char*)malloc((3+strlen((char*)val)+strlen(key))*sizeof(char));
[9]1617                    if(has == NULL){
[459]1618                      return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1619                    }
1620                    snprintf(has,(3+strlen((char*)val)+strlen(key)),"%s: %s",key,(char*)val);
[1]1621#ifdef POST_DEBUG
[9]1622                    fprintf(stderr,"%s\n",has);
[1]1623#endif
1624                  }
1625                }
[441]1626                hInternetP.header=curl_slist_append(hInternetP.header, has);
1627                if(has!=NULL)
1628                  free(has);
[9]1629              }
1630              else{
[1]1631#ifdef POST_DEBUG
[9]1632                fprintf(stderr,"Try to fetch the body part of the request ...\n");
[1]1633#endif
[9]1634                if(xmlStrcasecmp(cur3->name,BAD_CAST "Body")==0 ){
[1]1635#ifdef POST_DEBUG
[9]1636                  fprintf(stderr,"Body part found !!!\n",(char*)cur3->content);
[1]1637#endif
[9]1638                  char *tmp=new char[cgiContentLength];
1639                  memset(tmp,0,cgiContentLength);
1640                  xmlNodePtr cur4=cur3->children;
1641                  while(cur4!=NULL){
[127]1642                    while(cur4->type!=XML_ELEMENT_NODE)
1643                      cur4=cur4->next;
[9]1644                    xmlDocPtr bdoc = xmlNewDoc(BAD_CAST "1.0");
1645                    bdoc->encoding = xmlCharStrdup ("UTF-8");
1646                    xmlDocSetRootElement(bdoc,cur4);
1647                    xmlChar* btmps;
1648                    int bsize;
1649                    xmlDocDumpMemory(bdoc,&btmps,&bsize);
[1]1650#ifdef POST_DEBUG
[9]1651                    fprintf(stderr,"Body part found !!! %s %s\n",tmp,(char*)btmps);
[1]1652#endif
[9]1653                    if(btmps!=NULL)
1654                      sprintf(tmp,"%s",(char*)btmps);
1655                    xmlFreeDoc(bdoc);
1656                    cur4=cur4->next;
1657                  }
1658                  map *btmp=getMap(tmpmaps->content,"href");
1659                  if(btmp!=NULL){
1660#ifdef POST_DEBUG
1661                    fprintf(stderr,"%s %s\n",btmp->value,tmp);
[441]1662                    curl_easy_setopt(hInternetP.handle, CURLOPT_VERBOSE, 1);
[9]1663#endif
[441]1664                    res=InternetOpenUrl(hInternetP,btmp->value,tmp,strlen(tmp),
[9]1665                                        INTERNET_FLAG_NO_CACHE_WRITE,0);
[381]1666                    char* tmpContent = (char*)malloc((res.nDataLen+1)*sizeof(char));
[9]1667                    if(tmpContent == NULL){
[459]1668                      return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[1]1669                    }
[9]1670                    size_t dwRead;
1671                    InternetReadFile(res, (LPVOID)tmpContent,
1672                                     res.nDataLen, &dwRead);
1673                    tmpContent[res.nDataLen]=0;
[441]1674                    if(hInternetP.header!=NULL)
1675                      curl_slist_free_all(hInternetP.header);
[9]1676                    addToMap(tmpmaps->content,"value",tmpContent);
1677#ifdef POST_DEBUG
1678                    fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1679#endif
1680                  }
1681                }
1682                else
1683                  if(xmlStrcasecmp(cur3->name,BAD_CAST "BodyReference")==0 ){
1684                    xmlChar *val=xmlGetProp(cur3,BAD_CAST "href");
1685                    HINTERNET bInternet,res1;
1686                    bInternet=InternetOpen(
1687#ifndef WIN32
1688                                           (LPCTSTR)
1689#endif
1690                                           "ZooWPSClient\0",
1691                                           INTERNET_OPEN_TYPE_PRECONFIG,
1692                                           NULL,NULL, 0);
1693                    if(!CHECK_INET_HANDLE(bInternet))
1694                      fprintf(stderr,"WARNING : hInternet handle failed to initialize");
1695#ifdef POST_DEBUG
1696                    curl_easy_setopt(bInternet.handle, CURLOPT_VERBOSE, 1);
1697#endif
1698                    res1=InternetOpenUrl(bInternet,(char*)val,NULL,0,
1699                                         INTERNET_FLAG_NO_CACHE_WRITE,0);
1700                    char* tmp=
[381]1701                      (char*)malloc((res1.nDataLen+1)*sizeof(char));
[9]1702                    if(tmp == NULL){
[459]1703                      return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1704                    }
1705                    size_t bRead;
1706                    InternetReadFile(res1, (LPVOID)tmp,
1707                                     res1.nDataLen, &bRead);
1708                    tmp[res1.nDataLen]=0;
1709                    InternetCloseHandle(bInternet);
[1]1710                    map *btmp=getMap(tmpmaps->content,"href");
1711                    if(btmp!=NULL){
1712#ifdef POST_DEBUG
1713                      fprintf(stderr,"%s %s\n",btmp->value,tmp);
[441]1714                      curl_easy_setopt(hInternetP.handle, CURLOPT_VERBOSE, 1);
[1]1715#endif
[441]1716                      res=InternetOpenUrl(hInternetP,btmp->value,tmp,
[9]1717                                          strlen(tmp),
[1]1718                                          INTERNET_FLAG_NO_CACHE_WRITE,0);
[381]1719                      char* tmpContent = (char*)malloc((res.nDataLen+1)*sizeof(char));
[9]1720                      if(tmpContent == NULL){
[459]1721                        return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1722                      }
[1]1723                      size_t dwRead;
1724                      InternetReadFile(res, (LPVOID)tmpContent,
1725                                       res.nDataLen, &dwRead);
1726                      tmpContent[res.nDataLen]=0;
[441]1727                      if(hInternetP.header!=NULL)
1728                        curl_slist_free_all(hInternetP.header);
[1]1729                      addToMap(tmpmaps->content,"value",tmpContent);
1730#ifdef POST_DEBUG
1731                      fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent);
1732#endif
1733                    }
1734                  }
1735              }
[9]1736              cur3=cur3->next;
1737            }
[441]1738            InternetCloseHandle(hInternetP);
[1]1739#ifdef POST_DEBUG
[9]1740            fprintf(stderr,"Header and Body was parsed from Reference \n");
[1]1741#endif
1742#ifdef DEBUG
[9]1743            dumpMap(tmpmaps->content);
1744            fprintf(stderr, "= element 2 node \"%s\" = (%s)\n", 
1745                    cur2->name,cur2->content);
[1]1746#endif
[9]1747          }
1748          else if(xmlStrcasecmp(cur2->name,BAD_CAST "Data")==0){
[1]1749#ifdef DEBUG
[9]1750            fprintf(stderr,"DATA\n");
[1]1751#endif
[9]1752            xmlNodePtr cur4=cur2->children;
[32]1753            while(cur4!=NULL){
1754              while(cur4!=NULL &&cur4->type!=XML_ELEMENT_NODE)
[9]1755                cur4=cur4->next;
[32]1756              if(cur4==NULL)
1757                break;
[9]1758              if(xmlStrcasecmp(cur4->name, BAD_CAST "LiteralData")==0){
1759                /**
1760                 * Get every attribute from a LiteralData node
1761                 * dataType , uom
1762                 */
[114]1763                char *list[2];
[453]1764                list[0]=zStrdup("dataType");
1765                list[1]=zStrdup("uom");
[9]1766                for(int l=0;l<2;l++){
[1]1767#ifdef DEBUG
[114]1768                  fprintf(stderr,"*** LiteralData %s ***",list[l]);
[1]1769#endif
[114]1770                  xmlChar *val=xmlGetProp(cur4,BAD_CAST list[l]);
[9]1771                  if(val!=NULL && strlen((char*)val)>0){
1772                    if(tmpmaps->content!=NULL)
[114]1773                      addToMap(tmpmaps->content,list[l],(char*)val);
[9]1774                    else
[114]1775                      tmpmaps->content=createMap(list[l],(char*)val);
[1]1776#ifdef DEBUG
[280]1777                    fprintf(stderr,"%s\n",val);
[1]1778#endif
[280]1779                  }
[9]1780                  xmlFree(val);
[280]1781                  free(list[l]);                 
[1]1782                }
[9]1783              }
1784              else if(xmlStrcasecmp(cur4->name, BAD_CAST "ComplexData")==0){
1785                /**
1786                 * Get every attribute from a Reference node
1787                 * mimeType, encoding, schema
1788                 */
[282]1789                const char *coms[3]={"mimeType","encoding","schema"};
[9]1790                for(int l=0;l<3;l++){
[1]1791#ifdef DEBUG
[280]1792                  fprintf(stderr,"*** ComplexData %s ***\n",coms[l]);
[1]1793#endif
[9]1794                  xmlChar *val=xmlGetProp(cur4,BAD_CAST coms[l]);
1795                  if(val!=NULL && strlen((char*)val)>0){
1796                    if(tmpmaps->content!=NULL)
1797                      addToMap(tmpmaps->content,coms[l],(char*)val);
1798                    else
1799                      tmpmaps->content=createMap(coms[l],(char*)val);
[1]1800#ifdef DEBUG
[280]1801                    fprintf(stderr,"%s\n",val);
[1]1802#endif
[280]1803                  }
[9]1804                  xmlFree(val);
[1]1805                }
1806              }
[280]1807
[94]1808              map* test=getMap(tmpmaps->content,"encoding");
[280]1809              if(test==NULL){
1810                if(tmpmaps->content!=NULL)
1811                  addToMap(tmpmaps->content,"encoding","utf-8");
1812                else
1813                  tmpmaps->content=createMap("encoding","utf-8");
1814                test=getMap(tmpmaps->content,"encoding");
1815              }
1816
1817              if(strcasecmp(test->value,"base64")!=0){
[94]1818                xmlChar* mv=xmlNodeListGetString(doc,cur4->xmlChildrenNode,1);
[280]1819                map* ltmp=getMap(tmpmaps->content,"mimeType");
1820                if(mv==NULL || 
1821                   (xmlStrcasecmp(cur4->name, BAD_CAST "ComplexData")==0 &&
1822                    (ltmp==NULL || strncasecmp(ltmp->value,"text/xml",8)==0) )){
[94]1823                  xmlDocPtr doc1=xmlNewDoc(BAD_CAST "1.0");
1824                  int buffersize;
[280]1825                  xmlNodePtr cur5=cur4->children;
1826                  while(cur5!=NULL &&cur5->type!=XML_ELEMENT_NODE)
1827                    cur5=cur5->next;
1828                  xmlDocSetRootElement(doc1,cur5);
[94]1829                  xmlDocDumpFormatMemoryEnc(doc1, &mv, &buffersize, "utf-8", 1);
1830                  char size[1024];
1831                  sprintf(size,"%d",buffersize);
1832                  addToMap(tmpmaps->content,"size",size);
1833                }
1834                addToMap(tmpmaps->content,"value",(char*)mv);
1835                xmlFree(mv);
1836              }else{
[95]1837                xmlChar* tmp=xmlNodeListGetRawString(doc,cur4->xmlChildrenNode,0);
[94]1838                addToMap(tmpmaps->content,"value",(char*)tmp);
1839                map* tmpv=getMap(tmpmaps->content,"value");
1840                char *res=NULL;
1841                char *curs=tmpv->value;
1842                for(int i=0;i<=strlen(tmpv->value)/64;i++) {
1843                  if(res==NULL)
1844                    res=(char*)malloc(67*sizeof(char));
1845                  else
1846                    res=(char*)realloc(res,(((i+1)*65)+i)*sizeof(char));
1847                  int csize=i*65;
1848                  strncpy(res + csize,curs,64);
1849                  if(i==xmlStrlen(tmp)/64)
1850                    strcat(res,"\n\0");
1851                  else{
1852                    strncpy(res + (((i+1)*64)+i),"\n\0",2);
1853                    curs+=64;
1854                  }
1855                }
1856                free(tmpv->value);
[453]1857                tmpv->value=zStrdup(res);
[94]1858                free(res);
1859                xmlFree(tmp);
[69]1860              }
[9]1861              cur4=cur4->next;
[1]1862            }
[9]1863          }
[1]1864#ifdef DEBUG
[9]1865          fprintf(stderr,"cur2 next \n");
1866          fflush(stderr);
[1]1867#endif
[9]1868          cur2=cur2->next;
1869        }
[1]1870#ifdef DEBUG
[9]1871        fprintf(stderr,"ADD MAPS TO REQUEST MAPS !\n");
1872        fflush(stderr);
[1]1873#endif
[360]1874
1875        {
1876          maps* testPresence=getMaps(request_input_real_format,tmpmaps->name);
1877          if(testPresence!=NULL){
1878            elements* elem=getElements(s1->inputs,tmpmaps->name);
1879            if(elem!=NULL){
1880              if(appendMapsToMaps(m,request_input_real_format,tmpmaps,elem)<0){
1881                freeMaps(&m);
1882                free(m);
1883                free(REQUEST);
1884                free(SERVICE_URL);
1885                InternetCloseHandle(hInternet);
1886                freeService(&s1);
1887                free(s1);
1888                return 0;
1889              }
1890            }
1891          }
1892          else
1893            addMapsToMaps(&request_input_real_format,tmpmaps);
1894        }
[418]1895
[1]1896#ifdef DEBUG
[9]1897        fprintf(stderr,"******TMPMAPS*****\n");
[1]1898        dumpMaps(tmpmaps);
[9]1899        fprintf(stderr,"******REQUESTMAPS*****\n");
1900        dumpMaps(request_input_real_format);
[1]1901#endif
[32]1902        freeMaps(&tmpmaps);
1903        free(tmpmaps);
1904        tmpmaps=NULL;         
[1]1905      }
[25]1906#ifdef DEBUG
1907      dumpMaps(tmpmaps); 
1908#endif
[1]1909    }
1910#ifdef DEBUG
[9]1911    fprintf(stderr,"Search for response document node\n");
1912#endif
1913    xmlXPathFreeObject(tmpsptr);
[105]1914   
[9]1915    tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='ResponseDocument']");
[105]1916    bool asRaw=false;
[9]1917    tmps=tmpsptr->nodesetval;
[105]1918    if(tmps->nodeNr==0){
1919      tmpsptr=extractFromDoc(doc,"/*/*/*[local-name()='RawDataOutput']");
1920      tmps=tmpsptr->nodesetval;
1921      asRaw=true;
1922    }
[9]1923#ifdef DEBUG
[1]1924    fprintf(stderr,"*****%d*****\n",tmps->nodeNr);
1925#endif
1926    for(int k=0;k<tmps->nodeNr;k++){
[105]1927      if(asRaw==true)
1928        addToMap(request_inputs,"RawDataOutput","");
1929      else
1930        addToMap(request_inputs,"ResponseDocument","");
[1]1931      maps *tmpmaps=NULL;
1932      xmlNodePtr cur=tmps->nodeTab[k];
1933      if(cur->type == XML_ELEMENT_NODE) {
1934        /**
1935         * A specific responseDocument node.
1936         */
1937        if(tmpmaps==NULL){
[381]1938          tmpmaps=(maps*)malloc(MAPS_SIZE);
[9]1939          if(tmpmaps == NULL){
[459]1940            return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]1941          }
[453]1942          tmpmaps->name=zStrdup("unknownIdentifier");
[381]1943          tmpmaps->content=NULL;
[1]1944          tmpmaps->next=NULL;
1945        }
1946        /**
[384]1947         * Get every attribute: storeExecuteResponse, lineage, status
[1]1948         */
[283]1949        const char *ress[3]={"storeExecuteResponse","lineage","status"};
[1]1950        xmlChar *val;
1951        for(int l=0;l<3;l++){
1952#ifdef DEBUG
1953          fprintf(stderr,"*** %s ***\t",ress[l]);
1954#endif
1955          val=xmlGetProp(cur,BAD_CAST ress[l]);
1956          if(val!=NULL && strlen((char*)val)>0){
1957            if(tmpmaps->content!=NULL)
1958              addToMap(tmpmaps->content,ress[l],(char*)val);
1959            else
1960              tmpmaps->content=createMap(ress[l],(char*)val);
1961            addToMap(request_inputs,ress[l],(char*)val);
1962          }
1963#ifdef DEBUG
1964          fprintf(stderr,"%s\n",val);
1965#endif
1966          xmlFree(val);
1967        }
1968        xmlNodePtr cur1=cur->children;
[381]1969        while(cur1!=NULL && cur1->type != XML_ELEMENT_NODE)
1970          cur1=cur1->next;
[418]1971        int cur1cnt=0;
[1]1972        while(cur1){
[280]1973          /**
1974           * Indentifier
1975           */
1976          if(xmlStrncasecmp(cur1->name,BAD_CAST "Identifier",xmlStrlen(cur1->name))==0){
1977            xmlChar *val=
1978              xmlNodeListGetString(doc,cur1->xmlChildrenNode,1);
1979            if(tmpmaps==NULL){
[381]1980              tmpmaps=(maps*)malloc(MAPS_SIZE);
[280]1981              if(tmpmaps == NULL){
[459]1982                return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[280]1983              }
[453]1984              tmpmaps->name=zStrdup((char*)val);
[280]1985              tmpmaps->content=NULL;
1986              tmpmaps->next=NULL;
1987            }
[384]1988            else{
1989              //free(tmpmaps->name);
[453]1990              tmpmaps->name=zStrdup((char*)val);
[384]1991            }
[418]1992            if(asRaw==true)
1993              addToMap(request_inputs,"RawDataOutput",(char*)val);
1994            else{
1995              if(cur1cnt==0)
1996                addToMap(request_inputs,"ResponseDocument",(char*)val);
1997              else{
1998                map* tt=getMap(request_inputs,"ResponseDocument");
[453]1999                char* tmp=zStrdup(tt->value);
[418]2000                free(tt->value);
2001                tt->value=(char*)malloc((strlen(tmp)+strlen((char*)val)+1)*sizeof(char));
2002                sprintf(tt->value,"%s;%s",tmp,(char*)val);
2003                free(tmp);
2004              }
2005            }
2006            cur1cnt+=1;
[280]2007            xmlFree(val);
2008          }
2009          /**
2010           * Title, Asbtract
2011           */
2012          else if(xmlStrncasecmp(cur1->name,BAD_CAST "Title",xmlStrlen(cur1->name))==0 ||
2013                  xmlStrncasecmp(cur1->name,BAD_CAST "Abstract",xmlStrlen(cur1->name))==0){
2014            xmlChar *val=
2015              xmlNodeListGetString(doc,cur1->xmlChildrenNode,1);
2016            if(tmpmaps==NULL){
[381]2017              tmpmaps=(maps*)malloc(MAPS_SIZE);
[280]2018              if(tmpmaps == NULL){
[459]2019                return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[280]2020              }
[453]2021              tmpmaps->name=zStrdup("missingIndetifier");
[280]2022              tmpmaps->content=createMap((char*)cur1->name,(char*)val);
2023              tmpmaps->next=NULL;
2024            }
2025            else{
2026              if(tmpmaps->content!=NULL)
[384]2027                addToMap(tmpmaps->content,(char*)cur1->name,(char*)val);
[280]2028              else
[384]2029                tmpmaps->content=createMap((char*)cur1->name,(char*)val);
[280]2030            }
2031            xmlFree(val);
2032          }
2033          else if(xmlStrncasecmp(cur1->name,BAD_CAST "Output",xmlStrlen(cur1->name))==0){
[1]2034            /**
2035             * Get every attribute from a Output node
2036             * mimeType, encoding, schema, uom, asReference
2037             */
[283]2038            const char *outs[5]={"mimeType","encoding","schema","uom","asReference"};
[1]2039            for(int l=0;l<5;l++){
2040#ifdef DEBUG
2041              fprintf(stderr,"*** %s ***\t",outs[l]);
2042#endif
2043              val=xmlGetProp(cur1,BAD_CAST outs[l]);
2044              if(val!=NULL && strlen((char*)val)>0){
2045                if(tmpmaps->content!=NULL)
2046                  addToMap(tmpmaps->content,outs[l],(char*)val);
2047                else
2048                  tmpmaps->content=createMap(outs[l],(char*)val);
2049              }
2050#ifdef DEBUG
2051              fprintf(stderr,"%s\n",val);
2052#endif
2053              xmlFree(val);
2054            }
2055            xmlNodePtr cur2=cur1->children;
[384]2056            while(cur2!=NULL && cur2->type != XML_ELEMENT_NODE)
2057              cur2=cur2->next;
[1]2058            while(cur2){
2059              /**
2060               * Indentifier
2061               */
[9]2062              if(xmlStrncasecmp(cur2->name,BAD_CAST "Identifier",xmlStrlen(cur2->name))==0){
[1]2063                xmlChar *val=
2064                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
2065                if(tmpmaps==NULL){
[381]2066                  tmpmaps=(maps*)malloc(MAPS_SIZE);
[9]2067                  if(tmpmaps == NULL){
[459]2068                    return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]2069                  }
[453]2070                  tmpmaps->name=zStrdup((char*)val);
[1]2071                  tmpmaps->content=NULL;
2072                  tmpmaps->next=NULL;
2073                }
[380]2074                else{
2075                  if(tmpmaps->name!=NULL)
2076                    free(tmpmaps->name);
[453]2077                  tmpmaps->name=zStrdup((char*)val);;
[380]2078                }
[1]2079                xmlFree(val);
2080              }
2081              /**
2082               * Title, Asbtract
2083               */
[274]2084              else if(xmlStrncasecmp(cur2->name,BAD_CAST "Title",xmlStrlen(cur2->name))==0 ||
[331]2085                      xmlStrncasecmp(cur2->name,BAD_CAST "Abstract",xmlStrlen(cur2->name))==0){
[1]2086                xmlChar *val=
2087                  xmlNodeListGetString(doc,cur2->xmlChildrenNode,1);
2088                if(tmpmaps==NULL){
[381]2089                  tmpmaps=(maps*)malloc(MAPS_SIZE);
[9]2090                  if(tmpmaps == NULL){
[459]2091                    return errorException(m, _("Unable to allocate memory."), "InternalError",NULL);
[9]2092                  }
[453]2093                  tmpmaps->name=zStrdup("missingIndetifier");
[1]2094                  tmpmaps->content=createMap((char*)cur2->name,(char*)val);
2095                  tmpmaps->next=NULL;
2096                }
2097                else{
2098                  if(tmpmaps->content!=NULL)
2099                    addToMap(tmpmaps->content,
2100                             (char*)cur2->name,(char*)val);
2101                  else
2102                    tmpmaps->content=
2103                      createMap((char*)cur2->name,(char*)val);
2104                }
2105                xmlFree(val);
2106              }
2107              cur2=cur2->next;
[380]2108              while(cur2!=NULL && cur2->type != XML_ELEMENT_NODE)
2109                cur2=cur2->next;
[1]2110            }
2111          }
2112          cur1=cur1->next;
[380]2113          while(cur1!=NULL && cur1->type != XML_ELEMENT_NODE)
2114            cur1=cur1->next;
[1]2115        }
2116      }
[418]2117      if(request_output_real_format==NULL)
2118        request_output_real_format=dupMaps(&tmpmaps);
2119      else
2120        addMapsToMaps(&request_output_real_format,tmpmaps);
[380]2121      if(tmpmaps!=NULL){
2122        freeMaps(&tmpmaps);
2123        free(tmpmaps);
2124        tmpmaps=NULL;
2125      }
[1]2126    }
[9]2127    xmlXPathFreeObject(tmpsptr);
[1]2128    xmlCleanupParser();
2129  }
[105]2130 
[392]2131
2132  //  if(CHECK_INET_HANDLE(hInternet))
[9]2133  InternetCloseHandle(hInternet);
[1]2134
2135#ifdef DEBUG
[375]2136  fprintf(stderr,"\n%d\n",__LINE__);
2137  fflush(stderr);
[1]2138  dumpMaps(request_input_real_format);
2139  dumpMaps(request_output_real_format);
[9]2140  dumpMap(request_inputs);
[375]2141  fprintf(stderr,"\n%d\n",__LINE__);
2142  fflush(stderr);
[1]2143#endif
2144
2145  /**
2146   * Ensure that each requested arguments are present in the request
2147   * DataInputs and ResponseDocument / RawDataOutput
[92]2148   */
[63]2149  char *dfv=addDefaultValues(&request_input_real_format,s1->inputs,m,0);
[280]2150  char *dfv1=addDefaultValues(&request_output_real_format,s1->outputs,m,1);
2151  if(strcmp(dfv1,"")!=0 || strcmp(dfv,"")!=0){
[9]2152    char tmps[1024];
[459]2153    map* tmpe=createMap("code","MissingParameterValue");
[280]2154    if(strcmp(dfv,"")!=0){
2155      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);
[459]2156      addToMap(tmpe,"locator",dfv);
[280]2157    }
2158    else if(strcmp(dfv1,"")!=0){
2159      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);
[459]2160      addToMap(tmpe,"locator",dfv1);
[280]2161    }
[459]2162    addToMap(tmpe,"text",tmps);
[9]2163    printExceptionReportResponse(m,tmpe);
[63]2164    freeService(&s1);
2165    free(s1);
[9]2166    freeMap(&tmpe);
2167    free(tmpe);
2168    freeMaps(&m);
2169    free(m);
2170    free(REQUEST);
[63]2171    free(SERVICE_URL);
[9]2172    freeMaps(&request_input_real_format);
2173    free(request_input_real_format);
2174    freeMaps(&request_output_real_format);
2175    free(request_output_real_format);
2176    freeMaps(&tmpmaps);
2177    free(tmpmaps);
2178    return 1;
2179  }
[331]2180  maps* tmpReqI=request_input_real_format;
2181  while(tmpReqI!=NULL){
2182    char name[1024];
2183    if(getMap(tmpReqI->content,"isFile")!=NULL){
2184      if (cgiFormFileName(tmpReqI->name, name, sizeof(name)) == cgiFormSuccess) {
2185        int BufferLen=1024;
2186        cgiFilePtr file;
2187        int targetFile;
2188        mode_t mode;
2189        char storageNameOnServer[2048];
2190        char fileNameOnServer[64];
2191        char contentType[1024];
[364]2192        char buffer[1024];
[331]2193        char *tmpStr=NULL;
2194        int size;
2195        int got,t;
2196        map *path=getMapFromMaps(m,"main","tmpPath");
2197        cgiFormFileSize(tmpReqI->name, &size);
2198        cgiFormFileContentType(tmpReqI->name, contentType, sizeof(contentType));
2199        if (cgiFormFileOpen(tmpReqI->name, &file) == cgiFormSuccess) {
2200          t=-1;
2201          while(1){
2202            tmpStr=strstr(name+t+1,"\\");
2203            if(NULL==tmpStr)
2204              tmpStr=strstr(name+t+1,"/");
2205            if(NULL!=tmpStr)
2206              t=(int)(tmpStr-name);
2207            else
2208              break;
2209          }
2210          strcpy(fileNameOnServer,name+t+1);
2211         
2212          sprintf(storageNameOnServer,"%s/%s",path->value,fileNameOnServer);
[375]2213#ifdef DEBUG
[331]2214          fprintf(stderr,"Name on server %s\n",storageNameOnServer);
2215          fprintf(stderr,"fileNameOnServer: %s\n",fileNameOnServer);
[375]2216#endif
[331]2217          mode=S_IRWXU|S_IRGRP|S_IROTH;
[364]2218          targetFile = open (storageNameOnServer,O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP|S_IROTH);
[331]2219          if(targetFile<0){
[375]2220#ifdef DEBUG
[331]2221            fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer);         
[375]2222#endif
[331]2223          }else{
2224            while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
2225              if(got>0)
2226                write(targetFile,buffer,got);
2227            }
2228          }
2229          addToMap(tmpReqI->content,"lref",storageNameOnServer);
2230          cgiFormFileClose(file);
2231          close(targetFile);
[375]2232#ifdef DEBUG
[331]2233          fprintf(stderr,"File \"%s\" has been uploaded",fileNameOnServer);
[375]2234#endif
[331]2235        }
2236      }
2237    }
2238    tmpReqI=tmpReqI->next;
2239  }
2240
[88]2241  ensureDecodedBase64(&request_input_real_format);
2242
[9]2243#ifdef DEBUG
2244  fprintf(stderr,"REQUEST_INPUTS\n");
2245  dumpMaps(request_input_real_format);
2246  fprintf(stderr,"REQUEST_OUTPUTS\n");
2247  dumpMaps(request_output_real_format);
2248#endif
[1]2249
2250  maps* curs=getMaps(m,"env");
2251  if(curs!=NULL){
2252    map* mapcs=curs->content;
2253    while(mapcs!=NULLMAP){
2254#ifndef WIN32
2255      setenv(mapcs->name,mapcs->value,1);
2256#else
2257#ifdef DEBUG
2258      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
2259#endif
2260      if(mapcs->value[strlen(mapcs->value)-2]=='\r'){
2261#ifdef DEBUG
2262        fprintf(stderr,"[ZOO: Env var finish with \r]\n");
2263#endif
2264        mapcs->value[strlen(mapcs->value)-1]=0;
2265      }
2266#ifdef DEBUG
2267      fflush(stderr);
[364]2268      fprintf(stderr,"setting variable... %s\n",(
[1]2269#endif
2270              SetEnvironmentVariable(mapcs->name,mapcs->value)
2271#ifdef DEBUG
[364]2272              ==0)? "OK" : "FAILED");
[1]2273#else
2274      ;
2275#endif
[392]2276      char* toto=(char*)malloc((strlen(mapcs->name)+strlen(mapcs->value)+2)*sizeof(char));
[364]2277      sprintf(toto,"%s=%s",mapcs->name,mapcs->value);
[392]2278      putenv(toto);
[1]2279#ifdef DEBUG
2280      fflush(stderr);
2281#endif
2282#endif
2283#ifdef DEBUG
2284      fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value);
2285      fflush(stderr);
2286#endif
2287      mapcs=mapcs->next;
2288    }
2289  }
2290 
2291#ifdef DEBUG
2292  dumpMap(request_inputs);
2293#endif
2294
2295  /**
2296   * Need to check if we need to fork to load a status enabled
2297   */
2298  r_inputs=NULL;
[72]2299  map* store=getMap(request_inputs,"storeExecuteResponse");
2300  map* status=getMap(request_inputs,"status");
2301  /**
2302   * 05-007r7 WPS 1.0.0 page 57 :
2303   * 'If status="true" and storeExecuteResponse is "false" then the service
2304   * shall raise an exception.'
2305   */
2306  if(status!=NULL && strcmp(status->value,"true")==0 && 
2307     store!=NULL && strcmp(store->value,"false")==0){
[459]2308    errorException(m, _("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."), "InvalidParameterValue","storeExecuteResponse");
[72]2309    freeService(&s1);
2310    free(s1);
2311    freeMaps(&m);
2312    free(m);
2313   
2314    freeMaps(&request_input_real_format);
2315    free(request_input_real_format);
2316   
2317    freeMaps(&request_output_real_format);
2318    free(request_output_real_format);
2319   
2320    free(REQUEST);
2321    free(SERVICE_URL);
2322    return 1;
2323  }
[1]2324  r_inputs=getMap(request_inputs,"storeExecuteResponse");
2325  int eres=SERVICE_STARTED;
2326  int cpid=getpid();
[94]2327
[453]2328  /**
2329   * Initialize the specific [lenv] section which contains runtime variables:
2330   *
2331   *  - usid : it is an unique identification number
2332   *  - sid : it is the process idenfitication number (OS)
2333   *  - status : value between 0 and 100 to express the  completude of
2334   * the operations of the running service
2335   *  - message : is a string where you can store error messages, in case
2336   * service is failing, or o provide details on the ongoing operation.
2337   *  - cwd : is the current working directory
2338   *  - soap : is a boolean value, true if the request was contained in a SOAP
2339   * Envelop
2340   *  - sessid : string storing the session identifier (only when cookie is
2341   * used)
2342   *  - cgiSid : only defined on Window platforms (for being able to identify
2343   * the created process)
2344   *
2345   */
[32]2346  maps *_tmpMaps=(maps*)malloc(MAPS_SIZE);
[453]2347  _tmpMaps->name=zStrdup("lenv");
[32]2348  char tmpBuff[100];
[453]2349  sprintf(tmpBuff,"%i",(cpid+(int)time(NULL)));
2350  _tmpMaps->content=createMap("usid",tmpBuff);
2351  _tmpMaps->next=NULL;
[32]2352  sprintf(tmpBuff,"%i",cpid);
[453]2353  addToMap(_tmpMaps->content,"sid",tmpBuff);
[32]2354  addToMap(_tmpMaps->content,"status","0");
[465]2355  addToMap(_tmpMaps->content,"cwd",ntmp);
[453]2356  addToMap(_tmpMaps->content,"message",_("No message provided"));
[280]2357  map* ltmp=getMap(request_inputs,"soap");
2358  if(ltmp!=NULL)
2359    addToMap(_tmpMaps->content,"soap",ltmp->value);
2360  else
2361    addToMap(_tmpMaps->content,"soap","false");
[92]2362  if(cgiCookie!=NULL && strlen(cgiCookie)>0){
[390]2363    int hasValidCookie=-1;
[453]2364    char *tcook=zStrdup(cgiCookie);
[433]2365    char *tmp=NULL;
2366    int hasVal=-1;
2367    map* testing=getMapFromMaps(m,"main","cookiePrefix");
2368    if(testing==NULL){
[453]2369      tmp=zStrdup("ID=");
[433]2370    }else{
2371      tmp=(char*)malloc((strlen(testing->value)+2)*sizeof(char));
2372      sprintf(tmp,"%s=",testing->value);
2373      hasVal=1;
2374    }
[391]2375    if(strstr(cgiCookie,";")!=NULL){
[342]2376      char *token,*saveptr;
2377      token=strtok_r(cgiCookie,";",&saveptr);
2378      while(token!=NULL){
[433]2379        if(strcasestr(token,tmp)!=NULL){
[342]2380          if(tcook!=NULL)
2381            free(tcook);
[453]2382          tcook=zStrdup(token);
[390]2383          hasValidCookie=1;
[342]2384        }
2385        token=strtok_r(NULL,";",&saveptr);
2386      }
[391]2387    }else{
[433]2388      if(strstr(cgiCookie,"=")!=NULL && strcasestr(cgiCookie,tmp)!=NULL){
[453]2389        tcook=zStrdup(cgiCookie);
[391]2390        hasValidCookie=1;
2391      }
[433]2392      if(tmp!=NULL){
2393        free(tmp);
2394      }
[342]2395    }
[390]2396    if(hasValidCookie>0){
2397      addToMap(_tmpMaps->content,"sessid",strstr(tcook,"=")+1);
2398      char session_file_path[1024];
2399      map *tmpPath=getMapFromMaps(m,"main","sessPath");
2400      if(tmpPath==NULL)
2401        tmpPath=getMapFromMaps(m,"main","tmpPath");
2402      char *tmp1=strtok(tcook,";");
2403      if(tmp1!=NULL)
2404        sprintf(session_file_path,"%s/sess_%s.cfg",tmpPath->value,strstr(tmp1,"=")+1);
2405      else
2406        sprintf(session_file_path,"%s/sess_%s.cfg",tmpPath->value,strstr(cgiCookie,"=")+1);
2407      free(tcook);
2408      maps *tmpSess=(maps*)malloc(MAPS_SIZE);
2409      struct stat file_status;
2410      int istat = stat(session_file_path, &file_status);
2411      if(istat==0 && file_status.st_size>0){
2412        conf_read(session_file_path,tmpSess);
2413        addMapsToMaps(&m,tmpSess);
2414        freeMaps(&tmpSess);
2415        free(tmpSess);
2416      }
[92]2417    }
2418  }
[32]2419  addMapsToMaps(&m,_tmpMaps);
2420  freeMaps(&_tmpMaps);
2421  free(_tmpMaps);
[465]2422 
[1]2423#ifdef DEBUG
2424  dumpMap(request_inputs);
2425#endif
[216]2426#ifdef WIN32
2427  char *cgiSidL=NULL;
2428  if(getenv("CGISID")!=NULL)
[331]2429    addToMap(request_inputs,"cgiSid",getenv("CGISID"));
[216]2430  map* test1=getMap(request_inputs,"cgiSid");
2431  if(test1!=NULL){
2432    cgiSid=test1->value;
2433    addToMap(request_inputs,"storeExecuteResponse","true");
2434    addToMap(request_inputs,"status","true");
[384]2435    setMapInMaps(m,"lenv","sid",test1->value);
[216]2436    status=getMap(request_inputs,"status");
2437  }
2438#endif
[384]2439  char *fbkp,*fbkp1;
2440  FILE *f0,*f1;
[72]2441  if(status!=NULL)
2442    if(strcasecmp(status->value,"false")==0)
[364]2443      status=NULLMAP;
[72]2444  if(status==NULLMAP){
[34]2445    loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
2446  }
[1]2447  else{
[364]2448    int   pid;
[1]2449#ifdef DEBUG
2450    fprintf(stderr,"\nPID : %d\n",cpid);
2451#endif
[9]2452
[1]2453#ifndef WIN32
[9]2454    pid = fork ();
[1]2455#else
[216]2456    if(cgiSid==NULL){
2457      createProcess(m,request_inputs,s1,NULL,cpid,request_input_real_format,request_output_real_format);
2458      pid = cpid;
2459    }else{
2460      pid=0;
2461      cpid=atoi(cgiSid);
2462    }
[1]2463#endif
2464    if (pid > 0) {
2465      /**
2466       * dady :
2467       * set status to SERVICE_ACCEPTED
2468       */
2469#ifdef DEBUG
2470      fprintf(stderr,"father pid continue (origin %d) %d ...\n",cpid,getpid());
2471#endif
2472      eres=SERVICE_ACCEPTED;
2473    }else if (pid == 0) {
2474      /**
2475       * son : have to close the stdout, stdin and stderr to let the parent
2476       * process answer to http client.
2477       */
2478      r_inputs=getMapFromMaps(m,"main","tmpPath");
[9]2479      map* r_inputs1=getMap(s1->content,"ServiceProvider");
[384]2480      fbkp=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+1024)*sizeof(char));
[9]2481      sprintf(fbkp,"%s/%s_%d.xml",r_inputs->value,r_inputs1->value,cpid);
[384]2482      char* flog=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+1024)*sizeof(char));
[9]2483      sprintf(flog,"%s/%s_%d_error.log",r_inputs->value,r_inputs1->value,cpid);
[1]2484#ifdef DEBUG
2485      fprintf(stderr,"RUN IN BACKGROUND MODE \n");
2486      fprintf(stderr,"son pid continue (origin %d) %d ...\n",cpid,getpid());
[9]2487      fprintf(stderr,"\nFILE TO STORE DATA %s\n",r_inputs->value);
[1]2488#endif
[458]2489      freopen(flog, "w+", stderr);
[384]2490      f0=freopen(fbkp , "w+", stdout);
[458]2491#ifndef WIN32
[1]2492      fclose(stdin);
[458]2493#endif
[9]2494      free(flog);
[1]2495      /**
2496       * set status to SERVICE_STARTED and flush stdout to ensure full
2497       * content was outputed (the file used to store the ResponseDocument).
2498       * The rewind stdout to restart writing from the bgining of the file,
2499       * this way the data will be updated at the end of the process run.
2500       */
[9]2501      printProcessResponse(m,request_inputs,cpid,
[216]2502                           s1,r_inputs1->value,SERVICE_STARTED,
2503                           request_input_real_format,
2504                           request_output_real_format);
2505#ifndef WIN32
[1]2506      fflush(stdout);
2507      rewind(stdout);
[384]2508#else
[216]2509#endif
[384]2510      fbkp1=(char*)malloc((strlen(r_inputs->value)+strlen(r_inputs1->value)+1024)*sizeof(char));
2511      sprintf(fbkp1,"%s/%s_final_%d.xml",r_inputs->value,r_inputs1->value,cpid);
2512      f1=freopen(fbkp1 , "w+", stdout);
[34]2513      loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
[1]2514    } else {
2515      /**
2516       * error server don't accept the process need to output a valid
2517       * error response here !!!
2518       */
[34]2519      eres=-1;
[459]2520      errorException(m, _("Unable to run the child process properly"), "InternalError",NULL);
[1]2521    }
2522  }
2523
2524#ifdef DEBUG
2525  dumpMaps(request_output_real_format);
2526#endif
[9]2527  if(eres!=-1)
2528    outputResponse(s1,request_input_real_format,
2529                   request_output_real_format,request_inputs,
2530                   cpid,m,eres);
[216]2531  fflush(stdout);
[105]2532  /**
2533   * Ensure that if error occurs when freeing memory, no signal will return
2534   * an ExceptionReport document as the result was already returned to the
2535   * client.
2536   */
2537#ifndef USE_GDB
2538  (void) signal(SIGSEGV,donothing);
2539  (void) signal(SIGTERM,donothing);
2540  (void) signal(SIGINT,donothing);
2541  (void) signal(SIGILL,donothing);
2542  (void) signal(SIGFPE,donothing);
2543  (void) signal(SIGABRT,donothing);
2544#endif
[1]2545
[458]2546  if(((int)getpid())!=cpid || cgiSid!=NULL){
[32]2547    fclose(stdout);
2548    fclose(stderr);
2549    unhandleStatus(m);
[384]2550    /**
2551     * Dump back the final file fbkp1 to fbkp
2552     */
2553    fclose(f0);
2554    fclose(f1);
2555    FILE* f2=fopen(fbkp1,"rb");
2556    FILE* f3=fopen(fbkp,"wb+");
2557    free(fbkp);
2558    fseek(f2,0,SEEK_END);
2559    long flen=ftell(f2);
2560    fseek(f2,0,SEEK_SET);
2561    char *tmps1=(char*)malloc((flen+1)*sizeof(char));
2562    fread(tmps1,flen,1,f2);
[458]2563    fwrite(tmps1,1,flen,f3);
[384]2564    fclose(f2);
2565    fclose(f3);
[393]2566    unlink(fbkp1);
2567    free(fbkp1);
[32]2568  }
2569
[9]2570  freeService(&s1);
2571  free(s1);
[59]2572  freeMaps(&m);
[9]2573  free(m);
2574 
2575  freeMaps(&request_input_real_format);
2576  free(request_input_real_format);
[25]2577 
[59]2578  freeMaps(&request_output_real_format);
2579  free(request_output_real_format);
[9]2580 
2581  free(REQUEST);
2582  free(SERVICE_URL);
[1]2583#ifdef DEBUG
2584  fprintf(stderr,"Processed response \n");
2585  fflush(stdout);
2586  fflush(stderr);
2587#endif
2588
2589  return 0;
2590}
[364]2591
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