Changeset 863 for branches/prototype-v0/zoo-project/zoo-kernel/caching.c
- Timestamp:
- Feb 5, 2018, 1:19:13 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/prototype-v0/zoo-project/zoo-kernel/caching.c
r862 r863 142 142 * @param max_path the size of the allocated filepath buffer 143 143 */ 144 void cacheFile(maps* conf,char* request,char* mimeType,int length,char* filename){ 145 map* tmp=getMapFromMaps(conf,"main","cacheDir"); 146 char contentr[4096]; 147 int cred=0; 148 if(tmp!=NULL){ 149 char* myRequest=getFilenameForRequest(conf,request); 150 char* md5str=getMd5(myRequest); 151 free(myRequest); 152 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 153 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 154 zooLock* lck=lockFile(conf,fname,'w'); 155 if(lck!=NULL){ 156 #ifdef DEBUG 157 fprintf(stderr,"Cache list : %s\n",fname); 158 fflush(stderr); 159 #endif 160 FILE* fi=fopen(filename,"rb"); 161 FILE* fo=fopen(fname,"w+"); 162 if(fo==NULL){ 163 #ifdef DEBUG 164 fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); 165 #endif 166 unlockFile(conf,lck); 167 return; 168 } 169 if(fi==NULL){ 170 #ifdef DEBUG 171 fprintf (stderr, "Failed to open %s for reading: %s\n",filename, strerror(errno)); 172 #endif 173 unlockFile(conf,lck); 174 return; 175 } 176 memset(contentr,0,4096); 177 while((cred=fread(contentr,sizeof(char),4096,fi))>0){ 178 fwrite(contentr,sizeof(char),cred,fo); 179 fflush(fo); 180 memset(contentr,0,4096); 181 } 182 unlockFile(conf,lck); 183 fclose(fo); 184 fclose(fi); 185 186 sprintf(fname,"%s/%s.zcm",tmp->value,md5str); 187 fo=fopen(fname,"w+"); 188 #ifdef DEBUG 189 fprintf(stderr,"MIMETYPE: %s\n",mimeType); 190 #endif 191 fwrite(mimeType,sizeof(char),strlen(mimeType),fo); 192 fclose(fo); 193 194 sprintf(fname,"%s/%s.zcp",tmp->value,md5str); 195 fo=fopen(fname,"w+"); 196 char* origin=getProvenance(conf,request); 197 #ifdef DEBUG 198 fprintf(stderr,"ORIGIN: %s\n",mimeType); 199 #endif 200 fwrite(origin,sizeof(char),strlen(origin),fo); 201 fclose(fo); 202 203 free(md5str); 204 free(fname); 205 } 206 } 207 } 208 209 /** 210 * Cache a file for a given request. 211 * For each cached file, the are two files stored, a .zca and a .zcm containing 212 * the downloaded content and the mimeType respectively. 213 * 214 * @param conf the maps containing the settings of the main.cfg file 215 * @param request the url used too fetch the content 216 * @param content the downloaded content 217 * @param mimeType the content mimeType 218 * @param length the content size 219 * @param filepath a buffer for storing the path of the cached file; may be NULL 220 * @param max_path the size of the allocated filepath buffer 221 */ 144 222 void addToCache(maps* conf,char* request,char* content,char* mimeType,int length, 145 223 char* filepath, size_t max_path){ … … 243 321 int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){ 244 322 323 int shouldClean=-1; 245 324 map* tmp1; 246 325 char sindex[5]; 247 326 maps* content=*in; 248 327 map* length=getMap(content->content,"length"); 249 int shouldClean=-1;328 map* memUse=getMapFromMaps(*m,"main","memory"); 250 329 if(length==NULL){ 251 330 length=createMap("length","1"); … … 266 345 char hname[11]; 267 346 char oname[12]; 347 char ufile[12]; 268 348 if(*index>0) 269 349 sprintf(vname1,"value_%d",*index); … … 282 362 sprintf(hname,"headers_%d",i); 283 363 sprintf(oname,"Order_%d",i); 364 sprintf(ufile,"use_file_%d",i); 284 365 }else{ 285 366 sprintf(cname,"cache_file"); … … 292 373 sprintf(hname,"headers"); 293 374 sprintf(oname,"Order"); 375 sprintf(ufile,"use_file"); 294 376 } 295 377 … … 298 380 if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){ 299 381 300 if(getMap(content->content,icname)==NULL){ 301 fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); 302 if(fcontent == NULL){ 303 errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 304 return -1; 305 } 306 size_t dwRead; 307 InternetReadFile(hInternet->ihandle[*index], 308 (LPVOID)fcontent, 309 hInternet->ihandle[*index].nDataLen, 310 &dwRead); 311 fcontent[hInternet->ihandle[*index].nDataLen]=0; 382 if(getMap(content->content,icname)==NULL) { 383 if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ 384 fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char)); 385 if(fcontent == NULL){ 386 errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 387 return -1; 388 } 389 size_t dwRead; 390 InternetReadFile(hInternet->ihandle[*index], 391 (LPVOID)fcontent, 392 hInternet->ihandle[*index].nDataLen, 393 &dwRead); 394 fcontent[hInternet->ihandle[*index].nDataLen]=0; 395 } 312 396 fsize=hInternet->ihandle[*index].nDataLen; 313 397 if(hInternet->ihandle[*index].mimeType==NULL) … … 317 401 318 402 map* tmpMap=getMapOrFill(&(*in)->content,vname,""); 319 free(tmpMap->value); 320 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 321 if(tmpMap->value==NULL){ 322 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 323 } 324 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 403 if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ 404 free(tmpMap->value); 405 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 406 if(tmpMap->value==NULL){ 407 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 408 } 409 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 410 }else 411 addToMap((*in)->content,ufile,"true"); 325 412 if(hInternet->ihandle[*index].code!=200){ 326 413 char *error_rep_str=_("Unable to download the file for the input <%s>, response code was : %d."); … … 376 463 addToMap((*in)->content,sname,ltmp1); 377 464 addToMap((*in)->content,mname,mimeType); 378 addToCache(*m,request,fcontent,mimeType,fsize, NULL, 0); 379 free(fcontent); 465 if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ 466 addToCache(*m,request,fcontent,mimeType,fsize, NULL, 0); 467 free(fcontent); 468 }else{ 469 addToMap((*in)->content,ufile,"true"); 470 cacheFile(*m,request,mimeType,fsize,hInternet->ihandle[*index].filename); 471 } 380 472 free(mimeType); 381 473 free(request); … … 388 480 free(length); 389 481 } 482 dumpMaps(*in); 390 483 return 0; 391 484 } … … 438 531 hInternet->waitingRequests[hInternet->nb]=strdup(url); 439 532 if(req) 440 InternetOpenUrl(hInternet,hInternet->waitingRequests[hInternet->nb],NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0 );533 InternetOpenUrl(hInternet,hInternet->waitingRequests[hInternet->nb],NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0,*m); 441 534 maps *oreq=getMaps(*m,"orequests"); 442 535 if(oreq==NULL){ … … 465 558 char *mimeType=NULL; 466 559 int fsize=0; 560 map* memUse=getMapFromMaps(*m,"main","memory"); 467 561 468 562 map* t=getMap(*content,"xlink:href"); … … 479 573 if(lck==NULL) 480 574 return -1; 481 fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));482 FILE* f=fopen(cached,"rb");483 fread(fcontent,f_status.st_size,1,f);484 575 fsize=f_status.st_size; 485 fcontent[fsize]=0; 486 fclose(f); 576 if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ 577 fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 578 FILE* f=fopen(cached,"rb"); 579 if(f!=NULL){ 580 fread(fcontent,f_status.st_size,1,f); 581 fcontent[fsize]=0; 582 fclose(f); 583 } 584 } 487 585 addToMap(*content,"cache_file",cached); 488 586 unlockFile(*m,lck); … … 513 611 514 612 map* tmpMap=getMapOrFill(content,"value",""); 515 free(tmpMap->value); 516 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 517 if(tmpMap->value==NULL || fcontent == NULL) 518 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 519 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 520 613 if(memUse!=NULL && strcasecmp(memUse->value,"load")==0){ 614 free(tmpMap->value); 615 tmpMap->value=(char*)malloc((fsize+1)*sizeof(char)); 616 if(tmpMap->value==NULL || fcontent == NULL) 617 return errorException(*m, _("Unable to allocate memory"), "InternalError",NULL); 618 memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char)); 619 } 620 521 621 char ltmp1[256]; 522 622 sprintf(ltmp1,"%d",fsize); 523 623 addToMap(*content,"size",ltmp1); 524 624 if(cached==NULL){ 525 addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); 625 if(memUse!=NULL && strcasecmp(memUse->value,"load")==0) 626 addToCache(*m,url,fcontent,mimeType,fsize, NULL, 0); 627 else 628 cacheFile(*m,url,mimeType,fsize,hInternet->ihandle[hInternet->nb-1].filename); 526 629 } 527 630 else{
Note: See TracChangeset
for help on using the changeset viewer.