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

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

Various fixes for tickets #93, #94, #95, #95, #96.

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