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

Last change on this file since 360 was 360, checked in by djay, 12 years ago

Add support for multiple inputs values for the same identifier.

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