[642] | 1 | /* |
---|
| 2 | * Author : Gérald Fenoy |
---|
| 3 | * |
---|
| 4 | * Copyright 2008-2015 GeoLabs SARL. All rights reserved. |
---|
| 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 7 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 8 | * in the Software without restriction, including without limitation the rights |
---|
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 10 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 11 | * furnished to do so, subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in |
---|
| 14 | * all copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 22 | * THE SOFTWARE. |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "server_internal.h" |
---|
| 26 | #include "response_print.h" |
---|
| 27 | #include "mimetypes.h" |
---|
| 28 | #ifndef WIN32 |
---|
| 29 | #include <dlfcn.h> |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | int getVersionId(const char* version){ |
---|
| 33 | int schemaId=0; |
---|
| 34 | for(;schemaId<2;schemaId++){ |
---|
| 35 | if(strncasecmp(version,schemas[schemaId][0],5)==0) |
---|
| 36 | return schemaId; |
---|
| 37 | } |
---|
| 38 | return 0; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Extract the service identifier from the full service identifier |
---|
| 43 | * ie: |
---|
| 44 | * - Full service name: OTB.BandMath |
---|
| 45 | * - Service name: BandMath |
---|
| 46 | * |
---|
| 47 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 48 | * @param conf_dir the full path to the ZOO-Kernel directory |
---|
| 49 | * @param identifier the full service name (potentialy including a prefix, ie: |
---|
| 50 | * Prefix.MyService) |
---|
| 51 | * @param buffer the resulting service identifier (without any prefix) |
---|
| 52 | */ |
---|
| 53 | void parseIdentifier(maps* conf,char* conf_dir,char *identifier,char* buffer){ |
---|
| 54 | setMapInMaps(conf,"lenv","oIdentifier",identifier); |
---|
| 55 | char *lid=zStrdup(identifier); |
---|
| 56 | char *saveptr1; |
---|
| 57 | char *tmps1=strtok_r(lid,".",&saveptr1); |
---|
| 58 | int level=0; |
---|
| 59 | char key[25]; |
---|
| 60 | char levels[18]; |
---|
| 61 | while(tmps1!=NULL){ |
---|
| 62 | char *test=zStrdup(tmps1); |
---|
| 63 | char* tmps2=(char*)malloc((strlen(test)+2)*sizeof(char)); |
---|
| 64 | sprintf(key,"sprefix_%d",level); |
---|
| 65 | sprintf(tmps2,"%s.",test); |
---|
| 66 | sprintf(levels,"%d",level); |
---|
| 67 | setMapInMaps(conf,"lenv","level",levels); |
---|
| 68 | setMapInMaps(conf,"lenv",key,tmps2); |
---|
| 69 | free(tmps2); |
---|
| 70 | free(test); |
---|
| 71 | level++; |
---|
| 72 | tmps1=strtok_r(NULL,".",&saveptr1); |
---|
| 73 | } |
---|
| 74 | int i=0; |
---|
| 75 | sprintf(buffer,"%s",conf_dir); |
---|
| 76 | for(i=0;i<level;i++){ |
---|
| 77 | char *tmp0=zStrdup(buffer); |
---|
| 78 | sprintf(key,"sprefix_%d",i); |
---|
| 79 | map* tmp00=getMapFromMaps(conf,"lenv",key); |
---|
| 80 | if(tmp00!=NULL) |
---|
| 81 | sprintf(buffer,"%s/%s",tmp0,tmp00->value); |
---|
| 82 | free(tmp0); |
---|
| 83 | buffer[strlen(buffer)-1]=0; |
---|
| 84 | if(i+1<level){ |
---|
| 85 | #ifdef IGNORE_METAPATH |
---|
| 86 | map* tmpMap = createMap("metapath", ""); |
---|
| 87 | #else |
---|
| 88 | map* tmpMap=getMapFromMaps(conf,"lenv","metapath"); |
---|
| 89 | #endif |
---|
| 90 | if(tmpMap==NULL || strlen(tmpMap->value)==0){ |
---|
| 91 | char *tmp01=zStrdup(tmp00->value); |
---|
| 92 | tmp01[strlen(tmp01)-1]=0; |
---|
| 93 | setMapInMaps(conf,"lenv","metapath",tmp01); |
---|
| 94 | free(tmp01); |
---|
| 95 | tmp01=NULL; |
---|
| 96 | } |
---|
| 97 | else{ |
---|
| 98 | if(tmp00!=NULL && tmpMap!=NULL){ |
---|
| 99 | char *tmp00s=zStrdup(tmp00->value); |
---|
| 100 | tmp00s[strlen(tmp00s)-1]=0; |
---|
| 101 | char *value=(char*)malloc((strlen(tmp00s)+strlen(tmpMap->value)+2)*sizeof(char)); |
---|
| 102 | sprintf(value,"%s/%s",tmpMap->value,tmp00s); |
---|
| 103 | setMapInMaps(conf,"lenv","metapath",value); |
---|
| 104 | free(value); |
---|
| 105 | free(tmp00s); |
---|
| 106 | value=NULL; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | }else{ |
---|
| 110 | char *tmp01=zStrdup(tmp00->value); |
---|
| 111 | tmp01[strlen(tmp01)-1]=0; |
---|
| 112 | setMapInMaps(conf,"lenv","Identifier",tmp01); |
---|
| 113 | free(tmp01); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | char *tmp0=zStrdup(buffer); |
---|
| 117 | sprintf(buffer,"%s.zcfg",tmp0); |
---|
| 118 | free(tmp0); |
---|
| 119 | free(lid); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Converts a hex character to its integer value |
---|
| 124 | * |
---|
| 125 | * @param ch the char to convert |
---|
| 126 | * @return the converted char |
---|
| 127 | */ |
---|
| 128 | char from_hex(char ch) { |
---|
| 129 | return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Converts an integer value to its hec character |
---|
| 134 | * |
---|
| 135 | * @param code the char to convert |
---|
| 136 | * @return the converted char |
---|
| 137 | */ |
---|
| 138 | char to_hex(char code) { |
---|
| 139 | static char hex[] = "0123456789abcdef"; |
---|
| 140 | return hex[code & 15]; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * URLEncode an url |
---|
| 145 | * |
---|
| 146 | * @param str the url to encode |
---|
| 147 | * @return a url-encoded version of str |
---|
| 148 | * @warning be sure to free() the returned string after use |
---|
| 149 | */ |
---|
| 150 | char *url_encode(char *str) { |
---|
| 151 | char *pstr = str, *buf = (char*) malloc(strlen(str) * 3 + 1), *pbuf = buf; |
---|
| 152 | while (*pstr) { |
---|
| 153 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
---|
| 154 | *pbuf++ = *pstr; |
---|
| 155 | else if (*pstr == ' ') |
---|
| 156 | *pbuf++ = '+'; |
---|
| 157 | else |
---|
| 158 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
---|
| 159 | pstr++; |
---|
| 160 | } |
---|
| 161 | *pbuf = '\0'; |
---|
| 162 | return buf; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * Decode an URLEncoded url |
---|
| 167 | * |
---|
| 168 | * @param str the URLEncoded url to decode |
---|
| 169 | * @return a url-decoded version of str |
---|
| 170 | * @warning be sure to free() the returned string after use |
---|
| 171 | */ |
---|
| 172 | char *url_decode(char *str) { |
---|
| 173 | char *pstr = str, *buf = (char*) malloc(strlen(str) + 1), *pbuf = buf; |
---|
| 174 | while (*pstr) { |
---|
| 175 | if (*pstr == '%') { |
---|
| 176 | if (pstr[1] && pstr[2]) { |
---|
| 177 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
---|
| 178 | pstr += 2; |
---|
| 179 | } |
---|
| 180 | } else if (*pstr == '+') { |
---|
| 181 | *pbuf++ = ' '; |
---|
| 182 | } else { |
---|
| 183 | *pbuf++ = *pstr; |
---|
| 184 | } |
---|
| 185 | pstr++; |
---|
| 186 | } |
---|
| 187 | *pbuf = '\0'; |
---|
| 188 | return buf; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Verify if a given language is listed in the lang list defined in the [main] |
---|
| 193 | * section of the main.cfg file. |
---|
| 194 | * |
---|
| 195 | * @param conf the map containing the settings from the main.cfg file |
---|
| 196 | * @param str the specific language |
---|
| 197 | * @return 1 if the specific language is listed, -1 in other case. |
---|
| 198 | */ |
---|
| 199 | int isValidLang(maps* conf,const char *str){ |
---|
| 200 | map *tmpMap=getMapFromMaps(conf,"main","lang"); |
---|
| 201 | char *tmp=zStrdup(tmpMap->value); |
---|
| 202 | char *pToken,*saveptr; |
---|
| 203 | pToken=strtok_r(tmp,",",&saveptr); |
---|
| 204 | int res=-1; |
---|
| 205 | char *pToken1,*saveptr1; |
---|
| 206 | pToken1=strtok_r(tmp,",",&saveptr1); |
---|
| 207 | while(pToken1!=NULL){ |
---|
| 208 | while(pToken!=NULL){ |
---|
| 209 | if(strcasecmp(pToken1,pToken)==0){ |
---|
| 210 | res=1; |
---|
| 211 | break; |
---|
| 212 | } |
---|
| 213 | pToken=strtok_r(NULL,",",&saveptr); |
---|
| 214 | } |
---|
| 215 | pToken1=strtok_r(NULL,",",&saveptr1); |
---|
| 216 | } |
---|
| 217 | free(tmp); |
---|
| 218 | return res; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * Access the value of the encoding key in a maps |
---|
| 224 | * |
---|
| 225 | * @param m the maps to search for the encoding key |
---|
| 226 | * @return the value of the encoding key in a maps if encoding key exists, |
---|
| 227 | * "UTF-8" in other case. |
---|
| 228 | */ |
---|
| 229 | char* getEncoding(maps* m){ |
---|
| 230 | if(m!=NULL){ |
---|
| 231 | map* tmp=getMap(m->content,"encoding"); |
---|
| 232 | if(tmp!=NULL){ |
---|
| 233 | return tmp->value; |
---|
| 234 | } |
---|
| 235 | else |
---|
| 236 | return (char*)"UTF-8"; |
---|
| 237 | } |
---|
| 238 | else |
---|
| 239 | return (char*)"UTF-8"; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /** |
---|
| 243 | * Access the value of the version key in a maps |
---|
| 244 | * |
---|
| 245 | * @param m the maps to search for the version key |
---|
| 246 | * @return the value of the version key in a maps if encoding key exists, |
---|
| 247 | * "1.0.0" in other case. |
---|
| 248 | */ |
---|
| 249 | char* getVersion(maps* m){ |
---|
| 250 | if(m!=NULL){ |
---|
| 251 | map* tmp=getMap(m->content,"version"); |
---|
| 252 | if(tmp!=NULL){ |
---|
| 253 | return tmp->value; |
---|
| 254 | } |
---|
| 255 | else |
---|
| 256 | return (char*)"1.0.0"; |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | return (char*)"1.0.0"; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Read a file generated by a service. |
---|
| 264 | * |
---|
| 265 | * @param m the conf maps |
---|
| 266 | * @param content the output item |
---|
| 267 | * @param filename the file to read |
---|
| 268 | */ |
---|
| 269 | void readGeneratedFile(maps* m,map* content,char* filename){ |
---|
| 270 | FILE * file=fopen(filename,"rb"); |
---|
| 271 | if(file==NULL){ |
---|
| 272 | fprintf(stderr,"Failed to open file %s for reading purpose.\n",filename); |
---|
| 273 | setMapInMaps(m,"lenv","message","Unable to read produced file. Please try again later"); |
---|
| 274 | return ; |
---|
| 275 | } |
---|
| 276 | fseek(file, 0, SEEK_END); |
---|
| 277 | long count = ftell(file); |
---|
| 278 | rewind(file); |
---|
| 279 | struct stat file_status; |
---|
| 280 | stat(filename, &file_status); |
---|
| 281 | map* tmpMap1=getMap(content,"value"); |
---|
| 282 | if(tmpMap1==NULL){ |
---|
| 283 | addToMap(content,"value",""); |
---|
| 284 | tmpMap1=getMap(content,"value"); |
---|
| 285 | } |
---|
| 286 | free(tmpMap1->value); |
---|
| 287 | tmpMap1->value=(char*) malloc((count+1)*sizeof(char)); |
---|
| 288 | fread(tmpMap1->value,1,count,file); |
---|
| 289 | tmpMap1->value[count]=0; |
---|
| 290 | fclose(file); |
---|
| 291 | char rsize[1000]; |
---|
| 292 | sprintf(rsize,"%ld",count); |
---|
| 293 | addToMap(content,"size",rsize); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | * Write a file from value and length |
---|
| 299 | * |
---|
| 300 | * @param fname the file name |
---|
| 301 | * @param val the value |
---|
| 302 | * @param length the value length |
---|
| 303 | */ |
---|
| 304 | int writeFile(char* fname,char* val,int length){ |
---|
| 305 | FILE* of=fopen(fname,"wb"); |
---|
| 306 | if(of==NULL){ |
---|
| 307 | return -1; |
---|
| 308 | } |
---|
| 309 | size_t ret=fwrite(val,sizeof(char),length,of); |
---|
| 310 | if(ret<length){ |
---|
| 311 | fprintf(stderr,"Write error occured!\n"); |
---|
| 312 | fclose(of); |
---|
| 313 | return -1; |
---|
| 314 | } |
---|
| 315 | fclose(of); |
---|
| 316 | return 1; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * Dump all values in a maps as files |
---|
| 321 | * |
---|
| 322 | * @param main_conf the maps containing the settings of the main.cfg file |
---|
| 323 | * @param in the maps containing values to dump as files |
---|
| 324 | */ |
---|
| 325 | void dumpMapsValuesToFiles(maps** main_conf,maps** in){ |
---|
| 326 | map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath"); |
---|
| 327 | map* tmpSid=getMapFromMaps(*main_conf,"lenv","sid"); |
---|
| 328 | maps* inputs=*in; |
---|
| 329 | int length=0; |
---|
| 330 | while(inputs!=NULL){ |
---|
| 331 | if(getMap(inputs->content,"mimeType")!=NULL && |
---|
| 332 | getMap(inputs->content,"cache_file")==NULL){ |
---|
| 333 | map* cMap=inputs->content; |
---|
| 334 | if(getMap(cMap,"length")!=NULL){ |
---|
| 335 | map* tmpLength=getMap(cMap,"length"); |
---|
| 336 | int len=atoi(tmpLength->value); |
---|
| 337 | int k=0; |
---|
| 338 | for(k=0;k<len;k++){ |
---|
| 339 | map* cMimeType=getMapArray(cMap,"mimeType",k); |
---|
| 340 | map* cValue=getMapArray(cMap,"value",k); |
---|
| 341 | map* cSize=getMapArray(cMap,"size",k); |
---|
| 342 | char file_ext[32]; |
---|
| 343 | getFileExtension(cMimeType != NULL ? cMimeType->value : NULL, file_ext, 32); |
---|
| 344 | char* val=(char*)malloc((strlen(tmpPath->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char)); |
---|
| 345 | sprintf(val,"%s/Input_%s_%s_%d.%s",tmpPath->value,inputs->name,tmpSid->value,k,file_ext); |
---|
| 346 | length=0; |
---|
| 347 | if(cSize!=NULL){ |
---|
| 348 | length=atoi(cSize->value); |
---|
| 349 | } |
---|
| 350 | writeFile(val,cValue->value,length); |
---|
| 351 | setMapArray(cMap,"cache_file",k,val); |
---|
| 352 | free(val); |
---|
| 353 | } |
---|
| 354 | }else{ |
---|
| 355 | int length=0; |
---|
| 356 | map* cMimeType=getMap(cMap,"mimeType"); |
---|
| 357 | map* cValue=getMap(cMap,"value"); |
---|
| 358 | map* cSize=getMap(cMap,"size"); |
---|
| 359 | char file_ext[32]; |
---|
| 360 | getFileExtension(cMimeType != NULL ? cMimeType->value : NULL, file_ext, 32); |
---|
| 361 | char *val=(char*)malloc((strlen(tmpPath->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char)); |
---|
| 362 | sprintf(val,"%s/Input_%s_%s_%d.%s",tmpPath->value,inputs->name,tmpSid->value,0,file_ext); |
---|
| 363 | if(cSize!=NULL){ |
---|
| 364 | length=atoi(cSize->value); |
---|
| 365 | } |
---|
| 366 | writeFile(val,cValue->value,length); |
---|
| 367 | addToMap(cMap,"cache_file",val); |
---|
| 368 | free(val); |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | inputs=inputs->next; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | |
---|
| 376 | /** |
---|
| 377 | * Base64 encoding of a char* |
---|
| 378 | * |
---|
| 379 | * @param input the value to encode |
---|
| 380 | * @param length the value length |
---|
| 381 | * @return the buffer containing the base64 value |
---|
| 382 | * @warning make sure to free the returned value |
---|
| 383 | */ |
---|
| 384 | char *base64(const char *input, int length) |
---|
| 385 | { |
---|
| 386 | BIO *bmem, *b64; |
---|
| 387 | BUF_MEM *bptr; |
---|
| 388 | |
---|
| 389 | b64 = BIO_new(BIO_f_base64()); |
---|
| 390 | BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
---|
| 391 | bmem = BIO_new(BIO_s_mem()); |
---|
| 392 | b64 = BIO_push(b64, bmem); |
---|
| 393 | BIO_write(b64, input, length); |
---|
| 394 | BIO_flush(b64); |
---|
| 395 | BIO_get_mem_ptr(b64, &bptr); |
---|
| 396 | |
---|
| 397 | char *buff = (char *)malloc((bptr->length+1)*sizeof(char)); |
---|
| 398 | memcpy(buff, bptr->data, bptr->length); |
---|
| 399 | buff[bptr->length] = 0; |
---|
| 400 | |
---|
| 401 | BIO_free_all(b64); |
---|
| 402 | |
---|
| 403 | return buff; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | /** |
---|
| 407 | * Base64 decoding of a char* |
---|
| 408 | * |
---|
| 409 | * @param input the value to decode |
---|
| 410 | * @param length the value length |
---|
| 411 | * @param red the value length |
---|
| 412 | * @return the buffer containing the base64 value |
---|
| 413 | * @warning make sure to free the returned value |
---|
| 414 | */ |
---|
| 415 | char *base64d(const char *input, int length,int* red) |
---|
| 416 | { |
---|
| 417 | BIO *b64, *bmem; |
---|
| 418 | |
---|
| 419 | char *buffer = (char *)malloc(length); |
---|
| 420 | if(buffer){ |
---|
| 421 | memset(buffer, 0, length); |
---|
| 422 | b64 = BIO_new(BIO_f_base64()); |
---|
| 423 | if(b64){ |
---|
| 424 | bmem = BIO_new_mem_buf((unsigned char*)input,length); |
---|
| 425 | bmem = BIO_push(b64, bmem); |
---|
| 426 | *red=BIO_read(bmem, buffer, length); |
---|
| 427 | buffer[length-1]=0; |
---|
| 428 | BIO_free_all(bmem); |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | return buffer; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | /** |
---|
| 435 | * Read Base64 value and split it value by lines of 64 char. |
---|
| 436 | * |
---|
| 437 | * @param in the map containing the value to split |
---|
| 438 | */ |
---|
| 439 | void readBase64(map **in){ |
---|
| 440 | char *res = NULL; |
---|
| 441 | char *curs = (*in)->value; |
---|
| 442 | int i = 0; |
---|
| 443 | for (i = 0; i <= strlen ((*in)->value) / 64; |
---|
| 444 | i++) |
---|
| 445 | { |
---|
| 446 | if (res == NULL) |
---|
| 447 | res = |
---|
| 448 | (char *) malloc (65 * sizeof (char)); |
---|
| 449 | else |
---|
| 450 | res = |
---|
| 451 | (char *) realloc (res, |
---|
| 452 | (((i + 1) * 65) + |
---|
| 453 | i) * sizeof (char)); |
---|
| 454 | int csize = i * 65; |
---|
| 455 | strncpy (res + csize, curs, 64); |
---|
| 456 | if (i == strlen ((*in)->value) / 64) |
---|
| 457 | strcat (res, "\n\0"); |
---|
| 458 | else |
---|
| 459 | { |
---|
| 460 | strncpy (res + (((i + 1) * 64) + i), |
---|
| 461 | "\n\0", 2); |
---|
| 462 | curs += 64; |
---|
| 463 | } |
---|
| 464 | } |
---|
| 465 | free ((*in)->value); |
---|
| 466 | (*in)->value = zStrdup (res); |
---|
| 467 | free (res); |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | |
---|
| 471 | /** |
---|
| 472 | * Add the default values defined in the zcfg to a maps. |
---|
| 473 | * |
---|
| 474 | * @param out the maps containing the inputs or outputs given in the initial |
---|
| 475 | * HTTP request |
---|
| 476 | * @param in the description of all inputs or outputs available for a service |
---|
| 477 | * @param m the maps containing the settings of the main.cfg file |
---|
| 478 | * @param type 0 for inputs and 1 for outputs |
---|
| 479 | * @param err the map to store potential missing mandatory input parameters or |
---|
| 480 | * wrong output names depending on the type. |
---|
| 481 | * @return "" if no error was detected, the name of last input or output causing |
---|
| 482 | * an error. |
---|
| 483 | */ |
---|
| 484 | char* addDefaultValues(maps** out,elements* in,maps* m,int type,map** err){ |
---|
| 485 | map *res=*err; |
---|
| 486 | elements* tmpInputs=in; |
---|
| 487 | maps* out1=*out; |
---|
| 488 | char *result=NULL; |
---|
| 489 | int nb=0; |
---|
| 490 | if(type==1){ |
---|
| 491 | while(out1!=NULL){ |
---|
| 492 | if(getElements(in,out1->name)==NULL){ |
---|
| 493 | if(res==NULL){ |
---|
| 494 | res=createMap("value",out1->name); |
---|
| 495 | }else{ |
---|
| 496 | setMapArray(res,"value",nb,out1->name); |
---|
| 497 | } |
---|
| 498 | nb++; |
---|
| 499 | result=out1->name; |
---|
| 500 | } |
---|
| 501 | out1=out1->next; |
---|
| 502 | } |
---|
| 503 | if(res!=NULL){ |
---|
| 504 | *err=res; |
---|
| 505 | return result; |
---|
| 506 | } |
---|
| 507 | out1=*out; |
---|
| 508 | } |
---|
| 509 | while(tmpInputs!=NULL){ |
---|
| 510 | maps *tmpMaps=getMaps(out1,tmpInputs->name); |
---|
| 511 | if(tmpMaps==NULL){ |
---|
| 512 | maps* tmpMaps2=(maps*)malloc(MAPS_SIZE); |
---|
| 513 | tmpMaps2->name=strdup(tmpInputs->name); |
---|
| 514 | tmpMaps2->content=NULL; |
---|
| 515 | tmpMaps2->next=NULL; |
---|
| 516 | |
---|
| 517 | if(type==0){ |
---|
| 518 | map* tmpMapMinO=getMap(tmpInputs->content,"minOccurs"); |
---|
| 519 | if(tmpMapMinO!=NULL){ |
---|
| 520 | if(atoi(tmpMapMinO->value)>=1){ |
---|
| 521 | freeMaps(&tmpMaps2); |
---|
| 522 | free(tmpMaps2); |
---|
| 523 | if(res==NULL){ |
---|
| 524 | res=createMap("value",tmpInputs->name); |
---|
| 525 | }else{ |
---|
| 526 | setMapArray(res,"value",nb,tmpInputs->name); |
---|
| 527 | } |
---|
| 528 | nb++; |
---|
| 529 | result=tmpInputs->name; |
---|
| 530 | } |
---|
| 531 | else{ |
---|
| 532 | if(tmpMaps2->content==NULL) |
---|
| 533 | tmpMaps2->content=createMap("minOccurs",tmpMapMinO->value); |
---|
| 534 | else |
---|
| 535 | addToMap(tmpMaps2->content,"minOccurs",tmpMapMinO->value); |
---|
| 536 | } |
---|
| 537 | } |
---|
| 538 | if(res==NULL){ |
---|
| 539 | map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs"); |
---|
| 540 | if(tmpMaxO!=NULL){ |
---|
| 541 | if(tmpMaps2->content==NULL) |
---|
| 542 | tmpMaps2->content=createMap("maxOccurs",tmpMaxO->value); |
---|
| 543 | else |
---|
| 544 | addToMap(tmpMaps2->content,"maxOccurs",tmpMaxO->value); |
---|
| 545 | } |
---|
| 546 | map* tmpMaxMB=getMap(tmpInputs->content,"maximumMegabytes"); |
---|
| 547 | if(tmpMaxMB!=NULL){ |
---|
| 548 | if(tmpMaps2->content==NULL) |
---|
| 549 | tmpMaps2->content=createMap("maximumMegabytes",tmpMaxMB->value); |
---|
| 550 | else |
---|
| 551 | addToMap(tmpMaps2->content,"maximumMegabytes",tmpMaxMB->value); |
---|
| 552 | } |
---|
| 553 | } |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | if(res==NULL){ |
---|
| 557 | iotype* tmpIoType=tmpInputs->defaults; |
---|
| 558 | if(tmpIoType!=NULL){ |
---|
| 559 | map* tmpm=tmpIoType->content; |
---|
| 560 | while(tmpm!=NULL){ |
---|
| 561 | if(tmpMaps2->content==NULL) |
---|
| 562 | tmpMaps2->content=createMap(tmpm->name,tmpm->value); |
---|
| 563 | else |
---|
| 564 | addToMap(tmpMaps2->content,tmpm->name,tmpm->value); |
---|
| 565 | tmpm=tmpm->next; |
---|
| 566 | } |
---|
| 567 | } |
---|
| 568 | addToMap(tmpMaps2->content,"inRequest","false"); |
---|
| 569 | if(type==0){ |
---|
| 570 | map *tmpMap=getMap(tmpMaps2->content,"value"); |
---|
| 571 | if(tmpMap==NULL) |
---|
| 572 | addToMap(tmpMaps2->content,"value","NULL"); |
---|
| 573 | } |
---|
| 574 | if(out1==NULL){ |
---|
| 575 | *out=dupMaps(&tmpMaps2); |
---|
| 576 | out1=*out; |
---|
| 577 | } |
---|
| 578 | else |
---|
| 579 | addMapsToMaps(&out1,tmpMaps2); |
---|
| 580 | freeMap(&tmpMaps2->content); |
---|
| 581 | free(tmpMaps2->content); |
---|
| 582 | tmpMaps2->content=NULL; |
---|
| 583 | freeMaps(&tmpMaps2); |
---|
| 584 | free(tmpMaps2); |
---|
| 585 | tmpMaps2=NULL; |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | else{ |
---|
| 589 | iotype* tmpIoType=getIoTypeFromElement(tmpInputs,tmpInputs->name, |
---|
| 590 | tmpMaps->content); |
---|
| 591 | if(type==0) { |
---|
| 592 | /** |
---|
| 593 | * In case of an Input maps, then add the minOccurs and maxOccurs to the |
---|
| 594 | * content map. |
---|
| 595 | */ |
---|
| 596 | map* tmpMap1=getMap(tmpInputs->content,"minOccurs"); |
---|
| 597 | if(tmpMap1!=NULL){ |
---|
| 598 | if(tmpMaps->content==NULL) |
---|
| 599 | tmpMaps->content=createMap("minOccurs",tmpMap1->value); |
---|
| 600 | else |
---|
| 601 | addToMap(tmpMaps->content,"minOccurs",tmpMap1->value); |
---|
| 602 | } |
---|
| 603 | map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs"); |
---|
| 604 | if(tmpMaxO!=NULL){ |
---|
| 605 | if(tmpMaps->content==NULL) |
---|
| 606 | tmpMaps->content=createMap("maxOccurs",tmpMaxO->value); |
---|
| 607 | else |
---|
| 608 | addToMap(tmpMaps->content,"maxOccurs",tmpMaxO->value); |
---|
| 609 | } |
---|
| 610 | map* tmpMaxMB=getMap(tmpInputs->content,"maximumMegabytes"); |
---|
| 611 | if(tmpMaxMB!=NULL){ |
---|
| 612 | if(tmpMaps->content==NULL) |
---|
| 613 | tmpMaps->content=createMap("maximumMegabytes",tmpMaxMB->value); |
---|
| 614 | else |
---|
| 615 | addToMap(tmpMaps->content,"maximumMegabytes",tmpMaxMB->value); |
---|
| 616 | } |
---|
| 617 | /** |
---|
| 618 | * Parsing BoundingBoxData, fill the following map and then add it to |
---|
| 619 | * the content map of the Input maps: |
---|
| 620 | * lowerCorner, upperCorner, srs and dimensions |
---|
| 621 | * cf. parseBoundingBox |
---|
| 622 | */ |
---|
| 623 | if(tmpInputs->format!=NULL && strcasecmp(tmpInputs->format,"BoundingBoxData")==0){ |
---|
| 624 | maps* tmpI=getMaps(*out,tmpInputs->name); |
---|
| 625 | if(tmpI!=NULL){ |
---|
| 626 | map* tmpV=getMap(tmpI->content,"value"); |
---|
| 627 | if(tmpV!=NULL){ |
---|
| 628 | char *tmpVS=strdup(tmpV->value); |
---|
| 629 | map* tmp=parseBoundingBox(tmpVS); |
---|
| 630 | free(tmpVS); |
---|
| 631 | map* tmpC=tmp; |
---|
| 632 | while(tmpC!=NULL){ |
---|
| 633 | addToMap(tmpMaps->content,tmpC->name,tmpC->value); |
---|
| 634 | tmpC=tmpC->next; |
---|
| 635 | } |
---|
| 636 | freeMap(&tmp); |
---|
| 637 | free(tmp); |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | if(tmpIoType!=NULL){ |
---|
| 644 | map* tmpContent=tmpIoType->content; |
---|
| 645 | map* cval=NULL; |
---|
| 646 | int hasPassed=-1; |
---|
| 647 | while(tmpContent!=NULL){ |
---|
| 648 | if((cval=getMap(tmpMaps->content,tmpContent->name))==NULL){ |
---|
| 649 | #ifdef DEBUG |
---|
| 650 | fprintf(stderr,"addDefaultValues %s => %s\n",tmpContent->name,tmpContent->value); |
---|
| 651 | #endif |
---|
| 652 | if(tmpMaps->content==NULL) |
---|
| 653 | tmpMaps->content=createMap(tmpContent->name,tmpContent->value); |
---|
| 654 | else |
---|
| 655 | addToMap(tmpMaps->content,tmpContent->name,tmpContent->value); |
---|
| 656 | |
---|
| 657 | if(hasPassed<0 && type==0 && getMap(tmpMaps->content,"isArray")!=NULL){ |
---|
| 658 | map* length=getMap(tmpMaps->content,"length"); |
---|
| 659 | int i; |
---|
| 660 | char *tcn=strdup(tmpContent->name); |
---|
| 661 | for(i=1;i<atoi(length->value);i++){ |
---|
| 662 | #ifdef DEBUG |
---|
| 663 | dumpMap(tmpMaps->content); |
---|
| 664 | fprintf(stderr,"addDefaultValues %s_%d => %s\n",tcn,i,tmpContent->value); |
---|
| 665 | #endif |
---|
| 666 | int len=strlen((char*) tcn); |
---|
| 667 | char *tmp1=(char *)malloc((len+10)*sizeof(char)); |
---|
| 668 | sprintf(tmp1,"%s_%d",tcn,i); |
---|
| 669 | #ifdef DEBUG |
---|
| 670 | fprintf(stderr,"addDefaultValues %s => %s\n",tmp1,tmpContent->value); |
---|
| 671 | #endif |
---|
| 672 | addToMap(tmpMaps->content,tmp1,tmpContent->value); |
---|
| 673 | free(tmp1); |
---|
| 674 | hasPassed=1; |
---|
| 675 | } |
---|
| 676 | free(tcn); |
---|
| 677 | } |
---|
| 678 | } |
---|
| 679 | tmpContent=tmpContent->next; |
---|
| 680 | } |
---|
| 681 | #ifdef USE_MS |
---|
| 682 | /** |
---|
| 683 | * check for useMapServer presence |
---|
| 684 | */ |
---|
| 685 | map* tmpCheck=getMap(tmpIoType->content,"useMapServer"); |
---|
| 686 | if(tmpCheck!=NULL){ |
---|
| 687 | // Get the default value |
---|
| 688 | tmpIoType=getIoTypeFromElement(tmpInputs,tmpInputs->name,NULL); |
---|
| 689 | tmpCheck=getMap(tmpMaps->content,"mimeType"); |
---|
| 690 | addToMap(tmpMaps->content,"requestedMimeType",tmpCheck->value); |
---|
| 691 | map* cursor=tmpIoType->content; |
---|
| 692 | while(cursor!=NULL){ |
---|
| 693 | addToMap(tmpMaps->content,cursor->name,cursor->value); |
---|
| 694 | cursor=cursor->next; |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | cursor=tmpInputs->content; |
---|
| 698 | while(cursor!=NULL){ |
---|
| 699 | if(strcasecmp(cursor->name,"Title")==0 || |
---|
| 700 | strcasecmp(cursor->name,"Abstract")==0) |
---|
| 701 | addToMap(tmpMaps->content,cursor->name,cursor->value); |
---|
| 702 | cursor=cursor->next; |
---|
| 703 | } |
---|
| 704 | } |
---|
| 705 | #endif |
---|
| 706 | } |
---|
| 707 | if(tmpMaps->content==NULL) |
---|
| 708 | tmpMaps->content=createMap("inRequest","true"); |
---|
| 709 | else |
---|
| 710 | addToMap(tmpMaps->content,"inRequest","true"); |
---|
| 711 | |
---|
| 712 | } |
---|
| 713 | tmpInputs=tmpInputs->next; |
---|
| 714 | } |
---|
| 715 | if(res!=NULL){ |
---|
| 716 | *err=res; |
---|
| 717 | return result; |
---|
| 718 | } |
---|
| 719 | return ""; |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | /** |
---|
| 723 | * Access the last error message returned by the OS when trying to dynamically |
---|
| 724 | * load a shared library. |
---|
| 725 | * |
---|
| 726 | * @return the last error message |
---|
| 727 | * @warning The character string returned from getLastErrorMessage resides |
---|
| 728 | * in a static buffer. The application should not write to this |
---|
| 729 | * buffer or attempt to free() it. |
---|
| 730 | */ |
---|
| 731 | char* getLastErrorMessage() { |
---|
| 732 | #ifdef WIN32 |
---|
| 733 | LPVOID lpMsgBuf; |
---|
| 734 | DWORD errCode = GetLastError(); |
---|
| 735 | static char msg[ERROR_MSG_MAX_LENGTH]; |
---|
| 736 | size_t i; |
---|
| 737 | |
---|
| 738 | DWORD length = FormatMessage( |
---|
| 739 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
---|
| 740 | FORMAT_MESSAGE_FROM_SYSTEM | |
---|
| 741 | FORMAT_MESSAGE_IGNORE_INSERTS, |
---|
| 742 | NULL, |
---|
| 743 | errCode, |
---|
| 744 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
---|
| 745 | (LPTSTR) &lpMsgBuf, |
---|
| 746 | 0, NULL ); |
---|
| 747 | |
---|
| 748 | #ifdef UNICODE |
---|
| 749 | wcstombs_s( &i, msg, ERROR_MSG_MAX_LENGTH, |
---|
| 750 | (wchar_t*) lpMsgBuf, _TRUNCATE ); |
---|
| 751 | #else |
---|
| 752 | strcpy_s( msg, ERROR_MSG_MAX_LENGTH, |
---|
| 753 | (char *) lpMsgBuf ); |
---|
| 754 | #endif |
---|
| 755 | LocalFree(lpMsgBuf); |
---|
| 756 | |
---|
| 757 | return msg; |
---|
| 758 | #else |
---|
| 759 | return dlerror(); |
---|
| 760 | #endif |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | |
---|
| 764 | |
---|