Changeset 917 for trunk/zoo-project/zoo-kernel/caching.c
- Timestamp:
- May 7, 2019, 2:17:08 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
False
/branches/prototype-v0 merged eligible
-
Property
svn:mergeinfo
set to
False
-
trunk/zoo-project/zoo-kernel/caching.c
r837 r917 23 23 */ 24 24 25 #include <openssl/md5.h> 26 #include <openssl/evp.h> 25 27 #include "caching.h" 26 28 #include "service.h" 27 29 #include "service_internal.h" 28 30 #include "response_print.h" 29 #include <openssl/md5.h> 30 #include <openssl/hmac.h> 31 #include <openssl/evp.h> 32 #include <openssl/bio.h> 33 #include <openssl/buffer.h> 34 31 #ifdef MS_FORCE_LOCAL_FILE_USE 32 #include "ogr_api.h" 33 #include "mapserver.h" 34 #endif 35 35 /** 36 36 * Compute md5 … … 41 41 */ 42 42 char* getMd5(char* url){ 43 EVP_MD_CTX md5ctx;43 EVP_MD_CTX *md5ctx=EVP_MD_CTX_create(); 44 44 char* fresult=(char*)malloc((EVP_MAX_MD_SIZE+1)*sizeof(char)); 45 45 unsigned char result[EVP_MAX_MD_SIZE]; 46 46 unsigned int len; 47 EVP_DigestInit( &md5ctx, EVP_md5());48 EVP_DigestUpdate( &md5ctx, url, strlen(url));49 EVP_DigestFinal_ex( &md5ctx,result,&len);50 EVP_MD_CTX_ cleanup(&md5ctx);47 EVP_DigestInit(md5ctx, EVP_md5()); 48 EVP_DigestUpdate(md5ctx, url, strlen(url)); 49 EVP_DigestFinal_ex(md5ctx,result,&len); 50 EVP_MD_CTX_destroy(md5ctx); 51 51 int i; 52 52 for(i = 0; i < len; i++){ 53 53 if(i>0){ 54 char *tmp= strdup(fresult);54 char *tmp=zStrdup(fresult); 55 55 sprintf(fresult,"%s%02x", tmp,result[i]); 56 56 free(tmp); … … 62 62 } 63 63 64 /** 65 * Compute md5 of a file 66 * 67 * @param file the char* 68 * @return a char* representing the md5 of the url 69 * @warning make sure to free resources returned by this function 70 */ 71 char* getMd5f(char* file){ 72 EVP_MD_CTX *md5ctx=EVP_MD_CTX_create(); 73 char* fresult=(char*)malloc((EVP_MAX_MD_SIZE+1)*sizeof(char)); 74 unsigned char result[EVP_MAX_MD_SIZE]; 75 unsigned int len; 76 int bytes; 77 int dlen=65536; 78 unsigned char data[65537]; 79 FILE *inFile = fopen (file, "rb"); 80 EVP_DigestInit(md5ctx, EVP_md5()); 81 while ((bytes = fread (data, sizeof(unsigned char), dlen, inFile)) != 0) 82 EVP_DigestUpdate(md5ctx, data, bytes); 83 EVP_DigestFinal_ex(md5ctx,result,&len); 84 EVP_MD_CTX_destroy(md5ctx); 85 int i; 86 for(i = 0; i < len; i++){ 87 if(i>0){ 88 char *tmp=zStrdup(fresult); 89 sprintf(fresult,"%s%02x", tmp,result[i]); 90 free(tmp); 91 } 92 else 93 sprintf(fresult,"%02x",result[i]); 94 } 95 fclose (inFile); 96 return fresult; 97 } 98 99 100 101 /** 102 * Create a URL by appending every request header listed in the security 103 * section.This imply that the URL will contain any authentication 104 * informations that should be fowarded to the server from which de input 105 * was download. 106 * @param conf the main configuration maps 107 * @param request the URL to transform. 108 * @return a char* that contain the original URL plus potential header (only for 109 * hosts that are not shared). 110 * @warning Be sure to free the memory returned by this function. 111 */ 112 char* getFilenameForRequest(maps* conf, const char* request){ 113 map* passThrough=getMapFromMaps(conf,"security","attributes"); 114 map* targetHosts=getMapFromMaps(conf,"security","hosts"); 115 char* passedHeader[10]; 116 int cnt=0; 117 char *res=zStrdup(request); 118 char *toAppend=NULL; 119 if(passThrough!=NULL && targetHosts!=NULL){ 120 char *tmp=zStrdup(passThrough->value); 121 char *token, *saveptr; 122 token = strtok_r (tmp, ",", &saveptr); 123 int i; 124 if((strstr(targetHosts->value,"*")!=NULL || isProtectedHost(targetHosts->value,request)==1) && strncasecmp(getProvenance(conf,request),"SHARED",6)!=0){ 125 while (token != NULL){ 126 int length=strlen(token)+6; 127 char* tmp1=(char*)malloc(length*sizeof(char)); 128 map* tmpMap; 129 snprintf(tmp1,6,"HTTP_"); 130 int j; 131 for(j=0;token[j]!='\0';j++){ 132 if(token[j]!='-') 133 tmp1[5+j]=toupper(token[j]); 134 else 135 tmp1[5+j]='_'; 136 tmp1[5+j+1]='\0'; 137 } 138 tmpMap = getMapFromMaps(conf,"renv",tmp1); 139 if(tmpMap!=NULL){ 140 if(toAppend==NULL){ 141 toAppend=(char*)malloc((strlen(tmpMap->value)+1)*sizeof(char)); 142 sprintf(toAppend,"%s",tmpMap->value); 143 }else{ 144 char *tmp3=zStrdup(toAppend); 145 toAppend=(char*)realloc(toAppend,(strlen(tmpMap->value)+strlen(tmp3)+2)*sizeof(char)); 146 sprintf(toAppend,"%s,%s",tmp3,tmpMap->value); 147 free(tmp3); 148 } 149 } 150 free(tmp1); 151 cnt+=1; 152 token = strtok_r (NULL, ",", &saveptr); 153 } 154 } 155 free(tmp); 156 } 157 if(toAppend!=NULL){ 158 char *tmp3=zStrdup(res); 159 res=(char*)realloc(res,(strlen(tmp3)+strlen(toAppend)+1)*sizeof(char)); 160 sprintf(res,"%s%s",tmp3,toAppend); 161 free(tmp3); 162 free(toAppend); 163 } 164 return res; 165 } 166 167 /** 168 * Store MD5 of the content of a file 169 * @file char* the full path of the file 170 */ 171 int storeMd5(char* file){ 172 char* storage=zStrdup(file); 173 char* md5fstr=getMd5f(file); 174 storage[strlen(storage)-2]='m'; 175 storage[strlen(storage)-1]='d'; 176 FILE* fo=fopen(storage,"w+"); 177 if(fo==NULL) 178 return 1; 179 fwrite(md5fstr,sizeof(char),strlen(md5fstr),fo); 180 free(md5fstr); 181 free(storage); 182 fclose(fo); 183 return 0; 184 } 64 185 65 186 /** … … 76 197 * @param max_path the size of the allocated filepath buffer 77 198 */ 199 void cacheFile(maps* conf,char* request,char* mimeType,int length,char* filename){ 200 map* tmp=getMapFromMaps(conf,"main","cacheDir"); 201 char contentr[4096]; 202 int cred=0; 203 if(tmp!=NULL){ 204 char* myRequest=getFilenameForRequest(conf,request); 205 char* md5str=getMd5(myRequest); 206 free(myRequest); 207 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 208 // Store md5 209 char* md5fstr=getMd5f(filename); 210 sprintf(fname,"%s/%s.zmd",tmp->value,md5str); 211 FILE* fo=fopen(fname,"w+"); 212 #ifdef DEBUG 213 fprintf(stderr,"filename: %s\n",filename); 214 fprintf(stderr,"MD5: %s\n",md5fstr); 215 #endif 216 fwrite(md5fstr,sizeof(char),strlen(md5fstr),fo); 217 free(md5fstr); 218 fclose(fo); 219 220 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 221 zooLock* lck=lockFile(conf,fname,'w'); 222 if(lck!=NULL){ 223 #ifdef DEBUG 224 fprintf(stderr,"Cache list : %s\n",fname); 225 fflush(stderr); 226 #endif 227 FILE* fi=fopen(filename,"rb"); 228 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 229 fo=fopen(fname,"w+"); 230 if(fo==NULL){ 231 #ifdef DEBUG 232 fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); 233 #endif 234 unlockFile(conf,lck); 235 return; 236 } 237 if(fi==NULL){ 238 #ifdef DEBUG 239 fprintf (stderr, "Failed to open %s for reading: %s\n",filename, strerror(errno)); 240 #endif 241 unlockFile(conf,lck); 242 return; 243 } 244 memset(contentr,0,4096); 245 while((cred=fread(contentr,sizeof(char),4096,fi))>0){ 246 fwrite(contentr,sizeof(char),cred,fo); 247 fflush(fo); 248 memset(contentr,0,4096); 249 } 250 unlockFile(conf,lck); 251 fclose(fo); 252 fclose(fi); 253 254 // Store mimeType 255 sprintf(fname,"%s/%s.zcm",tmp->value,md5str); 256 fo=fopen(fname,"w+"); 257 #ifdef DEBUG 258 fprintf(stderr,"MIMETYPE: %s\n",mimeType); 259 #endif 260 fwrite(mimeType,sizeof(char),strlen(mimeType),fo); 261 fclose(fo); 262 263 // Store provenance 264 sprintf(fname,"%s/%s.zcp",tmp->value,md5str); 265 fo=fopen(fname,"w+"); 266 char* origin=getProvenance(conf,request); 267 #ifdef DEBUG 268 fprintf(stderr,"ORIGIN: %s\n",mimeType); 269 #endif 270 fwrite(origin,sizeof(char),strlen(origin),fo); 271 fclose(fo); 272 273 free(md5str); 274 275 } 276 free(fname); 277 } 278 } 279 280 /** 281 * Cache a file for a given request. 282 * For each cached file, the are two files stored, a .zca and a .zcm containing 283 * the downloaded content and the mimeType respectively. 284 * 285 * @param conf the maps containing the settings of the main.cfg file 286 * @param request the url used too fetch the content 287 * @param content the downloaded content 288 * @param mimeType the content mimeType 289 * @param length the content size 290 * @param filepath a buffer for storing the path of the cached file; may be NULL 291 * @param max_path the size of the allocated filepath buffer 292 */ 78 293 void addToCache(maps* conf,char* request,char* content,char* mimeType,int length, 79 294 char* filepath, size_t max_path){ 80 295 map* tmp=getMapFromMaps(conf,"main","cacheDir"); 81 296 if(tmp!=NULL){ 82 char* md5str=getMd5(request); 297 char* myRequest=getFilenameForRequest(conf,request); 298 char* md5str=getMd5(myRequest); 299 free(myRequest); 83 300 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 84 301 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 85 #ifdef DEBUG 86 fprintf(stderr,"Cache list : %s\n",fname); 87 fflush(stderr); 88 #endif 89 FILE* fo=fopen(fname,"w+"); 90 if(fo==NULL){ 91 #ifdef DEBUG 92 fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); 93 #endif 94 filepath = NULL; 95 return; 96 } 97 fwrite(content,sizeof(char),length,fo); 98 fclose(fo); 302 zooLock* lck=lockFile(conf,fname,'w'); 303 if(lck!=NULL){ 304 #ifdef DEBUG 305 fprintf(stderr,"Cache list : %s\n",fname); 306 fflush(stderr); 307 #endif 308 FILE* fo=fopen(fname,"w+"); 309 if(fo==NULL){ 310 #ifdef DEBUG 311 fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); 312 #endif 313 filepath = NULL; 314 unlockFile(conf,lck); 315 return; 316 } 317 fwrite(content,sizeof(char),length,fo); 318 unlockFile(conf,lck); 319 fclose(fo); 99 320 100 if (filepath != NULL) { 101 strncpy(filepath, fname, max_path); 102 } 103 104 sprintf(fname,"%s/%s.zcm",tmp->value,md5str); 105 fo=fopen(fname,"w+"); 106 #ifdef DEBUG 107 fprintf(stderr,"MIMETYPE: %s\n",mimeType); 108 #endif 109 fwrite(mimeType,sizeof(char),strlen(mimeType),fo); 110 fclose(fo); 111 112 free(md5str); 113 free(fname); 321 if (filepath != NULL) { 322 strncpy(filepath, fname, max_path); 323 } 324 325 sprintf(fname,"%s/%s.zcm",tmp->value,md5str); 326 fo=fopen(fname,"w+"); 327 #ifdef DEBUG 328 fprintf(stderr,"MIMETYPE: %s\n",mimeType); 329 #endif 330 fwrite(mimeType,sizeof(char),strlen(mimeType),fo); 331 fclose(fo); 332 333 sprintf(fname,"%s/%s.zcp",tmp->value,md5str); 334 fo=fopen(fname,"w+"); 335 char* origin=getProvenance(conf,request); 336 #ifdef DEBUG 337 fprintf(stderr,"ORIGIN: %s\n",mimeType); 338 #endif 339 fwrite(origin,sizeof(char),strlen(origin),fo); 340 fclose(fo); 341 342 free(md5str); 343 free(fname); 344 } 114 345 } 115 346 else { 116 347 filepath = NULL; 117 348 } 118 349 } … … 127 358 */ 128 359 char* isInCache(maps* conf,char* request){ 360 map* tmpUrl=getMapFromMaps(conf,"main","tmpUrl"); 129 361 map* tmpM=getMapFromMaps(conf,"main","cacheDir"); 130 if(tmpM!=NULL){ 131 char* md5str=getMd5(request); 362 if(tmpM==NULL) 363 tmpM=getMapFromMaps(conf,"main","tmpPath"); 364 if(strstr(request,tmpUrl->value)!=NULL){ 365 map* tmpPath=getMapFromMaps(conf,"main","tmpPath"); 366 char* tmpStr=strstr(request,tmpUrl->value); 367 char* tmpStr1=zStrdup(tmpStr+strlen(tmpUrl->value)); 368 char* res=(char*) malloc((strlen(tmpPath->value)+strlen(tmpStr1)+2)*sizeof(char)); 369 sprintf(res,"%s/%s",tmpPath->value,tmpStr1); 370 free(tmpStr1); 371 return res; 372 } 373 #ifdef MS_FORCE_LOCAL_FILE_USE 374 map* msUrl=getMapFromMaps(conf,"main","mapserverAddress"); 375 if(msUrl!=NULL && strstr(request,msUrl->value)!=NULL){ 376 char *tmpStr=strstr(request,"?"); 377 char *cursor=zStrdup(tmpStr+1); 378 char *token, *saveptr; 379 token = strtok_r (cursor, "&", &saveptr); 380 while(token!=NULL){ 381 char *token1, *saveptr1; 382 token1 = strtok_r (token, "=", &saveptr1); 383 char *name=NULL; 384 while(token1!=NULL){ 385 if(name==NULL) 386 name=zStrdup(token1); 387 else 388 if(strcasecmp(name,"map")==0){ 389 mapObj *myMap=msLoadMap(token1,NULL); 390 char * res=zStrdup(myMap->layers[0]->data); 391 free(name); 392 free(cursor); 393 msFreeMap(myMap); 394 return res; 395 } 396 token1 = strtok_r (NULL, "=", &saveptr1); 397 } 398 token = strtok_r (NULL, "&", &saveptr); 399 } 400 free(cursor); 401 } 402 #endif 403 if(strncasecmp(request,"file://",7)==0){ 404 char* tmpStr=zStrdup(request+7); 405 fprintf(stderr,"**** %s %d %s \n",__FILE__,__LINE__,tmpStr); 406 return tmpStr; 407 } 408 else{ 409 char* myRequest=getFilenameForRequest(conf,request); 410 char* md5str=getMd5(myRequest); 411 free(myRequest); 132 412 #ifdef DEBUG 133 413 fprintf(stderr,"MD5STR : (%s)\n\n",md5str); … … 135 415 char* fname=(char*)malloc(sizeof(char)*(strlen(tmpM->value)+strlen(md5str)+6)); 136 416 sprintf(fname,"%s/%s.zca",tmpM->value,md5str); 137 struct stat f_status;138 int s= stat(fname, &f_status);417 zStatStruct f_status; 418 int s=zStat(fname, &f_status); 139 419 if(s==0 && f_status.st_size>0){ 140 420 free(md5str); … … 158 438 */ 159 439 int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){ 440 441 int shouldClean=-1; 160 442 map* tmp1; 161 443 char sindex[5]; 162 444 maps* content=*in; 163 445 map* length=getMap(content->content,"length"); 164 int shouldClean=-1;446 map* memUse=getMapFromMaps(*m,"main","memory"); 165 447 if(length==NULL){ 166 448 length=createMap("length","1"); … … 171 453 char *mimeType=NULL; 172 454 int fsize=0; 455 char oriname[12]; 173 456 char cname[15]; 174 457 char vname[11]; … … 178 461 char icname[14]; 179 462 char xname[16]; 463 char bname[8]; 464 char hname[11]; 180 465 char oname[12]; 466 char ufile[12]; 181 467 if(*index>0) 182 468 sprintf(vname1,"value_%d",*index); … … 185 471 186 472 if(i>0){ 473 sprintf(cname,"cache_file_%d",i); 187 474 tmp1=getMap(content->content,cname); 188 sprintf( cname,"cache_file_%d",i);475 sprintf(oriname,"origin_%d",i); 189 476 sprintf(vname,"value_%d",i); 190 477 sprintf(sname,"size_%d",i); … … 192 479 sprintf(icname,"isCached_%d",i); 193 480 sprintf(xname,"Reference_%d",i); 481 sprintf(bname,"body_%d",i); 482 sprintf(hname,"headers_%d",i); 194 483 sprintf(oname,"Order_%d",i); 484 sprintf(ufile,"use_file_%d",i); 195 485 }else{ 196 486 sprintf(cname,"cache_file"); 487 sprintf(oriname,"origin"); 197 488 sprintf(vname,"value"); 198 489 sprintf(sname,"size"); … … 200 491 sprintf(icname,"isCached"); 201 492 sprintf(xname,"Reference"); 493 sprintf(bname,"body"); 494 sprintf(hname,"headers"); 202 495 sprintf(oname,"Order"); 496 sprintf(ufile,"use_file"); 203 497 } 204 498 … … 206 500 sprintf(sindex,"%d",*index+1); 207 501 if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){ 208 209 if(getMap(content->content,icname)==NULL){ 210 fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); 211 if(fcontent == NULL){ 212 errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 213 return -1; 502 503 if(getMap(content->content,icname)==NULL) { 504 if(memUse==NULL || strcasecmp(memUse->value,"load")==0){ 505 fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); 506 if(fcontent == NULL){ 507 errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 508 return -1; 509 } 510 size_t dwRead; 511 InternetReadFile(hInternet->ihandle[*index], 512 (LPVOID)fcontent, 513 hInternet->ihandle[*index].nDataLen, 514 &dwRead); 515 fcontent[hInternet->ihandle[*index].nDataLen]=0; 214 516 } 215 size_t dwRead;216 InternetReadFile(hInternet->ihandle[*index],217 (LPVOID)fcontent,218 hInternet->ihandle[*index].nDataLen,219 &dwRead);220 fcontent[hInternet->ihandle[*index].nDataLen]=0;221 517 fsize=hInternet->ihandle[*index].nDataLen; 222 518 if(hInternet->ihandle[*index].mimeType==NULL) … … 226 522 227 523 map* tmpMap=getMapOrFill(&(*in)->content,vname,""); 228 free(tmpMap->value); 229 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 230 if(tmpMap->value==NULL){ 231 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 232 } 233 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 524 if(memUse==NULL || strcasecmp(memUse->value,"load")==0){ 525 free(tmpMap->value); 526 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 527 if(tmpMap->value==NULL){ 528 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 529 } 530 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 531 }else 532 addToMap((*in)->content,ufile,"true"); 234 533 if(hInternet->ihandle[*index].code!=200){ 235 534 const char *error_rep_str=_("Unable to download the file for the input <%s>, response code was : %d."); … … 255 554 sprintf(ltmp1,"%d",fsize); 256 555 map* tmp=getMapFromMaps(*m,"main","cacheDir"); 556 char *request=NULL; 257 557 if(tmp!=NULL){ 258 char* md5str=getMd5(tmp1->value); 558 map* tmp2; 559 char* md5str=NULL; 560 if((tmp2=getMap(content->content,bname))!=NULL){ 561 char *tmpStr=(char*)malloc((strlen(tmp1->value)+strlen(tmp2->value)+1)*sizeof(char)); 562 sprintf(tmpStr,"%s%s",tmp1->value,tmp2->value); 563 if((tmp2=getMap(content->content,"headers"))!=NULL){ 564 char *tmpStr2=zStrdup(tmpStr); 565 free(tmpStr); 566 tmpStr=(char*)malloc((strlen(tmpStr2)+strlen(tmp2->value)+1)*sizeof(char)); 567 sprintf(tmpStr,"%s%s",tmpStr2,tmp2->value); 568 free(tmpStr2); 569 } 570 md5str=getMd5(tmpStr); 571 request=zStrdup(tmpStr); 572 free(tmpStr); 573 }else{ 574 char *myRequest=getFilenameForRequest(*m,tmp1->value); 575 md5str=getMd5(myRequest); 576 request=zStrdup(tmp1->value); 577 free(myRequest); 578 } 259 579 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 260 580 sprintf(fname,"%s/%s.zca",tmp->value,md5str); … … 264 584 addToMap((*in)->content,sname,ltmp1); 265 585 addToMap((*in)->content,mname,mimeType); 266 addToCache(*m,tmp1->value,fcontent,mimeType,fsize, NULL, 0); 267 free(fcontent); 586 char* origin=getProvenance(*m,request); 587 addToMap((*in)->content,oriname,origin); 588 if(memUse==NULL || strcasecmp(memUse->value,"load")==0){ 589 addToCache(*m,request,fcontent,mimeType,fsize, NULL, 0); 590 free(fcontent); 591 }else{ 592 addToMap((*in)->content,ufile,"true"); 593 cacheFile(*m,request,mimeType,fsize,hInternet->ihandle[*index].filename); 594 } 268 595 free(mimeType); 269 *index++;270 596 free(request); 597 (*index)++; 271 598 } 272 599 } … … 324 651 */ 325 652 void addRequestToQueue(maps** m,HINTERNET* hInternet,const char* url,bool req){ 326 hInternet->waitingRequests[hInternet->nb]= strdup(url);653 hInternet->waitingRequests[hInternet->nb]=zStrdup(url); 327 654 if(req) 328 InternetOpenUrl(hInternet,hInternet->waitingRequests[hInternet->nb],NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0 );655 InternetOpenUrl(hInternet,hInternet->waitingRequests[hInternet->nb],NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0,*m); 329 656 maps *oreq=getMaps(*m,"orequests"); 330 657 if(oreq==NULL){ … … 335 662 free(oreq); 336 663 }else{ 337 setMapArray(oreq->content,"value",hInternet->nb -1,url);664 setMapArray(oreq->content,"value",hInternet->nb,url); 338 665 } 339 666 } … … 352 679 char* cached=isInCache(*m,url); 353 680 char *mimeType=NULL; 354 int fsize=0; 681 char *origin=NULL; 682 long long fsize=0; 683 map* memUse=getMapFromMaps(*m,"main","memory"); 355 684 356 685 map* t=getMap(*content,"xlink:href"); … … 361 690 362 691 if(cached!=NULL){ 363 364 struct stat f_status; 365 int s=stat(cached, &f_status); 692 zStatStruct f_status; 693 int s=zStat(cached, &f_status); 366 694 if(s==0){ 367 fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));368 FILE* f=fopen(cached,"rb");369 fread(fcontent,f_status.st_size,1,f);695 zooLock* lck=lockFile(*m,cached,'r'); 696 if(lck==NULL) 697 return -1; 370 698 fsize=f_status.st_size; 371 fcontent[fsize]=0; 372 fclose(f); 699 if(memUse==NULL || strcasecmp(memUse->value,"load")==0){ 700 fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 701 FILE* f=fopen(cached,"rb"); 702 if(f!=NULL){ 703 fread(fcontent,f_status.st_size,1,f); 704 fcontent[fsize]=0; 705 fclose(f); 706 } 707 } 373 708 addToMap(*content,"cache_file",cached); 709 unlockFile(*m,lck); 374 710 } 375 711 cached[strlen(cached)-1]='m'; 376 s= stat(cached, &f_status);712 s=zStat(cached, &f_status); 377 713 if(s==0){ 714 zooLock* lck=lockFile(*m,cached,'r'); 715 if(lck==NULL) 716 return -1; 378 717 mimeType=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 379 718 FILE* f=fopen(cached,"rb"); … … 381 720 mimeType[f_status.st_size]=0; 382 721 fclose(f); 383 } 384 722 unlockFile(*m,lck); 723 } 724 cached[strlen(cached)-1]='p'; 725 s=zStat(cached, &f_status); 726 if(s==0){ 727 zooLock* lck=lockFile(*m,cached,'r'); 728 if(lck==NULL) 729 return -1; 730 origin=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 731 FILE* f=fopen(cached,"rb"); 732 fread(origin,f_status.st_size,1,f); 733 mimeType[f_status.st_size]=0; 734 fclose(f); 735 unlockFile(*m,lck); 736 } 385 737 }else{ 386 738 addRequestToQueue(m,hInternet,url,true); … … 393 745 addToMap(*content,"fmimeType",mimeType); 394 746 } 747 if(origin!=NULL){ 748 addToMap(*content,"origin",origin); 749 } 395 750 396 751 map* tmpMap=getMapOrFill(content,"value",""); 397 398 free(tmpMap->value); 399 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 400 if(tmpMap->value==NULL || fcontent == NULL) 401 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 402 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 403 752 if(memUse==NULL || strcasecmp(memUse->value,"load")==0){ 753 free(tmpMap->value); 754 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 755 if(tmpMap->value==NULL || fcontent == NULL) 756 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 757 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 758 } 759 404 760 char ltmp1[256]; 405 sprintf(ltmp1,"% d",fsize);761 sprintf(ltmp1,"%ld",fsize); 406 762 addToMap(*content,"size",ltmp1); 407 763 if(cached==NULL){ 408 addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); 764 if(memUse==NULL || strcasecmp(memUse->value,"load")==0) 765 addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); 766 else 767 cacheFile(*m,url,mimeType,fsize,hInternet->ihandle[hInternet->nb-1].filename); 409 768 } 410 769 else{ 411 770 addToMap(*content,"isCached","true"); 412 771 map* tmp=getMapFromMaps(*m,"main","cacheDir"); 413 if(tmp!=NULL){ 772 map* tmp1=getMap((*content),"cache_file"); 773 if(tmp!=NULL && tmp1==NULL){ 414 774 map *c=getMap((*content),"xlink:href"); 415 char* md5str=getMd5(c->value); 416 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 417 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 418 addToMap(*content,"cache_file",fname); 419 free(fname); 420 } 421 } 422 free(fcontent); 423 free(mimeType); 424 free(cached); 775 if(strncasecmp(c->value,"file://",7)!=0){ 776 char *myRequest=getFilenameForRequest(*m,c->value); 777 char* md5str=getMd5(myRequest); 778 free(myRequest); 779 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 780 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 781 addToMap(*content,"cache_file",fname); 782 free(fname); 783 free(md5str); 784 } 785 } 786 } 787 if(fcontent!=NULL) 788 free(fcontent); 789 if(mimeType!=NULL) 790 free(mimeType); 791 if(cached!=NULL) 792 free(cached); 425 793 return 0; 426 794 }
Note: See TracChangeset
for help on using the changeset viewer.