Changeset 949
- Timestamp:
- Sep 5, 2019, 10:16:21 AM (5 years ago)
- Location:
- trunk/zoo-project
- Files:
-
- 1 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/HISTORY.txt
r946 r949 1 1 Version 1.8.0-dev 2 * Start simplification of the runRequest function 3 * Integrate optional support for OGC API - Processing implementation 2 4 * Add the optional handleText parameter to provide pure text for 3 5 complex data node with mimetype="text/*" -
trunk/zoo-project/zoo-kernel/main_conf_read.l
r784 r949 34 34 %} 35 35 36 attname [a-zA-Z0-9_\-:. ]+36 attname [a-zA-Z0-9_\-:./\{\}-]+ 37 37 38 attvalue1 [%\*,;@a-zA-Z0-9_\-.:" "\"\'/\\\(\)\+\x41-\xff?&=\ !$]+38 attvalue1 [%\*,;@a-zA-Z0-9_\-.:" "\"\'/\\\(\)\+\x41-\xff?&=\=\!$-]+ 39 39 40 40 whitesp [ ] -
trunk/zoo-project/zoo-kernel/meta_sql.c
r917 r949 450 450 * @return the number of services found, -1 in case of failure 451 451 */ 452 int fetchServicesFromDb(registry* reg,maps* conf, xmlDocPtr doc, xmlNodePtr n,453 void (func) (registry *, maps *, xmlDocPtr, xmlNodePtr,452 int fetchServicesFromDb(registry* reg,maps* conf, void* doc0, void* n0, 453 void (func) (registry *, maps *, void*, void*, 454 454 service *), int minimal ){ 455 455 int result=0; 456 xmlDocPtr doc=(xmlDocPtr) doc0; 457 xmlNodePtr n=(xmlNodePtr) n0; 456 458 result=_init_sql(conf,"metadb"); 457 459 if(getMapFromMaps(conf,"lenv","dbIssue")!=NULL || result < 0) -
trunk/zoo-project/zoo-kernel/meta_sql.h
r917 r949 41 41 #endif 42 42 43 ZOO_DLL_EXPORT int fetchServicesFromDb(registry*,maps*, xmlDocPtr, xmlNodePtr,44 void (func) (registry *, maps *, xmlDocPtr,45 xmlNodePtr,service *),int);43 ZOO_DLL_EXPORT int fetchServicesFromDb(registry*,maps*, void*, void*, 44 void (func) (registry *, maps *, void*, 45 void*,service *),int); 46 46 ZOO_DLL_EXPORT service* extractServiceFromDb(maps*,const char*,int); 47 47 -
trunk/zoo-project/zoo-kernel/response_print.c
r945 r949 639 639 * @return the generated wps:ProcessOfferings xmlNodePtr 640 640 */ 641 void printGetCapabilitiesForProcess(registry *reg, maps* m, xmlDocPtr doc,xmlNodePtr nc,service* serv){641 void printGetCapabilitiesForProcess(registry *reg, maps* m,void* doc0,void* nc0,service* serv){ 642 642 xmlNsPtr ns,ns_ows,ns_xml,ns_xlink; 643 xmlDocPtr doc=(xmlDocPtr) doc0; 644 xmlNodePtr nc=(xmlNodePtr) nc0; 643 645 xmlNodePtr n=NULL,nc1,nc2,nc3; 644 646 map* version=getMapFromMaps(m,"main","rversion"); … … 1000 1002 * @return the generated wps:ProcessOfferings xmlNodePtr 1001 1003 */ 1002 void printDescribeProcessForProcess(registry *reg, maps* m, xmlDocPtr doc,xmlNodePtr nc,service* serv){1004 void printDescribeProcessForProcess(registry *reg, maps* m,void* doc0,void* nc0,service* serv){ 1003 1005 xmlNsPtr ns,ns_ows,ns_xlink; 1004 1006 xmlNodePtr n,nc1; 1007 xmlDocPtr doc=(xmlDocPtr) doc0; 1008 xmlNodePtr nc=(xmlNodePtr) nc0; 1005 1009 xmlNodePtr nc2 = NULL; 1006 1010 map* version=getMapFromMaps(m,"main","rversion"); -
trunk/zoo-project/zoo-kernel/response_print.h
r926 r949 210 210 xmlNodePtr printWPSHeader(xmlDocPtr,maps*,const char*,const char*,const char*,int); 211 211 xmlNodePtr printGetCapabilitiesHeader(xmlDocPtr,maps*,const char*); 212 void printGetCapabilitiesForProcess(registry*,maps*, xmlDocPtr,xmlNodePtr,service*);213 void printDescribeProcessForProcess(registry*,maps*, xmlDocPtr,xmlNodePtr,service*);212 void printGetCapabilitiesForProcess(registry*,maps*,void*,void*,service*); 213 void printDescribeProcessForProcess(registry*,maps*,void*,void*,service*); 214 214 void printFullDescription(xmlDocPtr,int,elements*,const char*,xmlNsPtr,xmlNsPtr,xmlNodePtr,int,int,const map*); 215 215 void printDocument(maps*,xmlDocPtr,int); -
trunk/zoo-project/zoo-kernel/service.c
r943 r949 389 389 * @return a pointer to the specific element if found, NULL in other case. 390 390 */ 391 elements* getElements(elements* m,c har *key){391 elements* getElements(elements* m,const char *key){ 392 392 elements* tmp=m; 393 393 while(tmp!=NULL){ -
trunk/zoo-project/zoo-kernel/service.h
r917 r949 425 425 ZOO_DLL_EXPORT void setElementsName(elements**,char*); 426 426 ZOO_DLL_EXPORT bool hasElement(elements*,const char*); 427 ZOO_DLL_EXPORT elements* getElements(elements*,c har*);427 ZOO_DLL_EXPORT elements* getElements(elements*,const char*); 428 428 ZOO_DLL_EXPORT void freeIOType(iotype**); 429 429 ZOO_DLL_EXPORT void freeElements(elements**); -
trunk/zoo-project/zoo-kernel/service_json.c
r917 r949 2 2 * Author : Gérald FENOY 3 3 * 4 * Copyright 2017 GeoLabs SARL. All rights reserved.4 * Copyright 2017-2019 GeoLabs SARL. All rights reserved. 5 5 * 6 6 * Permission is hereby granted, free of charge, to any person obtaining a copy … … 24 24 25 25 #include "service_json.h" 26 #include "json.h" 27 #include <errno.h> 28 #include "json_tokener.h" 29 #include "stdlib.h" 30 #include "mimetypes.h" 31 #include "server_internal.h" 32 #include "service_internal.h" 33 #include <dirent.h> 26 34 27 35 #ifdef __cplusplus 28 36 extern "C" { 29 37 #endif 30 38 /** 39 * Equivalent range keywords for WPS version 1 and 2 40 */ 41 const char* rangeCorrespondances[4][2]={ 42 { "rangeMin", "minimumValue" }, 43 { "rangeMax", "maximumValue"}, 44 { "rangeSpacing", "spacing" }, 45 { "rangeClosure", "rangeClosure" } 46 }; 47 48 49 /** 50 * Convert a map to a json object 51 * @param myMap the map to be converted into json object 52 * @return a json_object pointer to the created json_object 53 */ 31 54 json_object* mapToJson(map* myMap){ 32 55 json_object *res=json_object_new_object(); … … 67 90 } 68 91 92 /** 93 * Convert a maps to a json object 94 * @param myMap the maps to be converted into json object 95 * @return a json_object pointer to the created json_object 96 */ 69 97 json_object* mapsToJson(maps* myMap){ 70 98 json_object *res=json_object_new_object(); … … 73 101 json_object *obj=NULL; 74 102 if(cursor->content!=NULL){ 75 //json_object *content=NULL;76 103 obj=mapToJson(cursor->content); 77 //json_object_object_add(obj,"content",content);78 104 }else 79 105 obj=json_object_new_object(); … … 89 115 } 90 116 117 /** 118 * Convert an elements to a json object 119 * @param myElements the elements pointer to be converted into a json object 120 * @return a json_object pointer to the created json_object 121 */ 91 122 json_object* elementsToJson(elements* myElements){ 92 123 json_object *res=json_object_new_object(); … … 112 143 } 113 144 json_object_object_add(cres,"supported",resi); 114 fprintf(stderr,"%s %d\n",__FILE__,__LINE__);115 fflush(stderr);116 145 } 117 146 118 dumpElements(cur->child);119 147 json_object_object_add(cres,"child",elementsToJson(cur->child)); 120 fprintf(stderr,"%s %d\n",__FILE__,__LINE__);121 fflush(stderr);122 148 123 149 json_object_object_add(res,cur->name,cres); … … 127 153 } 128 154 155 /** 156 * Convert an service to a json object 157 * @param myService the service pointer to be converted into a json object 158 * @return a json_object pointer to the created json_object 159 */ 129 160 json_object* serviceToJson(service* myService){ 130 161 json_object *res=json_object_new_object(); … … 137 168 return res; 138 169 } 170 171 /** 172 * Add Allowed Range properties to a json_object 173 * @param m the main configuration maps pointer 174 * @param iot the iotype pointer 175 * @param prop the json_object pointer to add the allowed range properties 176 * @return a json_object pointer to the created json_object 177 */ 178 void printAllowedRangesJ(maps* m,iotype* iot,json_object* prop){ 179 map* tmpMap1; 180 json_object* prop4=json_object_new_object(); 181 for(int i=0;i<4;i++){ 182 tmpMap1=getMap(iot->content,rangeCorrespondances[i][0]); 183 if(tmpMap1!=NULL){ 184 if(i<3) 185 json_object_object_add(prop4,rangeCorrespondances[i][1],json_object_new_string(tmpMap1->value)); 186 else{ 187 char* currentValue=NULL; 188 int limit=strlen(tmpMap1->value); 189 for(int j=0;j<limit;j++){ 190 const char* tmpS="closed"; 191 if(tmpMap1->value[j]=='o'){ 192 tmpS="open"; 193 } 194 if(currentValue==NULL){ 195 currentValue=(char*)malloc((strlen(tmpS)+1)*sizeof(char)); 196 sprintf(currentValue,"%s",tmpS); 197 }else{ 198 char* tmpS1=zStrdup(currentValue); 199 currentValue=(char*)realloc(currentValue,(strlen(tmpS1)+strlen(tmpS)+2)*sizeof(char)); 200 sprintf(currentValue,"%s-%s",tmpS1,tmpS); 201 } 202 } 203 json_object_object_add(prop4,rangeCorrespondances[i][1],json_object_new_string(currentValue)); 204 } 205 } 206 } 207 json_object_array_add(prop,prop4); 208 } 139 209 210 /** 211 * Add literalDataDomains property to a json_object 212 * @param m the main configuration maps pointer 213 * @param in the elements pointer 214 * @param input the json_object pointer to add the literalDataDomains property 215 * @return a json_object pointer to the created json_object 216 */ 217 void printLiteralDataJ(maps* m,elements* in,json_object* input){ 218 json_object* prop0=json_object_new_array(); 219 json_object* prop1=json_object_new_object(); 220 json_object* prop2=json_object_new_object(); 221 if(in->defaults!=NULL){ 222 map* tmpMap1=getMap(in->defaults->content,"DataType"); 223 if(tmpMap1!=NULL){ 224 json_object_object_add(prop2,"name",json_object_new_string(tmpMap1->value)); 225 json_object_object_add(prop1,"dataType",prop2); 226 } 227 tmpMap1=getMap(in->defaults->content,"value"); 228 if(tmpMap1!=NULL) 229 json_object_object_add(prop1,"defaultValue",json_object_new_string(tmpMap1->value)); 230 json_object* prop3=json_object_new_object(); 231 tmpMap1=getMap(in->defaults->content,"rangeMin"); 232 if(tmpMap1!=NULL){ 233 json_object* prop5=json_object_new_array(); 234 printAllowedRangesJ(m,in->defaults,prop5); 235 if(in->supported!=NULL){ 236 iotype* iot=in->supported; 237 while(iot!=NULL){ 238 printAllowedRangesJ(m,iot,prop5); 239 iot=iot->next; 240 } 241 } 242 json_object_object_add(prop3,"allowedRanges",prop5); 243 } 244 else{ 245 tmpMap1=getMap(in->defaults->content,"range"); 246 if(tmpMap1!=NULL){ 247 // TODO: parse range = [rangeMin,rangeMax] 248 }else{ 249 // AllowedValues 250 tmpMap1=getMap(in->defaults->content,"AllowedValues"); 251 if(tmpMap1!=NULL){ 252 char* saveptr; 253 json_object* prop5=json_object_new_array(); 254 char *tmps = strtok_r (tmpMap1->value, ",", &saveptr); 255 while(tmps!=NULL){ 256 json_object_array_add(prop5,json_object_new_string(tmps)); 257 tmps = strtok_r (NULL, ",", &saveptr); 258 } 259 json_object_object_add(prop3,"allowedValues",prop5); 260 261 }else{ 262 json_object_object_add(prop3,"anyValue",json_object_new_boolean(true)); 263 } 264 } 265 } 266 json_object_object_add(prop1,"valueDefinition",prop3); 267 json_object_array_add(prop0,prop1); 268 } 269 json_object_object_add(input,"literalDataDomains",prop0); 270 } 271 272 /** 273 * Add Format properties to a json_object 274 * @param m the main configuration maps pointer 275 * @param iot the current iotype pointer 276 * @param res the json_object pointer to add the properties to 277 * @param isDefault boolean specifying if the currrent iotype is default 278 * @param maxSize a map pointer to the maximumMegabytes param defined in the zcfg file for this input/output 279 */ 280 void printFormatJ(maps* m,iotype* iot,json_object* res,bool isDefault,map* maxSize){ 281 if(iot!=NULL){ 282 map* tmpMap1=getMap(iot->content,"mimeType"); 283 json_object* prop1=json_object_new_object(); 284 json_object_object_add(prop1,"default",json_object_new_boolean(isDefault)); 285 json_object_object_add(prop1,"mimeType",json_object_new_string(tmpMap1->value)); 286 tmpMap1=getMap(iot->content,"encoding"); 287 if(tmpMap1!=NULL) 288 json_object_object_add(prop1,"encoding",json_object_new_string(tmpMap1->value)); 289 tmpMap1=getMap(iot->content,"schema"); 290 if(tmpMap1!=NULL) 291 json_object_object_add(prop1,"schema",json_object_new_string(tmpMap1->value)); 292 if(maxSize!=NULL) 293 json_object_object_add(prop1,"maximumMegabytes",json_object_new_int64(atoll(maxSize->value))); 294 json_object_array_add(res,prop1); 295 } 296 } 297 298 /** 299 * Add additionalParameters property to a json_object 300 * @param conf the main configuration maps pointer 301 * @param meta a map pointer to the current metadata informations 302 * @param doc the json_object pointer to add the property to 303 */ 304 void printJAdditionalParameters(maps* conf,map* meta,json_object* doc){ 305 map* cmeta=meta; 306 json_object* carr=json_object_new_array(); 307 int hasElement=-1; 308 json_object* jcaps=json_object_new_object(); 309 while(cmeta!=NULL){ 310 json_object* jcmeta=json_object_new_object(); 311 if(strcasecmp(cmeta->name,"role")!=0 && 312 strcasecmp(cmeta->name,"href")!=0 && 313 strcasecmp(cmeta->name,"title")!=0 && 314 strcasecmp(cmeta->name,"length")!=0 ){ 315 json_object_object_add(jcmeta,"name",json_object_new_string(cmeta->name)); 316 json_object_object_add(jcmeta,"value",json_object_new_string(cmeta->value)); 317 json_object_array_add(carr,jcmeta); 318 hasElement++; 319 }else{ 320 if(strcasecmp(cmeta->name,"length")!=0) 321 json_object_object_add(jcaps,cmeta->name,json_object_new_string(cmeta->value)); 322 } 323 cmeta=cmeta->next; 324 } 325 if(hasElement>=0){ 326 json_object_object_add(jcaps,"additionalParameter",carr); 327 json_object_object_add(doc,"additionalParameters",jcaps); 328 } 329 } 330 331 /** 332 * Add metadata property to a json_object 333 * @param conf the main configuration maps pointer 334 * @param meta a map pointer to the current metadata informations 335 * @param doc the json_object pointer to add the property to 336 */ 337 void printJMetadata(maps* conf,map* meta,json_object* doc){ 338 map* cmeta=meta; 339 json_object* carr=json_object_new_array(); 340 int hasElement=-1; 341 while(cmeta!=NULL){ 342 json_object* jcmeta=json_object_new_object(); 343 if(strcasecmp(cmeta->name,"role")==0 || 344 strcasecmp(cmeta->name,"href")==0 || 345 strcasecmp(cmeta->name,"title")==0 ){ 346 json_object_object_add(jcmeta,cmeta->name,json_object_new_string(cmeta->value)); 347 hasElement++; 348 } 349 json_object_array_add(carr,jcmeta); 350 cmeta=cmeta->next; 351 } 352 if(hasElement>=0) 353 json_object_object_add(doc,"metadata",carr); 354 } 355 356 /** 357 * Add metadata properties to a json_object 358 * @param m the main configuration maps pointer 359 * @param io a string 360 * @param in an elements pointer to the current input/output 361 * @param inputs the json_object pointer to add the property to 362 * @param serv the service pointer to extract the metadata from 363 */ 364 void printIOTypeJ(maps* m, const char *io, elements* in,json_object* inputs,service* serv){ 365 while(in!=NULL){ 366 json_object* input=json_object_new_object(); 367 json_object_object_add(input,"id",json_object_new_string(in->name)); 368 map* tmpMap=getMap(in->content,"title"); 369 if(tmpMap!=NULL) 370 json_object_object_add(input,"title",json_object_new_string(tmpMap->value)); 371 tmpMap=getMap(in->content,"abstract"); 372 if(tmpMap!=NULL) 373 json_object_object_add(input,"abstract",json_object_new_string(tmpMap->value)); 374 if(strcmp(io,"input")==0){ 375 tmpMap=getMap(in->content,"minOccurs"); 376 if(tmpMap!=NULL) 377 json_object_object_add(input,"minOccurs",json_object_new_string(tmpMap->value)); 378 tmpMap=getMap(in->content,"maxOccurs"); 379 if(tmpMap!=NULL) 380 json_object_object_add(input,"maxOccurs",json_object_new_string(tmpMap->value)); 381 } 382 if(in->format!=NULL){ 383 json_object* input1=json_object_new_object(); 384 json_object* prop0=json_object_new_array(); 385 if(strcasecmp(in->format,"LiteralData")==0 || 386 strcasecmp(in->format,"LiteralOutput")==0){ 387 printLiteralDataJ(m,in,input1); 388 }else{ 389 if(strcasecmp(in->format,"ComplexData")==0 || 390 strcasecmp(in->format,"ComplexOutput")==0){ 391 map* sizeMap=getMap(in->content,"maximumMegabytes"); 392 printFormatJ(m,in->defaults,prop0,true,sizeMap); 393 iotype* sup=in->supported; 394 while(sup!=NULL){ 395 printFormatJ(m,sup,prop0,false,sizeMap); 396 sup=sup->next; 397 } 398 json_object_object_add(input1,"formats",prop0); 399 } 400 else{ 401 json_object* prop1=json_object_new_object(); 402 json_object_object_add(prop1,"default",json_object_new_boolean(true)); 403 map* tmpMap1=getMap(in->defaults->content,"crs"); 404 if(tmpMap1==NULL) 405 return; 406 json_object_object_add(prop1,"crs",json_object_new_string(tmpMap1->value)); 407 json_object_array_add(prop0,prop1); 408 iotype* sup=in->supported; 409 while(sup!=NULL){ 410 json_object* prop1=json_object_new_object(); 411 json_object_object_add(prop1,"default",json_object_new_boolean(false)); 412 tmpMap1=getMap(sup->content,"crs"); 413 json_object_object_add(prop1,"crs",json_object_new_string(tmpMap1->value)); 414 json_object_array_add(prop0,prop1); 415 sup=sup->next; 416 } 417 json_object_object_add(input1,"supportedCRS",prop0); 418 } 419 } 420 json_object_object_add(input,io,input1); 421 } 422 printJMetadata(m,in->metadata,input); 423 printJAdditionalParameters(m,in->additional_parameters,input); 424 json_object_array_add(inputs,input); 425 in=in->next; 426 } 427 428 } 429 430 /** 431 * Add all the capabilities properties to a json_object 432 * @param ref the registry pointer 433 * @param m the main configuration maps pointer 434 * @param doc0 the void (json_object) pointer to add the property to 435 * @param nc0 the void (json_object) pointer to add the property to 436 * @param serv the service pointer to extract the metadata from 437 */ 438 void printGetCapabilitiesForProcessJ(registry *reg, maps* m,void* doc0,void* nc0,service* serv){ 439 json_object* doc=(json_object*) doc0; 440 json_object* nc=(json_object*) nc0; 441 json_object *res=json_object_new_object(); 442 map* tmpMap0=getMapFromMaps(m,"lenv","level"); 443 char* rUrl=serv->name; 444 if(tmpMap0!=NULL && atoi(tmpMap0->value)>0){ 445 int i=0; 446 maps* tmpMaps=getMaps(m,"lenv"); 447 char* tmpName=NULL; 448 for(i=0;i<atoi(tmpMap0->value);i++){ 449 char* key=(char*)malloc(15*sizeof(char)); 450 sprintf(key,"sprefix_%d",i); 451 map* tmpMap1=getMap(tmpMaps->content,key); 452 if(i+1==atoi(tmpMap0->value)) 453 if(tmpName==NULL){ 454 tmpName=(char*) malloc((strlen(serv->name)+strlen(tmpMap1->value)+1)*sizeof(char)); 455 sprintf(tmpName,"%s%s",tmpMap1->value,serv->name); 456 }else{ 457 char* tmpStr=zStrdup(tmpName); 458 tmpName=(char*) realloc(tmpName,(strlen(tmpStr)+strlen(tmpMap1->value)+strlen(serv->name)+1)*sizeof(char)); 459 sprintf(tmpName,"%s%s%s",tmpStr,tmpMap1->value,serv->name); 460 free(tmpStr); 461 } 462 else 463 if(tmpName==NULL){ 464 tmpName=(char*) malloc((strlen(tmpMap1->value)+1)*sizeof(char)); 465 sprintf(tmpName,"%s",tmpMap1->value); 466 }else{ 467 char* tmpStr=zStrdup(tmpName); 468 tmpName=(char*) realloc(tmpName,(strlen(tmpStr)+strlen(tmpMap1->value)+1)*sizeof(char)); 469 sprintf(tmpName,"%s%s",tmpStr,tmpMap1->value); 470 free(tmpStr); 471 } 472 } 473 json_object_object_add(res,"id",json_object_new_string(tmpName)); 474 if(tmpName!=NULL){ 475 rUrl=zStrdup(tmpName); 476 free(tmpName); 477 } 478 } 479 else 480 json_object_object_add(res,"id",json_object_new_string(serv->name)); 481 if(serv->content!=NULL){ 482 map* tmpMap=getMap(serv->content,"title"); 483 if(tmpMap!=NULL){ 484 json_object_object_add(res,"title",json_object_new_string(tmpMap->value)); 485 } 486 tmpMap=getMap(serv->content,"abstract"); 487 if(tmpMap!=NULL){ 488 json_object_object_add(res,"abstract",json_object_new_string(tmpMap->value)); 489 } 490 tmpMap=getMap(serv->content,"processVersion"); 491 if(tmpMap!=NULL){ 492 if(strlen(tmpMap->value)<5){ 493 char *val=(char*)malloc((strlen(tmpMap->value)+5)*sizeof(char)); 494 sprintf(val,"%s.0.0",tmpMap->value); 495 json_object_object_add(res,"version",json_object_new_string(val)); 496 free(val); 497 } 498 else 499 json_object_object_add(res,"version",json_object_new_string(tmpMap->value)); 500 } 501 int limit=4; 502 int i=0; 503 map* sType=getMap(serv->content,"serviceType"); 504 for(;i<limit;i+=2){ 505 json_object *res1=json_object_new_object(); 506 json_object *res2=json_object_new_array(); 507 char *saveptr; 508 char* dupStr=strdup(jcapabilities[i+1]); 509 char *tmps = strtok_r (dupStr, " ", &saveptr); 510 while(tmps!=NULL){ 511 json_object_array_add(res2,json_object_new_string(tmps)); 512 tmps = strtok_r (NULL, " ", &saveptr); 513 } 514 free(dupStr); 515 json_object_object_add(res,jcapabilities[i],res2); 516 } 517 json_object *res1=json_object_new_array(); 518 json_object *res2=json_object_new_object(); 519 json_object_object_add(res2,"rel",json_object_new_string("canonical")); 520 json_object_object_add(res2,"type",json_object_new_string("application/json")); 521 json_object_object_add(res2,"title",json_object_new_string("Process Description")); 522 map* tmpUrl=getMapFromMaps(m,"main","serverAddress"); 523 char* tmpStr=(char*) malloc((strlen(tmpUrl->value)+strlen(rUrl)+13)*sizeof(char)); 524 sprintf(tmpStr,"%s/processes/%s/",tmpUrl->value,rUrl); 525 if(doc==NULL){ 526 json_object_object_add(res2,"title",json_object_new_string("Execute End Point")); 527 char* tmpStr1=zStrdup(tmpStr); 528 tmpStr=(char*) realloc(tmpStr,(strlen(tmpStr)+6)*sizeof(char)); 529 sprintf(tmpStr,"%sjobs/",tmpStr1); 530 free(tmpStr1); 531 } 532 json_object_object_add(res2,"href",json_object_new_string(tmpStr)); 533 free(tmpStr); 534 json_object_array_add(res1,res2); 535 json_object_object_add(res,"links",res1); 536 } 537 if(doc==NULL){ 538 elements* in=serv->inputs; 539 json_object* inputs=json_object_new_array(); 540 printIOTypeJ(m,"input",in,inputs,serv); 541 json_object_object_add(res,"inputs",inputs); 542 543 in=serv->outputs; 544 json_object* outputs=json_object_new_array(); 545 printIOTypeJ(m,"output",in,outputs,serv); 546 json_object_object_add(res,"outputs",outputs); 547 548 } 549 if(strcmp(rUrl,serv->name)!=0) 550 free(rUrl); 551 if(doc!=NULL) 552 json_object_array_add(doc,res); 553 else 554 json_object_object_add(nc,"process",json_object_get(res)); 555 556 } 557 558 /** 559 * Print an OWS ExceptionReport Document and HTTP headers (when required) 560 * depending on the code. 561 * Set hasPrinted value to true in the [lenv] section. 562 * 563 * @param m the maps containing the settings of the main.cfg file 564 * @param s the map containing the text,code,locator keys (or a map array of the same keys) 565 */ 566 void printExceptionReportResponseJ(maps* m,map* s){ 567 if(getMapFromMaps(m,"lenv","hasPrinted")!=NULL) 568 return; 569 int buffersize; 570 json_object *res=json_object_new_object(); 571 572 maps* tmpMap=getMaps(m,"main"); 573 const char *exceptionCode; 574 575 map* tmp=getMap(s,"code"); 576 if(tmp!=NULL){ 577 if(strcmp(tmp->value,"OperationNotSupported")==0 || 578 strcmp(tmp->value,"NoApplicableCode")==0) 579 exceptionCode="501 Not Implemented"; 580 else 581 if(strcmp(tmp->value,"MissingParameterValue")==0 || 582 strcmp(tmp->value,"InvalidUpdateSequence")==0 || 583 strcmp(tmp->value,"OptionNotSupported")==0 || 584 strcmp(tmp->value,"VersionNegotiationFailed")==0 || 585 strcmp(tmp->value,"InvalidParameterValue")==0) 586 exceptionCode="400 Bad request"; 587 else 588 if(strcmp(tmp->value,"NotFound")==0) 589 exceptionCode="404 Not Found"; 590 else 591 exceptionCode="501 Internal Server Error"; 592 json_object_object_add(res,"code",json_object_new_string(tmp->value)); 593 } 594 else 595 exceptionCode="501 Internal Server Error"; 596 printHeaders(m); 597 598 tmp=getMapFromMaps(m,"lenv","status_code"); 599 if(tmp!=NULL) 600 exceptionCode=tmp->value; 601 if(m!=NULL){ 602 map *tmpSid=getMapFromMaps(m,"lenv","sid"); 603 if(tmpSid!=NULL){ 604 if( getpid()==atoi(tmpSid->value) ){ 605 printf("Status: %s\r\n\r\n",exceptionCode); 606 } 607 } 608 else{ 609 printf("Status: %s\r\n\r\n",exceptionCode); 610 } 611 }else{ 612 printf("Status: %s\r\n\r\n",exceptionCode); 613 } 614 tmp=getMap(s,"text"); 615 if(tmp==NULL) 616 tmp=getMap(s,"message"); 617 if(tmp==NULL) 618 tmp=getMapFromMaps(m,"lenv","message"); 619 if(tmp!=NULL) 620 json_object_object_add(res,"description",json_object_new_string(tmp->value)); 621 const char* jsonStr=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); 622 printf(jsonStr); 623 if(m!=NULL) 624 setMapInMaps(m,"lenv","hasPrinted","true"); 625 } 626 627 /** 628 * Parse LiteralData value 629 * @param conf the maps containing the settings of the main.cfg file 630 * @param req json_object pointing to the input/output 631 * @param element 632 * @param output the maps to set current json structure 633 */ 634 void parseJLiteral(maps* conf,json_object* req,elements* element,maps* output){ 635 json_object* json_cinput=NULL; 636 if(json_object_object_get_ex(req,"value",&json_cinput)!=FALSE){ 637 output->content=createMap("value",json_object_get_string(json_cinput)); 638 } 639 const char* tmpStrs[2]={ 640 "dataType", 641 "uom" 642 }; 643 for(int i=0;i<2;i++) 644 if(json_object_object_get_ex(req,tmpStrs[i],&json_cinput)!=FALSE){ 645 json_object* json_cinput1; 646 if(json_object_object_get_ex(json_cinput,"name",&json_cinput1)!=FALSE){ 647 if(output->content==NULL) 648 output->content=createMap(tmpStrs[i],json_object_get_string(json_cinput1)); 649 else 650 addToMap(output->content,tmpStrs[i],json_object_get_string(json_cinput1)); 651 } 652 if(json_object_object_get_ex(json_cinput,"reference",&json_cinput1)!=FALSE){ 653 if(output->content==NULL) 654 output->content=createMap(tmpStrs[i],json_object_get_string(json_cinput1)); 655 else 656 addToMap(output->content,tmpStrs[i],json_object_get_string(json_cinput1)); 657 } 658 } 659 if(json_object_object_get_ex(req,"transmissionMode",&json_cinput)!=FALSE){ 660 if(output->content==NULL) 661 output->content=createMap("transmissionMode",json_object_get_string(json_cinput)); 662 else 663 addToMap(output->content,"transmissionMode",json_object_get_string(json_cinput)); 664 } 665 } 666 667 /** 668 * Parse ComplexData value 669 * @param conf the maps containing the settings of the main.cfg file 670 * @param req json_object pointing to the input/output 671 * @param element 672 * @param output the maps to set current json structure 673 * @param name the name of the request from http_requests 674 */ 675 void parseJComplex(maps* conf,json_object* req,elements* element,maps* output,const char* name){ 676 json_object* json_cinput=NULL; 677 if(json_object_object_get_ex(req,"value",&json_cinput)!=FALSE){ 678 json_object* json_value=NULL; 679 if(json_object_object_get_ex(json_cinput,"inlineValue",&json_value)!=FALSE) 680 output->content=createMap("value",json_object_get_string(json_value)); 681 else{ 682 if(json_object_object_get_ex(json_cinput,"href",&json_value)!=FALSE){ 683 output->content=createMap("xlink:href",json_object_get_string(json_value)); 684 int len=0; 685 int createdStr=0; 686 char *tmpStr="url"; 687 char *tmpStr1="input"; 688 if(getMaps(conf,"http_requests")==NULL){ 689 maps* tmpMaps=createMaps("http_requests"); 690 tmpMaps->content=createMap("length","1"); 691 addMapsToMaps(&conf,tmpMaps); 692 freeMaps(&tmpMaps); 693 free(tmpMaps); 694 }else{ 695 map* tmpMap=getMapFromMaps(conf,"http_requests","length"); 696 int len=atoi(tmpMap->value); 697 createdStr=1; 698 tmpStr=(char*) malloc((12)*sizeof(char)); 699 sprintf(tmpStr,"%d",len+1); 700 setMapInMaps(conf,"http_requests","length",tmpStr); 701 sprintf(tmpStr,"url_%d",len); 702 tmpStr1=(char*) malloc((14)*sizeof(char)); 703 sprintf(tmpStr1,"input_%d",len); 704 } 705 setMapInMaps(conf,"http_requests",tmpStr,json_object_get_string(json_value)); 706 setMapInMaps(conf,"http_requests",tmpStr1,name); 707 if(createdStr>0){ 708 free(tmpStr); 709 free(tmpStr1); 710 } 711 } 712 } 713 } 714 if(json_object_object_get_ex(req,"format",&json_cinput)!=FALSE){ 715 json_object_object_foreach(json_cinput, key, val) { 716 if(output->content==NULL) 717 output->content=createMap(key,json_object_get_string(val)); 718 else 719 addToMap(output->content,key,json_object_get_string(val)); 720 721 } 722 } 723 if(json_object_object_get_ex(req,"transmissionMode",&json_cinput)!=FALSE){ 724 if(output->content==NULL) 725 output->content=createMap("transmissionMode",json_object_get_string(json_cinput)); 726 else 727 addToMap(output->content,"transmissionMode",json_object_get_string(json_cinput)); 728 } 729 } 730 731 /** 732 * Parse BoundingBox value 733 * @param conf the maps containing the settings of the main.cfg file 734 * @param req json_object pointing to the input/output 735 * @param element 736 * @param output the maps to set current json structure 737 */ 738 void parseJBoundingBox(maps* conf,json_object* req,elements* element,maps* output){ 739 json_object* json_cinput=NULL; 740 if(json_object_object_get_ex(req,"bbox",&json_cinput)!=FALSE){ 741 output->content=createMap("value",json_object_get_string(json_cinput)); 742 } 743 if(json_object_object_get_ex(req,"crs",&json_cinput)!=FALSE){ 744 if(output->content==NULL) 745 output->content=createMap("crs",json_object_get_string(json_cinput)); 746 else 747 addToMap(output->content,"crs","http://www.opengis.net/def/crs/OGC/1.3/CRS84"); 748 } 749 char* tmpStrs[2]={ 750 "lowerCorner", 751 "upperCorner" 752 }; 753 for(int i=0;i<2;i++) 754 if(json_object_object_get_ex(req,tmpStrs[i],&json_cinput)!=FALSE){ 755 if(output->content==NULL) 756 output->content=createMap(tmpStrs[i],json_object_get_string(json_cinput)); 757 else 758 addToMap(output->content,tmpStrs[i],json_object_get_string(json_cinput)); 759 } 760 if(json_object_object_get_ex(req,"transmissionMode",&json_cinput)!=FALSE){ 761 if(output->content==NULL) 762 output->content=createMap("transmissionMode",json_object_get_string(json_cinput)); 763 else 764 addToMap(output->content,"transmissionMode",json_object_get_string(json_cinput)); 765 } 766 } 767 768 /** 769 * Parse inputs / outputs 770 * @param conf the maps containing the settings of the main.cfg file 771 * @param req json_object pointing to the input/output 772 * @param ioElements the elements extracted from the zcfg 773 * @param ioMaps the produced maps containing inputs (or outputs) 774 * @param ioType the char* set to inputs or outputs 775 */ 776 void parseJIO(maps* conf, json_object* req, elements* ioElements, maps** ioMaps,const char* ioType){ 777 json_object* json_io=NULL; 778 if(json_object_object_get_ex(req,ioType,&json_io)!=FALSE){ 779 json_object* json_current_io=NULL; 780 size_t len=json_object_array_length(json_io); 781 for(int i=0;i<len;i++){ 782 maps *cMaps=NULL; 783 json_current_io=json_object_array_get_idx(json_io,i); 784 json_object* cname=NULL; 785 if(json_object_object_get_ex(json_current_io,"id",&cname)!=FALSE){ 786 cMaps=createMaps(json_object_get_string(cname)); 787 elements* cio=getElements(ioElements,json_object_get_string(cname)); 788 json_object* json_input; 789 json_object* json_cinput; 790 if(json_object_object_get_ex(json_current_io,"input",&json_input)!=FALSE){ 791 if(json_object_object_get_ex(json_input,"dataType",&json_cinput)!=FALSE){ 792 parseJLiteral(conf,json_input,cio,cMaps); 793 } else if(json_object_object_get_ex(json_input,"format",&json_cinput)!=FALSE){ 794 parseJComplex(conf,json_input,cio,cMaps,json_object_get_string(cname)); 795 } else if(json_object_object_get_ex(json_input,"bbox",&json_cinput)!=FALSE){ 796 parseJBoundingBox(conf,json_input,cio,cMaps); 797 }// else error! 798 else{ 799 if(json_object_object_get_ex(json_input,"value",&json_cinput)!=FALSE){ 800 map* error=createMap("code","BadRequest"); 801 char tmpS[1024]; 802 sprintf(tmpS,_("Missing input for %s"),ioElements->name); 803 addToMap(error,"message",tmpS); 804 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 805 printExceptionReportResponseJ(conf,error); 806 return; 807 }else{ 808 if(json_object_get_type(json_cinput)==json_type_string){ 809 parseJLiteral(conf,json_input,cio,cMaps); 810 }else if(json_object_get_type(json_cinput)==json_type_object){ 811 json_object* json_ccinput=NULL; 812 if(json_object_object_get_ex(json_cinput,"bbox",&json_ccinput)!=FALSE){ 813 parseJComplex(conf,json_input,cio,cMaps,json_object_get_string(cname)); 814 } 815 else{ 816 parseJBoundingBox(conf,json_input,cio,cMaps); 817 } 818 }else{ 819 if(strcmp(ioType,"input")==0){ 820 map* error=createMap("code","BadRequest"); 821 char tmpS1[1024]; 822 sprintf(tmpS1,_("Issue with input %s"),ioElements->name); 823 addToMap(error,"message",tmpS1); 824 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 825 printExceptionReportResponseJ(conf,error); 826 return; 827 } 828 } 829 } 830 } 831 }else{ 832 if(strcmp(ioType,"input")==0){ 833 map* error=createMap("code","BadRequest"); 834 char tmpS1[1024]; 835 sprintf(tmpS1,_("Missing input for %s"),ioElements->name); 836 addToMap(error,"message",tmpS1); 837 setMapInMaps(conf,"lenv","status_code","400 Bad Request"); 838 printExceptionReportResponseJ(conf,error); 839 return; 840 }else{ 841 // Outputs 842 if(json_object_object_get_ex(json_current_io,"transmissionMode",&json_cinput)!=FALSE){ 843 if(cMaps->content==NULL) 844 cMaps->content=createMap("transmissionMode",json_object_get_string(json_cinput)); 845 else 846 addToMap(cMaps->content,"transmissionMode",json_object_get_string(json_cinput)); 847 } 848 if(json_object_object_get_ex(json_current_io,"format",&json_cinput)!=FALSE){ 849 json_object_object_foreach(json_cinput, key, val) { 850 if(cMaps->content==NULL) 851 cMaps->content=createMap(key,json_object_get_string(val)); 852 else 853 addToMap(cMaps->content,key,json_object_get_string(val)); 854 } 855 } 856 } 857 } 858 } 859 addToMap(cMaps->content,"inRequest","true"); 860 if (ioMaps == NULL) 861 *ioMaps = dupMaps(&cMaps); 862 else 863 addMapsToMaps (ioMaps, cMaps); 864 } 865 } 866 } 867 868 869 870 /** 871 * Parse Json Request 872 * @param conf the maps containing the settings of the main.cfg file 873 * @param s the current service metadata 874 * @param req the JSON object of the request body 875 * @param inputs the produced maps 876 * @param outputs the produced maps 877 */ 878 void parseJRequest(maps* conf, service* s,json_object* req,maps** inputs,maps** outputs){ 879 elements* io=s->inputs; 880 881 json_object* json_io=NULL; 882 int parsed=0; 883 char* tmpS="input"; 884 maps* in=*inputs; 885 parseJIO(conf,req,s->inputs,inputs,"inputs"); 886 887 if(parsed==0){ 888 json_io=NULL; 889 if(json_object_object_get_ex(req,"outputs",&json_io)!=FALSE){ 890 parseJIO(conf,req,s->outputs,outputs,"outputs"); 891 } 892 } 893 } 894 895 /** 896 * Print the jobs list 897 * 898 * @param conf the maps containing the settings of the main.cfg file 899 * @return the JSON object pointer to the jobs list 900 */ 901 json_object* printJobList(maps* conf){ 902 json_object* res=json_object_new_array(); 903 map* tmpPath=getMapFromMaps(conf,"main","tmpPath"); 904 map* oIdentifier=getMapFromMaps(conf,"lenv","oIdentifier"); 905 char* cpath=(char*)malloc((strlen(tmpPath->value)+strlen(oIdentifier->value)+14)*sizeof(char)); 906 sprintf(cpath,"%s/statusInfos/%s",tmpPath->value,oIdentifier->value); 907 struct dirent *dp; 908 DIR *dirp = opendir (cpath); 909 if(dirp!=NULL){ 910 while ((dp = readdir (dirp)) != NULL){ 911 char* extn = strstr(dp->d_name, ".json"); 912 if(extn!=NULL){ 913 json_object* cjob=json_object_new_object(); 914 char* tmpStr=zStrdup(dp->d_name); 915 tmpStr[strlen(dp->d_name)-5]=0; 916 json_object_object_add(cjob,"id",json_object_new_string(tmpStr)); 917 char *tmps1=(char*)malloc((strlen(cpath)+strlen(dp->d_name)+2)*sizeof(char)); 918 sprintf (tmps1, "%s/%s", cpath, dp->d_name); 919 FILE* cdat=fopen(tmps1,"rb"); 920 if(cdat!=NULL){ 921 zStatStruct f_status; 922 int s=zStat(tmps1, &f_status); 923 char* mystring=(char*)malloc((f_status.st_size+1)*sizeof(char)); 924 fread(mystring,1,f_status.st_size,cdat); 925 mystring[f_status.st_size]=0; 926 json_object *jobj = NULL; 927 int slen = 0; 928 enum json_tokener_error jerr; 929 struct json_tokener* tok=json_tokener_new(); 930 do { 931 slen = strlen(mystring); 932 jobj = json_tokener_parse_ex(tok, mystring, slen); 933 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 934 if (jerr != json_tokener_success) { 935 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 936 return res; 937 } 938 if (tok->char_offset < slen){ 939 return res; 940 } 941 json_object_object_add(cjob,"infos",jobj); 942 free(mystring); 943 fclose(cdat); 944 } 945 free(tmpStr); 946 json_object_array_add(res,cjob); 947 } 948 } 949 closedir (dirp); 950 } 951 return res; 952 } 953 954 /** 955 * Print the result of an execution 956 * 957 * @param conf the maps containing the settings of the main.cfg file 958 * @param s service pointer to metadata 959 * @param result outputs of the service 960 * @param res the status of executino SERVICE_FAILED/SERVICE_SUCCEEDED 961 * @return the JSON object pointer to the result 962 */ 963 json_object* printJResult(maps* conf,service* s,maps* result,int res){ 964 printHeaders(conf); 965 json_object* eres1=json_object_new_object(); 966 json_object* eres=json_object_new_array(); 967 maps* resu=result; 968 int itn=0; 969 while(resu!=NULL){ 970 json_object* res1=json_object_new_object(); 971 json_object_object_add(res1,"id",json_object_new_string(resu->name)); 972 map* tmpMap=NULL; 973 if((tmpMap=getMap(resu->content,"value"))!=NULL || 974 (getMap(resu->content,"generated_file"))!=NULL){ 975 json_object* res2=json_object_new_object(); 976 map* tmpMap1=NULL; 977 if((tmpMap1=getMap(resu->content,"transmissionMode"))!=NULL) { 978 if(strcmp(tmpMap1->value,"value")==0) { 979 map* tmpMap2=getMap(resu->content,"mimeType"); 980 if(tmpMap2!=NULL && strstr(tmpMap2->value,"json")!=NULL){ 981 json_object *jobj = NULL; 982 int slen = 0; 983 enum json_tokener_error jerr; 984 struct json_tokener* tok=json_tokener_new(); 985 do { 986 slen = strlen(tmpMap->value); 987 jobj = json_tokener_parse_ex(tok, tmpMap->value, slen); 988 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 989 if (jerr != json_tokener_success) { 990 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 991 return eres1; 992 } 993 if (tok->char_offset < slen){ 994 return eres1; 995 } 996 json_object_object_add(res2,"inlineValue",jobj); 997 }else 998 json_object_object_add(res2,"inlineValue",json_object_new_string(tmpMap->value)); 999 } 1000 else{ 1001 // Create file for reference data if not existing 1002 map *gfile=getMap(resu->content,"generated_file"); 1003 char *file_name=NULL; 1004 char *file_path=NULL; 1005 char *file_url=NULL; 1006 map *tmp1=getMapFromMaps(conf,"main","tmpPath"); 1007 if(gfile!=NULL){ 1008 gfile=getMap(resu->content,"expected_generated_file"); 1009 if(gfile==NULL){ 1010 gfile=getMap(resu->content,"generated_file"); 1011 } 1012 if(strstr(gfile->value,tmp1->value)!=NULL) 1013 file_name=zStrdup(strstr(gfile->value,tmp1->value)+strlen(tmp1->value)); 1014 }/*else*/ 1015 { 1016 // Create file for reference data 1017 map *tmpUrl=getMapFromMaps(conf,"main","tmpUrl"); 1018 map *usid=getMapFromMaps(conf,"lenv","usid"); 1019 map *ext=getMap(resu->content,"extension"); 1020 if(gfile==NULL){ 1021 char file_ext[32]; 1022 if( ext != NULL && ext->value != NULL) { 1023 strncpy(file_ext, ext->value, 32); 1024 } 1025 else { 1026 // Obtain default file extension (see mimetypes.h). 1027 // If the MIME type is not recognized, txt is used as the default extension 1028 map* mtype=getMap(resu->content,"mimeType"); 1029 getFileExtension(mtype != NULL ? mtype->value : NULL, file_ext, 32); 1030 } 1031 if(file_name!=NULL) 1032 free(file_name); 1033 1034 file_name=(char*)malloc((strlen(s->name)+strlen(usid->value)+strlen(file_ext)+strlen(resu->name)+45)*sizeof(char)); 1035 sprintf(file_name,"ZOO_DATA_%s_%s_%s_%d.%s",s->name,resu->name,usid->value,itn,file_ext); 1036 1037 file_path=(char*)malloc((strlen(tmp1->value)+strlen(file_name)+2)*sizeof(char)); 1038 sprintf(file_path,"%s/%s",tmp1->value,file_name); 1039 }else{ 1040 file_path=(char*)malloc((strlen(gfile->value)+1)*sizeof(char)); 1041 sprintf(file_path,"%s",gfile->value); 1042 } 1043 1044 1045 itn++; 1046 1047 file_url=(char*)malloc((strlen(tmpUrl->value)+ 1048 strlen(file_name)+2)*sizeof(char)); 1049 sprintf(file_url,"%s/%s",tmpUrl->value,file_name); 1050 1051 if(gfile==NULL){ 1052 FILE *ofile=fopen(file_path,"wb"); 1053 if(ofile==NULL){ 1054 char tmpMsg[1024]; 1055 sprintf(tmpMsg, 1056 _("Unable to create the file \"%s\" for storing the %s final result."), 1057 file_name,resu->name); 1058 map* error=createMap("code","InternalError"); 1059 addToMap(error,"message",tmpMsg); 1060 printExceptionReportResponseJ(conf,error); 1061 free(file_name); 1062 free(file_path); 1063 return NULL; 1064 } 1065 1066 map* toto=getMap(resu->content,"value"); 1067 if(toto==NULL){ 1068 char tmpMsg[1024]; 1069 sprintf(tmpMsg, 1070 _("No value found for the requested output %s."), 1071 resu->name); 1072 map* error=createMap("code","InternalError"); 1073 addToMap(error,"message",tmpMsg); 1074 printExceptionReportResponseJ(conf,error); 1075 fclose(ofile); 1076 free(file_name); 1077 free(file_path); 1078 return NULL; 1079 } 1080 map* size=getMap(resu->content,"size"); 1081 if(size!=NULL && toto!=NULL) 1082 fwrite(toto->value,1,(atoi(size->value))*sizeof(char),ofile); 1083 else 1084 if(toto!=NULL && toto->value!=NULL) 1085 fwrite(toto->value,1, 1086 strlen(toto->value)*sizeof(char),ofile); 1087 fclose(ofile); 1088 } 1089 json_object_object_add(res2,"href", 1090 json_object_new_string(file_url)); 1091 free(file_url); 1092 free(file_name); 1093 free(file_path); 1094 1095 } 1096 1097 } 1098 } 1099 json_object_object_add(res1,"value",res2); 1100 json_object_array_add(eres,res1); 1101 } 1102 resu=resu->next; 1103 } 1104 json_object_object_add(eres1,"outputs",eres); 1105 const char* jsonStr = 1106 json_object_to_json_string_ext(eres1,JSON_C_TO_STRING_PLAIN); 1107 map *tmpPath = getMapFromMaps (conf, "main", "tmpPath"); 1108 map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); 1109 map *sessId = getMapFromMaps (conf, "lenv", "usid"); 1110 char tmp[1024]; 1111 sprintf(tmp,"%s/%s_%s.json", 1112 tmpPath->value,cIdentifier->value,sessId->value); 1113 FILE* foutput=fopen(tmp,"w+"); 1114 if(foutput!=NULL){ 1115 fclose(foutput); 1116 char tmpUrl[1024]; 1117 map* tmpPath1 = getMapFromMaps (conf, "main", "tmpUrl"); 1118 sprintf(tmpUrl,"%s/%s_%s.json",tmpPath1->value, 1119 cIdentifier->value,sessId->value); 1120 setMapInMaps(conf,"headers","Location",tmpUrl); 1121 } 1122 if(res==3){ 1123 setMapInMaps(conf,"headers","Status","201 Created"); 1124 } 1125 else{ 1126 setMapInMaps(conf,"headers","Status","500 Issue running your service"); 1127 } 1128 1129 return eres1; 1130 } 1131 1132 1133 /** 1134 * Create the status links 1135 * 1136 * @param conf the maps containing the settings of the main.cfg file 1137 * @param result an integer (>0 for adding the /result link) 1138 * @param obj the JSON object pointer to add the links to 1139 * @return 0 1140 */ 1141 int createStatusLinks(maps* conf,int result,json_object* obj){ 1142 json_object* res=json_object_new_array(); 1143 map *tmpPath = getMapFromMaps (conf, "main", "serverAddress"); 1144 map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); 1145 map *sessId = getMapFromMaps (conf, "lenv", "usid"); 1146 char *Url0=(char*) malloc((strlen(tmpPath->value)+ 1147 strlen(cIdentifier->value)+ 1148 strlen(sessId->value)+18)*sizeof(char)); 1149 int needResult=-1; 1150 char *message, *status; 1151 sprintf(Url0,"%s/processes/%s/jobs/%s", 1152 tmpPath->value, 1153 cIdentifier->value, 1154 sessId->value); 1155 setMapInMaps(conf,"headers","Location",Url0); 1156 json_object* val=json_object_new_object(); 1157 json_object_object_add(val,"Title", 1158 json_object_new_string(_("Status location"))); 1159 json_object_object_add(val,"href",json_object_new_string(Url0)); 1160 json_object_array_add(res,val); 1161 if(result>0){ 1162 free(Url0); 1163 Url0=(char*) malloc((strlen(tmpPath->value)+ 1164 strlen(cIdentifier->value)+strlen(sessId->value)+ 1165 25)*sizeof(char)); 1166 sprintf(Url0,"%s/processes/%s/jobs/%s/result", 1167 tmpPath->value, 1168 cIdentifier->value, 1169 sessId->value); 1170 json_object* val1=json_object_new_object(); 1171 json_object_object_add(val1,"Title", 1172 json_object_new_string(_("Result location"))); 1173 json_object_object_add(val1,"href",json_object_new_string(Url0)); 1174 json_object_array_add(res,val1); 1175 } 1176 json_object_object_add(obj,"links",res); 1177 free(Url0); 1178 return 0; 1179 } 1180 1181 /** 1182 * Get the status file path 1183 * 1184 * @param conf the maps containing the settings of the main.cfg file 1185 * @return a char* containing the full status file path 1186 */ 1187 char* json_getStatusFilePath(maps* conf){ 1188 map *tmpPath = getMapFromMaps (conf, "main", "tmpPath"); 1189 map *cIdentifier = getMapFromMaps (conf, "lenv", "oIdentifier"); 1190 map *sessId = getMapFromMaps (conf, "lenv", "usid"); 1191 if(sessId!=NULL){ 1192 sessId = getMapFromMaps (conf, "lenv", "gs_usid"); 1193 if(sessId==NULL) 1194 sessId = getMapFromMaps (conf, "lenv", "usid"); 1195 }else 1196 sessId = getMapFromMaps (conf, "lenv", "gs_usid"); 1197 char *tmp1=(char*) malloc((strlen(tmpPath->value)+ 1198 strlen(cIdentifier->value)+14)*sizeof(char)); 1199 sprintf(tmp1,"%s/statusInfos/%s", 1200 tmpPath->value, 1201 cIdentifier->value); 1202 if(mkdir(tmp1,0777) != 0 && errno != EEXIST){ 1203 fprintf(stderr,"Issue creating directory %s\n",tmp1); 1204 return NULL; 1205 } 1206 free(tmp1); 1207 tmp1=(char*) malloc((strlen(tmpPath->value)+ 1208 strlen(cIdentifier->value)+ 1209 strlen(sessId->value)+20)*sizeof(char)); 1210 int needResult=0; 1211 char *message, *rstatus; 1212 sprintf(tmp1,"%s/statusInfos/%s/%s.json", 1213 tmpPath->value, 1214 cIdentifier->value, 1215 sessId->value); 1216 1217 return tmp1; 1218 } 1219 1220 /** 1221 * Create the status file 1222 * 1223 * @param conf the maps containing the settings of the main.cfg file 1224 * @param status an integer (SERVICE_ACCEPTED,SERVICE_STARTED...) 1225 * @return an integer (0 in case of success, 1 in case of failure) 1226 */ 1227 int createStatusFile(maps* conf,int status){ 1228 int needResult=0; 1229 const char *message, *rstatus; 1230 char* tmp1=json_getStatusFilePath(conf); 1231 // Create statusInfo JSON object 1232 // cf. https://github.com/opengeospatial/wps-rest-binding/blob/master/core/ 1233 // openapi/schemas/statusInfo.yaml 1234 json_object* res=json_object_new_object(); 1235 json_object_object_add(res,"status",json_object_new_string("successful")); 1236 switch(status){ 1237 case SERVICE_ACCEPTED: 1238 { 1239 message=_("ZOO-Kernel accepted to run your service!"); 1240 rstatus="accepted"; 1241 break; 1242 } 1243 case SERVICE_STARTED: 1244 { 1245 message=_("ZOO-Kernel is currently running your service!"); 1246 rstatus="running"; 1247 break; 1248 } 1249 case SERVICE_PAUSED: 1250 { 1251 message=_("ZOO-Kernel pause your service!"); 1252 rstatus="paused"; 1253 break; 1254 } 1255 case SERVICE_SUCCEEDED: 1256 { 1257 message=_("ZOO-Kernel successfully run your service!"); 1258 rstatus="successful"; 1259 needResult=1; 1260 break; 1261 } 1262 default: 1263 { 1264 message=_("ZOO-Kernel failed to run your service!"); 1265 rstatus="failed"; 1266 break; 1267 } 1268 } 1269 setMapInMaps(conf,"lenv","message",message); 1270 setMapInMaps(conf,"lenv","status",rstatus); 1271 1272 map* mess=getMapFromMaps(conf,"lenv","message"); 1273 if(mess!=NULL) 1274 json_object_object_add(res,"message",json_object_new_string(mess->value)); 1275 1276 createStatusLinks(conf,needResult,res); 1277 1278 FILE* foutput1=fopen(tmp1,"w+"); 1279 if(foutput1!=NULL){ 1280 const char* jsonStr1=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); 1281 fprintf(foutput1,"%s",jsonStr1); 1282 fclose(foutput1); 1283 }else{ 1284 // Failure 1285 setMapInMaps(conf,"lenv","message",_("Unable to store the statusInfo!")); 1286 free(tmp1); 1287 return 1; 1288 } 1289 free(tmp1); 1290 return 0; 1291 } 1292 1293 /** 1294 * Create the status file 1295 * 1296 * @param conf the maps containing the settings of the main.cfg file 1297 * @return an integer (0 in case of success, 1 in case of failure) 1298 */ 1299 int json_getStatusFile(maps* conf){ 1300 char* tmp1=json_getStatusFilePath(conf); 1301 FILE* statusFile=fopen(tmp1,"rb"); 1302 1303 map* tmpMap=getMapFromMaps(conf,"lenv","gs_usid"); 1304 if(tmpMap!=NULL){ 1305 char* tmpStr=_getStatus(conf,tmpMap->value); 1306 if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){ 1307 char *tmpStr1=zStrdup(tmpStr); 1308 char *tmpStr0=zStrdup(strstr(tmpStr,"|")+1); 1309 free(tmpStr); 1310 tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0'; 1311 setMapInMaps(conf,"lenv","PercentCompleted",tmpStr1); 1312 setMapInMaps(conf,"lenv","gs_message",tmpStr0); 1313 free(tmpStr0); 1314 free(tmpStr1); 1315 } 1316 } 1317 } 1318 1319 /** 1320 * Produce the JSON object for api info object 1321 * 1322 * @param conf the maps containing the settings of the main.cfg file 1323 * @param res the JSON object for the api info 1324 */ 1325 void produceApiInfo(maps* conf,json_object* res,json_object* res5){ 1326 json_object *res1=json_object_new_object(); 1327 map* tmpMap=getMapFromMaps(conf,"provider","providerName"); 1328 if(tmpMap!=NULL){ 1329 json_object *res2=json_object_new_object(); 1330 json_object_object_add(res2,"name",json_object_new_string(tmpMap->value)); 1331 tmpMap=getMapFromMaps(conf,"provider","addressElectronicMailAddress"); 1332 if(tmpMap!=NULL) 1333 json_object_object_add(res2,"email",json_object_new_string(tmpMap->value)); 1334 tmpMap=getMapFromMaps(conf,"provider","providerSite"); 1335 if(tmpMap!=NULL) 1336 json_object_object_add(res2,"url",json_object_new_string(tmpMap->value)); 1337 json_object_object_add(res1,"contact",res2); 1338 } 1339 tmpMap=getMapFromMaps(conf,"identification","abstract"); 1340 if(tmpMap!=NULL){ 1341 json_object_object_add(res1,"description",json_object_new_string(tmpMap->value)); 1342 json_object_object_add(res5,"description",json_object_new_string(tmpMap->value)); 1343 tmpMap=getMapFromMaps(conf,"main","serverAddress"); 1344 json_object_object_add(res5,"url",json_object_new_string(tmpMap->value)); 1345 } 1346 tmpMap=getMapFromMaps(conf,"identification","title"); 1347 if(tmpMap!=NULL) 1348 json_object_object_add(res1,"title",json_object_new_string(tmpMap->value)); 1349 json_object_object_add(res1,"version",json_object_new_string(ZOO_VERSION)); 1350 tmpMap=getMapFromMaps(conf,"identification","keywords"); 1351 if(tmpMap!=NULL){ 1352 char *saveptr; 1353 char *tmps = strtok_r (tmpMap->value, ",", &saveptr); 1354 json_object *res3=json_object_new_array(); 1355 while (tmps != NULL){ 1356 json_object_array_add(res3,json_object_new_string(tmps)); 1357 tmps = strtok_r (NULL, ",", &saveptr); 1358 } 1359 json_object_object_add(res1,"x-keywords",res3); 1360 } 1361 maps* tmpMaps=getMaps(conf,"provider"); 1362 if(tmpMaps!=NULL){ 1363 json_object *res4=json_object_new_object(); 1364 map* cItem=tmpMaps->content; 1365 while(cItem!=NULL){ 1366 json_object_object_add(res4,cItem->name,json_object_new_string(cItem->value)); 1367 cItem=cItem->next; 1368 } 1369 json_object_object_add(res1,"x-ows-servicecontact",res4); 1370 } 1371 1372 json_object *res4=json_object_new_object(); 1373 tmpMap=getMapFromMaps(conf,"main","license_name"); 1374 if(tmpMap!=NULL){ 1375 json_object_object_add(res4,"name",json_object_new_string(tmpMap->value)); 1376 tmpMap=getMapFromMaps(conf,"main","license_url"); 1377 if(tmpMap!=NULL){ 1378 json_object_object_add(res4,"url",json_object_new_string(tmpMap->value)); 1379 } 1380 } 1381 json_object_object_add(res1,"license",res4); 1382 1383 json_object_object_add(res,"info",res1); 1384 } 1385 1386 /** 1387 * Produce the JSON object for api parameter 1388 * 1389 * @param conf the maps containing the settings of the main.cfg file 1390 * @param res the JSON object to populate with the parameters 1391 */ 1392 void produceApiParameters(maps* conf,json_object* res){ 1393 json_object *res9=json_object_new_object(); 1394 maps* tmpMaps1=getMaps(conf,"{id}"); 1395 map* tmpMap2=getMapFromMaps(conf,"openapi","parameters"); 1396 char *saveptr12; 1397 char *tmps12 = strtok_r (tmpMap2->value, ",", &saveptr12); 1398 while(tmps12!=NULL){ 1399 char* tmpId=(char*) malloc((strlen(tmps12)+3)*sizeof(char)); 1400 sprintf(tmpId,"{%s}",tmps12); 1401 tmpMaps1=getMaps(conf,tmpId); 1402 json_object *res8=json_object_new_object(); 1403 if(tmpMaps1!=NULL){ 1404 map* tmpMap=getMap(tmpMaps1->content,"title"); 1405 if(tmpMap!=NULL) 1406 json_object_object_add(res8,"x-internal-summary",json_object_new_string(tmpMap->value)); 1407 tmpMap=getMap(tmpMaps1->content,"abstract"); 1408 if(tmpMap!=NULL) 1409 json_object_object_add(res8,"description",json_object_new_string(tmpMap->value)); 1410 tmpMap=getMap(tmpMaps1->content,"example"); 1411 if(tmpMap!=NULL) 1412 json_object_object_add(res8,"example",json_object_new_string(tmpMap->value)); 1413 tmpMap=getMap(tmpMaps1->content,"required"); 1414 if(tmpMap!=NULL){ 1415 if(strcmp(tmpMap->value,"true")==0) 1416 json_object_object_add(res8,"required",json_object_new_boolean(TRUE)); 1417 else 1418 json_object_object_add(res8,"required",json_object_new_boolean(FALSE)); 1419 } 1420 else 1421 json_object_object_add(res8,"required",json_object_new_boolean(FALSE)); 1422 json_object_object_add(res8,"in",json_object_new_string("path")); 1423 json_object_object_add(res8,"name",json_object_new_string(tmps12)); 1424 json_object *res6=json_object_new_object(); 1425 json_object *res7=json_object_new_object(); 1426 tmpMap=getMap(tmpMaps1->content,"type"); 1427 if(tmpMap!=NULL) 1428 json_object_object_add(res6,"type",json_object_new_string(tmpMap->value)); 1429 else 1430 json_object_object_add(res6,"type",json_object_new_string("string")); 1431 1432 json_object_object_add(res8,"schema",res6); 1433 1434 } 1435 1436 json_object_object_add(res9,tmps12,res8); 1437 tmps12 = strtok_r (NULL, ",", &saveptr12); 1438 } 1439 tmpMap2=getMapFromMaps(conf,"openapi","header_parameters"); 1440 char *saveptr13; 1441 char *tmps13 = strtok_r (tmpMap2->value, ",", &saveptr13); 1442 while(tmps13!=NULL){ 1443 char* tmpId=zStrdup(tmps13); 1444 maps *tmpMaps2=getMaps(conf,tmpId); 1445 json_object *res8=json_object_new_object(); 1446 if(tmpMaps2!=NULL){ 1447 map* tmpMap=getMap(tmpMaps2->content,"title"); 1448 if(tmpMap!=NULL) 1449 json_object_object_add(res8,"x-internal-summary",json_object_new_string(tmpMap->value)); 1450 tmpMap=getMap(tmpMaps2->content,"abstract"); 1451 if(tmpMap!=NULL) 1452 json_object_object_add(res8,"description",json_object_new_string(tmpMap->value)); 1453 tmpMap=getMap(tmpMaps2->content,"example"); 1454 if(tmpMap!=NULL) 1455 json_object_object_add(res8,"example",json_object_new_string(tmpMap->value)); 1456 tmpMap=getMap(tmpMaps2->content,"required"); 1457 if(tmpMap!=NULL){ 1458 if(strcmp(tmpMap->value,"true")==0) 1459 json_object_object_add(res8,"required",json_object_new_boolean(TRUE)); 1460 else 1461 json_object_object_add(res8,"required",json_object_new_boolean(FALSE)); 1462 } 1463 else 1464 json_object_object_add(res8,"required",json_object_new_boolean(FALSE)); 1465 json_object_object_add(res8,"in",json_object_new_string("header")); 1466 tmpMap=getMap(tmpMaps2->content,"name"); 1467 if(tmpMap!=NULL) 1468 json_object_object_add(res8,"name",json_object_new_string(tmpMap->value)); 1469 json_object *res6=json_object_new_object(); 1470 json_object *res7=json_object_new_object(); 1471 tmpMap=getMap(tmpMaps2->content,"type"); 1472 if(tmpMap!=NULL) 1473 json_object_object_add(res6,"type",json_object_new_string(tmpMap->value)); 1474 else 1475 json_object_object_add(res6,"type",json_object_new_string("string")); 1476 1477 { 1478 tmpMap=getMap(tmpMaps2->content,"enum"); 1479 if(tmpMap!=NULL){ 1480 char *saveptr11; 1481 char *tmps11 = strtok_r (tmpMap->value, ",", &saveptr11); 1482 json_object *res71=json_object_new_array(); 1483 while(tmps11!=NULL){ 1484 json_object_array_add(res71,json_object_new_string(tmps11)); 1485 tmps11 = strtok_r (NULL, ",", &saveptr11); 1486 } 1487 json_object_object_add(res6,"enum",res71); 1488 } 1489 } 1490 1491 json_object_object_add(res8,"schema",res6); 1492 1493 } 1494 1495 json_object_object_add(res9,tmpId,res8); 1496 tmps13 = strtok_r (NULL, ",", &saveptr13); 1497 } 1498 json_object_object_add(res,"parameters",res9); 1499 } 1500 1501 void produceApiComponents(maps*conf,json_object* res){ 1502 json_object* res1=json_object_new_object(); 1503 produceApiParameters(conf,res1); 1504 json_object_object_add(res,"components",res1); 1505 } 1506 1507 /** 1508 * Produce the JSON object for /api 1509 * 1510 * @param conf the maps containing the settings of the main.cfg file 1511 * @param res the JSON object to populate 1512 */ 1513 void produceApi(maps* conf,json_object* res){ 1514 json_object *res9=json_object_new_object(); 1515 json_object *res10=json_object_new_object(); 1516 maps* tmpMaps1=getMaps(conf,"{id}"); 1517 1518 produceApiComponents(conf,res); 1519 json_object *res4=json_object_new_object(); 1520 setMapInMaps(conf,"headers","Content-Type","application/openapi+json; version=3.0;charset=UTF-8"); 1521 1522 produceApiInfo(conf,res,res4); 1523 map* tmpMap=getMapFromMaps(conf,"provider","providerName"); 1524 1525 tmpMap=getMapFromMaps(conf,"openapi","version"); 1526 if(tmpMap!=NULL) 1527 json_object_object_add(res,"openapi",json_object_new_string(tmpMap->value)); 1528 else 1529 json_object_object_add(res,"openapi",json_object_new_string("3.0.2")); 1530 1531 tmpMap=getMapFromMaps(conf,"openapi","paths"); 1532 if(tmpMap!=NULL){ 1533 json_object *res5=json_object_new_object(); 1534 char *saveptr; 1535 char *tmps = strtok_r (tmpMap->value, ",", &saveptr); 1536 int cnt=0; 1537 maps* tmpMaps; 1538 json_object *paths=json_object_new_object(); 1539 while (tmps != NULL){ 1540 tmpMaps=getMaps(conf,tmps+1); 1541 json_object *method=json_object_new_object(); 1542 if(tmpMaps!=NULL){ 1543 if(getMap(tmpMaps->content,"length")==NULL) 1544 addToMap(tmpMaps->content,"length","1"); 1545 map* len=getMap(tmpMaps->content,"length"); 1546 1547 for(int i=0;i<atoi(len->value);i++){ 1548 json_object *methodc=json_object_new_object(); 1549 map* cMap=getMapArray(tmpMaps->content,"method",i); 1550 map* vMap=getMapArray(tmpMaps->content,"title",i); 1551 if(vMap!=NULL) 1552 json_object_object_add(methodc,"summary",json_object_new_string(vMap->value)); 1553 vMap=getMapArray(tmpMaps->content,"abstract",i); 1554 if(vMap!=NULL) 1555 json_object_object_add(methodc,"description",json_object_new_string(vMap->value)); 1556 vMap=getMapArray(tmpMaps->content,"tags",i); 1557 if(vMap!=NULL){ 1558 json_object *cc=json_object_new_array(); 1559 json_object_array_add(cc,json_object_new_string(vMap->value)); 1560 json_object_object_add(methodc,"tags",cc); 1561 } 1562 json_object *responses=json_object_new_object(); 1563 json_object *cc3=json_object_new_object(); 1564 vMap=getMapArray(tmpMaps->content,"schema",i); 1565 if(vMap!=NULL){ 1566 json_object *cc=json_object_new_object(); 1567 json_object_object_add(cc,"$ref",json_object_new_string(vMap->value)); 1568 json_object *cc0=json_object_new_object(); 1569 json_object_object_add(cc0,"schema",cc); 1570 json_object *cc1=json_object_new_object(); 1571 map* tmpMap3=getMapFromMaps(conf,tmps,"type"); 1572 if(tmpMap3!=NULL) 1573 json_object_object_add(cc1,tmpMap3->value,cc0); 1574 else 1575 json_object_object_add(cc1,"application/json",cc0); 1576 json_object *cc2=json_object_new_object(); 1577 json_object_object_add(cc2,"content",cc1); 1578 json_object_object_add(cc2,"description",json_object_new_string("successful operation")); 1579 if(i==1) 1580 json_object_object_add(cc3,"201",cc2); 1581 else 1582 json_object_object_add(cc3,"200",cc2); 1583 }else{ 1584 json_object *cc1=json_object_new_object(); 1585 map* tmpMap3=getMapFromMaps(conf,tmps,"type"); 1586 if(tmpMap3!=NULL) 1587 json_object_object_add(cc1,tmpMap3->value,json_object_new_object()); 1588 else 1589 json_object_object_add(cc1,"application/json",json_object_new_object()); 1590 json_object *cc2=json_object_new_object(); 1591 json_object_object_add(cc2,"content",cc1); 1592 json_object_object_add(cc2,"description",json_object_new_string("successful operation")); 1593 if(i==1) 1594 json_object_object_add(cc3,"201",cc2); 1595 else 1596 json_object_object_add(cc3,"200",cc2); 1597 } 1598 if(strstr(tmps,"{id}")!=NULL){ 1599 vMap=getMapFromMaps(conf,"exception","schema"); 1600 json_object *cc=json_object_new_object(); 1601 json_object_object_add(cc,"$ref",json_object_new_string(vMap->value)); 1602 json_object *cc01=json_object_new_array(); 1603 json_object_array_add(cc01,cc); 1604 1605 json_object *cc0=json_object_new_object(); 1606 json_object_object_add(cc0,"schema",cc); 1607 json_object *cc1=json_object_new_object(); 1608 map* tmpMap3=getMapFromMaps(conf,"exception","type"); 1609 if(tmpMap3!=NULL) 1610 json_object_object_add(cc1,tmpMap3->value,cc0); 1611 else 1612 json_object_object_add(cc1,"application/json",cc0); 1613 1614 json_object *cc2=json_object_new_object(); 1615 json_object_object_add(cc2,"content",cc1); 1616 if(strstr(tmps,"{jobID}")==NULL) 1617 json_object_object_add(cc2,"description",json_object_new_string("The process with id {id} does not exist.")); 1618 else 1619 json_object_object_add(cc2,"description",json_object_new_string("The process with id {id} or job with id {jobID} does not exist.")); 1620 json_object_object_add(cc3,"404",cc2); 1621 json_object_object_add(cc3,"default",cc2); 1622 } 1623 json_object_object_add(methodc,"responses",cc3); 1624 vMap=getMapArray(tmpMaps->content,"parameters",i); 1625 if(vMap!=NULL){ 1626 char *saveptr0; 1627 char *tmps1 = strtok_r (vMap->value, ",", &saveptr0); 1628 json_object *cc2=json_object_new_array(); 1629 int cnt=0; 1630 while(tmps1!=NULL){ 1631 char* tmpStr=(char*)malloc((strlen(tmps1)+2)*sizeof(char)); 1632 sprintf(tmpStr,"#%s",tmps1); 1633 json_object *cc1=json_object_new_object(); 1634 json_object_object_add(cc1,"$ref",json_object_new_string(tmpStr)); 1635 json_object_array_add(cc2,cc1); 1636 free(tmpStr); 1637 cnt++; 1638 tmps1 = strtok_r (NULL, ",", &saveptr0); 1639 } 1640 json_object_object_add(methodc,"parameters",cc2); 1641 } 1642 if(i==1){ 1643 maps* tmpMaps1=getMaps(conf,"requestBody"); 1644 if(tmpMaps1!=NULL){ 1645 vMap=getMap(tmpMaps1->content,"schema"); 1646 if(vMap!=NULL){ 1647 json_object *cc=json_object_new_object(); 1648 json_object_object_add(cc,"$ref",json_object_new_string(vMap->value)); 1649 json_object *cc0=json_object_new_object(); 1650 json_object_object_add(cc0,"schema",cc); 1651 json_object *cc1=json_object_new_object(); 1652 map* tmpMap3=getMap(tmpMaps->content,"type"); 1653 if(tmpMap3!=NULL) 1654 json_object_object_add(cc1,tmpMap3->value,cc0); 1655 else 1656 json_object_object_add(cc1,"application/json",cc0); 1657 json_object *cc2=json_object_new_object(); 1658 json_object_object_add(cc2,"content",cc1); 1659 vMap=getMap(tmpMaps1->content,"abstract"); 1660 if(vMap!=NULL) 1661 json_object_object_add(cc2,"description",json_object_new_string(vMap->value)); 1662 json_object_object_add(cc2,"required",json_object_new_boolean(true)); 1663 json_object_object_add(methodc,"requestBody",cc2); 1664 1665 } 1666 } 1667 } 1668 map* mMap=getMapArray(tmpMaps->content,"method",i); 1669 if(mMap!=NULL) 1670 json_object_object_add(method,mMap->value,methodc); 1671 else 1672 json_object_object_add(method,"get",methodc); 1673 1674 } 1675 1676 tmpMap=getMapFromMaps(conf,"openapi","version"); 1677 if(tmpMap!=NULL) 1678 json_object_object_add(res,"openapi",json_object_new_string(tmpMap->value)); 1679 else 1680 json_object_object_add(res,"openapi",json_object_new_string("3.0.2")); 1681 if(strstr(tmps,"/root")!=NULL) 1682 json_object_object_add(paths,"/",method); 1683 else 1684 json_object_object_add(paths,tmps,method); 1685 } 1686 tmps = strtok_r (NULL, ",", &saveptr); 1687 cnt++; 1688 } 1689 json_object_object_add(res,"paths",paths); 1690 } 1691 1692 tmpMap=getMapFromMaps(conf,"openapi","links"); 1693 if(tmpMap!=NULL){ 1694 1695 } 1696 1697 json_object *res3=json_object_new_array(); 1698 json_object_array_add(res3,res4); 1699 json_object_object_add(res,"servers",res3); 1700 } 140 1701 141 1702 #ifdef __cplusplus -
trunk/zoo-project/zoo-kernel/service_json.h
r917 r949 28 28 #pragma once 29 29 30 #include "response_print.h" 30 31 #include <stdio.h> 31 32 #include <ctype.h> … … 36 37 extern "C" { 37 38 #endif 39 /** 40 * Name and corresponding attributes depending on the WPS version 41 */ 42 static const char* jcapabilities[4]={ 43 "jobControlOptions","sync-execute async-execute dismiss", 44 "outputTransmission","value reference" 45 }; 46 38 47 json_object* mapToJson(map*); 39 48 json_object* mapsToJson(maps*); 40 49 json_object* serviceToJson(service*); 50 void printGetCapabilitiesForProcessJ(registry*, maps*,void*,void*,service*); 51 void printExceptionReportResponseJ(maps*,map*); 52 void parseJRequest(maps*,service* s,json_object*,maps**,maps**); 53 json_object* printJResult(maps*,service*,maps*,int); 54 json_object* printJobList(maps*); 55 int createStatusFile(maps*,int); 56 int json_getStatusFile(maps*); 57 void produceApi(maps*,json_object*); 58 //void printIOTypeJ(maps*, const char*, elements*,json_object*,service*); 41 59 #ifdef __cplusplus 42 60 } -
trunk/zoo-project/zoo-kernel/zoo_loader.c
r917 r949 98 98 map* tmpMap=NULL; 99 99 100 if(strncmp(cgiContentType,"text/xml",8)==0 || 100 if(strncmp(cgiContentType,"application/json",16)==0 && 101 strncasecmp(cgiRequestMethod,"post",4)==0){ 102 char *buffer=new char[2]; 103 char *res=NULL; 104 int r=0; 105 int len=0; 106 while((r=fread(buffer,sizeof(char),1,cgiIn))>0){ 107 fprintf(stderr,"%s",buffer); 108 buffer[1]=0; 109 if(res==NULL){ 110 res=(char*)malloc(2*sizeof(char)); 111 sprintf(res,"%s",buffer); 112 } 113 else{ 114 res=(char*)realloc(res,(len+2)*sizeof(char)); 115 memcpy(res + len, buffer, sizeof(char)); 116 res[len+1]=0; 117 } 118 len+=1; 119 } 120 delete[] buffer; 121 tmpMap=createMap("jrequest",res); 122 free(res); 123 }else if(strncmp(cgiContentType,"text/xml",8)==0 || 101 124 strncasecmp(cgiRequestMethod,"post",4)==0){ 102 125 if(cgiContentLength==0){ … … 123 146 if(res!=NULL) 124 147 free(res); 125 }else{ 148 }else{ 126 149 char *buffer=new char[cgiContentLength+1]; 127 150 if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)>0){ … … 363 386 xmlCleanupParser(); 364 387 }else{ 365 freeMap(&tmpMap); 366 free(tmpMap); 367 tmpMap=createMap("not_valid","true"); 388 if(tmpMap!=NULL){ 389 if(getMap(tmpMap,"jrequest")==NULL){ 390 freeMap(&tmpMap); 391 free(tmpMap); 392 tmpMap=createMap("not_valid","true"); 393 } 394 }else 395 tmpMap=createMap("not_valid","true"); 368 396 } 369 397 … … 408 436 if(strQuery!=NULL) 409 437 free(strQuery); 410 438 411 439 runRequest(&tmpMap); 412 440 -
trunk/zoo-project/zoo-kernel/zoo_service_loader.c
r943 r949 70 70 #include "request_parser.h" 71 71 #include "sqlapi.h" 72 #ifdef WIN3272 #ifdef USE_JSON 73 73 #include "caching.h" 74 74 #endif … … 107 107 108 108 #ifdef USE_CALLBACK 109 #include "service_callback.h" 110 #endif 111 112 #ifdef USE_JSON 109 113 #include "service_json.h" 110 #include " service_callback.h"114 #include "json_tokener.h" 111 115 #endif 112 116 … … 258 262 */ 259 263 int 260 recursReaddirF ( maps * m, registry *r, xmlDocPtr doc, xmlNodePtr n, char *conf_dir, 261 char *prefix, int saved_stdout, int level, 262 void (func) (registry *, maps *, xmlDocPtr, xmlNodePtr, service *) ) 264 recursReaddirF ( maps * m, registry *r, void* doc1, void* n1, char *conf_dir, 265 //( maps * m, registry *r, xmlDocPtr doc, xmlNodePtr n, char *conf_dir, 266 char *prefix, int saved_stdout, int level, 267 void (func) (registry *, maps *, void*, void*, service *) ) 268 //void (func) (registry *, maps *, xmlDocPtr, xmlNodePtr, service *) ) 263 269 { 264 270 struct dirent *dp; 265 271 int scount = 0; 272 xmlDocPtr doc=(xmlDocPtr) doc1; 273 xmlNodePtr n=(xmlNodePtr) n1; 266 274 267 275 if (conf_dir == NULL) … … 385 393 386 394 /** 395 * When th zcfg file is not found, print error message and cleanup memory 396 * @param zooRegistry the populated registry 397 * @param m the maps pointer to the content of main.cfg file 398 * @param zcfg the zcfg file name 399 * @param code the string determining the nature of the error 400 * @param locator the string determining which parameter the error refer to 401 * @param orig the service name 402 * @param corig the current service name (in case multiple services was parsed) 403 * @param funcError the function used to print the error back 404 */ 405 void exitAndCleanUp(registry* zooRegistry, maps* m, 406 const char* zcfg,const char* code,const char* locator, 407 char* orig,char* corig, 408 void (funcError) (maps*, map*)){ 409 map *tmp00 = getMapFromMaps (m, "lenv", "message"); 410 char tmp01[1024]; 411 if (tmp00 != NULL) 412 sprintf (tmp01, 413 _("Unable to parse the ZCFG file: %s (%s)"), 414 zcfg, tmp00->value); 415 else 416 sprintf (tmp01, 417 _("Unable to parse the ZCFG file: %s."), 418 zcfg); 419 map* errormap = createMap("text", tmp01); 420 addToMap(errormap,"code", code); 421 addToMap(errormap,"locator", locator); 422 funcError(m,errormap); 423 freeMaps (&m); 424 free (m); 425 if(zooRegistry!=NULL){ 426 freeRegistry(&zooRegistry); 427 free(zooRegistry); 428 } 429 free (orig); 430 if (corig != NULL) 431 free (corig); 432 //xmlFreeDoc (doc); 433 xmlCleanupParser (); 434 zooXmlCleanupNs (); 435 freeMap(&errormap); 436 free(errormap); 437 } 438 439 440 /** 441 * Search services from various possible sources 442 * 443 * @param zopRegistry the populated registry 444 * @param m the maps pointer to the content of main.cfg file 445 * @param r_inputs the service(s) name(s) 446 * @param func the function used to print the result back 447 * @param doc the xml document or NULL (for json) 448 * @param n the xmlNode of JSON object pointer to the current element 449 * @param conf_dir the directory where the main.cfg has been found 450 * @param request_inputs the map pointer to the request KVP if any 451 * @param funcError the function used to print the error back 452 * @return 0 in case of success, 1 otherwise 453 */ 454 int fetchServicesForDescription(registry* zooRegistry, maps* m, char* r_inputs, 455 void (func) (registry *, maps *, void*, void*, service *), 456 void* doc, void* n, char* conf_dir, map* request_inputs, 457 void (funcError) (maps*, map*) ){ 458 char *orig = zStrdup (r_inputs); 459 service* s1=NULL; 460 int saved_stdout = zDup (fileno (stdout)); 461 int t; 462 int scount = 0; 463 struct dirent *dp; 464 465 zDup2 (fileno (stderr), fileno (stdout)); 466 if (strcasecmp ("all", orig) == 0) 467 { 468 maps* imports = getMaps(m, IMPORTSERVICE); 469 if (imports != NULL) { 470 map* zcfg = imports->content; 471 472 while (zcfg != NULL) { 473 if (zcfg->value != NULL) { 474 service* svc = (service*) malloc(SERVICE_SIZE); 475 if (svc == NULL || readServiceFile(m, zcfg->value, &svc, zcfg->name) < 0) { 476 // pass over silently 477 zcfg = zcfg->next; 478 continue; 479 } 480 inheritance(zooRegistry, &svc); 481 #ifdef USE_HPC 482 addNestedOutputs(&svc); 483 #endif 484 485 func(zooRegistry, m, doc, n, svc); 486 freeService(&svc); 487 free(svc); 488 } 489 zcfg = zcfg->next; 490 } 491 } 492 if (int res = 493 recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0, 494 func) < 0) 495 return res; 496 #ifdef META_DB 497 fetchServicesFromDb(zooRegistry,m,doc,n,func,0); 498 close_sql(m,0); 499 #endif 500 } 501 else 502 { 503 DIR *dirp = opendir (conf_dir); 504 char *saveptr; 505 char *tmps = strtok_r (orig, ",", &saveptr); 506 507 char buff[256]; 508 char buff1[1024]; 509 while (tmps != NULL) 510 { 511 int hasVal = -1; 512 char *corig = zStrdup (tmps); 513 map* import = getMapFromMaps (m, IMPORTSERVICE, corig); 514 if (import != NULL && import->value != NULL) 515 { 516 #ifdef META_DB 517 service* s2=extractServiceFromDb(m,import->name,0); 518 if(s2==NULL){ 519 #endif 520 s1 = createService(); 521 t = readServiceFile (m, import->value, &s1, import->name); 522 523 if (t < 0) // failure reading zcfg 524 { 525 zDup2 (saved_stdout, fileno (stdout)); 526 exitAndCleanUp(zooRegistry, m, 527 tmps,"InvalidParameterValue","identifier", 528 orig,corig, 529 funcError); 530 return 1; 531 } 532 #ifdef DEBUG 533 dumpService (s1); 534 #endif 535 inheritance(zooRegistry,&s1); 536 #ifdef USE_HPC 537 addNestedOutputs(&s1); 538 #endif 539 func (zooRegistry, m, doc, n, s1); 540 freeService (&s1); 541 free (s1); 542 s1 = NULL; 543 scount++; 544 hasVal = 1; 545 #ifdef META_DB 546 } 547 #endif 548 } 549 else if (strstr (corig, ".") != NULL) 550 { 551 parseIdentifier (m, conf_dir, corig, buff1); 552 map *tmpMap = getMapFromMaps (m, "lenv", "metapath"); 553 if (tmpMap != NULL) 554 addToMap (request_inputs, "metapath", tmpMap->value); 555 map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier"); 556 /** 557 * No support for dot in service name stored in metadb!? 558 #ifdef META_DB 559 service* s2=extractServiceFromDb(m,tmpMapI->value,0); 560 if(s2==NULL){ 561 #endif 562 */ 563 s1 = createService(); 564 t = readServiceFile (m, buff1, &s1, tmpMapI->value); 565 if (t < 0) 566 { 567 zDup2 (saved_stdout, fileno (stdout)); 568 exitAndCleanUp(zooRegistry, m, 569 tmps,"InvalidParameterValue","identifier", 570 orig,corig, 571 funcError); 572 return 1; 573 } 574 #ifdef DEBUG 575 dumpService (s1); 576 #endif 577 inheritance(zooRegistry,&s1); 578 #ifdef USE_HPC 579 addNestedOutputs(&s1); 580 #endif 581 func (zooRegistry, m, doc, n, s1); 582 freeService (&s1); 583 free (s1); 584 s1 = NULL; 585 scount++; 586 hasVal = 1; 587 setMapInMaps (m, "lenv", "level", "0"); 588 } 589 else 590 { 591 #ifdef META_DB 592 _init_sql(m,"metadb"); 593 //FAILED CONNECTING DB 594 if(getMapFromMaps(m,"lenv","dbIssue")!=NULL){ 595 fprintf(stderr,"ERROR CONNECTING METADB"); 596 } 597 service* s2=extractServiceFromDb(m,corig,0); 598 if(s2!=NULL){ 599 inheritance(zooRegistry,&s2); 600 #ifdef USE_HPC 601 addNestedOutputs(&s2); 602 #endif 603 func (zooRegistry,m, doc, n, s2); 604 freeService (&s2); 605 free (s2); 606 s2 = NULL; 607 hasVal = 1; 608 }else /*TOTO*/{ 609 #endif 610 memset (buff, 0, 256); 611 snprintf (buff, 256, "%s.zcfg", corig); 612 memset (buff1, 0, 1024); 613 #ifdef DEBUG 614 printf ("\n#######%s\n########\n", buff); 615 #endif 616 while ((dp = readdir (dirp)) != NULL) 617 { 618 if (strcasecmp (dp->d_name, buff) == 0) 619 { 620 memset (buff1, 0, 1024); 621 snprintf (buff1, 1024, "%s/%s", conf_dir, 622 dp->d_name); 623 s1 = createService(); 624 if (s1 == NULL) 625 { 626 zDup2 (saved_stdout, fileno (stdout)); 627 map* errormap = createMap("text", _("Unable to allocate memory")); 628 addToMap(errormap,"code", "InternalError"); 629 addToMap(errormap,"locator", "NULL"); 630 funcError(m,errormap); 631 return -1; 632 } 633 #ifdef DEBUG_SERVICE_CONF 634 fprintf 635 (stderr,"#################\n(%s) %s\n#################\n", 636 r_inputs->value, buff1); 637 #endif 638 char *tmp0 = zStrdup (dp->d_name); 639 tmp0[strlen (tmp0) - 5] = 0; 640 t = readServiceFile (m, buff1, &s1, tmp0); 641 free (tmp0); 642 if (t < 0) 643 { 644 zDup2 (saved_stdout, fileno (stdout)); 645 exitAndCleanUp(zooRegistry, m, 646 buff,"InternalError","NULL", 647 orig,corig, 648 funcError); 649 return 1; 650 } 651 #ifdef DEBUG 652 dumpService (s1); 653 #endif 654 inheritance(zooRegistry,&s1); 655 #ifdef USE_HPC 656 addNestedOutputs(&s1); 657 #endif 658 func (zooRegistry,m, doc, n, s1); 659 freeService (&s1); 660 free (s1); 661 s1 = NULL; 662 scount++; 663 hasVal = 1; 664 } 665 } 666 #ifdef META_DB 667 } 668 #endif 669 } 670 if (hasVal < 0) 671 { 672 zDup2 (saved_stdout, fileno (stdout)); 673 exitAndCleanUp(zooRegistry, m, 674 buff,"InvalidParameterValue","Identifier", 675 orig,corig, 676 funcError); 677 return 1; 678 } 679 rewinddir (dirp); 680 tmps = strtok_r (NULL, ",", &saveptr); 681 if (corig != NULL) 682 free (corig); 683 } 684 } 685 fflush (stdout); 686 zDup2 (saved_stdout, fileno (stdout)); 687 free (orig); 688 return 0; 689 } 690 691 /** 692 * Run every HTTP request to download inputs passed as reference 693 * 694 * @param conf the maps pointing to the main.cfg file content 695 * @param inputs the maps pointing to the inputs provided in the request 696 */ 697 int loadHttpRequests(maps* conf,maps* inputs){ 698 // Resolve reference 699 // TODO: add erro gesture 700 int eres; 701 maps* tmpMaps=getMaps(conf,"http_requests"); 702 if(tmpMaps!=NULL){ 703 map* lenMap=getMap(tmpMaps->content,"length"); 704 int len=0; 705 if(lenMap!=NULL){ 706 len=atoi(lenMap->value); 707 } 708 HINTERNET hInternet; 709 HINTERNET res; 710 if(len>0) 711 hInternet = InternetOpen ( 712 #ifndef WIN32 713 (LPCTSTR) 714 #endif 715 "ZOO-Project WPS Client\0", 716 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 717 for(int j=0;j<len;j++){ 718 map* tmpUrl=getMapArray(tmpMaps->content,"url",j); 719 map* tmpInput=getMapArray(tmpMaps->content,"input",j); 720 maps* currentMaps=getMaps(inputs,tmpInput->value); 721 loadRemoteFile(&conf,¤tMaps->content,&hInternet,tmpUrl->value); 722 addIntToMap(currentMaps->content,"Order",hInternet.nb); 723 addToMap(currentMaps->content,"Reference",tmpUrl->value); 724 } 725 if(len>0){ 726 map* error=NULL; 727 runHttpRequests(&conf,&inputs,&hInternet,&error); 728 InternetCloseHandle(&hInternet); 729 } 730 } 731 return 0; 732 } 733 734 /** 735 * Initialize environment sections, load env, and populate lenv and renv. 736 * 737 * @param conf the maps pointing to the main.cfg file content 738 * @param request_inputs the map pointing to the request KVP 739 * @param cPath a string pointing to the cwd 740 * @param request a string pointing to the request key (xrequest or jrequest) 741 */ 742 void initAllEnvironment(maps* conf,map* request_inputs, 743 const char* cPath,const char* request){ 744 // Define each env variable in runing environment 745 maps *curs = getMaps (conf, "env"); 746 if (curs != NULL) { 747 map *mapcs = curs->content; 748 while (mapcs != NULLMAP) 749 { 750 #ifndef WIN32 751 setenv (mapcs->name, mapcs->value, 1); 752 #ifdef DEBUG 753 fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name, 754 mapcs->value); 755 #endif 756 #else 757 if (mapcs->value[strlen (mapcs->value) - 2] == '\r') 758 { 759 #ifdef DEBUG 760 fprintf (stderr, "[ZOO: Env var finish with \r]\n"); 761 #endif 762 mapcs->value[strlen (mapcs->value) - 1] = 0; 763 } 764 #ifdef DEBUG 765 if (SetEnvironmentVariable (mapcs->name, mapcs->value) == 0) 766 { 767 fflush (stderr); 768 fprintf (stderr, "setting variable... %s\n", "OK"); 769 } 770 else 771 { 772 fflush (stderr); 773 fprintf (stderr, "setting variable... %s\n", "OK"); 774 } 775 #else 776 SetEnvironmentVariable (mapcs->name, mapcs->value); 777 #endif 778 char *toto = 779 (char *) 780 malloc ((strlen (mapcs->name) + strlen (mapcs->value) + 781 2) * sizeof (char)); 782 sprintf (toto, "%s=%s", mapcs->name, mapcs->value); 783 _putenv (toto); 784 #ifdef DEBUG 785 fflush (stderr); 786 #endif 787 #endif 788 789 #ifdef DEBUG 790 fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name, 791 mapcs->value); 792 fflush (stderr); 793 #endif 794 mapcs = mapcs->next; 795 } 796 } 797 798 799 800 int eres = SERVICE_STARTED; 801 int cpid = zGetpid (); 802 803 // Create a map containing a copy of the request map 804 maps *_tmpMaps = createMaps("request"); 805 addMapToMap(&_tmpMaps->content,request_inputs); 806 addMapsToMaps (&conf, _tmpMaps); 807 freeMaps (&_tmpMaps); 808 free (_tmpMaps); 809 /** 810 * Initialize the specific [lenv] section which contains runtime variables: 811 * 812 * - usid : it is an universally unique identifier 813 * - osid : it is an idenfitication number 814 * - sid : it is the process idenfitication number (OS) 815 * - uusid : it is an universally unique identifier 816 * - status : value between 0 and 100 to express the completude of 817 * the operations of the running service 818 * - message : is a string where you can store error messages, in case 819 * service is failing, or o provide details on the ongoing operation. 820 * - cwd : the current working directory or servicePath if defined 821 * - soap : is a boolean value, true if the request was contained in a SOAP 822 * Envelop 823 * - sessid : string storing the session identifier (only when cookie is 824 * used) 825 * - cgiSid : only defined on Window platforms (for being able to identify 826 * the created process) 827 * 828 */ 829 _tmpMaps = createMaps("lenv"); 830 char tmpBuff[100]; 831 struct ztimeval tp; 832 if (zGettimeofday (&tp, NULL) == 0) 833 sprintf (tmpBuff, "%i", (cpid + ((int) tp.tv_sec + (int) tp.tv_usec))); 834 else 835 sprintf (tmpBuff, "%i", (cpid + (int) time (NULL))); 836 _tmpMaps->content = createMap ("osid", tmpBuff); 837 sprintf (tmpBuff, "%i", cpid); 838 addToMap (_tmpMaps->content, "sid", tmpBuff); 839 char* tmpUuid=get_uuid(); 840 addToMap (_tmpMaps->content, "uusid", tmpUuid); 841 addToMap (_tmpMaps->content, "usid", tmpUuid); 842 free(tmpUuid); 843 addToMap (_tmpMaps->content, "status", "0"); 844 map* cwdMap0=getMapFromMaps(conf,"main","servicePath"); 845 if(cwdMap0!=NULL){ 846 addToMap (_tmpMaps->content, "cwd", cwdMap0->value); 847 addToMap (_tmpMaps->content, "rcwd", cPath); 848 } 849 else 850 addToMap (_tmpMaps->content, "cwd", cPath); 851 addToMap (_tmpMaps->content, "message", _("No message provided")); 852 map *ltmp = getMap (request_inputs, "soap"); 853 if (ltmp != NULL) 854 addToMap (_tmpMaps->content, "soap", ltmp->value); 855 else 856 addToMap (_tmpMaps->content, "soap", "false"); 857 858 // Parse the session file and add it to the main maps 859 char* originalCookie=NULL; 860 if (cgiCookie != NULL && strlen (cgiCookie) > 0) 861 { 862 int hasValidCookie = -1; 863 char *tcook = originalCookie = zStrdup (cgiCookie); 864 map *testing = getMapFromMaps (conf, "main", "cookiePrefix"); 865 parseCookie(&conf,originalCookie); 866 map *sessId=getMapFromMaps(conf,"cookies", 867 (testing==NULL?"ID":testing->value)); 868 if (sessId!=NULL) 869 { 870 addToMap (_tmpMaps->content, "sessid", sessId->value); 871 char session_file_path[1024]; 872 map *tmpPath = getMapFromMaps (conf, "main", "sessPath"); 873 if (tmpPath == NULL) 874 tmpPath = getMapFromMaps (conf, "main", "tmpPath"); 875 char *tmp1 = strtok (tcook, ";"); 876 if (tmp1 != NULL) 877 sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value, 878 sessId->value); 879 else 880 sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value, 881 sessId->value); 882 free (tcook); 883 maps *tmpSess = (maps *) malloc (MAPS_SIZE); 884 tmpSess->child=NULL; 885 struct stat file_status; 886 int istat = stat (session_file_path, &file_status); 887 if (istat == 0 && file_status.st_size > 0) 888 { 889 int saved_stdout = zDup (fileno (stdout)); 890 zDup2 (fileno (stderr), fileno (stdout)); 891 conf_read (session_file_path, tmpSess); 892 addMapsToMaps (&conf, tmpSess); 893 freeMaps (&tmpSess); 894 fflush(stdout); 895 zDup2 (saved_stdout, fileno (stdout)); 896 zClose(saved_stdout); 897 } 898 free (tmpSess); 899 } 900 } 901 addMapsToMaps (&conf, _tmpMaps); 902 freeMaps (&_tmpMaps); 903 free (_tmpMaps); 904 maps* bmap=NULL; 905 #ifdef DEBUG 906 dumpMap (request_inputs); 907 #endif 908 int ei = 1; 909 910 _tmpMaps = createMaps("renv"); 911 912 #ifdef WIN32 913 LPVOID orig = GetEnvironmentStrings(); 914 LPTSTR s = (LPTSTR) orig; 915 916 while (*s != NULL) { 917 char* env = strdup(s); 918 char* delim = strchr(env,'='); 919 if (delim != NULL) { 920 char* val = delim+1; 921 *delim = '\0'; 922 if(_tmpMaps->content == NULL) { 923 _tmpMaps->content = createMap(env,val); 924 } 925 else { 926 addToMap(_tmpMaps->content,env,val); 927 } 928 } 929 s += strlen(s)+1; 930 } 931 FreeEnvironmentStrings((LPCH)orig); 932 #else 933 char **orig = environ; 934 char *s=*orig; 935 936 if(orig!=NULL) 937 for (; s; ei++ ) { 938 if(strstr(s,"=")!=NULL && strlen(strstr(s,"="))>1){ 939 int len=strlen(s); 940 char* tmpName=zStrdup(s); 941 char* tmpValue=strstr(s,"=")+1; 942 char* tmpName1=(char*)malloc((1+(len-(strlen(tmpValue)+1)))*sizeof(char)); 943 snprintf(tmpName1,(len-strlen(tmpValue)),"%s",tmpName); 944 if(_tmpMaps->content == NULL) 945 _tmpMaps->content = createMap (tmpName1,tmpValue); 946 else 947 addToMap (_tmpMaps->content,tmpName1,tmpValue); 948 free(tmpName1); 949 free(tmpName); 950 } 951 s = *(orig+ei); 952 } 953 #endif 954 955 if(_tmpMaps->content!=NULL && getMap(_tmpMaps->content,"HTTP_COOKIE")!=NULL){ 956 addToMap(_tmpMaps->content,"HTTP_COOKIE1",&cgiCookie[0]); 957 } 958 addMapsToMaps (&conf, _tmpMaps); 959 freeMaps (&_tmpMaps); 960 free (_tmpMaps); 961 map* postRequest=getMap(request_inputs,request); 962 if(postRequest!=NULL) 963 setMapInMaps (conf, "renv", request, postRequest->value); 964 #ifdef WIN32 965 char *cgiSidL = NULL; 966 if (getenv ("CGISID") != NULL) 967 addToMap (request_inputs, "cgiSid", getenv ("CGISID")); 968 969 char* usidp; 970 if ( (usidp = getenv("USID")) != NULL ) { 971 setMapInMaps (conf, "lenv", "usid", usidp); 972 } 973 974 map *test1 = getMap (request_inputs, "cgiSid"); 975 if (test1 != NULL){ 976 cgiSid = zStrdup(test1->value); 977 addToMap (request_inputs, "storeExecuteResponse", "true"); 978 addToMap (request_inputs, "status", "true"); 979 setMapInMaps (conf, "lenv", "osid", test1->value); 980 status = getMap (request_inputs, "status"); 981 } 982 test1 = getMap (request_inputs, "usid"); 983 if (test1 != NULL){ 984 setMapInMaps (conf, "lenv", "usid", test1->value); 985 setMapInMaps (conf, "lenv", "uusid", test1->value); 986 } 987 #endif 988 989 } 990 991 /** 387 992 * Signal handling function which simply call exit(0). 388 993 * … … 444 1049 exit (0); 445 1050 } 1051 1052 #ifdef USE_JSON 1053 /** 1054 * Signal handling function which create an ExceptionReport node containing the 1055 * information message corresponding to the signal number. 1056 * 1057 * @param sig the signal number 1058 */ 1059 void json_sig_handler (int sig){ 1060 char tmp[100]; 1061 const char *ssig; 1062 switch (sig) 1063 { 1064 case SIGSEGV: 1065 ssig = "SIGSEGV"; 1066 break; 1067 case SIGTERM: 1068 ssig = "SIGTERM"; 1069 break; 1070 case SIGINT: 1071 ssig = "SIGINT"; 1072 break; 1073 case SIGILL: 1074 ssig = "SIGILL"; 1075 break; 1076 case SIGFPE: 1077 ssig = "SIGFPE"; 1078 break; 1079 case SIGABRT: 1080 ssig = "SIGABRT"; 1081 break; 1082 default: 1083 ssig = "UNKNOWN"; 1084 break; 1085 } 1086 sprintf (tmp, 1087 _ 1088 ("ZOO Kernel failed to process your request, receiving signal %d = %s "), 1089 sig, ssig); 1090 map* tmpMap=createMap("decode","suze"); 1091 maps* tmpMaps=createMaps("lenv"); 1092 setMapInMaps(tmpMaps,"lenv","message",tmp); 1093 setMapInMaps(tmpMaps,"lenv","status","failed"); 1094 printExceptionReportResponseJ(tmpMaps,tmpMap); 1095 //errorException (NULL, tmp, "InternalError", NULL); 1096 #ifdef DEBUG 1097 fprintf (stderr, "Not this time!\n"); 1098 #endif 1099 exit (0); 1100 } 1101 1102 #endif 446 1103 447 1104 /** … … 509 1166 #ifdef WIN32 510 1167 HINSTANCE so = 511 LoadLibraryEx (tmps1, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 1168 LoadLibraryEx (tmps1, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); 512 1169 #else 513 1170 void *so = dlopen (tmps1, RTLD_LAZY); … … 974 1631 runRequest (map ** inputs) 975 1632 { 976 1633 977 1634 #ifndef USE_GDB 978 1635 #ifndef WIN32 … … 990 1647 map *r_inputs = NULL; 991 1648 map *request_inputs = *inputs; 992 //fprintf(stderr,"%s \n",json_object_to_json_string_ext(mapToJson(request_inputs),JSON_C_TO_STRING_PLAIN));993 994 1649 #ifdef IGNORE_METAPATH 995 1650 addToMap(request_inputs, "metapath", ""); … … 1021 1676 char conf_file[10240]; 1022 1677 snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value); 1023 #ifdef ETC_DIR1024 #ifndef WIN321025 getcwd (ntmp, 1024);1026 #else1027 _getcwd (ntmp, 1024);1028 #endif1029 #endif1030 1031 1678 if (conf_read (conf_file, m) == 2) 1032 1679 { … … 1064 1711 fstde = freopen (fstdem->value, "a+", stderr); 1065 1712 1713 /** 1714 * Language gesture 1715 */ 1066 1716 r_inputs = getMap (request_inputs, "language"); 1067 1717 if (r_inputs == NULL) … … 1160 1810 fprintf(stderr,"*** %s ***\n",tmpUrl); 1161 1811 #endif 1162 setMapInMaps(m,"main","serverAddress",tmpUrl); 1812 if(getMapFromMaps(m,"main","proxied")==NULL) 1813 setMapInMaps(m,"main","serverAddress",tmpUrl); 1814 else 1815 setMapInMaps(m,"lenv","serverAddress",tmpUrl); 1163 1816 } 1164 1817 … … 1178 1831 } 1179 1832 1833 // Populate the Registry 1834 char conf_dir[1024]; 1835 int t; 1836 char tmps1[1024]; 1837 r_inputs = NULL; 1838 r_inputs = getMap (request_inputs, "metapath"); 1839 map* cwdMap0=getMapFromMaps(m,"main","servicePath"); 1840 if (r_inputs != NULL) 1841 if(cwdMap0!=NULL) 1842 snprintf (conf_dir, 1024, "%s/%s", cwdMap0->value, r_inputs->value); 1843 else 1844 snprintf (conf_dir, 1024, "%s/%s", ntmp, r_inputs->value); 1845 else 1846 if(cwdMap0!=NULL) 1847 snprintf (conf_dir, 1024, "%s", cwdMap0->value); 1848 else 1849 snprintf (conf_dir, 1024, "%s", ntmp); 1850 map* reg = getMapFromMaps (m, "main", "registry"); 1851 registry* zooRegistry=NULL; 1852 if(reg!=NULL){ 1853 #ifndef WIN32 1854 int saved_stdout = zDup (fileno (stdout)); 1855 zDup2 (fileno (stderr), fileno (stdout)); 1856 #endif 1857 if(createRegistry (m,&zooRegistry,reg->value)<0){ 1858 map *message=getMapFromMaps(m,"lenv","message"); 1859 map *type=getMapFromMaps(m,"lenv","type"); 1860 #ifndef WIN32 1861 zDup2 (saved_stdout, fileno (stdout)); 1862 #endif 1863 errorException (m, message->value, 1864 type->value, NULL); 1865 return 0; 1866 } 1867 #ifndef WIN32 1868 zDup2 (saved_stdout, fileno (stdout)); 1869 zClose(saved_stdout); 1870 #endif 1871 } 1872 1873 setMapInMaps(m,"lenv","executionType","xml"); 1874 if((strlen(cgiQueryString)>0 && cgiQueryString[0]=='/') || strstr(cgiAccept,"json")!=NULL){ 1875 // 1876 // OGC API - Processing starts here 1877 // 1878 #ifndef USE_JSON 1879 errorException (m, _("OGC API - Processing is not supported by this ZOO-Kernel."), "InternalError", NULL); 1880 return 1; 1881 #else 1882 #ifndef USE_GDB 1883 #ifndef WIN32 1884 signal (SIGCHLD, SIG_IGN); 1885 #endif 1886 signal (SIGSEGV, json_sig_handler); 1887 signal (SIGTERM, json_sig_handler); 1888 signal (SIGINT, json_sig_handler); 1889 signal (SIGILL, json_sig_handler); 1890 signal (SIGFPE, json_sig_handler); 1891 signal (SIGABRT, json_sig_handler); 1892 #endif 1893 r_inputs = getMapOrFill (&request_inputs, "metapath", ""); 1894 char conf_file1[10240]; 1895 maps* m1 = (maps *) malloc (MAPS_SIZE); 1896 snprintf (conf_file1, 10240, "%s/%s/oas.cfg", ntmp, r_inputs->value); 1897 if (conf_read (conf_file1, m1) == 2) 1898 { 1899 errorException (NULL, _("Unable to load the oas.cfg file."), 1900 "InternalError", NULL); 1901 free (m); 1902 return 1; 1903 } 1904 addMapsToMaps(&m,m1); 1905 setMapInMaps(m,"lenv","executionType","json"); 1906 1907 json_object *res=json_object_new_object(); 1908 setMapInMaps(m,"headers","Content-Type","application/json;charset=UTF-8"); 1909 /* - Root url */ 1910 if(cgiContentLength==1){ 1911 map* tmpMap=getMapFromMaps(m,"main","serverAddress"); 1912 json_object *res1=json_object_new_array(); 1913 const char* urls[4]={ 1914 "/","/api","/conformance","/processes" 1915 }; 1916 map* tmpUrl=getMapFromMaps(m,"main","serverAddress"); 1917 for(int kk=0;kk<4;kk++){ 1918 maps* tmpMaps=getMaps(m,urls[kk]); 1919 json_object *res2; 1920 if(tmpMaps!=NULL){ 1921 res2=mapToJson(tmpMaps->content); 1922 char* tmpStr=(char*) malloc((strlen(tmpUrl->value)+strlen(urls[kk])+2)*sizeof(char)); 1923 sprintf(tmpStr,"%s%s",tmpUrl->value,urls[kk]); 1924 json_object_object_add(res2,"href",json_object_new_string(tmpStr)); 1925 json_object_array_add(res1,res2); 1926 } 1927 } 1928 json_object_object_add(res,"links",res1); 1929 }else if(strcmp(cgiQueryString,"/conformance")==0){ 1930 /* - /conformance url */ 1931 map* rootUrl=getMapFromMaps(m,"conformTo","rootUrl"); 1932 json_object *res1=json_object_new_array(); 1933 map* length=getMapFromMaps(m,"conformTo","length"); 1934 maps* tmpMaps=getMaps(m,"conformTo"); 1935 for(int kk=0;kk<atoi(length->value);kk++){ 1936 map* tmpMap1=getMapArray(tmpMaps->content,"link",kk); 1937 json_object *res2; 1938 if(tmpMap1!=NULL){ 1939 char* tmpStr=(char*) malloc((strlen(rootUrl->value)+strlen(tmpMap1->value)+1)*sizeof(char)); 1940 sprintf(tmpStr,"%s%s",rootUrl->value,tmpMap1->value); 1941 json_object_array_add(res1,json_object_new_string(tmpStr)); 1942 } 1943 } 1944 json_object_object_add(res,"conformTo",res1); 1945 }else if(strcmp(cgiQueryString,"/api")==0){ 1946 /* - /api url */ 1947 produceApi(m,res); 1948 }else if(strcmp(cgiQueryString,"/processes")==0 || strcmp(cgiQueryString,"/processes/")==0){ 1949 /* - /processes */ 1950 json_object *res3=json_object_new_array(); 1951 int saved_stdout = zDup (fileno (stdout)); 1952 zDup2 (fileno (stderr), fileno (stdout)); 1953 1954 if (int res = 1955 recursReaddirF (m, NULL, res3, NULL, ntmp, NULL, saved_stdout, 0, 1956 printGetCapabilitiesForProcessJ) < 0) 1957 { 1958 } 1959 zDup2 (saved_stdout, fileno (stdout)); 1960 zClose(saved_stdout); 1961 1962 json_object_object_add(res,"processes",res3); 1963 }else{ 1964 service* s1=NULL; 1965 int t=0; 1966 if(strstr(cgiQueryString,"/processes/")==NULL){ 1967 map* error=createMap("code","BadRequest"); 1968 addToMap(error,"message",_("The ressource is not available")); 1969 //setMapInMaps(conf,"lenv","status_code","404 Bad Request"); 1970 printExceptionReportResponseJ(m,error); 1971 freeMaps (&m); 1972 free (m); 1973 free (REQUEST); 1974 xmlCleanupParser (); 1975 zooXmlCleanupNs (); 1976 return 1; 1977 } else 1978 if(strstr(cgiQueryString,"/jobs")==NULL && strstr(cgiQueryString,"/jobs/")==NULL){ 1979 /* - /processes/{id}/ */ 1980 DIR *dirp = opendir (ntmp); 1981 json_object *res3=json_object_new_object(); 1982 char *orig = zStrdup (strstr(cgiQueryString,"/processes/")+11); 1983 if(orig[strlen(orig)-1]=='/') 1984 orig[strlen(orig)-1]=0; 1985 json_object* res1=json_object_new_object(); 1986 int t=fetchServicesForDescription(NULL, m, orig, 1987 printGetCapabilitiesForProcessJ, 1988 NULL, (void*) res3, ntmp, 1989 request_inputs, 1990 printExceptionReportResponseJ); 1991 if(t==1){ 1992 /*map* error=createMap("code","BadRequest"); 1993 addToMap(error,"message",_("Failed to acces the requested service")); 1994 printExceptionReportResponseJ(m,error);*/ 1995 return 1; 1996 } 1997 res=json_object_get(res3); 1998 }else{ 1999 char* queryString=zStrdup(cgiQueryString); 2000 int len0=strlen(strstr(cgiQueryString,"/processes/")+11); 2001 int len1=strlen(cgiQueryString)-strlen(strstr(cgiQueryString,"/job")); 2002 char* cIdentifier=(char*)malloc((len1-10)*sizeof(char)); 2003 int cnt=0; 2004 for(int j=11;j<len1;j++){ 2005 fprintf(stderr,"%s %c\n",cIdentifier,cgiQueryString[j]); 2006 cIdentifier[cnt]=cgiQueryString[j]; 2007 cIdentifier[cnt+1]=0; 2008 cnt++; 2009 } 2010 char tmps1[1024]; 2011 2012 map* import = getMapFromMaps (m, IMPORTSERVICE, cIdentifier); 2013 if (import != NULL && import->value != NULL) { 2014 strncpy(tmps1, import->value, 1024); 2015 setMapInMaps (m, "lenv", "Identifier", cIdentifier); 2016 setMapInMaps (m, "lenv", "oIdentifier", cIdentifier); 2017 } 2018 else { 2019 snprintf (tmps1, 1024, "%s/%s.zcfg", ntmp, cIdentifier); 2020 #ifdef DEBUG 2021 fprintf (stderr, "Trying to load %s\n", tmps1); 2022 #endif 2023 if (strstr (cIdentifier, ".") != NULL) 2024 { 2025 char *identifier = zStrdup (cIdentifier); 2026 parseIdentifier (m, ntmp, identifier, tmps1); 2027 map *tmpMap = getMapFromMaps (m, "lenv", "metapath"); 2028 if (tmpMap != NULL) 2029 addToMap (request_inputs, "metapath", tmpMap->value); 2030 free (identifier); 2031 } 2032 else 2033 { 2034 setMapInMaps (m, "lenv", "oIdentifier", cIdentifier); 2035 setMapInMaps (m, "lenv", "Identifier", cIdentifier); 2036 } 2037 } 2038 2039 r_inputs = getMapFromMaps (m, "lenv", "Identifier"); 2040 2041 #ifdef META_DB 2042 int metadb_id=_init_sql(m,"metadb"); 2043 //FAILED CONNECTING DB 2044 if(getMapFromMaps(m,"lenv","dbIssue")!=NULL || metadb_id<0){ 2045 fprintf(stderr,"ERROR CONNECTING METADB\n"); 2046 } 2047 if(metadb_id>=0) 2048 s1=extractServiceFromDb(m,cIdentifier,0); 2049 if(s1!=NULL){ 2050 inheritance(zooRegistry,&s1); 2051 #ifdef USE_HPC 2052 addNestedOutputs(&s1); 2053 #endif 2054 if(zooRegistry!=NULL){ 2055 freeRegistry(&zooRegistry); 2056 free(zooRegistry); 2057 } 2058 }else /* Not found in MetaDB */{ 2059 #endif 2060 s1 = createService(); 2061 if (s1 == NULL) 2062 { 2063 freeMaps (&m); 2064 free (m); 2065 if(zooRegistry!=NULL){ 2066 freeRegistry(&zooRegistry); 2067 free(zooRegistry); 2068 } 2069 free (REQUEST); 2070 free (SERVICE_URL); 2071 map* error=createMap("code","InternalError"); 2072 addToMap(error,"message",_("Unable to allocate memory")); 2073 //setMapInMaps(conf,"lenv","status_code","404 Bad Request"); 2074 printExceptionReportResponseJ(m,error); 2075 2076 return 1; /*errorException (m, _("Unable to allocate memory"), 2077 "InternalError", NULL);*/ 2078 } 2079 2080 int saved_stdout = zDup (fileno (stdout)); 2081 zDup2 (fileno (stderr), fileno (stdout)); 2082 t = readServiceFile (m, tmps1, &s1, cIdentifier); 2083 if(t>=0){ 2084 inheritance(zooRegistry,&s1); 2085 #ifdef USE_HPC 2086 addNestedOutputs(&s1); 2087 #endif 2088 } 2089 if(zooRegistry!=NULL){ 2090 freeRegistry(&zooRegistry); 2091 free(zooRegistry); 2092 } 2093 fflush (stdout); 2094 zDup2 (saved_stdout, fileno (stdout)); 2095 if (t < 0) 2096 { 2097 char *tmpMsg = (char *) malloc (2048 + strlen (r_inputs->value)); 2098 sprintf (tmpMsg, 2099 _ 2100 ("The value for <identifier> seems to be wrong (%s). Please specify one of the processes in the list returned by a GetCapabilities request."), 2101 r_inputs->value); 2102 map* error=createMap("code","InvalidParameterValue"); 2103 addToMap(error,"message",tmpMsg); 2104 //setMapInMaps(conf,"lenv","status_code","404 Bad Request"); 2105 printExceptionReportResponseJ(m,error); 2106 2107 //errorException (m, tmpMsg, "InvalidParameterValue", "identifier"); 2108 free (tmpMsg); 2109 free (s1); 2110 freeMaps (&m); 2111 free (m); 2112 free (REQUEST); 2113 free (SERVICE_URL); 2114 return 0; 2115 } 2116 zClose (saved_stdout); 2117 2118 #ifdef META_DB 2119 } 2120 #endif 2121 if(strstr(cgiQueryString,"/jobs/")!=NULL && strlen(strstr(cgiQueryString,"/jobs/"))>6){ 2122 /* - /jobs/{jobID} and /jobs/{jobID}/result urls */ 2123 if(strstr(cgiQueryString,"/result")!=NULL && strlen(strstr(cgiQueryString,"/result"))>=7){ 2124 fprintf(stderr,"For GetResult %s %d \n",__FILE__,__LINE__); 2125 }else{ 2126 char* tmpUrl=strstr(cgiQueryString,"/jobs/"); 2127 if(tmpUrl!=NULL && strlen(tmpUrl)>6){ 2128 char* jobId=zStrdup(strstr(cgiQueryString,"/jobs/")+6); 2129 setMapInMaps(m,"lenv","gs_usid",jobId); 2130 json_getStatusFile(m); 2131 free(jobId); 2132 return 1; 2133 } 2134 } 2135 }else{ 2136 /* - /jobs url */ 2137 fprintf(stderr,"For Execute or Job list %s %d \n",__FILE__,__LINE__); 2138 if(strcasecmp(cgiRequestMethod,"get")==0){ 2139 /* - /jobs List (GET) */ 2140 res=printJobList(m); 2141 }else if(strcasecmp(cgiRequestMethod,"post")==0){ 2142 /* - /jobs Execution (POST) */ 2143 int eres = SERVICE_STARTED; 2144 2145 initAllEnvironment(m,request_inputs,ntmp,"jrequest"); 2146 map* req=getMapFromMaps(m,"renv","jrequest"); 2147 json_object *jobj = NULL; 2148 const char *mystring = NULL; 2149 int slen = 0; 2150 enum json_tokener_error jerr; 2151 struct json_tokener* tok=json_tokener_new(); 2152 do { 2153 mystring = req->value; // get JSON string, e.g. read from file, etc... 2154 slen = strlen(mystring); 2155 jobj = json_tokener_parse_ex(tok, mystring, slen); 2156 } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue); 2157 if (jerr != json_tokener_success) { 2158 fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr)); 2159 return 1; 2160 } 2161 if (tok->char_offset < slen){ 2162 return 1; 2163 } 2164 fprintf(stderr,"%s %d \n",__FILE__,__LINE__); 2165 // Success, use jobj here. 2166 maps* inputs_real_format=NULL, *outputs_real_format= NULL; 2167 parseJRequest(m,s1,jobj,&inputs_real_format,&outputs_real_format); 2168 map* preference=getMapFromMaps(m,"renv","HTTP_PREFER"); 2169 if(preference!=NULL && strcasecmp(preference->value,"respond-async")==0){ 2170 fprintf(stderr,"Asynchronous call!\n"); 2171 char *fbkp, *fbkpid, *fbkpres, *fbkp1, *flog; 2172 FILE *f0, *f1; 2173 int pid; 2174 #ifdef DEBUG 2175 fprintf (stderr, "\nPID : %d\n", cpid); 2176 #endif 2177 #ifndef WIN32 2178 pid = fork (); 2179 #else 2180 if (cgiSid == NULL) 2181 { 2182 createProcess (m, request_inputs, s1, NULL, cpid, 2183 request_input_real_format, 2184 request_output_real_format); 2185 pid = cpid; 2186 } 2187 else 2188 { 2189 pid = 0; 2190 cpid = atoi (cgiSid); 2191 updateStatus(m,0,_("Initializing")); 2192 } 2193 #endif 2194 if (pid > 0) 2195 { 2196 // 2197 // dady : 2198 // set status to SERVICE_ACCEPTED 2199 // 2200 #ifdef DEBUG 2201 fprintf (stderr, "father pid continue (origin %d) %d ...\n", cpid, 2202 zGetpid ()); 2203 #endif 2204 eres = SERVICE_ACCEPTED; 2205 createStatusFile(m,eres); 2206 printHeaders(m); 2207 printf("Status: 201 Created \r\n\r\n"); 2208 return 1; 2209 } 2210 else if (pid == 0) 2211 { 2212 eres = SERVICE_STARTED; 2213 // 2214 // son : have to close the stdout, stdin and stderr to let the parent 2215 // process answer to http client. 2216 // 2217 map* usid = getMapFromMaps (m, "lenv", "uusid"); 2218 map* tmpm = getMapFromMaps (m, "lenv", "osid"); 2219 int cpid = atoi (tmpm->value); 2220 pid=cpid; 2221 r_inputs = getMapFromMaps (m, "main", "tmpPath"); 2222 setMapInMaps (m, "lenv", "async","true"); 2223 map* r_inputs1 = createMap("ServiceName", s1->name); 2224 // Create the filename for the result file (.res) 2225 fbkpres = 2226 (char *) 2227 malloc ((strlen (r_inputs->value) + 2228 strlen (usid->value) + 7) * sizeof (char)); 2229 sprintf (fbkpres, "%s/%s.res", r_inputs->value, usid->value); 2230 maps* bmap = createMaps("status"); 2231 bmap->content=createMap("usid",usid->value); 2232 addToMap(bmap->content,"sid",tmpm->value); 2233 addIntToMap(bmap->content,"pid",zGetpid()); 2234 2235 // Create PID file referencing the OS process identifier 2236 fbkpid = 2237 (char *) 2238 malloc ((strlen (r_inputs->value) + 2239 strlen (usid->value) + 7) * sizeof (char)); 2240 sprintf (fbkpid, "%s/%s.pid", r_inputs->value, usid->value); 2241 setMapInMaps (m, "lenv", "file.pid", fbkpid); 2242 2243 f0 = freopen (fbkpid, "w+",stdout); 2244 printf("%d",zGetpid()); 2245 fflush(stdout); 2246 2247 // Create SID file referencing the semaphore name 2248 fbkp = 2249 (char *) 2250 malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) + 2251 strlen (usid->value) + 7) * sizeof (char)); 2252 sprintf (fbkp, "%s/%s.sid", r_inputs->value, usid->value); 2253 setMapInMaps (m, "lenv", "file.sid", fbkp); 2254 FILE* f2 = freopen (fbkp, "w+",stdout); 2255 printf("%s",tmpm->value); 2256 fflush(f2); 2257 free(fbkp); 2258 2259 fbkp = 2260 (char *) 2261 malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) + 2262 strlen (usid->value) + 7) * sizeof (char)); 2263 sprintf (fbkp, "%s/%s_%s.xml", r_inputs->value, r_inputs1->value, 2264 usid->value); 2265 setMapInMaps (m, "lenv", "file.responseInit", fbkp); 2266 flog = 2267 (char *) 2268 malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) + 2269 strlen (usid->value) + 13) * sizeof (char)); 2270 sprintf (flog, "%s/%s_%s_error.log", r_inputs->value, 2271 r_inputs1->value, usid->value); 2272 setMapInMaps (m, "lenv", "file.log", flog); 2273 #ifdef DEBUG 2274 fprintf (stderr, "RUN IN BACKGROUND MODE \n"); 2275 fprintf (stderr, "son pid continue (origin %d) %d ...\n", cpid, 2276 zGetpid ()); 2277 fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value); 2278 #endif 2279 freopen (flog, "w+", stderr); 2280 fflush (stderr); 2281 f0 = freopen (fbkp, "w+", stdout); 2282 rewind (stdout); 2283 #ifndef WIN32 2284 fclose (stdin); 2285 #endif 2286 #ifdef RELY_ON_DB 2287 init_sql(m); 2288 recordServiceStatus(m); 2289 #endif 2290 #ifdef USE_CALLBACK 2291 invokeCallback(m,NULL,NULL,0,0); 2292 #endif 2293 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 2294 fflush(stderr); 2295 2296 createStatusFile(m,eres); 2297 2298 2299 fbkp1 = 2300 (char *) 2301 malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) + 2302 strlen (usid->value) + 15) * sizeof (char)); 2303 sprintf (fbkp1, "%s/%s_final_%s.json", r_inputs->value, 2304 r_inputs1->value, usid->value); 2305 setMapInMaps (m, "lenv", "file.responseFinal", fbkp1); 2306 2307 f1 = freopen (fbkp1, "w+", stdout); 2308 2309 map* serviceTypeMap=getMap(s1->content,"serviceType"); 2310 if(serviceTypeMap!=NULL) 2311 setMapInMaps (m, "lenv", "serviceType", serviceTypeMap->value); 2312 2313 char *flenv = 2314 (char *) 2315 malloc ((strlen (r_inputs->value) + 2316 strlen (usid->value) + 12) * sizeof (char)); 2317 sprintf (flenv, "%s/%s_lenv.cfg", r_inputs->value, usid->value); 2318 maps* lenvMaps=getMaps(m,"lenv"); 2319 dumpMapsToFile(lenvMaps,flenv,0); 2320 free(flenv); 2321 2322 map* testMap=getMapFromMaps(m,"main","memory"); 2323 loadHttpRequests(m,inputs_real_format); 2324 loadServiceAndRun (&m, s1, request_inputs, 2325 &inputs_real_format, 2326 &outputs_real_format, &eres); 2327 createStatusFile(m,eres); 2328 res=printJResult(m,s1,outputs_real_format,eres); 2329 char tmpUrl0[1024]; 2330 const char* jsonStr=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); 2331 printf(jsonStr); 2332 printf("\n"); 2333 return 1; 2334 2335 } 2336 }else{ 2337 loadHttpRequests(m,inputs_real_format); 2338 loadServiceAndRun (&m,s1,request_inputs,&inputs_real_format,&outputs_real_format,&eres); 2339 res=printJResult(m,s1,outputs_real_format,eres); 2340 2341 printHeaders(m); 2342 printf("Status: 201 Created \r\n\r\n"); 2343 const char* jsonStr=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); 2344 printf(jsonStr); 2345 printf("\n"); 2346 return 1; 2347 } 2348 2349 2350 }//else error 2351 2352 } 2353 } 2354 } 2355 printHeaders(m); 2356 printf("Status: 200 OK \r\n\r\n"); 2357 const char* jsonStr=json_object_to_json_string_ext(res,JSON_C_TO_STRING_PLAIN); 2358 printf(jsonStr); 2359 printf("\n"); 2360 return 1; 2361 #endif 2362 } 2363 2364 // 2365 // WPS 1.0.0 and 2.0.0 starts here 2366 // 1180 2367 //Check for minimum inputs 1181 2368 map* version=getMap(request_inputs,"version"); … … 1282 2469 1283 2470 2471 1284 2472 service *s1; 1285 2473 int scount = 0; … … 1287 2475 dumpMap (r_inputs); 1288 2476 #endif 1289 char conf_dir[1024]; 1290 int t; 1291 char tmps1[1024]; 1292 1293 r_inputs = NULL; 1294 r_inputs = getMap (request_inputs, "metapath"); 1295 map* cwdMap0=getMapFromMaps(m,"main","servicePath"); 1296 if (r_inputs != NULL) 1297 if(cwdMap0!=NULL) 1298 snprintf (conf_dir, 1024, "%s/%s", cwdMap0->value, r_inputs->value); 1299 else 1300 snprintf (conf_dir, 1024, "%s/%s", ntmp, r_inputs->value); 1301 else 1302 if(cwdMap0!=NULL) 1303 snprintf (conf_dir, 1024, "%s", cwdMap0->value); 1304 else 1305 snprintf (conf_dir, 1024, "%s", ntmp); 1306 map* reg = getMapFromMaps (m, "main", "registry"); 1307 registry* zooRegistry=NULL; 1308 if(reg!=NULL){ 1309 #ifndef WIN32 1310 int saved_stdout = zDup (fileno (stdout)); 1311 zDup2 (fileno (stderr), fileno (stdout)); 1312 #endif 1313 if(createRegistry (m,&zooRegistry,reg->value)<0){ 1314 map *message=getMapFromMaps(m,"lenv","message"); 1315 map *type=getMapFromMaps(m,"lenv","type"); 1316 #ifndef WIN32 1317 zDup2 (saved_stdout, fileno (stdout)); 1318 #endif 1319 errorException (m, message->value, 1320 type->value, NULL); 1321 return 0; 1322 } 1323 #ifndef WIN32 1324 zDup2 (saved_stdout, fileno (stdout)); 1325 zClose(saved_stdout); 1326 #endif 1327 } 1328 2477 1329 2478 if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0) 1330 { 2479 { 1331 2480 #ifdef DEBUG 1332 2481 dumpMap (r_inputs); … … 1396 2545 } 1397 2546 else 1398 { 2547 { 1399 2548 r_inputs = getMap (request_inputs, "JobId"); 1400 2549 if(reqId>nbReqIdentifier){ … … 1471 2620 r_inputs = getMap (request_inputs, "Identifier"); 1472 2621 1473 char *orig = zStrdup (r_inputs->value); 1474 1475 int saved_stdout = zDup (fileno (stdout)); 1476 zDup2 (fileno (stderr), fileno (stdout)); 1477 if (strcasecmp ("all", orig) == 0) 1478 { 1479 maps* imports = getMaps(m, IMPORTSERVICE); 1480 if (imports != NULL) { 1481 map* zcfg = imports->content; 1482 1483 while (zcfg != NULL) { 1484 if (zcfg->value != NULL) { 1485 service* svc = (service*) malloc(SERVICE_SIZE); 1486 if (svc == NULL || readServiceFile(m, zcfg->value, &svc, zcfg->name) < 0) { 1487 // pass over silently 1488 zcfg = zcfg->next; 1489 continue; 1490 } 1491 inheritance(zooRegistry, &svc); 1492 #ifdef USE_HPC 1493 addNestedOutputs(&svc); 1494 #endif 1495 1496 printDescribeProcessForProcess(zooRegistry, m, doc, n, svc); 1497 freeService(&svc); 1498 free(svc); 1499 } 1500 zcfg = zcfg->next; 1501 } 1502 } 1503 1504 if (int res = 1505 recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0, 1506 printDescribeProcessForProcess) < 0) 1507 return res; 1508 #ifdef META_DB 1509 fetchServicesFromDb(zooRegistry,m,doc,n,printDescribeProcessForProcess,0); 1510 close_sql(m,0); 1511 #endif 1512 1513 } 1514 else 1515 { 1516 char *saveptr; 1517 char *tmps = strtok_r (orig, ",", &saveptr); 1518 1519 char buff[256]; 1520 char buff1[1024]; 1521 while (tmps != NULL) 1522 { 1523 int hasVal = -1; 1524 char *corig = zStrdup (tmps); 1525 map* import = getMapFromMaps (m, IMPORTSERVICE, corig); 1526 if (import != NULL && import->value != NULL) 1527 { 1528 #ifdef META_DB 1529 service* s2=extractServiceFromDb(m,import->name,0); 1530 if(s2==NULL){ 1531 #endif 1532 s1 = createService(); 1533 t = readServiceFile (m, import->value, &s1, import->name); 1534 1535 if (t < 0) // failure reading zcfg 1536 { 1537 map *tmp00 = getMapFromMaps (m, "lenv", "message"); 1538 char tmp01[1024]; 1539 if (tmp00 != NULL) 1540 sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"), import->value, tmp00->value); 1541 else 1542 sprintf (tmp01, _("Unable to parse the ZCFG file: %s."), import->value); 1543 1544 zDup2 (saved_stdout, fileno (stdout)); 1545 errorException (m, tmp01, "InternalError", NULL); 1546 1547 freeMaps (&m); 1548 free (m); 1549 1550 if(zooRegistry!=NULL){ 1551 freeRegistry(&zooRegistry); 1552 free(zooRegistry); 1553 } 1554 free (orig); 1555 free (REQUEST); 1556 closedir (dirp); 1557 //xmlFreeDoc (doc); 1558 xmlCleanupParser (); 1559 zooXmlCleanupNs (); 1560 1561 return 1; 1562 } 1563 #ifdef DEBUG 1564 dumpService (s1); 1565 #endif 1566 inheritance(zooRegistry,&s1); 1567 #ifdef USE_HPC 1568 addNestedOutputs(&s1); 1569 #endif 1570 printDescribeProcessForProcess (zooRegistry, m, doc, n, s1); 1571 freeService (&s1); 1572 free (s1); 1573 s1 = NULL; 1574 scount++; 1575 hasVal = 1; 1576 #ifdef META_DB 1577 } 1578 #endif 1579 } 1580 else if (strstr (corig, ".") != NULL) 1581 { 1582 1583 parseIdentifier (m, conf_dir, corig, buff1); 1584 map *tmpMap = getMapFromMaps (m, "lenv", "metapath"); 1585 if (tmpMap != NULL) 1586 addToMap (request_inputs, "metapath", tmpMap->value); 1587 map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier"); 1588 /** 1589 * No support for dot in service name stored in metadb!? 1590 // #ifdef META_DB 1591 service* s2=extractServiceFromDb(m,tmpMapI->value,0); 1592 if(s2==NULL){ 1593 // #endif 1594 */ 1595 s1 = createService(); 1596 t = readServiceFile (m, buff1, &s1, tmpMapI->value); 1597 if (t < 0) 1598 { 1599 map *tmp00 = getMapFromMaps (m, "lenv", "message"); 1600 char tmp01[1024]; 1601 if (tmp00 != NULL) 1602 sprintf (tmp01, 1603 _ 1604 ("Unable to parse the ZCFG file for the following ZOO-Service: %s. Message: %s"), 1605 tmps, tmp00->value); 1606 else 1607 sprintf (tmp01, 1608 _ 1609 ("Unable to parse the ZCFG file for the following ZOO-Service: %s."), 1610 tmps); 1611 zDup2 (saved_stdout, fileno (stdout)); 1612 errorException (m, tmp01, "InvalidParameterValue", 1613 "identifier"); 1614 freeMaps (&m); 1615 free (m); 1616 if(zooRegistry!=NULL){ 1617 freeRegistry(&zooRegistry); 1618 free(zooRegistry); 1619 } 1620 free (REQUEST); 1621 free (corig); 1622 free (orig); 1623 free (SERVICE_URL); 1624 free (s1); 1625 closedir (dirp); 1626 //xmlFreeDoc (doc); 1627 xmlCleanupParser (); 1628 zooXmlCleanupNs (); 1629 return 1; 1630 } 1631 #ifdef DEBUG 1632 dumpService (s1); 1633 #endif 1634 inheritance(zooRegistry,&s1); 1635 #ifdef USE_HPC 1636 addNestedOutputs(&s1); 1637 #endif 1638 printDescribeProcessForProcess (zooRegistry, m, doc, n, s1); 1639 freeService (&s1); 1640 free (s1); 1641 s1 = NULL; 1642 scount++; 1643 hasVal = 1; 1644 setMapInMaps (m, "lenv", "level", "0"); 1645 } 1646 else 1647 { 1648 #ifdef META_DB 1649 _init_sql(m,"metadb"); 1650 //FAILED CONNECTING DB 1651 if(getMapFromMaps(m,"lenv","dbIssue")!=NULL){ 1652 fprintf(stderr,"ERROR CONNECTING METADB"); 1653 } 1654 service* s2=extractServiceFromDb(m,corig,0); 1655 if(s2!=NULL){ 1656 inheritance(zooRegistry,&s2); 1657 #ifdef USE_HPC 1658 addNestedOutputs(&s2); 1659 #endif 1660 printDescribeProcessForProcess (zooRegistry,m, doc, n, s2); 1661 freeService (&s2); 1662 free (s2); 1663 s2 = NULL; 1664 hasVal = 1; 1665 }else /*TOTO*/{ 1666 #endif 1667 memset (buff, 0, 256); 1668 snprintf (buff, 256, "%s.zcfg", corig); 1669 memset (buff1, 0, 1024); 1670 #ifdef DEBUG 1671 printf ("\n#######%s\n########\n", buff); 1672 #endif 1673 while ((dp = readdir (dirp)) != NULL) 1674 { 1675 if (strcasecmp (dp->d_name, buff) == 0) 1676 { 1677 memset (buff1, 0, 1024); 1678 snprintf (buff1, 1024, "%s/%s", conf_dir, 1679 dp->d_name); 1680 s1 = createService(); 1681 if (s1 == NULL) 1682 { 1683 zDup2 (saved_stdout, fileno (stdout)); 1684 return errorException (m, 1685 _ 1686 ("Unable to allocate memory"), 1687 "InternalError", 1688 NULL); 1689 } 1690 #ifdef DEBUG_SERVICE_CONF 1691 fprintf 1692 (stderr,"#################\n(%s) %s\n#################\n", 1693 r_inputs->value, buff1); 1694 #endif 1695 char *tmp0 = zStrdup (dp->d_name); 1696 tmp0[strlen (tmp0) - 5] = 0; 1697 t = readServiceFile (m, buff1, &s1, tmp0); 1698 free (tmp0); 1699 if (t < 0) 1700 { 1701 map *tmp00 = 1702 getMapFromMaps (m, "lenv", "message"); 1703 char tmp01[1024]; 1704 if (tmp00 != NULL) 1705 sprintf (tmp01, 1706 _ 1707 ("Unable to parse the ZCFG file: %s (%s)"), 1708 dp->d_name, tmp00->value); 1709 else 1710 sprintf (tmp01, 1711 _ 1712 ("Unable to parse the ZCFG file: %s."), 1713 dp->d_name); 1714 zDup2 (saved_stdout, fileno (stdout)); 1715 errorException (m, tmp01, "InternalError", 1716 NULL); 1717 freeMaps (&m); 1718 free (m); 1719 if(zooRegistry!=NULL){ 1720 freeRegistry(&zooRegistry); 1721 free(zooRegistry); 1722 } 1723 free (orig); 1724 free (REQUEST); 1725 closedir (dirp); 1726 //xmlFreeDoc (doc); 1727 xmlCleanupParser (); 1728 zooXmlCleanupNs (); 1729 return 1; 1730 } 1731 #ifdef DEBUG 1732 dumpService (s1); 1733 #endif 1734 inheritance(zooRegistry,&s1); 1735 #ifdef USE_HPC 1736 addNestedOutputs(&s1); 1737 #endif 1738 /*json_object* jobj=serviceToJson(s1); 1739 const char* jsonStr=json_object_to_json_string_ext(jobj,JSON_C_TO_STRING_PLAIN); 1740 fprintf(stderr,"*** %s %d %s \n",__FILE__,__LINE__,jsonStr);*/ 1741 1742 printDescribeProcessForProcess (zooRegistry,m, doc, n, s1); 1743 freeService (&s1); 1744 free (s1); 1745 s1 = NULL; 1746 scount++; 1747 hasVal = 1; 1748 } 1749 } 1750 #ifdef META_DB 1751 } 1752 #endif 1753 } 1754 if (hasVal < 0) 1755 { 1756 map *tmp00 = getMapFromMaps (m, "lenv", "message"); 1757 char tmp01[1024]; 1758 if (tmp00 != NULL) 1759 sprintf (tmp01, 1760 _("Unable to parse the ZCFG file: %s (%s)"), 1761 buff, tmp00->value); 1762 else 1763 sprintf (tmp01, 1764 _("Unable to parse the ZCFG file: %s."), 1765 buff); 1766 zDup2 (saved_stdout, fileno (stdout)); 1767 errorException (m, tmp01, "InvalidParameterValue", 1768 "Identifier"); 1769 freeMaps (&m); 1770 free (m); 1771 if(zooRegistry!=NULL){ 1772 freeRegistry(&zooRegistry); 1773 free(zooRegistry); 1774 } 1775 free (orig); 1776 free (REQUEST); 1777 closedir (dirp); 1778 if (corig != NULL) 1779 free (corig); 1780 xmlFreeDoc (doc); 1781 xmlCleanupParser (); 1782 zooXmlCleanupNs (); 1783 return 1; 1784 } 1785 rewinddir (dirp); 1786 tmps = strtok_r (NULL, ",", &saveptr); 1787 if (corig != NULL) 1788 free (corig); 1789 } 1790 } 1791 closedir (dirp); 1792 fflush (stdout); 1793 zDup2 (saved_stdout, fileno (stdout)); 1794 free (orig); 2622 fetchServicesForDescription(zooRegistry, m, r_inputs->value, 2623 printDescribeProcessForProcess, 2624 (void*) doc, (void*) n, conf_dir, 2625 request_inputs,printExceptionReportResponse); 2626 1795 2627 printDocument (m, doc, zGetpid ()); 1796 2628 freeMaps (&m); … … 1810 2642 } 1811 2643 else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0) 1812 { 2644 { 1813 2645 map* version=getMapFromMaps(m,"main","rversion"); 1814 2646 int vid=getVersionId(version->value); … … 1867 2699 } 1868 2700 } 1869 2701 1870 2702 map *postRequest = NULL; 1871 2703 postRequest = getMap (request_inputs, "xrequest"); … … 1950 2782 "InternalError", NULL); 1951 2783 } 1952 2784 1953 2785 int saved_stdout = zDup (fileno (stdout)); 1954 2786 zDup2 (fileno (stderr), fileno (stdout)); … … 2024 2856 } 2025 2857 //InternetCloseHandle (&hInternet); 2026 2027 // Define each env variable in runing environment 2028 maps *curs = getMaps (m, "env"); 2029 if (curs != NULL) 2030 { 2031 map *mapcs = curs->content; 2032 while (mapcs != NULLMAP) 2033 { 2034 #ifndef WIN32 2035 setenv (mapcs->name, mapcs->value, 1); 2036 #ifdef DEBUG 2037 fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name, 2038 mapcs->value); 2039 #endif 2040 #else 2041 if (mapcs->value[strlen (mapcs->value) - 2] == '\r') 2042 { 2043 #ifdef DEBUG 2044 fprintf (stderr, "[ZOO: Env var finish with \r]\n"); 2045 #endif 2046 mapcs->value[strlen (mapcs->value) - 1] = 0; 2047 } 2048 #ifdef DEBUG 2049 if (SetEnvironmentVariable (mapcs->name, mapcs->value) == 0) 2050 { 2051 fflush (stderr); 2052 fprintf (stderr, "setting variable... %s\n", "OK"); 2053 } 2054 else 2055 { 2056 fflush (stderr); 2057 fprintf (stderr, "setting variable... %s\n", "OK"); 2058 } 2059 #else 2060 SetEnvironmentVariable (mapcs->name, mapcs->value); 2061 #endif 2062 char *toto = 2063 (char *) 2064 malloc ((strlen (mapcs->name) + strlen (mapcs->value) + 2065 2) * sizeof (char)); 2066 sprintf (toto, "%s=%s", mapcs->name, mapcs->value); 2067 _putenv (toto); 2068 #ifdef DEBUG 2069 fflush (stderr); 2070 #endif 2071 #endif 2072 2073 #ifdef DEBUG 2074 fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name, 2075 mapcs->value); 2076 fflush (stderr); 2077 #endif 2078 mapcs = mapcs->next; 2079 } 2080 } 2858 2859 initAllEnvironment(m,request_inputs,ntmp,"xrequest"); 2860 2081 2861 2082 2862 #ifdef DEBUG 2083 2863 dumpMap (request_inputs); 2084 2864 #endif 2085 2865 2086 2866 map *status = getMap (request_inputs, "status"); 2087 2867 if(vid==0){ … … 2164 2944 int eres = SERVICE_STARTED; 2165 2945 int cpid = zGetpid (); 2166 2167 // Create a map containing a copy of the request map 2168 maps *_tmpMaps = createMaps("request"); 2169 addMapToMap(&_tmpMaps->content,request_inputs); 2170 addMapsToMaps (&m, _tmpMaps); 2171 freeMaps (&_tmpMaps); 2172 free (_tmpMaps); 2173 /** 2174 * Initialize the specific [lenv] section which contains runtime variables: 2175 * 2176 * - usid : it is an universally unique identifier 2177 * - osid : it is an idenfitication number 2178 * - sid : it is the process idenfitication number (OS) 2179 * - uusid : it is an universally unique identifier 2180 * - status : value between 0 and 100 to express the completude of 2181 * the operations of the running service 2182 * - message : is a string where you can store error messages, in case 2183 * service is failing, or o provide details on the ongoing operation. 2184 * - cwd : the current working directory or servicePath if defined 2185 * - soap : is a boolean value, true if the request was contained in a SOAP 2186 * Envelop 2187 * - sessid : string storing the session identifier (only when cookie is 2188 * used) 2189 * - cgiSid : only defined on Window platforms (for being able to identify 2190 * the created process) 2191 * 2192 */ 2193 _tmpMaps = createMaps("lenv"); 2194 char tmpBuff[100]; 2195 struct ztimeval tp; 2196 if (zGettimeofday (&tp, NULL) == 0) 2197 sprintf (tmpBuff, "%i", (cpid + ((int) tp.tv_sec + (int) tp.tv_usec))); 2198 else 2199 sprintf (tmpBuff, "%i", (cpid + (int) time (NULL))); 2200 _tmpMaps->content = createMap ("osid", tmpBuff); 2201 sprintf (tmpBuff, "%i", cpid); 2202 addToMap (_tmpMaps->content, "sid", tmpBuff); 2203 char* tmpUuid=get_uuid(); 2204 addToMap (_tmpMaps->content, "uusid", tmpUuid); 2205 addToMap (_tmpMaps->content, "usid", tmpUuid); 2206 free(tmpUuid); 2207 addToMap (_tmpMaps->content, "status", "0"); 2208 if(cwdMap0!=NULL){ 2209 addToMap (_tmpMaps->content, "cwd", cwdMap0->value); 2210 addToMap (_tmpMaps->content, "rcwd", ntmp); 2211 } 2212 else 2213 addToMap (_tmpMaps->content, "cwd", ntmp); 2214 addToMap (_tmpMaps->content, "message", _("No message provided")); 2215 map *ltmp = getMap (request_inputs, "soap"); 2216 if (ltmp != NULL) 2217 addToMap (_tmpMaps->content, "soap", ltmp->value); 2218 else 2219 addToMap (_tmpMaps->content, "soap", "false"); 2220 2221 // Parse the session file and add it to the main maps 2222 char* originalCookie=NULL; 2223 if (cgiCookie != NULL && strlen (cgiCookie) > 0) 2224 { 2225 int hasValidCookie = -1; 2226 char *tcook = originalCookie = zStrdup (cgiCookie); 2227 map *testing = getMapFromMaps (m, "main", "cookiePrefix"); 2228 parseCookie(&m,originalCookie); 2229 map *sessId=getMapFromMaps(m,"cookies",(testing==NULL?"ID":testing->value)); 2230 if (sessId!=NULL) 2231 { 2232 addToMap (_tmpMaps->content, "sessid", sessId->value); 2233 char session_file_path[1024]; 2234 map *tmpPath = getMapFromMaps (m, "main", "sessPath"); 2235 if (tmpPath == NULL) 2236 tmpPath = getMapFromMaps (m, "main", "tmpPath"); 2237 char *tmp1 = strtok (tcook, ";"); 2238 if (tmp1 != NULL) 2239 sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value, 2240 sessId->value); 2241 else 2242 sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value, 2243 sessId->value); 2244 free (tcook); 2245 maps *tmpSess = (maps *) malloc (MAPS_SIZE); 2246 tmpSess->child=NULL; 2247 struct stat file_status; 2248 int istat = stat (session_file_path, &file_status); 2249 if (istat == 0 && file_status.st_size > 0) 2250 { 2251 int saved_stdout = zDup (fileno (stdout)); 2252 zDup2 (fileno (stderr), fileno (stdout)); 2253 conf_read (session_file_path, tmpSess); 2254 addMapsToMaps (&m, tmpSess); 2255 freeMaps (&tmpSess); 2256 fflush(stdout); 2257 zDup2 (saved_stdout, fileno (stdout)); 2258 zClose(saved_stdout); 2259 } 2260 free (tmpSess); 2261 } 2262 } 2263 addMapsToMaps (&m, _tmpMaps); 2264 freeMaps (&_tmpMaps); 2265 free (_tmpMaps); 2946 2266 2947 maps* bmap=NULL; 2267 #ifdef DEBUG2268 dumpMap (request_inputs);2269 #endif2270 int ei = 1;2271 2272 _tmpMaps = createMaps("renv");2273 2274 #ifdef WIN322275 LPVOID orig = GetEnvironmentStrings();2276 LPTSTR s = (LPTSTR) orig;2277 2278 while (*s != NULL) {2279 char* env = strdup(s);2280 char* delim = strchr(env,'=');2281 if (delim != NULL) {2282 char* val = delim+1;2283 *delim = '\0';2284 if(_tmpMaps->content == NULL) {2285 _tmpMaps->content = createMap(env,val);2286 }2287 else {2288 addToMap(_tmpMaps->content,env,val);2289 }2290 }2291 s += strlen(s)+1;2292 }2293 FreeEnvironmentStrings((LPCH)orig);2294 #else2295 char **orig = environ;2296 char *s=*orig;2297 2298 if(orig!=NULL)2299 for (;2300 s;2301 ei++2302 ) {2303 if(strstr(s,"=")!=NULL && strlen(strstr(s,"="))>1){2304 int len=strlen(s);2305 char* tmpName=zStrdup(s);2306 char* tmpValue=strstr(s,"=")+1;2307 char* tmpName1=(char*)malloc((1+(len-(strlen(tmpValue)+1)))*sizeof(char));2308 snprintf(tmpName1,(len-strlen(tmpValue)),"%s",tmpName);2309 if(_tmpMaps->content == NULL)2310 _tmpMaps->content = createMap (tmpName1,tmpValue);2311 else2312 addToMap (_tmpMaps->content,tmpName1,tmpValue);2313 free(tmpName1);2314 free(tmpName);2315 }2316 s = *(orig+ei);2317 }2318 #endif2319 2320 if(_tmpMaps->content!=NULL && getMap(_tmpMaps->content,"HTTP_COOKIE")!=NULL){2321 addToMap(_tmpMaps->content,"HTTP_COOKIE1",&cgiCookie[0]);2322 }2323 addMapsToMaps (&m, _tmpMaps);2324 freeMaps (&_tmpMaps);2325 free (_tmpMaps);2326 2327 if(postRequest!=NULL)2328 setMapInMaps (m, "renv", "xrequest", postRequest->value);2329 //dumpMaps(m);2330 #ifdef WIN322331 char *cgiSidL = NULL;2332 if (getenv ("CGISID") != NULL)2333 addToMap (request_inputs, "cgiSid", getenv ("CGISID"));2334 2335 char* usidp;2336 if ( (usidp = getenv("USID")) != NULL ) {2337 setMapInMaps (m, "lenv", "usid", usidp);2338 }2339 2340 map *test1 = getMap (request_inputs, "cgiSid");2341 if (test1 != NULL){2342 cgiSid = zStrdup(test1->value);2343 addToMap (request_inputs, "storeExecuteResponse", "true");2344 addToMap (request_inputs, "status", "true");2345 setMapInMaps (m, "lenv", "osid", test1->value);2346 status = getMap (request_inputs, "status");2347 }2348 test1 = getMap (request_inputs, "usid");2349 if (test1 != NULL){2350 setMapInMaps (m, "lenv", "usid", test1->value);2351 setMapInMaps (m, "lenv", "uusid", test1->value);2352 }2353 #endif2354 2355 2948 char *fbkp, *fbkpid, *fbkpres, *fbkp1, *flog; 2356 2949 FILE *f0, *f1; … … 2360 2953 if (status == NULLMAP) 2361 2954 { 2362 2363 2955 if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){ 2364 2956 freeService (&s1); … … 2699 3291 if(cgiSid!=NULL) 2700 3292 free(cgiSid); 2701 //InternetCloseHandle (&hInternet);3293 InternetCloseHandle (&hInternet); 2702 3294 fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__); 2703 3295 fflush(stderr);
Note: See TracChangeset
for help on using the changeset viewer.