[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 | |
---|
[877] | 25 | #include <openssl/md5.h> |
---|
| 26 | #include <openssl/evp.h> |
---|
[642] | 27 | #include "caching.h" |
---|
| 28 | #include "service.h" |
---|
| 29 | #include "service_internal.h" |
---|
| 30 | #include "response_print.h" |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Compute md5 |
---|
| 34 | * |
---|
| 35 | * @param url the char* |
---|
| 36 | * @return a char* representing the md5 of the url |
---|
[781] | 37 | * @warning make sure to free resources returned by this function |
---|
[642] | 38 | */ |
---|
| 39 | char* getMd5(char* url){ |
---|
| 40 | EVP_MD_CTX md5ctx; |
---|
| 41 | char* fresult=(char*)malloc((EVP_MAX_MD_SIZE+1)*sizeof(char)); |
---|
| 42 | unsigned char result[EVP_MAX_MD_SIZE]; |
---|
| 43 | unsigned int len; |
---|
| 44 | EVP_DigestInit(&md5ctx, EVP_md5()); |
---|
| 45 | EVP_DigestUpdate(&md5ctx, url, strlen(url)); |
---|
| 46 | EVP_DigestFinal_ex(&md5ctx,result,&len); |
---|
| 47 | EVP_MD_CTX_cleanup(&md5ctx); |
---|
| 48 | int i; |
---|
| 49 | for(i = 0; i < len; i++){ |
---|
| 50 | if(i>0){ |
---|
| 51 | char *tmp=strdup(fresult); |
---|
| 52 | sprintf(fresult,"%s%02x", tmp,result[i]); |
---|
| 53 | free(tmp); |
---|
| 54 | } |
---|
| 55 | else |
---|
| 56 | sprintf(fresult,"%02x",result[i]); |
---|
| 57 | } |
---|
| 58 | return fresult; |
---|
| 59 | } |
---|
| 60 | |
---|
[877] | 61 | /** |
---|
| 62 | * Compute md5 of a file |
---|
| 63 | * |
---|
| 64 | * @param file the char* |
---|
| 65 | * @return a char* representing the md5 of the url |
---|
| 66 | * @warning make sure to free resources returned by this function |
---|
| 67 | */ |
---|
| 68 | char* getMd5f(char* file){ |
---|
| 69 | EVP_MD_CTX md5ctx; |
---|
| 70 | char* fresult=(char*)malloc((EVP_MAX_MD_SIZE+1)*sizeof(char)); |
---|
| 71 | unsigned char result[EVP_MAX_MD_SIZE]; |
---|
| 72 | unsigned int len; |
---|
| 73 | int bytes; |
---|
| 74 | unsigned char data[1024]; |
---|
| 75 | FILE *inFile = fopen (file, "rb"); |
---|
| 76 | EVP_DigestInit(&md5ctx, EVP_md5()); |
---|
| 77 | while ((bytes = fread (data, 1, 1024, inFile)) != 0) |
---|
| 78 | EVP_DigestUpdate(&md5ctx, data, bytes); |
---|
| 79 | EVP_DigestFinal_ex(&md5ctx,result,&len); |
---|
| 80 | EVP_MD_CTX_cleanup(&md5ctx); |
---|
| 81 | int i; |
---|
| 82 | for(i = 0; i < len; i++){ |
---|
| 83 | if(i>0){ |
---|
| 84 | char *tmp=zStrdup(fresult); |
---|
| 85 | sprintf(fresult,"%s%02x", tmp,result[i]); |
---|
| 86 | free(tmp); |
---|
| 87 | } |
---|
| 88 | else |
---|
| 89 | sprintf(fresult,"%02x",result[i]); |
---|
| 90 | } |
---|
| 91 | fclose (inFile); |
---|
| 92 | return fresult; |
---|
| 93 | } |
---|
[642] | 94 | |
---|
[877] | 95 | |
---|
| 96 | |
---|
[642] | 97 | /** |
---|
[854] | 98 | * Create a URL by appending every request header listed in the security |
---|
| 99 | * section.This imply that the URL will contain any authentication |
---|
| 100 | * informations that should be fowarded to the server from which de input |
---|
| 101 | * was download. |
---|
| 102 | * @param conf the main configuration maps |
---|
| 103 | * @param request the URL to transform. |
---|
| 104 | * @return a char* that contain the original URL plus potential header (only for |
---|
| 105 | * hosts that are not shared). |
---|
| 106 | * @warning Be sure to free the memory returned by this function. |
---|
| 107 | */ |
---|
| 108 | char* getFilenameForRequest(maps* conf, const char* request){ |
---|
| 109 | map* passThrough=getMapFromMaps(conf,"security","attributes"); |
---|
| 110 | map* targetHosts=getMapFromMaps(conf,"security","hosts"); |
---|
| 111 | char* passedHeader[10]; |
---|
| 112 | int cnt=0; |
---|
| 113 | char *res=zStrdup(request); |
---|
| 114 | char *toAppend=NULL; |
---|
| 115 | if(passThrough!=NULL && targetHosts!=NULL){ |
---|
| 116 | char *tmp=zStrdup(passThrough->value); |
---|
| 117 | char *token, *saveptr; |
---|
| 118 | token = strtok_r (tmp, ",", &saveptr); |
---|
| 119 | int i; |
---|
| 120 | if((strstr(targetHosts->value,"*")!=NULL || isProtectedHost(targetHosts->value,request)==1) && strncasecmp(getProvenance(conf,request),"SHARED",6)!=0){ |
---|
| 121 | while (token != NULL){ |
---|
| 122 | int length=strlen(token)+6; |
---|
| 123 | char* tmp1=(char*)malloc(length*sizeof(char)); |
---|
| 124 | map* tmpMap; |
---|
| 125 | snprintf(tmp1,6,"HTTP_"); |
---|
| 126 | int j; |
---|
| 127 | for(j=0;token[j]!='\0';j++){ |
---|
| 128 | if(token[j]!='-') |
---|
| 129 | tmp1[5+j]=toupper(token[j]); |
---|
| 130 | else |
---|
| 131 | tmp1[5+j]='_'; |
---|
| 132 | tmp1[5+j+1]='\0'; |
---|
| 133 | } |
---|
| 134 | tmpMap = getMapFromMaps(conf,"renv",tmp1); |
---|
| 135 | if(tmpMap!=NULL){ |
---|
| 136 | if(toAppend==NULL){ |
---|
| 137 | toAppend=(char*)malloc((strlen(tmpMap->value)+1)*sizeof(char)); |
---|
| 138 | sprintf(toAppend,"%s",tmpMap->value); |
---|
| 139 | }else{ |
---|
| 140 | char *tmp3=zStrdup(toAppend); |
---|
| 141 | toAppend=(char*)realloc(toAppend,(strlen(tmpMap->value)+strlen(tmp3)+2)*sizeof(char)); |
---|
| 142 | sprintf(toAppend,"%s,%s",tmp3,tmpMap->value); |
---|
| 143 | free(tmp3); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | free(tmp1); |
---|
| 147 | cnt+=1; |
---|
| 148 | token = strtok_r (NULL, ",", &saveptr); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | free(tmp); |
---|
| 152 | } |
---|
| 153 | if(toAppend!=NULL){ |
---|
| 154 | char *tmp3=zStrdup(res); |
---|
| 155 | res=(char*)realloc(res,(strlen(tmp3)+strlen(toAppend)+1)*sizeof(char)); |
---|
| 156 | sprintf(res,"%s%s",tmp3,toAppend); |
---|
| 157 | free(tmp3); |
---|
| 158 | free(toAppend); |
---|
| 159 | } |
---|
| 160 | return res; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
[642] | 164 | * Cache a file for a given request. |
---|
| 165 | * For each cached file, the are two files stored, a .zca and a .zcm containing |
---|
| 166 | * the downloaded content and the mimeType respectively. |
---|
| 167 | * |
---|
| 168 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 169 | * @param request the url used too fetch the content |
---|
| 170 | * @param content the downloaded content |
---|
| 171 | * @param mimeType the content mimeType |
---|
| 172 | * @param length the content size |
---|
| 173 | * @param filepath a buffer for storing the path of the cached file; may be NULL |
---|
| 174 | * @param max_path the size of the allocated filepath buffer |
---|
| 175 | */ |
---|
[863] | 176 | void cacheFile(maps* conf,char* request,char* mimeType,int length,char* filename){ |
---|
| 177 | map* tmp=getMapFromMaps(conf,"main","cacheDir"); |
---|
| 178 | char contentr[4096]; |
---|
[877] | 179 | char* md5fstr=NULL; |
---|
[863] | 180 | int cred=0; |
---|
| 181 | if(tmp!=NULL){ |
---|
| 182 | char* myRequest=getFilenameForRequest(conf,request); |
---|
| 183 | char* md5str=getMd5(myRequest); |
---|
| 184 | free(myRequest); |
---|
| 185 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 186 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
| 187 | zooLock* lck=lockFile(conf,fname,'w'); |
---|
| 188 | if(lck!=NULL){ |
---|
| 189 | #ifdef DEBUG |
---|
| 190 | fprintf(stderr,"Cache list : %s\n",fname); |
---|
| 191 | fflush(stderr); |
---|
| 192 | #endif |
---|
| 193 | FILE* fi=fopen(filename,"rb"); |
---|
| 194 | FILE* fo=fopen(fname,"w+"); |
---|
| 195 | if(fo==NULL){ |
---|
| 196 | #ifdef DEBUG |
---|
| 197 | fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); |
---|
| 198 | #endif |
---|
| 199 | unlockFile(conf,lck); |
---|
| 200 | return; |
---|
| 201 | } |
---|
| 202 | if(fi==NULL){ |
---|
| 203 | #ifdef DEBUG |
---|
| 204 | fprintf (stderr, "Failed to open %s for reading: %s\n",filename, strerror(errno)); |
---|
| 205 | #endif |
---|
| 206 | unlockFile(conf,lck); |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | memset(contentr,0,4096); |
---|
| 210 | while((cred=fread(contentr,sizeof(char),4096,fi))>0){ |
---|
| 211 | fwrite(contentr,sizeof(char),cred,fo); |
---|
| 212 | fflush(fo); |
---|
| 213 | memset(contentr,0,4096); |
---|
| 214 | } |
---|
| 215 | unlockFile(conf,lck); |
---|
| 216 | fclose(fo); |
---|
| 217 | fclose(fi); |
---|
[877] | 218 | |
---|
| 219 | // Store mimeType |
---|
[863] | 220 | sprintf(fname,"%s/%s.zcm",tmp->value,md5str); |
---|
| 221 | fo=fopen(fname,"w+"); |
---|
| 222 | #ifdef DEBUG |
---|
| 223 | fprintf(stderr,"MIMETYPE: %s\n",mimeType); |
---|
| 224 | #endif |
---|
| 225 | fwrite(mimeType,sizeof(char),strlen(mimeType),fo); |
---|
| 226 | fclose(fo); |
---|
| 227 | |
---|
[877] | 228 | // Store provenance |
---|
[863] | 229 | sprintf(fname,"%s/%s.zcp",tmp->value,md5str); |
---|
| 230 | fo=fopen(fname,"w+"); |
---|
| 231 | char* origin=getProvenance(conf,request); |
---|
| 232 | #ifdef DEBUG |
---|
| 233 | fprintf(stderr,"ORIGIN: %s\n",mimeType); |
---|
| 234 | #endif |
---|
| 235 | fwrite(origin,sizeof(char),strlen(origin),fo); |
---|
| 236 | fclose(fo); |
---|
| 237 | |
---|
[877] | 238 | // Store md5 |
---|
| 239 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
| 240 | md5fstr=getMd5f(fname); |
---|
| 241 | sprintf(fname,"%s/%s.zmd",tmp->value,md5str); |
---|
| 242 | fo=fopen(fname,"w+"); |
---|
| 243 | #ifdef DEBUG |
---|
| 244 | fprintf(stderr,"MD5: %s\n",md5fstr); |
---|
| 245 | #endif |
---|
| 246 | fwrite(md5fstr,sizeof(char),strlen(md5fstr),fo); |
---|
| 247 | free(md5fstr); |
---|
| 248 | fclose(fo); |
---|
| 249 | |
---|
[863] | 250 | free(md5str); |
---|
| 251 | free(fname); |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | /** |
---|
| 257 | * Cache a file for a given request. |
---|
| 258 | * For each cached file, the are two files stored, a .zca and a .zcm containing |
---|
| 259 | * the downloaded content and the mimeType respectively. |
---|
| 260 | * |
---|
| 261 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 262 | * @param request the url used too fetch the content |
---|
| 263 | * @param content the downloaded content |
---|
| 264 | * @param mimeType the content mimeType |
---|
| 265 | * @param length the content size |
---|
| 266 | * @param filepath a buffer for storing the path of the cached file; may be NULL |
---|
| 267 | * @param max_path the size of the allocated filepath buffer |
---|
| 268 | */ |
---|
[642] | 269 | void addToCache(maps* conf,char* request,char* content,char* mimeType,int length, |
---|
| 270 | char* filepath, size_t max_path){ |
---|
| 271 | map* tmp=getMapFromMaps(conf,"main","cacheDir"); |
---|
| 272 | if(tmp!=NULL){ |
---|
[854] | 273 | char* myRequest=getFilenameForRequest(conf,request); |
---|
| 274 | char* md5str=getMd5(myRequest); |
---|
| 275 | free(myRequest); |
---|
[642] | 276 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 277 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
[854] | 278 | zooLock* lck=lockFile(conf,fname,'w'); |
---|
| 279 | if(lck!=NULL){ |
---|
[642] | 280 | #ifdef DEBUG |
---|
[854] | 281 | fprintf(stderr,"Cache list : %s\n",fname); |
---|
| 282 | fflush(stderr); |
---|
[642] | 283 | #endif |
---|
[854] | 284 | FILE* fo=fopen(fname,"w+"); |
---|
| 285 | if(fo==NULL){ |
---|
[642] | 286 | #ifdef DEBUG |
---|
[854] | 287 | fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); |
---|
[642] | 288 | #endif |
---|
[854] | 289 | filepath = NULL; |
---|
| 290 | unlockFile(conf,lck); |
---|
| 291 | return; |
---|
| 292 | } |
---|
| 293 | fwrite(content,sizeof(char),length,fo); |
---|
| 294 | unlockFile(conf,lck); |
---|
| 295 | fclose(fo); |
---|
[642] | 296 | |
---|
[854] | 297 | if (filepath != NULL) { |
---|
| 298 | strncpy(filepath, fname, max_path); |
---|
| 299 | } |
---|
[642] | 300 | |
---|
[854] | 301 | sprintf(fname,"%s/%s.zcm",tmp->value,md5str); |
---|
| 302 | fo=fopen(fname,"w+"); |
---|
[642] | 303 | #ifdef DEBUG |
---|
[854] | 304 | fprintf(stderr,"MIMETYPE: %s\n",mimeType); |
---|
[642] | 305 | #endif |
---|
[854] | 306 | fwrite(mimeType,sizeof(char),strlen(mimeType),fo); |
---|
| 307 | fclose(fo); |
---|
[642] | 308 | |
---|
[854] | 309 | sprintf(fname,"%s/%s.zcp",tmp->value,md5str); |
---|
| 310 | fo=fopen(fname,"w+"); |
---|
| 311 | char* origin=getProvenance(conf,request); |
---|
| 312 | #ifdef DEBUG |
---|
| 313 | fprintf(stderr,"ORIGIN: %s\n",mimeType); |
---|
| 314 | #endif |
---|
| 315 | fwrite(origin,sizeof(char),strlen(origin),fo); |
---|
| 316 | fclose(fo); |
---|
| 317 | |
---|
| 318 | free(md5str); |
---|
| 319 | free(fname); |
---|
| 320 | } |
---|
[642] | 321 | } |
---|
| 322 | else { |
---|
[854] | 323 | filepath = NULL; |
---|
[642] | 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | /** |
---|
| 328 | * Verify if a url is available in the cache |
---|
| 329 | * |
---|
| 330 | * @param conf the maps containing the settings of the main.cfg file |
---|
| 331 | * @param request the url |
---|
| 332 | * @return the full name of the cached file if any, NULL in other case |
---|
[781] | 333 | * @warning make sure to free resources returned by this function (if not NULL) |
---|
[642] | 334 | */ |
---|
| 335 | char* isInCache(maps* conf,char* request){ |
---|
| 336 | map* tmpM=getMapFromMaps(conf,"main","cacheDir"); |
---|
| 337 | if(tmpM!=NULL){ |
---|
[872] | 338 | if(strncasecmp(request,"file://",7)==0){ |
---|
| 339 | char* tmpStr=zStrdup(request+7); |
---|
| 340 | fprintf(stderr,"**** %s %d %s \n",__FILE__,__LINE__,tmpStr); |
---|
| 341 | return tmpStr; |
---|
| 342 | } |
---|
| 343 | else{ |
---|
| 344 | |
---|
| 345 | char* myRequest=getFilenameForRequest(conf,request); |
---|
| 346 | char* md5str=getMd5(myRequest); |
---|
| 347 | free(myRequest); |
---|
[642] | 348 | #ifdef DEBUG |
---|
[872] | 349 | fprintf(stderr,"MD5STR : (%s)\n\n",md5str); |
---|
[642] | 350 | #endif |
---|
[872] | 351 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmpM->value)+strlen(md5str)+6)); |
---|
| 352 | sprintf(fname,"%s/%s.zca",tmpM->value,md5str); |
---|
| 353 | struct stat f_status; |
---|
| 354 | int s=stat(fname, &f_status); |
---|
| 355 | if(s==0 && f_status.st_size>0){ |
---|
| 356 | free(md5str); |
---|
| 357 | return fname; |
---|
| 358 | } |
---|
[642] | 359 | free(md5str); |
---|
[872] | 360 | free(fname); |
---|
[642] | 361 | } |
---|
| 362 | } |
---|
| 363 | return NULL; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | /** |
---|
[790] | 367 | * Read the downloaded file for a specific input |
---|
| 368 | * |
---|
| 369 | * @param m the maps containing the settings of the main.cfg file |
---|
| 370 | * @param in the input |
---|
| 371 | * @param index the input index |
---|
| 372 | * @param hInternet the internet connection |
---|
[797] | 373 | * @param error the error map pointer |
---|
| 374 | * @return 0 in case of success, -1 in case of failure |
---|
[790] | 375 | */ |
---|
[797] | 376 | int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){ |
---|
[839] | 377 | |
---|
[863] | 378 | int shouldClean=-1; |
---|
[790] | 379 | map* tmp1; |
---|
| 380 | char sindex[5]; |
---|
| 381 | maps* content=*in; |
---|
| 382 | map* length=getMap(content->content,"length"); |
---|
[863] | 383 | map* memUse=getMapFromMaps(*m,"main","memory"); |
---|
[790] | 384 | if(length==NULL){ |
---|
| 385 | length=createMap("length","1"); |
---|
| 386 | shouldClean=1; |
---|
| 387 | } |
---|
| 388 | for(int i=0;i<atoi(length->value);i++){ |
---|
| 389 | char* fcontent; |
---|
| 390 | char *mimeType=NULL; |
---|
| 391 | int fsize=0; |
---|
| 392 | char cname[15]; |
---|
| 393 | char vname[11]; |
---|
| 394 | char vname1[11]; |
---|
| 395 | char sname[9]; |
---|
| 396 | char mname[15]; |
---|
| 397 | char icname[14]; |
---|
| 398 | char xname[16]; |
---|
[822] | 399 | char bname[8]; |
---|
| 400 | char hname[11]; |
---|
[790] | 401 | char oname[12]; |
---|
[863] | 402 | char ufile[12]; |
---|
[790] | 403 | if(*index>0) |
---|
| 404 | sprintf(vname1,"value_%d",*index); |
---|
| 405 | else |
---|
| 406 | sprintf(vname1,"value"); |
---|
| 407 | |
---|
| 408 | if(i>0){ |
---|
[860] | 409 | sprintf(cname,"cache_file_%d",i); |
---|
[790] | 410 | tmp1=getMap(content->content,cname); |
---|
| 411 | sprintf(vname,"value_%d",i); |
---|
| 412 | sprintf(sname,"size_%d",i); |
---|
| 413 | sprintf(mname,"mimeType_%d",i); |
---|
| 414 | sprintf(icname,"isCached_%d",i); |
---|
| 415 | sprintf(xname,"Reference_%d",i); |
---|
[822] | 416 | sprintf(bname,"body_%d",i); |
---|
| 417 | sprintf(hname,"headers_%d",i); |
---|
[790] | 418 | sprintf(oname,"Order_%d",i); |
---|
[863] | 419 | sprintf(ufile,"use_file_%d",i); |
---|
[790] | 420 | }else{ |
---|
| 421 | sprintf(cname,"cache_file"); |
---|
| 422 | sprintf(vname,"value"); |
---|
| 423 | sprintf(sname,"size"); |
---|
| 424 | sprintf(mname,"mimeType"); |
---|
| 425 | sprintf(icname,"isCached"); |
---|
| 426 | sprintf(xname,"Reference"); |
---|
[822] | 427 | sprintf(bname,"body"); |
---|
[854] | 428 | sprintf(hname,"headers"); |
---|
[790] | 429 | sprintf(oname,"Order"); |
---|
[863] | 430 | sprintf(ufile,"use_file"); |
---|
[790] | 431 | } |
---|
| 432 | |
---|
| 433 | map* tmap=getMap(content->content,oname); |
---|
| 434 | sprintf(sindex,"%d",*index+1); |
---|
| 435 | if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){ |
---|
[839] | 436 | |
---|
[863] | 437 | if(getMap(content->content,icname)==NULL) { |
---|
| 438 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 439 | fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); |
---|
| 440 | if(fcontent == NULL){ |
---|
| 441 | errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); |
---|
| 442 | return -1; |
---|
| 443 | } |
---|
| 444 | size_t dwRead; |
---|
| 445 | InternetReadFile(hInternet->ihandle[*index], |
---|
| 446 | (LPVOID)fcontent, |
---|
| 447 | hInternet->ihandle[*index].nDataLen, |
---|
| 448 | &dwRead); |
---|
| 449 | fcontent[hInternet->ihandle[*index].nDataLen]=0; |
---|
[790] | 450 | } |
---|
| 451 | fsize=hInternet->ihandle[*index].nDataLen; |
---|
| 452 | if(hInternet->ihandle[*index].mimeType==NULL) |
---|
| 453 | mimeType=zStrdup("none"); |
---|
| 454 | else |
---|
| 455 | mimeType=zStrdup(hInternet->ihandle[*index].mimeType); |
---|
| 456 | |
---|
| 457 | map* tmpMap=getMapOrFill(&(*in)->content,vname,""); |
---|
[863] | 458 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 459 | free(tmpMap->value); |
---|
| 460 | tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); |
---|
| 461 | if(tmpMap->value==NULL){ |
---|
| 462 | return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); |
---|
| 463 | } |
---|
| 464 | memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); |
---|
| 465 | }else |
---|
| 466 | addToMap((*in)->content,ufile,"true"); |
---|
[797] | 467 | if(hInternet->ihandle[*index].code!=200){ |
---|
| 468 | char *error_rep_str=_("Unable to download the file for the input <%s>, response code was : %d."); |
---|
| 469 | char *error_msg=(char*)malloc((strlen(error_rep_str)+strlen(content->name)+4)*sizeof(char)); |
---|
| 470 | sprintf(error_msg,error_rep_str,content->name,hInternet->ihandle[*index].code); |
---|
| 471 | if(*error==NULL){ |
---|
| 472 | *error=createMap("text",error_msg); |
---|
| 473 | addToMap(*error,"locator",content->name); |
---|
| 474 | addToMap(*error,"code","InvalidParameterValue"); |
---|
| 475 | }else{ |
---|
| 476 | int nb=1; |
---|
| 477 | map* tmpMap=getMap(*error,"length"); |
---|
| 478 | if(tmpMap!=NULL) |
---|
| 479 | nb=atoi(tmpMap->value); |
---|
| 480 | setMapArray(*error,"text",nb,error_msg); |
---|
| 481 | setMapArray(*error,"locator",nb,content->name); |
---|
| 482 | setMapArray(*error,"code",nb,"InvalidParameterValue"); |
---|
| 483 | } |
---|
| 484 | return -1; |
---|
| 485 | } |
---|
[790] | 486 | |
---|
| 487 | char ltmp1[256]; |
---|
| 488 | sprintf(ltmp1,"%d",fsize); |
---|
| 489 | map* tmp=getMapFromMaps(*m,"main","cacheDir"); |
---|
[822] | 490 | char *request=NULL; |
---|
[790] | 491 | if(tmp!=NULL){ |
---|
[822] | 492 | map* tmp2; |
---|
| 493 | char* md5str=NULL; |
---|
| 494 | if((tmp2=getMap(content->content,bname))!=NULL){ |
---|
| 495 | char *tmpStr=(char*)malloc((strlen(tmp1->value)+strlen(tmp2->value)+1)*sizeof(char)); |
---|
| 496 | sprintf(tmpStr,"%s%s",tmp1->value,tmp2->value); |
---|
| 497 | if((tmp2=getMap(content->content,"headers"))!=NULL){ |
---|
| 498 | char *tmpStr2=zStrdup(tmpStr); |
---|
[839] | 499 | free(tmpStr); |
---|
[822] | 500 | tmpStr=(char*)malloc((strlen(tmpStr2)+strlen(tmp2->value)+1)*sizeof(char)); |
---|
| 501 | sprintf(tmpStr,"%s%s",tmpStr2,tmp2->value); |
---|
| 502 | free(tmpStr2); |
---|
| 503 | } |
---|
| 504 | md5str=getMd5(tmpStr); |
---|
| 505 | request=zStrdup(tmpStr); |
---|
| 506 | free(tmpStr); |
---|
| 507 | }else{ |
---|
[854] | 508 | char *myRequest=getFilenameForRequest(*m,tmp1->value); |
---|
| 509 | md5str=getMd5(myRequest); |
---|
[822] | 510 | request=zStrdup(tmp1->value); |
---|
[854] | 511 | free(myRequest); |
---|
[822] | 512 | } |
---|
[790] | 513 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 514 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
| 515 | addToMap((*in)->content,cname,fname); |
---|
| 516 | free(fname); |
---|
| 517 | } |
---|
| 518 | addToMap((*in)->content,sname,ltmp1); |
---|
| 519 | addToMap((*in)->content,mname,mimeType); |
---|
[863] | 520 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 521 | addToCache(*m,request,fcontent,mimeType,fsize, NULL, 0); |
---|
| 522 | free(fcontent); |
---|
| 523 | }else{ |
---|
| 524 | addToMap((*in)->content,ufile,"true"); |
---|
| 525 | cacheFile(*m,request,mimeType,fsize,hInternet->ihandle[*index].filename); |
---|
| 526 | } |
---|
[790] | 527 | free(mimeType); |
---|
[822] | 528 | free(request); |
---|
[839] | 529 | (*index)++; |
---|
[790] | 530 | } |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | if(shouldClean>0){ |
---|
| 534 | freeMap(&length); |
---|
| 535 | free(length); |
---|
| 536 | } |
---|
| 537 | return 0; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | /** |
---|
[642] | 541 | * Effectively run all the HTTP requests in the queue |
---|
| 542 | * |
---|
| 543 | * @param m the maps containing the settings of the main.cfg file |
---|
| 544 | * @param inputs the maps containing the inputs (defined in the requests+added |
---|
| 545 | * per default based on the zcfg file) |
---|
| 546 | * @param hInternet the HINTERNET pointer |
---|
[797] | 547 | * @param error the error map pointer |
---|
| 548 | * @return 0 on success, -1 on failure |
---|
[642] | 549 | */ |
---|
[797] | 550 | int runHttpRequests(maps** m,maps** inputs,HINTERNET* hInternet,map** error){ |
---|
| 551 | int hasAFailure=0; |
---|
[790] | 552 | if(hInternet!=NULL && hInternet->nb>0){ |
---|
[817] | 553 | AddHeaderEntries(hInternet,*m); |
---|
[642] | 554 | processDownloads(hInternet); |
---|
| 555 | maps* content=*inputs; |
---|
| 556 | int index=0; |
---|
| 557 | while(content!=NULL){ |
---|
[790] | 558 | if(content->child!=NULL){ |
---|
| 559 | maps* cursor=content->child; |
---|
| 560 | while(cursor!=NULL){ |
---|
[797] | 561 | int red=readCurrentInput(m,&cursor,&index,hInternet,error); |
---|
| 562 | if(red<0) |
---|
| 563 | hasAFailure=red; |
---|
[790] | 564 | cursor=cursor->next; |
---|
[642] | 565 | } |
---|
| 566 | } |
---|
[797] | 567 | else{ |
---|
| 568 | int red=readCurrentInput(m,&content,&index,hInternet,error); |
---|
| 569 | if(red<0) |
---|
| 570 | hasAFailure=red; |
---|
| 571 | } |
---|
[642] | 572 | content=content->next; |
---|
[797] | 573 | } |
---|
[642] | 574 | } |
---|
[797] | 575 | return hasAFailure; |
---|
[642] | 576 | } |
---|
| 577 | |
---|
| 578 | /** |
---|
| 579 | * Add a request in the download queue |
---|
| 580 | * |
---|
| 581 | * @param m the maps containing the settings of the main.cfg file |
---|
| 582 | * @param url the url to add to the queue |
---|
| 583 | */ |
---|
| 584 | void addRequestToQueue(maps** m,HINTERNET* hInternet,const char* url,bool req){ |
---|
| 585 | hInternet->waitingRequests[hInternet->nb]=strdup(url); |
---|
| 586 | if(req) |
---|
[863] | 587 | InternetOpenUrl(hInternet,hInternet->waitingRequests[hInternet->nb],NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0,*m); |
---|
[642] | 588 | maps *oreq=getMaps(*m,"orequests"); |
---|
| 589 | if(oreq==NULL){ |
---|
[790] | 590 | oreq=createMaps("orequests"); |
---|
[642] | 591 | oreq->content=createMap("value",url); |
---|
| 592 | addMapsToMaps(m,oreq); |
---|
| 593 | freeMaps(&oreq); |
---|
| 594 | free(oreq); |
---|
| 595 | }else{ |
---|
[839] | 596 | setMapArray(oreq->content,"value",hInternet->nb,url); |
---|
[642] | 597 | } |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | /** |
---|
| 601 | * Try to load file from cache or download a remote file if not in cache |
---|
| 602 | * |
---|
| 603 | * @param m the maps containing the settings of the main.cfg file |
---|
| 604 | * @param content the map to update |
---|
| 605 | * @param hInternet the HINTERNET pointer |
---|
| 606 | * @param url the url to fetch |
---|
| 607 | * @return 0 |
---|
| 608 | */ |
---|
| 609 | int loadRemoteFile(maps** m,map** content,HINTERNET* hInternet,char *url){ |
---|
[738] | 610 | char* fcontent = NULL; |
---|
[642] | 611 | char* cached=isInCache(*m,url); |
---|
| 612 | char *mimeType=NULL; |
---|
| 613 | int fsize=0; |
---|
[863] | 614 | map* memUse=getMapFromMaps(*m,"main","memory"); |
---|
[642] | 615 | |
---|
| 616 | map* t=getMap(*content,"xlink:href"); |
---|
| 617 | if(t==NULL){ |
---|
| 618 | t=getMap((*content),"href"); |
---|
| 619 | addToMap(*content,"xlink:href",url); |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | if(cached!=NULL){ |
---|
| 623 | struct stat f_status; |
---|
| 624 | int s=stat(cached, &f_status); |
---|
| 625 | if(s==0){ |
---|
[854] | 626 | zooLock* lck=lockFile(*m,cached,'r'); |
---|
| 627 | if(lck==NULL) |
---|
| 628 | return -1; |
---|
[642] | 629 | fsize=f_status.st_size; |
---|
[863] | 630 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 631 | fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 632 | FILE* f=fopen(cached,"rb"); |
---|
| 633 | if(f!=NULL){ |
---|
| 634 | fread(fcontent,f_status.st_size,1,f); |
---|
| 635 | fcontent[fsize]=0; |
---|
| 636 | fclose(f); |
---|
| 637 | } |
---|
| 638 | } |
---|
[642] | 639 | addToMap(*content,"cache_file",cached); |
---|
[854] | 640 | unlockFile(*m,lck); |
---|
[642] | 641 | } |
---|
| 642 | cached[strlen(cached)-1]='m'; |
---|
| 643 | s=stat(cached, &f_status); |
---|
| 644 | if(s==0){ |
---|
[854] | 645 | zooLock* lck=lockFile(*m,cached,'r'); |
---|
| 646 | if(lck==NULL) |
---|
| 647 | return -1; |
---|
[642] | 648 | mimeType=(char*)malloc(sizeof(char)*(f_status.st_size+1)); |
---|
| 649 | FILE* f=fopen(cached,"rb"); |
---|
| 650 | fread(mimeType,f_status.st_size,1,f); |
---|
| 651 | mimeType[f_status.st_size]=0; |
---|
| 652 | fclose(f); |
---|
[854] | 653 | unlockFile(*m,lck); |
---|
[642] | 654 | } |
---|
| 655 | }else{ |
---|
| 656 | addRequestToQueue(m,hInternet,url,true); |
---|
| 657 | return 0; |
---|
| 658 | } |
---|
| 659 | if(fsize==0){ |
---|
| 660 | return errorException(*m, _("Unable to download the file."), "InternalError",NULL); |
---|
| 661 | } |
---|
| 662 | if(mimeType!=NULL){ |
---|
| 663 | addToMap(*content,"fmimeType",mimeType); |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | map* tmpMap=getMapOrFill(content,"value",""); |
---|
[863] | 667 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ |
---|
| 668 | free(tmpMap->value); |
---|
| 669 | tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); |
---|
| 670 | if(tmpMap->value==NULL || fcontent == NULL) |
---|
| 671 | return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); |
---|
| 672 | memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); |
---|
| 673 | } |
---|
| 674 | |
---|
[642] | 675 | char ltmp1[256]; |
---|
| 676 | sprintf(ltmp1,"%d",fsize); |
---|
| 677 | addToMap(*content,"size",ltmp1); |
---|
| 678 | if(cached==NULL){ |
---|
[863] | 679 | if(memUse!=NULL && strcasecmp(memUse->value,"load")==0) |
---|
| 680 | addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); |
---|
| 681 | else |
---|
| 682 | cacheFile(*m,url,mimeType,fsize,hInternet->ihandle[hInternet->nb-1].filename); |
---|
[642] | 683 | } |
---|
| 684 | else{ |
---|
| 685 | addToMap(*content,"isCached","true"); |
---|
| 686 | map* tmp=getMapFromMaps(*m,"main","cacheDir"); |
---|
| 687 | if(tmp!=NULL){ |
---|
| 688 | map *c=getMap((*content),"xlink:href"); |
---|
[872] | 689 | if(strncasecmp(c->value,"file://",7)!=0){ |
---|
| 690 | char *myRequest=getFilenameForRequest(*m,c->value); |
---|
| 691 | char* md5str=getMd5(myRequest); |
---|
| 692 | free(myRequest); |
---|
| 693 | char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); |
---|
| 694 | sprintf(fname,"%s/%s.zca",tmp->value,md5str); |
---|
| 695 | addToMap(*content,"cache_file",fname); |
---|
| 696 | free(fname); |
---|
| 697 | free(md5str); |
---|
| 698 | } |
---|
[642] | 699 | } |
---|
| 700 | } |
---|
| 701 | free(fcontent); |
---|
| 702 | free(mimeType); |
---|
| 703 | free(cached); |
---|
| 704 | return 0; |
---|
| 705 | } |
---|