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

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

Fix issue with language gesture on Ubuntu platforms.

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