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