[1] | 1 | /** |
---|
| 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
| 4 | * Copyright 2008-2009 GeoLabs SARL. All rights reserved. |
---|
| 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #define length(x) (sizeof(x) / sizeof(x[0])) |
---|
| 26 | |
---|
| 27 | extern "C" int yylex(); |
---|
| 28 | extern "C" int crlex(); |
---|
| 29 | extern "C" { |
---|
| 30 | #include <libxml/tree.h> |
---|
| 31 | #include <libxml/xmlmemory.h> |
---|
| 32 | #include <libxml/parser.h> |
---|
| 33 | #include <libxml/xpath.h> |
---|
| 34 | #include <libxml/xpathInternals.h> |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | #include "cgic.h" |
---|
| 38 | #include "ulinet.h" |
---|
| 39 | |
---|
| 40 | #include <string.h> |
---|
| 41 | |
---|
| 42 | #include "service.h" |
---|
| 43 | #include "service_internal.h" |
---|
| 44 | #include "service_internal_python.h" |
---|
| 45 | |
---|
| 46 | #ifdef USE_JAVA |
---|
| 47 | #include "service_internal_java.h" |
---|
| 48 | #endif |
---|
| 49 | |
---|
| 50 | #ifdef USE_PHP |
---|
| 51 | #include "service_internal_php.h" |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | #ifdef USE_JS |
---|
| 55 | #include "service_internal_js.h" |
---|
| 56 | #endif |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | #include <dirent.h> |
---|
| 60 | #include <signal.h> |
---|
| 61 | #include <unistd.h> |
---|
| 62 | #ifndef WIN32 |
---|
| 63 | #include <dlfcn.h> |
---|
| 64 | #include <libgen.h> |
---|
| 65 | #else |
---|
| 66 | #include <windows.h> |
---|
| 67 | #include <direct.h> |
---|
| 68 | #endif |
---|
| 69 | #include <fcntl.h> |
---|
| 70 | #include <time.h> |
---|
| 71 | #include <stdarg.h> |
---|
| 72 | |
---|
| 73 | xmlNodeSet* extractFromDoc(xmlDocPtr doc,char* search){ |
---|
| 74 | xmlXPathContextPtr xpathCtx; |
---|
| 75 | xmlXPathObjectPtr xpathObj; |
---|
| 76 | xpathCtx = xmlXPathNewContext(doc); |
---|
| 77 | xpathObj = xmlXPathEvalExpression(BAD_CAST search,xpathCtx); |
---|
| 78 | xmlXPathFreeContext(xpathCtx); |
---|
| 79 | return xpathObj->nodesetval; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void sigint_handler(int sig){ |
---|
| 83 | fprintf(stderr,"Not this time!\n"); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | int runRequest(map* request_inputs) |
---|
| 87 | { |
---|
| 88 | |
---|
| 89 | map* r_inputs=NULL,*tmps=NULL; |
---|
| 90 | maps* m=NULL; |
---|
| 91 | int argc=count(request_inputs); |
---|
| 92 | |
---|
| 93 | char* REQUEST=NULL; |
---|
| 94 | /** |
---|
| 95 | * Parsing service specfic configuration file |
---|
| 96 | */ |
---|
| 97 | m=(maps*)malloc(MAPS_SIZE); |
---|
| 98 | char ntmp[1024]; |
---|
| 99 | #ifndef WIN32 |
---|
| 100 | getcwd(ntmp,1024); |
---|
| 101 | #else |
---|
| 102 | _getcwd(ntmp,1024); |
---|
| 103 | #endif |
---|
| 104 | char conf_file[1024]; |
---|
| 105 | sprintf(conf_file,"%s/main.cfg",ntmp); |
---|
| 106 | conf_read(conf_file,m); |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Check for minimum inputs |
---|
| 110 | */ |
---|
| 111 | r_inputs=getMap(request_inputs,"Request"); |
---|
| 112 | if(r_inputs==NULLMAP){ |
---|
| 113 | tmps=createMap("text","Parameter <request> was not specified"); |
---|
| 114 | addToMap(tmps,"code","MissingParameterValue"); |
---|
| 115 | printExceptionReportResponse(m,tmps); |
---|
| 116 | return 1; |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | REQUEST=strdup(r_inputs->value); |
---|
| 120 | r_inputs=NULL; |
---|
| 121 | r_inputs=getMap(request_inputs,"Service"); |
---|
| 122 | if(r_inputs==NULLMAP){ |
---|
| 123 | tmps=createMap("text","Parameter <service> was not specified"); |
---|
| 124 | addToMap(tmps,"code","MissingParameterValue"); |
---|
| 125 | printExceptionReportResponse(m,tmps); |
---|
| 126 | return 1; |
---|
| 127 | } |
---|
| 128 | if(strcmp(mtoupper(REQUEST),"GETCAPABILITIES")!=0){ |
---|
| 129 | r_inputs=getMap(request_inputs,"Version"); |
---|
| 130 | if(r_inputs==NULL){ |
---|
| 131 | tmps=createMap("text","Parameter <version> was not specified"); |
---|
| 132 | addToMap(tmps,"code","MissingParameterValue"); |
---|
| 133 | printExceptionReportResponse(m,tmps); |
---|
| 134 | return 1; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Need to print std exception message here |
---|
| 140 | */ |
---|
| 141 | char SP[1024]; |
---|
| 142 | if ((argc < 5 && strcmp(mtoupper(REQUEST),"GETCAPABILITIES")!=0) || (argc < 4 && strcmp(mtoupper(REQUEST),"GETCAPABILITIES")==0)){ |
---|
| 143 | r_inputs=getMap(request_inputs,"ServiceProvider"); |
---|
| 144 | if(r_inputs==NULLMAP){ |
---|
| 145 | tmps=createMap("text","Parameter <serviceprovider> was not specified"); |
---|
| 146 | addToMap(tmps,"code","MissingParameterValue"); |
---|
| 147 | printExceptionReportResponse(m,tmps); |
---|
| 148 | return 1; |
---|
| 149 | } |
---|
| 150 | else |
---|
| 151 | sprintf(SP,"%s",r_inputs->value); |
---|
| 152 | r_inputs=getMap(request_inputs,"MetaPath"); |
---|
| 153 | if(r_inputs==NULLMAP){ |
---|
| 154 | tmps=createMap("text","Parameter <metapath> was not specified"); |
---|
| 155 | addToMap(tmps,"code","MissingParameterValue"); |
---|
| 156 | printExceptionReportResponse(m,tmps); |
---|
| 157 | return 1; |
---|
| 158 | } |
---|
| 159 | tmps=createMap("text","Parameter <unknown> was not specified"); |
---|
| 160 | addToMap(tmps,"code","MissingParameterValue"); |
---|
| 161 | printExceptionReportResponse(m,tmps); |
---|
| 162 | return 1; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | map* outputs=NULL; |
---|
| 166 | maps* request_output_real_format=NULL; |
---|
| 167 | map* tmpm=getMapFromMaps(m,"main","serverAddress"); |
---|
| 168 | if(tmpm!=NULL) |
---|
| 169 | SERVICE_URL=strdup(tmpm->value); |
---|
| 170 | else |
---|
| 171 | SERVICE_URL=DEFAULT_SERVICE_URL; |
---|
| 172 | |
---|
| 173 | service* s[100]; |
---|
| 174 | service* s1; |
---|
| 175 | int scount=0; |
---|
| 176 | |
---|
| 177 | #ifdef DEBUG |
---|
| 178 | dumpMap(r_inputs); |
---|
| 179 | #endif |
---|
| 180 | char conf_dir[1024]; |
---|
| 181 | int t; |
---|
| 182 | char tmps1[1024]; |
---|
| 183 | |
---|
| 184 | if(strcmp(mtoupper(REQUEST),mtoupper("GetCapabilities"))==0){ |
---|
| 185 | int i=0; |
---|
| 186 | struct dirent *dp; |
---|
| 187 | r_inputs=NULL; |
---|
| 188 | r_inputs=getMap(request_inputs,"metapath"); |
---|
| 189 | #ifdef DEBUG |
---|
| 190 | dumpMap(r_inputs); |
---|
| 191 | #endif |
---|
| 192 | sprintf(conf_dir,"%s/%s",ntmp,r_inputs->value); |
---|
| 193 | DIR *dirp = opendir(conf_dir); |
---|
| 194 | if(dirp==NULL){ |
---|
| 195 | tmps=createMap("text","The specified path doesn't exist."); |
---|
| 196 | addToMap(tmps,"code","InvalidParameterValue"); |
---|
| 197 | printExceptionReportResponse(m,tmps); |
---|
| 198 | return -1; |
---|
| 199 | } |
---|
| 200 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
---|
| 201 | doc->encoding = xmlCharStrdup ("UTF-8"); |
---|
| 202 | r_inputs=NULL; |
---|
| 203 | r_inputs=getMap(request_inputs,"ServiceProvider"); |
---|
| 204 | xmlNodePtr n = printGetCapabilitiesHeader(doc,r_inputs->value,m); |
---|
| 205 | /** |
---|
| 206 | * Strange, here we need to close stdout to ensure that no uneeded |
---|
| 207 | * char will be printed (parser issue ?) |
---|
| 208 | */ |
---|
| 209 | int saved_stdout = dup(fileno(stdout)); |
---|
| 210 | dup2(fileno(stderr),fileno(stdout)); |
---|
| 211 | while ((dp = readdir(dirp)) != NULL) |
---|
| 212 | if(strstr(dp->d_name,".zcfg")!=0){ |
---|
| 213 | sprintf(tmps1,"%s/%s",conf_dir,dp->d_name); |
---|
| 214 | char *tmps=tmps1; |
---|
| 215 | s1=(service*)malloc(sizeof(service*)); |
---|
| 216 | s[0]=(service*)malloc(sizeof(service*)); |
---|
| 217 | #ifdef DEBUG |
---|
| 218 | fprintf(stderr,"#################\n%s\n#################\n",tmps1); |
---|
| 219 | #endif |
---|
| 220 | t=getServiceFromFile(tmps1,&s1); |
---|
| 221 | #ifdef DEBUG |
---|
| 222 | dumpService(s1); |
---|
| 223 | fflush(stdout); |
---|
| 224 | fflush(stderr); |
---|
| 225 | #endif |
---|
| 226 | s[0]=s1; |
---|
| 227 | printGetCapabilitiesForProcess(m,n,s1); |
---|
| 228 | //freeService(&s1); |
---|
| 229 | //s1=NULL; |
---|
| 230 | scount++; |
---|
| 231 | } |
---|
| 232 | (void)closedir(dirp); |
---|
| 233 | fflush(stdout); |
---|
| 234 | dup2(saved_stdout,fileno(stdout)); |
---|
| 235 | |
---|
| 236 | printDocument(doc); |
---|
| 237 | fflush(stdout); |
---|
| 238 | xmlFree(n); |
---|
| 239 | return 0; |
---|
| 240 | } |
---|
| 241 | else{ |
---|
| 242 | r_inputs=getMap(request_inputs,"Identifier"); |
---|
| 243 | if(r_inputs==NULL |
---|
| 244 | || strlen(r_inputs->name)==0 || strlen(r_inputs->value)==0){ |
---|
| 245 | if(r_inputs!=NULL && strlen(r_inputs->value)==0) |
---|
| 246 | tmps=createMap("text","Mandatory value for <identifier> was not specified"); |
---|
| 247 | else |
---|
| 248 | tmps=createMap("text","Mandatory <identifier> was not specified"); |
---|
| 249 | addToMap(tmps,"code","MissingParameterValue"); |
---|
| 250 | printExceptionReportResponse(m,tmps); |
---|
| 251 | return 1; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | r_inputs=getMap(request_inputs,"metapath"); |
---|
| 255 | sprintf(conf_dir,"%s/%s",ntmp,r_inputs->value); |
---|
| 256 | struct dirent *dp; |
---|
| 257 | DIR *dirp = opendir(conf_dir); |
---|
| 258 | if(dirp==NULL){ |
---|
| 259 | tmps=createMap("text","The specified metapath path doesn't exist."); |
---|
| 260 | addToMap(tmps,"code","InvalidParameterValue"); |
---|
| 261 | printExceptionReportResponse(m,tmps); |
---|
| 262 | return -1; |
---|
| 263 | } |
---|
| 264 | if(strcmp(mtoupper(REQUEST),"DESCRIBEPROCESS")==0){ |
---|
| 265 | /** |
---|
| 266 | * Loop over Identifier list |
---|
| 267 | */ |
---|
| 268 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
---|
| 269 | doc->encoding = xmlCharStrdup ("UTF-8"); |
---|
| 270 | r_inputs=NULL; |
---|
| 271 | r_inputs=getMap(request_inputs,"ServiceProvider"); |
---|
| 272 | xmlNodePtr n; |
---|
| 273 | if(r_inputs!=NULL) |
---|
| 274 | n = printDescribeProcessHeader(doc,r_inputs->value,m); |
---|
| 275 | |
---|
| 276 | r_inputs=getMap(request_inputs,"Identifier"); |
---|
| 277 | char *tmps=strtok(r_inputs->value,","); |
---|
| 278 | |
---|
| 279 | char buff[256]; |
---|
| 280 | char buff1[1024]; |
---|
| 281 | int i=0; |
---|
| 282 | int j=0; |
---|
| 283 | int end=-1; |
---|
| 284 | int saved_stdout = dup(fileno(stdout)); |
---|
| 285 | dup2(fileno(stderr),fileno(stdout)); |
---|
| 286 | while(tmps){ |
---|
| 287 | memset(buff,0,256); |
---|
| 288 | sprintf(buff,"%s.zcfg",tmps); |
---|
| 289 | memset(buff1,0,1024); |
---|
| 290 | #ifdef DEBUG |
---|
| 291 | fprintf(stderr,"\n#######%s\n########\n",buff1); |
---|
| 292 | #endif |
---|
| 293 | while ((dp = readdir(dirp)) != NULL) |
---|
| 294 | if(strcmp(dp->d_name,buff)==0){ |
---|
| 295 | memset(buff1,0,1024); |
---|
| 296 | sprintf(buff1,"%s/%s",conf_dir,dp->d_name); |
---|
| 297 | s1=(service*)malloc(sizeof(service*)); |
---|
| 298 | s[scount]=(service*)malloc(sizeof(service*)); |
---|
| 299 | #ifdef DEBUG |
---|
| 300 | fprintf(stderr,"#################\n%s\n#################\n",tmps1); |
---|
| 301 | #endif |
---|
| 302 | t=getServiceFromFile(buff1,&s1); |
---|
| 303 | /*dumpService(s1); |
---|
| 304 | fflush(stdout); |
---|
| 305 | fflush(stderr);*/ |
---|
| 306 | s[0]=s1; |
---|
| 307 | printDescribeProcessForProcess(m,n,s,1); |
---|
| 308 | scount++; |
---|
| 309 | } |
---|
| 310 | rewinddir(dirp); |
---|
| 311 | tmps=strtok(NULL,","); |
---|
| 312 | } |
---|
| 313 | (void)closedir(dirp); |
---|
| 314 | fflush(stdout); |
---|
| 315 | dup2(saved_stdout,fileno(stdout)); |
---|
| 316 | |
---|
| 317 | printDocument(doc); |
---|
| 318 | fflush(stdout); |
---|
| 319 | //xmlFree(n); |
---|
| 320 | #ifndef LINUX_FREE_ISSUE |
---|
| 321 | free(s1); |
---|
| 322 | #endif |
---|
| 323 | return 0; |
---|
| 324 | } |
---|
| 325 | else |
---|
| 326 | if(strcmp(mtoupper(REQUEST),"EXECUTE")!=0){ |
---|
| 327 | tmps=createMap("text","Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."); |
---|
| 328 | addToMap(tmps,"code","InvalidParameterValue"); |
---|
| 329 | printExceptionReportResponse(m,tmps); |
---|
| 330 | #ifdef DEBUG |
---|
| 331 | fprintf(stderr,"No request found %s",REQUEST); |
---|
| 332 | #endif |
---|
| 333 | free(s); |
---|
| 334 | return 0; |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | s1=NULL; |
---|
| 339 | s1=(service*)malloc(sizeof(service*)); |
---|
| 340 | s[0]=(service*)malloc(sizeof(service*)); |
---|
| 341 | r_inputs=getMap(request_inputs,"MetaPath"); |
---|
| 342 | sprintf(tmps1,"%s/%s",ntmp,r_inputs->value); |
---|
| 343 | r_inputs=getMap(request_inputs,"Identifier"); |
---|
| 344 | |
---|
| 345 | sprintf(tmps1,"%s/%s.zcfg",strdup(tmps1),r_inputs->value); |
---|
| 346 | |
---|
| 347 | #ifdef DEBUG |
---|
| 348 | fprintf(stderr,"Trying to load %s\n", tmps1); |
---|
| 349 | #endif |
---|
| 350 | int saved_stdout = dup(fileno(stdout)); |
---|
| 351 | dup2(fileno(stderr),fileno(stdout)); |
---|
| 352 | t=getServiceFromFile(tmps1,&s1); |
---|
| 353 | fflush(stdout); |
---|
| 354 | dup2(saved_stdout,fileno(stdout)); |
---|
| 355 | if(t==22){ |
---|
| 356 | tmps=createMap("text","The value for <indetifier> seems to be wrong. Please, ensure that the process exsits using the GetCapabilities request."); |
---|
| 357 | addToMap(tmps,"code","InvalidParameterValue"); |
---|
| 358 | printExceptionReportResponse(m,tmps); |
---|
| 359 | exit(0); |
---|
| 360 | } |
---|
| 361 | s[0]=s1; |
---|
| 362 | |
---|
| 363 | #ifdef DEBUG |
---|
| 364 | dumpService(s1); |
---|
| 365 | #endif |
---|
| 366 | map* inputs=NULL; |
---|
| 367 | elements* c_inputs=s1->inputs; |
---|
| 368 | int j; |
---|
| 369 | |
---|
| 370 | /** |
---|
| 371 | * Create the input maps data structure |
---|
| 372 | */ |
---|
| 373 | int i=0; |
---|
| 374 | HINTERNET hInternet; |
---|
| 375 | HINTERNET res; |
---|
| 376 | hInternet=InternetOpen( |
---|
| 377 | #ifndef WIN32 |
---|
| 378 | (LPCTSTR) |
---|
| 379 | #endif |
---|
| 380 | "ZooWPSClient\0", |
---|
| 381 | INTERNET_OPEN_TYPE_PRECONFIG, |
---|
| 382 | NULL,NULL, 0); |
---|
| 383 | |
---|
| 384 | #ifndef WIN32 |
---|
| 385 | if(!CHECK_INET_HANDLE(hInternet)) |
---|
| 386 | fprintf(stderr,"WARNING : hInternet handle failed to initialize"); |
---|
| 387 | #endif |
---|
| 388 | maps* request_input_real_format=NULL; |
---|
| 389 | maps* tmpmaps = request_input_real_format; |
---|
| 390 | map* postRequest=NULL; |
---|
| 391 | postRequest=getMap(request_inputs,"xrequest"); |
---|
| 392 | if(postRequest==NULLMAP){ |
---|
| 393 | /** |
---|
| 394 | * Parsing outputs provided as KVP |
---|
| 395 | */ |
---|
| 396 | r_inputs=NULL; |
---|
| 397 | #ifdef DEBUG |
---|
| 398 | fprintf(stderr,"OUTPUT Parsing ... \n"); |
---|
| 399 | #endif |
---|
| 400 | r_inputs=getMap(request_inputs,"ResponseDocument"); |
---|
| 401 | if(r_inputs==NULLMAP) |
---|
| 402 | r_inputs=getMap(request_inputs,"RawDataOutput"); |
---|
| 403 | #ifdef DEBUG |
---|
| 404 | fprintf(stderr,"OUTPUT Parsing ... \n"); |
---|
| 405 | #endif |
---|
| 406 | if(r_inputs!=NULLMAP){ |
---|
| 407 | #ifdef DEBUG |
---|
| 408 | fprintf(stderr,"OUTPUT Parsing start now ... \n"); |
---|
| 409 | #endif |
---|
| 410 | char current_output_as_string[10240]; |
---|
| 411 | char cursor_output[10240]; |
---|
| 412 | sprintf(cursor_output,"%s",strdup(r_inputs->value)); |
---|
| 413 | j=0; |
---|
| 414 | map* request_kvp_outputs=NULL; |
---|
| 415 | |
---|
| 416 | /** |
---|
| 417 | * Put each Output into the outputs_as_text array |
---|
| 418 | */ |
---|
| 419 | char * pToken; |
---|
| 420 | maps* tmp_output=NULL; |
---|
| 421 | #ifdef DEBUG |
---|
| 422 | fprintf(stderr,"OUTPUT [%s]\n",cursor_output); |
---|
| 423 | #endif |
---|
| 424 | pToken=strtok(cursor_output,";"); |
---|
| 425 | char** outputs_as_text=(char**)malloc(128*sizeof(char*)); |
---|
| 426 | i=0; |
---|
| 427 | while(pToken!=NULL){ |
---|
| 428 | #ifdef DEBUG |
---|
| 429 | fprintf(stderr,"***%s***\n",pToken); |
---|
| 430 | fflush(stderr); |
---|
| 431 | fprintf(stderr,"***%s***\n",pToken); |
---|
| 432 | #endif |
---|
| 433 | outputs_as_text[i]=(char*)malloc(strlen(pToken)*sizeof(char)); |
---|
| 434 | sprintf(outputs_as_text[i],"%s",pToken); |
---|
| 435 | pToken = strtok(NULL,";"); |
---|
| 436 | i++; |
---|
| 437 | } |
---|
| 438 | for(j=0;j<i;j++){ |
---|
| 439 | char *tmp=strdup(outputs_as_text[j]); |
---|
| 440 | char *tmpc; |
---|
| 441 | tmpc=strtok(tmp,"@"); |
---|
| 442 | int k=0; |
---|
| 443 | while(tmpc!=NULL){ |
---|
| 444 | if(k==0){ |
---|
| 445 | if(tmp_output==NULL){ |
---|
| 446 | tmp_output=(maps*)malloc(MAPS_SIZE); |
---|
| 447 | tmp_output->name=strdup(tmpc); |
---|
| 448 | tmp_output->content=NULL; |
---|
| 449 | tmp_output->next=NULL; |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | else{ |
---|
| 453 | char *tmpv=strstr(tmpc,"="); |
---|
| 454 | char tmpn[256]; |
---|
| 455 | memset(tmpn,0,256); |
---|
| 456 | strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char)); |
---|
| 457 | tmpn[strlen(tmpc)-strlen(tmpv)]=0; |
---|
| 458 | #ifdef DEBUG |
---|
| 459 | fprintf(stderr,"OUTPUT DEF [%s]=[%s]\n",tmpn,tmpv+1); |
---|
| 460 | #endif |
---|
| 461 | if(tmp_output->content==NULL){ |
---|
| 462 | tmp_output->content=createMap(tmpn,tmpv+1); |
---|
| 463 | tmp_output->content->next=NULL; |
---|
| 464 | } |
---|
| 465 | else |
---|
| 466 | addToMap(tmp_output->content,tmpn,tmpv+1); |
---|
| 467 | } |
---|
| 468 | k++; |
---|
| 469 | #ifdef DEBUG |
---|
| 470 | fprintf(stderr,"***%s***\n",tmpc); |
---|
| 471 | #endif |
---|
| 472 | tmpc=strtok(NULL,"@"); |
---|
| 473 | } |
---|
| 474 | if(request_output_real_format==NULL) |
---|
| 475 | request_output_real_format=tmp_output; |
---|
| 476 | else |
---|
| 477 | addMapsToMaps(&request_output_real_format,tmp_output); |
---|
| 478 | #ifdef DEBUG |
---|
| 479 | dumpMaps(tmp_output); |
---|
| 480 | fflush(stderr); |
---|
| 481 | #endif |
---|
| 482 | tmp_output=tmp_output->next; |
---|
| 483 | } |
---|
| 484 | //dumpMaps(request_output_real_format); |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | /** |
---|
| 489 | * Parsing inputs provided as KVP |
---|
| 490 | */ |
---|
| 491 | r_inputs=getMap(request_inputs,"DataInputs"); |
---|
| 492 | #ifdef DEBUG |
---|
| 493 | fprintf(stderr,"DATA INPUTS [%s]\n",r_inputs->value); |
---|
| 494 | #endif |
---|
| 495 | char current_input_as_string[40960]; |
---|
| 496 | char cursor_input[40960]; |
---|
| 497 | sprintf(cursor_input,"%s",strdup(r_inputs->value)); |
---|
| 498 | j=0; |
---|
| 499 | map* request_kvp_inputs=NULL; |
---|
| 500 | |
---|
| 501 | /** |
---|
| 502 | * Put each DataInputs into the inputs_as_text array |
---|
| 503 | */ |
---|
| 504 | char * pToken; |
---|
| 505 | pToken=strtok(cursor_input,";"); |
---|
| 506 | char** inputs_as_text=(char**)malloc(100*sizeof(char*)); |
---|
| 507 | i=0; |
---|
| 508 | while(pToken!=NULL){ |
---|
| 509 | #ifdef DEBUG |
---|
| 510 | fprintf(stderr,"***%s***\n",pToken); |
---|
| 511 | #endif |
---|
| 512 | fflush(stderr); |
---|
| 513 | #ifdef DEBUG |
---|
| 514 | fprintf(stderr,"***%s***\n",pToken); |
---|
| 515 | #endif |
---|
| 516 | inputs_as_text[i]=(char*)malloc(strlen(pToken)*sizeof(char)); |
---|
| 517 | sprintf(inputs_as_text[i],"%s",pToken); |
---|
| 518 | pToken = strtok(NULL,";"); |
---|
| 519 | i++; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | for(j=0;j<i;j++){ |
---|
| 523 | char *tmp=strdup(inputs_as_text[j]); |
---|
| 524 | char *tmpc; |
---|
| 525 | tmpc=strtok(tmp,"@"); |
---|
| 526 | while(tmpc!=NULL){ |
---|
| 527 | #ifdef DEBUG |
---|
| 528 | fprintf(stderr,"***\n***%s***\n",tmpc); |
---|
| 529 | #endif |
---|
| 530 | char *tmpv=strstr(tmpc,"="); |
---|
| 531 | char tmpn[256]; |
---|
| 532 | memset(tmpn,0,256); |
---|
| 533 | strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char)); |
---|
| 534 | tmpn[strlen(tmpc)-strlen(tmpv)]=0; |
---|
| 535 | int cnt=0; |
---|
| 536 | #ifdef DEBUG |
---|
| 537 | fprintf(stderr,"***\n*** %s = %s ***\n",tmpn,tmpv+1); |
---|
| 538 | #endif |
---|
| 539 | if(tmpmaps==NULL){ |
---|
| 540 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 541 | tmpmaps->name=strdup(tmpn); |
---|
| 542 | tmpmaps->content=createMap("value",tmpv+1); |
---|
| 543 | tmpmaps->next=NULL; |
---|
| 544 | } |
---|
| 545 | tmpc=strtok(NULL,"@"); |
---|
| 546 | while(tmpc!=NULL){ |
---|
| 547 | #ifdef DEBUG |
---|
| 548 | fprintf(stderr,"*** KVP NON URL-ENCODED \n***%s***\n",tmpc); |
---|
| 549 | #endif |
---|
| 550 | char *tmpv1=strstr(tmpc,"="); |
---|
| 551 | #ifdef DEBUG |
---|
| 552 | fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1); |
---|
| 553 | #endif |
---|
| 554 | char tmpn1[1024]; |
---|
| 555 | memset(tmpn1,0,1024); |
---|
| 556 | strncpy(tmpn1,tmpc,strlen(tmpc)-strlen(tmpv1)); |
---|
| 557 | tmpn1[strlen(tmpc)-strlen(tmpv1)]=0; |
---|
| 558 | #ifdef DEBUG |
---|
| 559 | fprintf(stderr,"*** NAME NON URL-ENCODED \n***%s***\n",tmpn1); |
---|
| 560 | fprintf(stderr,"*** VALUE NON URL-ENCODED \n***%s***\n",tmpv1+1); |
---|
| 561 | #endif |
---|
| 562 | if(strcmp(tmpn1,"xlink:href")!=0) |
---|
| 563 | addToMap(tmpmaps->content,tmpn1,tmpv1+1); |
---|
| 564 | else{ |
---|
| 565 | #ifdef DEBUG |
---|
| 566 | fprintf(stderr,"REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",tmpv1+1); |
---|
| 567 | #endif |
---|
| 568 | #ifndef WIN32 |
---|
| 569 | if(CHECK_INET_HANDLE(hInternet)) |
---|
| 570 | #endif |
---|
| 571 | { |
---|
| 572 | res=InternetOpenUrl(hInternet,tmpv1+1,NULL,0, |
---|
| 573 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 574 | #ifdef DEBUG |
---|
| 575 | fprintf(stderr,"(%s) content-length : %d,,res.nDataAlloc %d \n", |
---|
| 576 | tmpv1+1,res.nDataAlloc,res.nDataLen); |
---|
| 577 | #endif |
---|
| 578 | char* tmpContent=(char*)malloc((res.nDataLen+1)*sizeof(char)); |
---|
| 579 | size_t dwRead; |
---|
| 580 | InternetReadFile(res, (LPVOID)tmpContent,res.nDataLen, &dwRead); |
---|
| 581 | map* tmpMap=getMap(tmpmaps->content,"value"); |
---|
| 582 | if(tmpMap!=NULL) |
---|
| 583 | tmpMap->value=tmpContent; |
---|
| 584 | } |
---|
| 585 | addToMap(tmpmaps->content,tmpn1,tmpv1+1); |
---|
| 586 | } |
---|
| 587 | tmpc=strtok(NULL,"@"); |
---|
| 588 | } |
---|
| 589 | if(request_input_real_format==NULL) |
---|
| 590 | request_input_real_format=tmpmaps; |
---|
| 591 | else |
---|
| 592 | addMapsToMaps(&request_input_real_format,tmpmaps); |
---|
| 593 | #ifdef DEBUG |
---|
| 594 | dumpMaps(tmpmaps); |
---|
| 595 | fflush(stderr); |
---|
| 596 | #endif |
---|
| 597 | tmpmaps=tmpmaps->next; |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | } |
---|
| 601 | else { |
---|
| 602 | xmlInitParser(); |
---|
| 603 | //dumpMap(postRequest); |
---|
| 604 | #ifdef DEBUG |
---|
| 605 | fflush(stderr); |
---|
| 606 | fprintf(stderr,"BEFORE %s\n",postRequest->value); |
---|
| 607 | fflush(stderr); |
---|
| 608 | #endif |
---|
| 609 | xmlDocPtr doc = |
---|
| 610 | xmlParseMemory(postRequest->value,cgiContentLength);//strlen(postRequest->value)); |
---|
| 611 | #ifdef DEBUG |
---|
| 612 | fprintf(stderr,"AFTER\n"); |
---|
| 613 | fflush(stderr); |
---|
| 614 | #endif |
---|
| 615 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
| 616 | /** |
---|
| 617 | * Parse every Input in DataInputs node. |
---|
| 618 | */ |
---|
| 619 | maps* tempMaps=NULL; |
---|
| 620 | xmlNodeSet* tmps=extractFromDoc(doc, |
---|
| 621 | "/*/*/*[local-name()='Input']"); |
---|
| 622 | #ifdef DEBUG |
---|
| 623 | fprintf(stderr,"*****%d*****\n",tmps->nodeNr); |
---|
| 624 | #endif |
---|
| 625 | for(int k=0;k<tmps->nodeNr;k++){ |
---|
| 626 | maps *tmpmaps=NULL; |
---|
| 627 | xmlNodePtr cur=tmps->nodeTab[k]; |
---|
| 628 | if(tmps->nodeTab[k]->type == XML_ELEMENT_NODE) { |
---|
| 629 | /** |
---|
| 630 | * A specific Input node. |
---|
| 631 | */ |
---|
| 632 | #ifdef DEBUG |
---|
| 633 | fprintf(stderr, "= element 0 node \"%s\"\n", cur->name); |
---|
| 634 | #endif |
---|
| 635 | xmlNodePtr cur1=cur->children; |
---|
| 636 | while(cur1){ |
---|
| 637 | #ifdef DEBUG |
---|
| 638 | fprintf(stderr, "= element 1 node \"%s\" = (%s)\n", |
---|
| 639 | cur1->name,xmlNodeListGetString(doc,cur1,1)); |
---|
| 640 | #endif |
---|
| 641 | xmlNodePtr cur2=cur1/*->next*/; |
---|
| 642 | while(cur2){ |
---|
| 643 | /** |
---|
| 644 | * Indentifier |
---|
| 645 | */ |
---|
| 646 | if(strcmp(mtoupper((char*)cur2->name), |
---|
| 647 | mtoupper("Identifier"))==0){ |
---|
| 648 | xmlChar *val= |
---|
| 649 | xmlNodeListGetString(doc,cur2->xmlChildrenNode,1); |
---|
| 650 | if(tmpmaps==NULL){ |
---|
| 651 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 652 | tmpmaps->name=strdup((char*)val); |
---|
| 653 | tmpmaps->content=NULL; |
---|
| 654 | tmpmaps->next=NULL; |
---|
| 655 | } |
---|
| 656 | xmlFree(val); |
---|
| 657 | } |
---|
| 658 | /** |
---|
| 659 | * Title, Asbtract |
---|
| 660 | */ |
---|
| 661 | if(strcmp(mtoupper((char*)cur2->name), |
---|
| 662 | mtoupper("Title"))==0 || |
---|
| 663 | strcmp(mtoupper((char*)cur2->name), |
---|
| 664 | mtoupper("Abstract"))==0){ |
---|
| 665 | xmlChar *val= |
---|
| 666 | xmlNodeListGetString(doc,cur2->xmlChildrenNode,1); |
---|
| 667 | if(tmpmaps==NULL){ |
---|
| 668 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 669 | tmpmaps->name="missingIndetifier"; |
---|
| 670 | tmpmaps->content=createMap((char*)cur2->name,(char*)val); |
---|
| 671 | tmpmaps->next=NULL; |
---|
| 672 | } |
---|
| 673 | else{ |
---|
| 674 | if(tmpmaps->content!=NULL) |
---|
| 675 | addToMap(tmpmaps->content, |
---|
| 676 | (char*)cur2->name,(char*)val); |
---|
| 677 | else |
---|
| 678 | tmpmaps->content= |
---|
| 679 | createMap((char*)cur2->name,(char*)val); |
---|
| 680 | } |
---|
| 681 | #ifdef DEBUG |
---|
| 682 | dumpMaps(tmpmaps); |
---|
| 683 | #endif |
---|
| 684 | xmlFree(val); |
---|
| 685 | } |
---|
| 686 | /** |
---|
| 687 | * InputDataFormChoice (Reference or Data ?) |
---|
| 688 | */ |
---|
| 689 | if(strcmp(mtoupper((char*)cur2->name), |
---|
| 690 | mtoupper("Reference"))==0){ |
---|
| 691 | /** |
---|
| 692 | * Get every attribute from a Reference node |
---|
| 693 | * mimeType, encoding, schema, href, method |
---|
| 694 | * Header and Body gesture should be added here |
---|
| 695 | */ |
---|
| 696 | #ifdef DEBUG |
---|
| 697 | fprintf(stderr,"REFERENCE\n"); |
---|
| 698 | #endif |
---|
| 699 | map* referenceMap=NULL; |
---|
| 700 | char *refs[5]; |
---|
| 701 | refs[0]="mimeType"; |
---|
| 702 | refs[1]="encoding"; |
---|
| 703 | refs[2]="schema"; |
---|
| 704 | refs[3]="method"; |
---|
| 705 | refs[4]="href"; |
---|
| 706 | char*url; |
---|
| 707 | for(int l=0;l<5;l++){ |
---|
| 708 | #ifdef DEBUG |
---|
| 709 | fprintf(stderr,"*** %s ***\t",refs[l]); |
---|
| 710 | #endif |
---|
| 711 | xmlChar *val=xmlGetProp(cur2,BAD_CAST refs[l]); |
---|
| 712 | if(val!=NULL && strlen((char*)val)>0){ |
---|
| 713 | if(tmpmaps->content!=NULL) |
---|
| 714 | addToMap(tmpmaps->content,refs[l],(char*)val); |
---|
| 715 | else |
---|
| 716 | tmpmaps->content=createMap(refs[l],(char*)val); |
---|
| 717 | map* ltmp=getMap(tmpmaps->content,"method"); |
---|
| 718 | if(l==4){ |
---|
| 719 | if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0) |
---|
| 720 | && CHECK_INET_HANDLE(hInternet)){ |
---|
| 721 | res=InternetOpenUrl(hInternet,(char*)val,NULL,0, |
---|
| 722 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 723 | char* tmpContent= |
---|
| 724 | (char*)malloc((res.nDataLen+1)*sizeof(char)); |
---|
| 725 | size_t dwRead; |
---|
| 726 | InternetReadFile(res, (LPVOID)tmpContent, |
---|
| 727 | res.nDataLen, &dwRead); |
---|
| 728 | tmpContent[res.nDataLen]=0; |
---|
| 729 | addToMap(tmpmaps->content,"value",tmpContent); |
---|
| 730 | } |
---|
| 731 | } |
---|
| 732 | |
---|
| 733 | } |
---|
| 734 | #ifdef DEBUG |
---|
| 735 | fprintf(stderr,"%s\n",val); |
---|
| 736 | #endif |
---|
| 737 | xmlFree(val); |
---|
| 738 | } |
---|
| 739 | #ifdef POST_DEBUG |
---|
| 740 | fprintf(stderr,"Parse Header and Body from Reference \n"); |
---|
| 741 | #endif |
---|
| 742 | xmlNodePtr cur3=cur2->children; |
---|
| 743 | hInternet.header=NULL; |
---|
| 744 | while(cur3){ |
---|
| 745 | if(strcmp(mtoupper((char*)cur3->name), mtoupper("Header"))==0 ){ |
---|
| 746 | xmlNodePtr cur4=cur3; |
---|
| 747 | char *tmp=new char[cgiContentLength]; |
---|
| 748 | char *ha[2]; |
---|
| 749 | ha[0]="key"; |
---|
| 750 | ha[1]="value"; |
---|
| 751 | int hai; |
---|
| 752 | char *has; |
---|
| 753 | char *key; |
---|
| 754 | for(hai=0;hai<2;hai++){ |
---|
| 755 | xmlChar *val=xmlGetProp(cur3,BAD_CAST ha[hai]); |
---|
| 756 | #ifdef POST_DEBUG |
---|
| 757 | fprintf(stderr,"%s = %s\n",ha[hai],(char*)val); |
---|
| 758 | #endif |
---|
| 759 | if(hai==0){ |
---|
| 760 | key=(char*)malloc((1+strlen((char*)val))*sizeof(char)); |
---|
| 761 | sprintf(key,"%s",(char*)val); |
---|
| 762 | }else{ |
---|
| 763 | has=(char*)malloc((3+strlen((char*)val)+strlen(key))*sizeof(char)); |
---|
| 764 | sprintf(has,"%s: %s",key,(char*)val); |
---|
| 765 | #ifdef POST_DEBUG |
---|
| 766 | fprintf(stderr,"%s\n",has); |
---|
| 767 | #endif |
---|
| 768 | } |
---|
| 769 | } |
---|
| 770 | hInternet.header=curl_slist_append(hInternet.header, has); |
---|
| 771 | //free(has); |
---|
| 772 | } |
---|
| 773 | else{ |
---|
| 774 | #ifdef POST_DEBUG |
---|
| 775 | fprintf(stderr,"Try to fetch the body part of the request ...\n"); |
---|
| 776 | #endif |
---|
| 777 | if(strcmp(mtoupper((char*)cur3->name),mtoupper("Body"))==0 ){ |
---|
| 778 | #ifdef POST_DEBUG |
---|
| 779 | fprintf(stderr,"Body part found !!!\n",(char*)cur3->content); |
---|
| 780 | #endif |
---|
| 781 | char *tmp=new char[cgiContentLength]; |
---|
| 782 | memset(tmp,cgiContentLength,0); |
---|
| 783 | xmlNodePtr cur4=cur3->children; |
---|
| 784 | while(cur4!=NULL){ |
---|
| 785 | xmlDocPtr bdoc = xmlNewDoc(BAD_CAST "1.0"); |
---|
| 786 | bdoc->encoding = xmlCharStrdup ("UTF-8"); |
---|
| 787 | xmlDocSetRootElement(bdoc,cur4); |
---|
| 788 | xmlChar* btmps; |
---|
| 789 | int bsize; |
---|
| 790 | xmlDocDumpMemory(bdoc,&btmps,&bsize); |
---|
| 791 | #ifdef POST_DEBUG |
---|
| 792 | fprintf(stderr,"Body part found !!! %s %s\n",tmp,(char*)btmps); |
---|
| 793 | #endif |
---|
| 794 | if(btmps!=NULL) |
---|
| 795 | sprintf(tmp,"%s",(char*)btmps); |
---|
| 796 | xmlFreeDoc(bdoc); |
---|
| 797 | cur4=cur4->next; |
---|
| 798 | } |
---|
| 799 | map *btmp=getMap(tmpmaps->content,"href"); |
---|
| 800 | if(btmp!=NULL){ |
---|
| 801 | #ifdef POST_DEBUG |
---|
| 802 | fprintf(stderr,"%s %s\n",btmp->value,tmp); |
---|
| 803 | curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1); |
---|
| 804 | #endif |
---|
| 805 | res=InternetOpenUrl(hInternet,btmp->value,tmp,strlen(tmp), |
---|
| 806 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 807 | char* tmpContent= |
---|
| 808 | (char*)malloc((res.nDataLen+1)*sizeof(char)); |
---|
| 809 | size_t dwRead; |
---|
| 810 | InternetReadFile(res, (LPVOID)tmpContent, |
---|
| 811 | res.nDataLen, &dwRead); |
---|
| 812 | tmpContent[res.nDataLen]=0; |
---|
| 813 | if(hInternet.header!=NULL) |
---|
| 814 | curl_slist_free_all(hInternet.header); |
---|
| 815 | addToMap(tmpmaps->content,"value",tmpContent); |
---|
| 816 | #ifdef POST_DEBUG |
---|
| 817 | fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent); |
---|
| 818 | #endif |
---|
| 819 | } |
---|
| 820 | } |
---|
| 821 | else |
---|
| 822 | if(strcmp(mtoupper((char*)cur3->name),mtoupper("BodyReference"))==0 ){ |
---|
| 823 | xmlChar *val=xmlGetProp(cur3,BAD_CAST "href"); |
---|
| 824 | HINTERNET bInternet,res1; |
---|
| 825 | bInternet=InternetOpen( |
---|
| 826 | #ifndef WIN32 |
---|
| 827 | (LPCTSTR) |
---|
| 828 | #endif |
---|
| 829 | "ZooWPSClient\0", |
---|
| 830 | INTERNET_OPEN_TYPE_PRECONFIG, |
---|
| 831 | NULL,NULL, 0); |
---|
| 832 | if(!CHECK_INET_HANDLE(bInternet)) |
---|
| 833 | fprintf(stderr,"WARNING : hInternet handle failed to initialize"); |
---|
| 834 | #ifdef POST_DEBUG |
---|
| 835 | curl_easy_setopt(bInternet.handle, CURLOPT_VERBOSE, 1); |
---|
| 836 | #endif |
---|
| 837 | res1=InternetOpenUrl(bInternet,(char*)val,NULL,0, |
---|
| 838 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 839 | char* tmp= |
---|
| 840 | (char*)malloc((res1.nDataLen+1)*sizeof(char)); |
---|
| 841 | size_t bRead; |
---|
| 842 | InternetReadFile(res1, (LPVOID)tmp, |
---|
| 843 | res1.nDataLen, &bRead); |
---|
| 844 | tmp[res1.nDataLen]=0; |
---|
| 845 | InternetCloseHandle(bInternet); |
---|
| 846 | map *btmp=getMap(tmpmaps->content,"href"); |
---|
| 847 | if(btmp!=NULL){ |
---|
| 848 | #ifdef POST_DEBUG |
---|
| 849 | fprintf(stderr,"%s %s\n",btmp->value,tmp); |
---|
| 850 | curl_easy_setopt(hInternet.handle, CURLOPT_VERBOSE, 1); |
---|
| 851 | #endif |
---|
| 852 | res=InternetOpenUrl(hInternet,btmp->value,tmp, |
---|
| 853 | strlen(tmp), |
---|
| 854 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 855 | char* tmpContent= |
---|
| 856 | (char*)malloc((res.nDataLen+1)*sizeof(char)); |
---|
| 857 | size_t dwRead; |
---|
| 858 | InternetReadFile(res, (LPVOID)tmpContent, |
---|
| 859 | res.nDataLen, &dwRead); |
---|
| 860 | tmpContent[res.nDataLen]=0; |
---|
| 861 | if(hInternet.header!=NULL) |
---|
| 862 | curl_slist_free_all(hInternet.header); |
---|
| 863 | addToMap(tmpmaps->content,"value",tmpContent); |
---|
| 864 | #ifdef POST_DEBUG |
---|
| 865 | fprintf(stderr,"DL CONTENT : (%s)\n",tmpContent); |
---|
| 866 | #endif |
---|
| 867 | } |
---|
| 868 | } |
---|
| 869 | } |
---|
| 870 | cur3=cur3->next; |
---|
| 871 | } |
---|
| 872 | #ifdef POST_DEBUG |
---|
| 873 | fprintf(stderr,"Header and Body was parsed from Reference \n"); |
---|
| 874 | #endif |
---|
| 875 | #ifdef DEBUG |
---|
| 876 | dumpMap(tmpmaps->content); |
---|
| 877 | fprintf(stderr, "= element 2 node \"%s\" = (%s)\n", |
---|
| 878 | cur2->name,cur2->content); |
---|
| 879 | #endif |
---|
| 880 | } |
---|
| 881 | else if(strcmp(mtoupper((char*)cur2->name), |
---|
| 882 | mtoupper("Data"))==0){ |
---|
| 883 | #ifdef DEBUG |
---|
| 884 | fprintf(stderr,"DATA\n"); |
---|
| 885 | #endif |
---|
| 886 | xmlNodePtr cur4=cur2->children; |
---|
| 887 | while(cur4){ |
---|
| 888 | if(strcmp(mtoupper((char*)cur4->name), |
---|
| 889 | mtoupper("LiteralData"))==0){ |
---|
| 890 | /** |
---|
| 891 | * Get every attribute from a LiteralData node |
---|
| 892 | * dataType , uom |
---|
| 893 | */ |
---|
| 894 | char *lits[2]; |
---|
| 895 | lits[0]="dataType"; |
---|
| 896 | lits[1]="uom"; |
---|
| 897 | for(int l=0;l<2;l++){ |
---|
| 898 | #ifdef DEBUG |
---|
| 899 | fprintf(stderr,"*** %s ***\t",lits[l]); |
---|
| 900 | #endif |
---|
| 901 | xmlChar *val=xmlGetProp(cur4,BAD_CAST lits[l]); |
---|
| 902 | if(val!=NULL && strlen((char*)val)>0){ |
---|
| 903 | if(tmpmaps->content!=NULL) |
---|
| 904 | addToMap(tmpmaps->content,lits[l],(char*)val); |
---|
| 905 | else |
---|
| 906 | tmpmaps->content=createMap(lits[l],(char*)val); |
---|
| 907 | } |
---|
| 908 | #ifdef DEBUG |
---|
| 909 | fprintf(stderr,"%s\n",val); |
---|
| 910 | #endif |
---|
| 911 | xmlFree(val); |
---|
| 912 | } |
---|
| 913 | xmlChar* mv=xmlNodeListGetString(doc, |
---|
| 914 | cur4->xmlChildrenNode,1); |
---|
| 915 | if(tmpmaps->content!=NULL) |
---|
| 916 | addToMap(tmpmaps->content, |
---|
| 917 | "value", |
---|
| 918 | (char*)mv); |
---|
| 919 | else |
---|
| 920 | tmpmaps->content= |
---|
| 921 | createMap("value", |
---|
| 922 | (char*)mv); |
---|
| 923 | xmlFree(mv); |
---|
| 924 | } |
---|
| 925 | else if(strcmp(mtoupper((char*)cur4->name), |
---|
| 926 | mtoupper("ComplexData"))==0){ |
---|
| 927 | /** |
---|
| 928 | * Get every attribute from a Reference node |
---|
| 929 | * mimeType, encoding, schema |
---|
| 930 | */ |
---|
| 931 | char *coms[2]; |
---|
| 932 | coms[0]="mimeType"; |
---|
| 933 | coms[1]="encoding"; |
---|
| 934 | coms[2]="schema"; |
---|
| 935 | for(int l=0;l<2;l++){ |
---|
| 936 | #ifdef DEBUG |
---|
| 937 | fprintf(stderr,"*** %s ***\t",coms[l]); |
---|
| 938 | #endif |
---|
| 939 | xmlChar *val=xmlGetProp(cur4,BAD_CAST coms[l]); |
---|
| 940 | if(val!=NULL && strlen((char*)val)>0){ |
---|
| 941 | if(tmpmaps->content!=NULL) |
---|
| 942 | addToMap(tmpmaps->content,coms[l],(char*)val); |
---|
| 943 | else |
---|
| 944 | tmpmaps->content=createMap(coms[l],(char*)val); |
---|
| 945 | } |
---|
| 946 | #ifdef DEBUG |
---|
| 947 | fprintf(stderr,"%s\n",val); |
---|
| 948 | #endif |
---|
| 949 | xmlFree(val); |
---|
| 950 | } |
---|
| 951 | xmlChar* mv=xmlNodeListGetString(doc, |
---|
| 952 | cur4->xmlChildrenNode,1); |
---|
| 953 | if(tmpmaps->content!=NULL) |
---|
| 954 | addToMap(tmpmaps->content, |
---|
| 955 | "value", |
---|
| 956 | (char*)mv); |
---|
| 957 | else |
---|
| 958 | tmpmaps->content= |
---|
| 959 | createMap("value", |
---|
| 960 | (char*)mv); |
---|
| 961 | xmlFree(mv); |
---|
| 962 | } |
---|
| 963 | cur4=cur4->next; |
---|
| 964 | } |
---|
| 965 | } |
---|
| 966 | #ifdef DEBUG |
---|
| 967 | dumpMap(tmpmaps->content); |
---|
| 968 | fprintf(stderr, "= element 2 node \"%s\" = (%s)\n", |
---|
| 969 | cur2->name,cur2->content); |
---|
| 970 | #endif |
---|
| 971 | xmlNodePtr cur3=cur->children; |
---|
| 972 | while(cur3){ |
---|
| 973 | #ifdef DEBUG |
---|
| 974 | fprintf(stderr, "= element 3 node \"%s\" = (%s)\n",cur3->name, |
---|
| 975 | xmlNodeListGetString(doc,cur3->xmlChildrenNode,1)); |
---|
| 976 | #endif |
---|
| 977 | if(strcmp(mtoupper((char*)cur3->name), |
---|
| 978 | mtoupper("Title"))==0){ |
---|
| 979 | #ifdef DEBUG |
---|
| 980 | fprintf(stderr, "= element 3 node \"%s\" = (%s)\n", |
---|
| 981 | cur3->name,xmlNodeListGetString(doc,cur3,1)); |
---|
| 982 | #endif |
---|
| 983 | } |
---|
| 984 | cur3=cur3->children; |
---|
| 985 | } |
---|
| 986 | cur2=cur2->next; |
---|
| 987 | } |
---|
| 988 | cur1=cur1->children; |
---|
| 989 | } |
---|
| 990 | if(request_input_real_format==NULL) |
---|
| 991 | request_input_real_format=tmpmaps; |
---|
| 992 | else |
---|
| 993 | addMapsToMaps(&request_input_real_format,tmpmaps); |
---|
| 994 | #ifdef DEBUG |
---|
| 995 | dumpMaps(tmpmaps); |
---|
| 996 | #endif |
---|
| 997 | fflush(stderr); |
---|
| 998 | tmpmaps=tmpmaps->next; |
---|
| 999 | |
---|
| 1000 | } else { |
---|
| 1001 | #ifdef DEBUG |
---|
| 1002 | fprintf(stderr, "= node \"%s\": type %d\n", cur->name, cur->type); |
---|
| 1003 | #endif |
---|
| 1004 | } |
---|
| 1005 | #ifdef DEBUG |
---|
| 1006 | dumpMaps(tmpmaps); |
---|
| 1007 | #endif |
---|
| 1008 | } |
---|
| 1009 | |
---|
| 1010 | xmlFree(tmps); |
---|
| 1011 | tmps=extractFromDoc(doc,"/*/*/*[local-name()='ResponseDocument']"); |
---|
| 1012 | #ifdef DEBUG |
---|
| 1013 | fprintf(stderr,"*****%d*****\n",tmps->nodeNr); |
---|
| 1014 | #endif |
---|
| 1015 | for(int k=0;k<tmps->nodeNr;k++){ |
---|
| 1016 | addToMap(request_inputs,"ResponseDocument",""); |
---|
| 1017 | request_output_real_format; |
---|
| 1018 | maps *tmpmaps=NULL; |
---|
| 1019 | xmlNodePtr cur=tmps->nodeTab[k]; |
---|
| 1020 | if(cur->type == XML_ELEMENT_NODE) { |
---|
| 1021 | /** |
---|
| 1022 | * A specific responseDocument node. |
---|
| 1023 | */ |
---|
| 1024 | if(tmpmaps==NULL){ |
---|
| 1025 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 1026 | tmpmaps->name="unknownIdentifier"; |
---|
| 1027 | tmpmaps->next=NULL; |
---|
| 1028 | } |
---|
| 1029 | /** |
---|
| 1030 | * Get every attribute from a LiteralData node |
---|
| 1031 | * storeExecuteResponse, lineage, status |
---|
| 1032 | */ |
---|
| 1033 | char *ress[3]; |
---|
| 1034 | ress[0]="storeExecuteResponse"; |
---|
| 1035 | ress[1]="lineage"; |
---|
| 1036 | ress[2]="status"; |
---|
| 1037 | xmlChar *val; |
---|
| 1038 | for(int l=0;l<3;l++){ |
---|
| 1039 | #ifdef DEBUG |
---|
| 1040 | fprintf(stderr,"*** %s ***\t",ress[l]); |
---|
| 1041 | #endif |
---|
| 1042 | val=xmlGetProp(cur,BAD_CAST ress[l]); |
---|
| 1043 | if(val!=NULL && strlen((char*)val)>0){ |
---|
| 1044 | if(tmpmaps->content!=NULL) |
---|
| 1045 | addToMap(tmpmaps->content,ress[l],(char*)val); |
---|
| 1046 | else |
---|
| 1047 | tmpmaps->content=createMap(ress[l],(char*)val); |
---|
| 1048 | addToMap(request_inputs,ress[l],(char*)val); |
---|
| 1049 | } |
---|
| 1050 | #ifdef DEBUG |
---|
| 1051 | fprintf(stderr,"%s\n",val); |
---|
| 1052 | #endif |
---|
| 1053 | xmlFree(val); |
---|
| 1054 | } |
---|
| 1055 | xmlNodePtr cur1=cur->children; |
---|
| 1056 | while(cur1){ |
---|
| 1057 | if(strcmp(mtoupper((char*)cur1->name), |
---|
| 1058 | mtoupper("Output"))==0){ |
---|
| 1059 | /** |
---|
| 1060 | * Get every attribute from a Output node |
---|
| 1061 | * mimeType, encoding, schema, uom, asReference |
---|
| 1062 | */ |
---|
| 1063 | char *outs[5]; |
---|
| 1064 | outs[0]="mimeType"; |
---|
| 1065 | outs[1]="encoding"; |
---|
| 1066 | outs[2]="schema"; |
---|
| 1067 | outs[3]="uom"; |
---|
| 1068 | outs[4]="asReference"; |
---|
| 1069 | for(int l=0;l<5;l++){ |
---|
| 1070 | #ifdef DEBUG |
---|
| 1071 | fprintf(stderr,"*** %s ***\t",outs[l]); |
---|
| 1072 | #endif |
---|
| 1073 | val=xmlGetProp(cur1,BAD_CAST outs[l]); |
---|
| 1074 | if(val!=NULL && strlen((char*)val)>0){ |
---|
| 1075 | if(tmpmaps->content!=NULL) |
---|
| 1076 | addToMap(tmpmaps->content,outs[l],(char*)val); |
---|
| 1077 | else |
---|
| 1078 | tmpmaps->content=createMap(outs[l],(char*)val); |
---|
| 1079 | } |
---|
| 1080 | #ifdef DEBUG |
---|
| 1081 | fprintf(stderr,"%s\n",val); |
---|
| 1082 | #endif |
---|
| 1083 | xmlFree(val); |
---|
| 1084 | } |
---|
| 1085 | |
---|
| 1086 | xmlNodePtr cur2=cur1->children; |
---|
| 1087 | while(cur2){ |
---|
| 1088 | /** |
---|
| 1089 | * Indentifier |
---|
| 1090 | */ |
---|
| 1091 | if(strcmp(mtoupper((char*)cur2->name), |
---|
| 1092 | mtoupper("Identifier"))==0){ |
---|
| 1093 | xmlChar *val= |
---|
| 1094 | xmlNodeListGetString(doc,cur2->xmlChildrenNode,1); |
---|
| 1095 | if(tmpmaps==NULL){ |
---|
| 1096 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 1097 | tmpmaps->name=strdup((char*)val); |
---|
| 1098 | tmpmaps->content=NULL; |
---|
| 1099 | tmpmaps->next=NULL; |
---|
| 1100 | } |
---|
| 1101 | else |
---|
| 1102 | tmpmaps->name=strdup((char*)val);; |
---|
| 1103 | xmlFree(val); |
---|
| 1104 | } |
---|
| 1105 | /** |
---|
| 1106 | * Title, Asbtract |
---|
| 1107 | */ |
---|
| 1108 | if(strcmp(mtoupper((char*)cur2->name), |
---|
| 1109 | mtoupper("Title"))==0 || |
---|
| 1110 | strcmp(mtoupper((char*)cur2->name), |
---|
| 1111 | mtoupper("Abstract"))==0){ |
---|
| 1112 | xmlChar *val= |
---|
| 1113 | xmlNodeListGetString(doc,cur2->xmlChildrenNode,1); |
---|
| 1114 | if(tmpmaps==NULL){ |
---|
| 1115 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 1116 | tmpmaps->name="missingIndetifier"; |
---|
| 1117 | tmpmaps->content=createMap((char*)cur2->name,(char*)val); |
---|
| 1118 | tmpmaps->next=NULL; |
---|
| 1119 | } |
---|
| 1120 | else{ |
---|
| 1121 | if(tmpmaps->content!=NULL) |
---|
| 1122 | addToMap(tmpmaps->content, |
---|
| 1123 | (char*)cur2->name,(char*)val); |
---|
| 1124 | else |
---|
| 1125 | tmpmaps->content= |
---|
| 1126 | createMap((char*)cur2->name,(char*)val); |
---|
| 1127 | } |
---|
| 1128 | xmlFree(val); |
---|
| 1129 | } |
---|
| 1130 | cur2=cur2->next; |
---|
| 1131 | } |
---|
| 1132 | } |
---|
| 1133 | cur1=cur1->next; |
---|
| 1134 | } |
---|
| 1135 | } |
---|
| 1136 | //xmlFree(cur); |
---|
| 1137 | if(request_output_real_format==NULL) |
---|
| 1138 | request_output_real_format=tmpmaps; |
---|
| 1139 | else |
---|
| 1140 | addMapsToMaps(&request_output_real_format,tmpmaps); |
---|
| 1141 | #ifdef DEBUG |
---|
| 1142 | dumpMaps(tmpmaps); |
---|
| 1143 | #endif |
---|
| 1144 | } |
---|
| 1145 | |
---|
| 1146 | xmlFree(tmps); |
---|
| 1147 | tmps=extractFromDoc(doc,"/*/*/*[local-name()='RawDataOutput']"); |
---|
| 1148 | #ifdef DEBUG |
---|
| 1149 | fprintf(stderr,"*****%d*****\n",tmps->nodeNr); |
---|
| 1150 | #endif |
---|
| 1151 | for(int k=0;k<tmps->nodeNr;k++){ |
---|
| 1152 | addToMap(request_inputs,"RawDataOutput",""); |
---|
| 1153 | xmlNodePtr cur1=tmps->nodeTab[k]; |
---|
| 1154 | xmlChar *val; |
---|
| 1155 | /** |
---|
| 1156 | * Get every attribute from a Output node |
---|
| 1157 | * mimeType, encoding, schema, uom, asReference |
---|
| 1158 | */ |
---|
| 1159 | char *outs[4]; |
---|
| 1160 | outs[0]="mimeType"; |
---|
| 1161 | outs[1]="encoding"; |
---|
| 1162 | outs[2]="schema"; |
---|
| 1163 | outs[3]="uom"; |
---|
| 1164 | for(int l=0;l<4;l++){ |
---|
| 1165 | #ifdef DEBUG |
---|
| 1166 | fprintf(stderr,"*** %s ***\t",outs[l]); |
---|
| 1167 | #endif |
---|
| 1168 | val=xmlGetProp(cur1,BAD_CAST outs[l]); |
---|
| 1169 | if(val!=NULL && strlen((char*)val)>0){ |
---|
| 1170 | if(tmpmaps==NULL){ |
---|
| 1171 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 1172 | tmpmaps->name="unknownIdentifier"; |
---|
| 1173 | tmpmaps->content=createMap(outs[l],(char*)val); |
---|
| 1174 | tmpmaps->next=NULL; |
---|
| 1175 | } |
---|
| 1176 | else |
---|
| 1177 | addToMap(tmpmaps->content,outs[l],(char*)val); |
---|
| 1178 | } |
---|
| 1179 | #ifdef DEBUG |
---|
| 1180 | fprintf(stderr,"%s\n",val); |
---|
| 1181 | #endif |
---|
| 1182 | xmlFree(val); |
---|
| 1183 | } |
---|
| 1184 | |
---|
| 1185 | xmlNodePtr cur2=cur1->children; |
---|
| 1186 | while(cur2){ |
---|
| 1187 | /** |
---|
| 1188 | * Indentifier |
---|
| 1189 | */ |
---|
| 1190 | if(strcmp(mtoupper((char*)cur2->name), |
---|
| 1191 | mtoupper("Identifier"))==0){ |
---|
| 1192 | val= |
---|
| 1193 | xmlNodeListGetString(doc,cur2->xmlChildrenNode,1); |
---|
| 1194 | if(tmpmaps==NULL){ |
---|
| 1195 | tmpmaps=(maps*)malloc(MAPS_SIZE); |
---|
| 1196 | tmpmaps->name=strdup((char*)val); |
---|
| 1197 | tmpmaps->content=NULL; |
---|
| 1198 | tmpmaps->next=NULL; |
---|
| 1199 | } |
---|
| 1200 | else |
---|
| 1201 | tmpmaps->name=strdup((char*)val);; |
---|
| 1202 | xmlFree(val); |
---|
| 1203 | } |
---|
| 1204 | cur2=cur2->next; |
---|
| 1205 | } |
---|
| 1206 | if(request_output_real_format==NULL) |
---|
| 1207 | request_output_real_format=tmpmaps; |
---|
| 1208 | else |
---|
| 1209 | addMapsToMaps(&request_output_real_format,tmpmaps); |
---|
| 1210 | #ifdef DEBUG |
---|
| 1211 | dumpMaps(tmpmaps); |
---|
| 1212 | #endif |
---|
| 1213 | } |
---|
| 1214 | |
---|
| 1215 | xmlFree(tmps); |
---|
| 1216 | xmlCleanupParser(); |
---|
| 1217 | } |
---|
| 1218 | |
---|
| 1219 | if(CHECK_INET_HANDLE(hInternet)) |
---|
| 1220 | InternetCloseHandle(hInternet); |
---|
| 1221 | |
---|
| 1222 | #ifdef DEBUG |
---|
| 1223 | fprintf(stderr,"\n%i\n",i); |
---|
| 1224 | dumpMaps(request_input_real_format); |
---|
| 1225 | dumpMaps(request_output_real_format); |
---|
| 1226 | dumpElements(); |
---|
| 1227 | #endif |
---|
| 1228 | |
---|
| 1229 | /** |
---|
| 1230 | * Ensure that each requested arguments are present in the request |
---|
| 1231 | * DataInputs and ResponseDocument / RawDataOutput |
---|
| 1232 | */ |
---|
| 1233 | addDefaultValues(&request_input_real_format,s1->inputs,m,"inputs"); |
---|
| 1234 | addDefaultValues(&request_output_real_format,s1->outputs,m,"outputs"); |
---|
| 1235 | |
---|
| 1236 | const struct tm *tm; |
---|
| 1237 | size_t len; |
---|
| 1238 | time_t now; |
---|
| 1239 | char *sDate; |
---|
| 1240 | |
---|
| 1241 | now = time ( NULL ); |
---|
| 1242 | tm = localtime ( &now ); |
---|
| 1243 | |
---|
| 1244 | sDate = new char[TIME_SIZE]; |
---|
| 1245 | |
---|
| 1246 | maps* curs=getMaps(m,"env"); |
---|
| 1247 | if(curs!=NULL){ |
---|
| 1248 | map* mapcs=curs->content; |
---|
| 1249 | while(mapcs!=NULLMAP){ |
---|
| 1250 | #ifndef WIN32 |
---|
| 1251 | setenv(mapcs->name,mapcs->value,1); |
---|
| 1252 | #else |
---|
| 1253 | #ifdef DEBUG |
---|
| 1254 | fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value); |
---|
| 1255 | #endif |
---|
| 1256 | if(mapcs->value[strlen(mapcs->value)-2]=='\r'){ |
---|
| 1257 | #ifdef DEBUG |
---|
| 1258 | fprintf(stderr,"[ZOO: Env var finish with \r]\n"); |
---|
| 1259 | #endif |
---|
| 1260 | mapcs->value[strlen(mapcs->value)-1]=0; |
---|
| 1261 | } |
---|
| 1262 | #ifdef DEBUG |
---|
| 1263 | fflush(stderr); |
---|
| 1264 | fprintf(stderr,"setting variable... %s\n", |
---|
| 1265 | #endif |
---|
| 1266 | SetEnvironmentVariable(mapcs->name,mapcs->value) |
---|
| 1267 | #ifdef DEBUG |
---|
| 1268 | ? "OK" : "FAILED"); |
---|
| 1269 | #else |
---|
| 1270 | ; |
---|
| 1271 | #endif |
---|
| 1272 | #ifdef DEBUG |
---|
| 1273 | fflush(stderr); |
---|
| 1274 | #endif |
---|
| 1275 | #endif |
---|
| 1276 | #ifdef DEBUG |
---|
| 1277 | fprintf(stderr,"[ZOO: setenv (%s=%s)]\n",mapcs->name,mapcs->value); |
---|
| 1278 | fflush(stderr); |
---|
| 1279 | #endif |
---|
| 1280 | mapcs=mapcs->next; |
---|
| 1281 | } |
---|
| 1282 | } |
---|
| 1283 | |
---|
| 1284 | len = strftime ( sDate, TIME_SIZE, "%d-%B-%YT%I:%M:%SZ", tm ); |
---|
| 1285 | |
---|
| 1286 | #ifdef DEBUG |
---|
| 1287 | dumpMap(request_inputs); |
---|
| 1288 | #endif |
---|
| 1289 | |
---|
| 1290 | /** |
---|
| 1291 | * Need to check if we need to fork to load a status enabled |
---|
| 1292 | */ |
---|
| 1293 | r_inputs=NULL; |
---|
| 1294 | r_inputs=getMap(request_inputs,"storeExecuteResponse"); |
---|
| 1295 | int eres=SERVICE_STARTED; |
---|
| 1296 | int cpid=getpid(); |
---|
| 1297 | #ifdef DEBUG |
---|
| 1298 | dumpMap(request_inputs); |
---|
| 1299 | #endif |
---|
| 1300 | |
---|
| 1301 | if(r_inputs==NULLMAP){ |
---|
| 1302 | /** |
---|
| 1303 | * Extract serviceType to know what kind of service shoudl be loaded |
---|
| 1304 | */ |
---|
| 1305 | r_inputs=NULL; |
---|
| 1306 | r_inputs=getMap(s1->content,"serviceType"); |
---|
| 1307 | #ifdef DEBUG |
---|
| 1308 | fprintf(stderr,"LOAD A %s SERVICE PROVIDER IN NORMAL MODE \n",r_inputs->value); |
---|
| 1309 | fflush(stderr); |
---|
| 1310 | #endif |
---|
| 1311 | |
---|
| 1312 | if(strncmp(mtoupper(r_inputs->value),"C",1)==0){ |
---|
| 1313 | r_inputs=getMap(request_inputs,"metapath"); |
---|
| 1314 | sprintf(tmps1,"%s/%s",ntmp,r_inputs->value); |
---|
| 1315 | char *altPath=strdup(tmps1); |
---|
| 1316 | r_inputs=getMap(s1->content,"ServiceProvider"); |
---|
| 1317 | sprintf(tmps1,"%s/%s",altPath,r_inputs->value); |
---|
| 1318 | free(altPath); |
---|
| 1319 | #ifdef DEBUG |
---|
| 1320 | fprintf(stderr,"Trying to load %s\n",tmps1); |
---|
| 1321 | #endif |
---|
| 1322 | #ifdef WIN32 |
---|
| 1323 | HINSTANCE so = LoadLibraryEx(tmps1,NULL,LOAD_WITH_ALTERED_SEARCH_PATH); |
---|
| 1324 | #else |
---|
| 1325 | void* so = dlopen(tmps1, RTLD_LAZY); |
---|
| 1326 | #endif |
---|
| 1327 | #ifdef WIN32 |
---|
| 1328 | DWORD errstr; |
---|
| 1329 | errstr = GetLastError(); |
---|
| 1330 | #ifdef DEBUG |
---|
| 1331 | fprintf(stderr,"%s loaded (%d) \n",tmps1,errstr); |
---|
| 1332 | #endif |
---|
| 1333 | #else |
---|
| 1334 | char *errstr; |
---|
| 1335 | errstr = dlerror(); |
---|
| 1336 | #endif |
---|
| 1337 | |
---|
| 1338 | if( so != NULL ) { |
---|
| 1339 | #ifdef DEBUG |
---|
| 1340 | fprintf(stderr,"Library loaded %s \n",errstr); |
---|
| 1341 | fprintf(stderr,"Service Shared Object = %s\n",r_inputs->value); |
---|
| 1342 | #endif |
---|
| 1343 | r_inputs=getMap(s1->content,"serviceType"); |
---|
| 1344 | #ifdef DEBUG |
---|
| 1345 | dumpMap(r_inputs); |
---|
| 1346 | fprintf(stderr,"%s %s",r_inputs->value,mtoupper(r_inputs->value)); |
---|
| 1347 | fflush(stderr); |
---|
| 1348 | #endif |
---|
| 1349 | if(strcmp(mtoupper(r_inputs->value),"C-FORTRAN")==0){ |
---|
| 1350 | #ifdef WIN32 |
---|
| 1351 | //Strange return value needed here ! |
---|
| 1352 | return 1; |
---|
| 1353 | #endif |
---|
| 1354 | r_inputs=getMap(request_inputs,"Identifier"); |
---|
| 1355 | char fname[1024]; |
---|
| 1356 | sprintf(fname,"%s",r_inputs->value); |
---|
| 1357 | #ifdef DEBUG |
---|
| 1358 | fprintf(stderr,"Try to load function %s\n",fname); |
---|
| 1359 | #endif |
---|
| 1360 | #ifdef WIN32 |
---|
| 1361 | typedef int (CALLBACK* execute_t)(char***,char***,char***); |
---|
| 1362 | execute_t execute=(execute_t)GetProcAddress(so,fname); |
---|
| 1363 | #else |
---|
| 1364 | typedef int (*execute_t)(char***,char***,char***); |
---|
| 1365 | execute_t execute=(execute_t)dlsym(so,fname); |
---|
| 1366 | #endif |
---|
| 1367 | #ifdef DEBUG |
---|
| 1368 | #ifdef WIN32 |
---|
| 1369 | errstr = GetLastError(); |
---|
| 1370 | #else |
---|
| 1371 | errstr = dlerror(); |
---|
| 1372 | #endif |
---|
| 1373 | fprintf(stderr,"Function loaded %s\n",errstr); |
---|
| 1374 | #endif |
---|
| 1375 | char main_conf[10][30][1024]; |
---|
| 1376 | char inputs[10][30][1024]; |
---|
| 1377 | char outputs[10][30][1024]; |
---|
| 1378 | for(int i=0;i<10;i++){ |
---|
| 1379 | for(int j=0;j<30;j++){ |
---|
| 1380 | memset(main_conf[i][j],0,1024); |
---|
| 1381 | memset(inputs[i][j],0,1024); |
---|
| 1382 | memset(outputs[i][j],0,1024); |
---|
| 1383 | } |
---|
| 1384 | } |
---|
| 1385 | mapsToCharXXX(m,(char***)main_conf); |
---|
| 1386 | mapsToCharXXX(request_input_real_format,(char***)inputs); |
---|
| 1387 | mapsToCharXXX(request_output_real_format,(char***)outputs); |
---|
| 1388 | eres=execute((char***)&main_conf[0],(char***)&inputs[0],(char***)&outputs[0]); |
---|
| 1389 | #ifdef DEBUG |
---|
| 1390 | fprintf(stderr,"Function run successfully \n"); |
---|
| 1391 | #endif |
---|
| 1392 | charxxxToMaps((char***)&outputs[0],&request_output_real_format); |
---|
| 1393 | }else{ |
---|
[2] | 1394 | #ifdef WIN32 |
---|
[1] | 1395 | errstr = GetLastError(); |
---|
| 1396 | #ifdef DEBUG |
---|
| 1397 | fprintf(stderr,"Function %s failed to load because of %d\n",r_inputs->value,errstr); |
---|
| 1398 | #endif |
---|
[2] | 1399 | #endif |
---|
[1] | 1400 | r_inputs=getMap(request_inputs,"Identifier"); |
---|
| 1401 | #ifdef DEBUG |
---|
| 1402 | fprintf(stderr,"Try to load function %s\n",r_inputs->value); |
---|
| 1403 | #endif |
---|
| 1404 | typedef int (*execute_t)(maps**,maps**,maps**); |
---|
| 1405 | #ifdef WIN32 |
---|
| 1406 | execute_t execute=(execute_t)GetProcAddress(so,r_inputs->value); |
---|
| 1407 | #ifdef DEBUG |
---|
| 1408 | /*if(!execute)*/ |
---|
| 1409 | errstr = GetLastError(); |
---|
| 1410 | fprintf(stderr,"Function %s failed to load because of %d\n",r_inputs->value,errstr); |
---|
| 1411 | /*else |
---|
| 1412 | fprintf(stderr,"Function %s loaded\n",r_inputs->value);*/ |
---|
| 1413 | #endif |
---|
| 1414 | #else |
---|
| 1415 | execute_t execute=(execute_t)dlsym(so,r_inputs->value); |
---|
| 1416 | #endif |
---|
| 1417 | |
---|
| 1418 | #ifdef DEBUG |
---|
| 1419 | #ifdef WIN32 |
---|
| 1420 | errstr = GetLastError(); |
---|
| 1421 | #else |
---|
| 1422 | errstr = dlerror(); |
---|
| 1423 | #endif |
---|
| 1424 | fprintf(stderr,"Function loaded %s\n",errstr); |
---|
| 1425 | #endif |
---|
| 1426 | |
---|
| 1427 | #ifdef DEBUG |
---|
| 1428 | fprintf(stderr,"Now run the function \n"); |
---|
| 1429 | fflush(stderr); |
---|
| 1430 | #endif |
---|
| 1431 | eres=execute(&m,&request_input_real_format,&request_output_real_format); |
---|
| 1432 | #ifdef DEBUG |
---|
| 1433 | fprintf(stderr,"Function loaded and returned %d\n",eres); |
---|
| 1434 | fflush(stderr); |
---|
| 1435 | #endif |
---|
| 1436 | //return 1; |
---|
| 1437 | } |
---|
| 1438 | |
---|
| 1439 | //dlclose(so); |
---|
| 1440 | } else { |
---|
| 1441 | /** |
---|
| 1442 | * Unable to load the specified shared library |
---|
| 1443 | */ |
---|
| 1444 | char tmps[1024]; |
---|
| 1445 | sprintf(tmps,"C Library can't be loaded %s \n",errstr); |
---|
| 1446 | map* tmps1=createMap("text",tmps); |
---|
| 1447 | printExceptionReportResponse(m,tmps1); |
---|
| 1448 | exit(1); |
---|
| 1449 | } |
---|
| 1450 | } |
---|
| 1451 | else{ |
---|
| 1452 | if(strcmp(mtoupper(r_inputs->value),"PYTHON")==0){ |
---|
| 1453 | eres=zoo_python_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1454 | } |
---|
| 1455 | else |
---|
| 1456 | |
---|
| 1457 | #ifdef USE_JAVA |
---|
| 1458 | if(strcmp(mtoupper(r_inputs->value),"JAVA")==0){ |
---|
| 1459 | eres=zoo_java_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1460 | } |
---|
| 1461 | else |
---|
| 1462 | #endif |
---|
| 1463 | |
---|
| 1464 | #ifdef USE_PHP |
---|
| 1465 | if(strcmp(mtoupper(r_inputs->value),"PHP")==0){ |
---|
| 1466 | eres=zoo_php_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1467 | } |
---|
| 1468 | else |
---|
| 1469 | #endif |
---|
| 1470 | |
---|
| 1471 | #ifdef USE_JS |
---|
| 1472 | if(strcmp(mtoupper(r_inputs->value),"JS")==0){ |
---|
| 1473 | eres=zoo_js_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1474 | //dumpMaps(request_output_real_format); |
---|
| 1475 | //exit(-1); |
---|
| 1476 | } |
---|
| 1477 | else |
---|
| 1478 | #endif |
---|
| 1479 | { |
---|
| 1480 | char tmpv[1024]; |
---|
| 1481 | sprintf(tmpv,"Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n",r_inputs->value); |
---|
| 1482 | map* tmps=createMap("text",tmpv); |
---|
| 1483 | printExceptionReportResponse(m,tmps); |
---|
| 1484 | return(-1); |
---|
| 1485 | } |
---|
| 1486 | } |
---|
| 1487 | } |
---|
| 1488 | else{ |
---|
| 1489 | |
---|
| 1490 | pid_t pid; |
---|
| 1491 | #ifdef DEBUG |
---|
| 1492 | fprintf(stderr,"\nPID : %d\n",cpid); |
---|
| 1493 | #endif |
---|
| 1494 | #ifndef WIN32 |
---|
| 1495 | pid = fork (); |
---|
| 1496 | #else |
---|
| 1497 | pid = 0; |
---|
| 1498 | #endif |
---|
| 1499 | if (pid > 0) { |
---|
| 1500 | /** |
---|
| 1501 | * dady : |
---|
| 1502 | * set status to SERVICE_ACCEPTED |
---|
| 1503 | */ |
---|
| 1504 | #ifdef DEBUG |
---|
| 1505 | fprintf(stderr,"father pid continue (origin %d) %d ...\n",cpid,getpid()); |
---|
| 1506 | #endif |
---|
| 1507 | eres=SERVICE_ACCEPTED; |
---|
| 1508 | }else if (pid == 0) { |
---|
| 1509 | /** |
---|
| 1510 | * son : have to close the stdout, stdin and stderr to let the parent |
---|
| 1511 | * process answer to http client. |
---|
| 1512 | */ |
---|
| 1513 | char tmp1[256]; |
---|
| 1514 | r_inputs=getMapFromMaps(m,"main","tmpPath"); |
---|
| 1515 | sprintf(tmp1,"%s",r_inputs->value); |
---|
| 1516 | r_inputs=getMap(s1->content,"ServiceProvider"); |
---|
| 1517 | sprintf(tmp1,"%s/%s",strdup(tmp1),r_inputs->value); |
---|
| 1518 | sprintf(tmp1,"%s_%d.xml",strdup(tmp1),cpid); |
---|
| 1519 | #ifdef DEBUG |
---|
| 1520 | fprintf(stderr,"RUN IN BACKGROUND MODE \n"); |
---|
| 1521 | fprintf(stderr,"son pid continue (origin %d) %d ...\n",cpid,getpid()); |
---|
| 1522 | fprintf(stderr,"\nFILE TO STORE DATA %s\n",tmp1); |
---|
| 1523 | #endif |
---|
| 1524 | freopen(tmp1 , "w+", stdout); |
---|
| 1525 | fclose(stdin); |
---|
| 1526 | fclose(stderr); |
---|
| 1527 | /** |
---|
| 1528 | * set status to SERVICE_STARTED and flush stdout to ensure full |
---|
| 1529 | * content was outputed (the file used to store the ResponseDocument). |
---|
| 1530 | * The rewind stdout to restart writing from the bgining of the file, |
---|
| 1531 | * this way the data will be updated at the end of the process run. |
---|
| 1532 | */ |
---|
| 1533 | printProcessResponse1(m,request_inputs,cpid, |
---|
| 1534 | s[0],r_inputs->value,SERVICE_STARTED, |
---|
| 1535 | request_input_real_format, |
---|
| 1536 | request_output_real_format); |
---|
| 1537 | fflush(stdout); |
---|
| 1538 | rewind(stdout); |
---|
| 1539 | /** |
---|
| 1540 | * Extract serviceType to know what kind of service shoudl be loaded |
---|
| 1541 | */ |
---|
| 1542 | r_inputs=NULL; |
---|
| 1543 | r_inputs=getMap(s1->content,"serviceType"); |
---|
| 1544 | #ifdef DEBUG |
---|
| 1545 | fprintf(stderr,"LOAD A %s SERVICE PROVIDER IN BACKGROUND MODE \n",r_inputs->value); |
---|
| 1546 | #endif |
---|
| 1547 | |
---|
| 1548 | if(strncmp(mtoupper(r_inputs->value),"C",1)==0){ |
---|
| 1549 | r_inputs=getMap(request_inputs,"metapath"); |
---|
| 1550 | sprintf(tmps1,"%s/%s",ntmp,r_inputs->value); |
---|
| 1551 | r_inputs=getMap(s1->content,"ServiceProvider"); |
---|
| 1552 | sprintf(tmps1,"%s/%s",strdup(tmps1),r_inputs->value); |
---|
| 1553 | |
---|
| 1554 | #ifdef DEBUG |
---|
| 1555 | fprintf(stderr,"Trying to load %s\n",tmps1); |
---|
| 1556 | #endif |
---|
| 1557 | #ifdef WIN32 |
---|
| 1558 | HINSTANCE so = LoadLibraryEx(tmps1,NULL,LOAD_WITH_ALTERED_SEARCH_PATH); |
---|
| 1559 | #else |
---|
| 1560 | void* so = dlopen(tmps1, RTLD_LAZY); |
---|
| 1561 | #endif |
---|
| 1562 | #ifdef WIN32 |
---|
| 1563 | DWORD errstr; |
---|
| 1564 | errstr = GetLastError(); |
---|
| 1565 | #else |
---|
| 1566 | char *errstr; |
---|
| 1567 | errstr = dlerror(); |
---|
| 1568 | #endif |
---|
| 1569 | if( so != NULL ) { |
---|
| 1570 | r_inputs=getMap(s1->content,"serviceType"); |
---|
| 1571 | #ifdef DEBUG |
---|
| 1572 | fprintf(stderr,"r_inputs->value = %s\n",r_inputs->value); |
---|
| 1573 | #endif |
---|
| 1574 | if(strcmp(mtoupper(r_inputs->value),"C-FORTRAN")==0){ |
---|
| 1575 | r_inputs=getMap(request_inputs,"Identifier"); |
---|
| 1576 | #ifdef DEBUG |
---|
| 1577 | fprintf(stderr,"Try to load function %s\n",r_inputs->value); |
---|
| 1578 | #endif |
---|
| 1579 | typedef int (*execute_t)(char***,char***,char***); |
---|
| 1580 | char fname[1024]; |
---|
| 1581 | sprintf(fname,"%s_",r_inputs->value); |
---|
| 1582 | #ifdef DEBUG |
---|
| 1583 | fprintf(stderr,"Try to load function %s\n",fname); |
---|
| 1584 | #endif |
---|
| 1585 | #ifdef WIN32 |
---|
| 1586 | execute_t execute=(execute_t)GetProcAddress(so,fname); |
---|
| 1587 | #else |
---|
| 1588 | execute_t execute=(execute_t)dlsym(so,fname); |
---|
| 1589 | #endif |
---|
| 1590 | #ifdef DEBUG |
---|
| 1591 | #ifdef WIN32 |
---|
| 1592 | errstr = GetLastError(); |
---|
| 1593 | #else |
---|
| 1594 | errstr = dlerror(); |
---|
| 1595 | #endif |
---|
| 1596 | #endif |
---|
| 1597 | char main_conf[10][10][1024]; |
---|
| 1598 | char inputs[10][10][1024]; |
---|
| 1599 | char outputs[10][10][1024]; |
---|
| 1600 | for(int i=0;i<10;i++){ |
---|
| 1601 | for(int j=0;j<10;j++){ |
---|
| 1602 | memset(main_conf[i][j],0,1024); |
---|
| 1603 | memset(inputs[i][j],0,1024); |
---|
| 1604 | memset(outputs[i][j],0,1024); |
---|
| 1605 | } |
---|
| 1606 | } |
---|
| 1607 | mapsToCharXXX(m,(char***)main_conf); |
---|
| 1608 | mapsToCharXXX(request_input_real_format,(char***)inputs); |
---|
| 1609 | //mapsToCharXXX(request_output_real_format,(char***)outputs); |
---|
| 1610 | eres=execute((char***)&main_conf[0],(char***)&inputs[0],(char***)&outputs[0]); |
---|
| 1611 | charxxxToMaps((char***)&outputs[0],&request_output_real_format); |
---|
| 1612 | |
---|
| 1613 | }else{ |
---|
| 1614 | |
---|
| 1615 | typedef int (*execute_t)(maps**,maps**,maps**); |
---|
| 1616 | #ifdef DEBUG |
---|
| 1617 | fprintf(stderr,"Library loaded %s \n",errstr); |
---|
| 1618 | #endif |
---|
| 1619 | r_inputs=getMap(request_inputs,"Identifier"); |
---|
| 1620 | #ifdef DEBUG |
---|
| 1621 | fprintf(stderr,"Try to load function %s\n",r_inputs->value); |
---|
| 1622 | #endif |
---|
| 1623 | #ifdef WIN32 |
---|
| 1624 | execute_t execute=(execute_t)GetProcAddress(so,r_inputs->value); |
---|
| 1625 | #else |
---|
| 1626 | execute_t execute=(execute_t)dlsym(so,r_inputs->value); |
---|
| 1627 | #endif |
---|
| 1628 | #ifdef DEBUG |
---|
| 1629 | #ifdef WIN32 |
---|
| 1630 | errstr = GetLastError(); |
---|
| 1631 | #else |
---|
| 1632 | errstr = dlerror(); |
---|
| 1633 | #endif |
---|
| 1634 | fprintf(stderr,"Function loaded %s\n",errstr); |
---|
| 1635 | #endif |
---|
| 1636 | /** |
---|
| 1637 | * set the status code value returned by the service function itself |
---|
| 1638 | */ |
---|
| 1639 | eres=execute(&m,&request_input_real_format,&request_output_real_format); |
---|
| 1640 | //dlclose(so); |
---|
| 1641 | } |
---|
| 1642 | } else { |
---|
| 1643 | //#ifdef DEBUG |
---|
| 1644 | /** |
---|
| 1645 | * Should fallback to others languages. |
---|
| 1646 | * Maybe beter to keep this informations anywhere |
---|
| 1647 | * (in the service metadata ?) |
---|
| 1648 | */ |
---|
| 1649 | char tmps2[1024]; |
---|
| 1650 | sprintf(tmps1,"C Library can't be loaded %s \n",errstr); |
---|
| 1651 | map* tmps=createMap("text",tmps1); |
---|
| 1652 | printExceptionReportResponse(m,tmps); |
---|
| 1653 | exit(1); |
---|
| 1654 | } |
---|
| 1655 | } else{ |
---|
| 1656 | if(strcmp(mtoupper(r_inputs->value),"PYTHON")==0){ |
---|
| 1657 | eres=zoo_python_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1658 | } |
---|
| 1659 | else |
---|
| 1660 | |
---|
| 1661 | #ifdef USE_JAVA |
---|
| 1662 | if(strcmp(mtoupper(r_inputs->value),"JAVA")==0){ |
---|
| 1663 | eres=zoo_java_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1664 | } |
---|
| 1665 | else |
---|
| 1666 | #endif |
---|
| 1667 | |
---|
| 1668 | #ifdef USE_PHP |
---|
| 1669 | if(strcmp(mtoupper(r_inputs->value),"PHP")==0){ |
---|
| 1670 | eres=zoo_php_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1671 | } |
---|
| 1672 | else |
---|
| 1673 | #endif |
---|
| 1674 | |
---|
| 1675 | #ifdef USE_JS |
---|
| 1676 | if(strcmp(mtoupper(r_inputs->value),"JS")==0){ |
---|
| 1677 | eres=zoo_js_support(&m,request_inputs,s1,&request_input_real_format,&request_output_real_format); |
---|
| 1678 | } |
---|
| 1679 | else |
---|
| 1680 | #endif |
---|
| 1681 | { |
---|
| 1682 | char tmpv[1024]; |
---|
| 1683 | sprintf(tmpv,"Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n",r_inputs->value); |
---|
| 1684 | map* tmps=createMap("text",tmpv); |
---|
| 1685 | printExceptionReportResponse(m,tmps); |
---|
| 1686 | return -1; |
---|
| 1687 | } |
---|
| 1688 | } |
---|
| 1689 | |
---|
| 1690 | //res=execute(&m,&request_input_real_format,&request_output_real_format); |
---|
| 1691 | } else { |
---|
| 1692 | /** |
---|
| 1693 | * error server don't accept the process need to output a valid |
---|
| 1694 | * error response here !!! |
---|
| 1695 | */ |
---|
| 1696 | } |
---|
| 1697 | |
---|
| 1698 | } |
---|
| 1699 | |
---|
| 1700 | #ifdef DEBUG |
---|
| 1701 | dumpMaps(request_output_real_format); |
---|
| 1702 | #endif |
---|
| 1703 | outputResponse(s1,request_input_real_format, |
---|
| 1704 | request_output_real_format,request_inputs, |
---|
| 1705 | cpid,m,eres); |
---|
| 1706 | |
---|
| 1707 | #ifdef DEBUG |
---|
| 1708 | fprintf(stderr,"Processed response \n"); |
---|
| 1709 | fflush(stdout); |
---|
| 1710 | fflush(stderr); |
---|
| 1711 | #endif |
---|
| 1712 | |
---|
| 1713 | //dlclose(so); |
---|
| 1714 | return 0; |
---|
| 1715 | } |
---|