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