Changeset 854 for branches/prototype-v0/zoo-project/zoo-kernel/caching.c
- Timestamp:
- Nov 21, 2017, 10:24:14 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/prototype-v0/zoo-project/zoo-kernel/caching.c
r839 r854 64 64 65 65 /** 66 * Create a URL by appending every request header listed in the security 67 * section.This imply that the URL will contain any authentication 68 * informations that should be fowarded to the server from which de input 69 * was download. 70 * @param conf the main configuration maps 71 * @param request the URL to transform. 72 * @return a char* that contain the original URL plus potential header (only for 73 * hosts that are not shared). 74 * @warning Be sure to free the memory returned by this function. 75 */ 76 char* getFilenameForRequest(maps* conf, const char* request){ 77 map* passThrough=getMapFromMaps(conf,"security","attributes"); 78 map* targetHosts=getMapFromMaps(conf,"security","hosts"); 79 char* passedHeader[10]; 80 int cnt=0; 81 char *res=zStrdup(request); 82 char *toAppend=NULL; 83 if(passThrough!=NULL && targetHosts!=NULL){ 84 char *tmp=zStrdup(passThrough->value); 85 char *token, *saveptr; 86 token = strtok_r (tmp, ",", &saveptr); 87 int i; 88 if((strstr(targetHosts->value,"*")!=NULL || isProtectedHost(targetHosts->value,request)==1) && strncasecmp(getProvenance(conf,request),"SHARED",6)!=0){ 89 while (token != NULL){ 90 int length=strlen(token)+6; 91 char* tmp1=(char*)malloc(length*sizeof(char)); 92 map* tmpMap; 93 snprintf(tmp1,6,"HTTP_"); 94 int j; 95 for(j=0;token[j]!='\0';j++){ 96 if(token[j]!='-') 97 tmp1[5+j]=toupper(token[j]); 98 else 99 tmp1[5+j]='_'; 100 tmp1[5+j+1]='\0'; 101 } 102 tmpMap = getMapFromMaps(conf,"renv",tmp1); 103 if(tmpMap!=NULL){ 104 if(toAppend==NULL){ 105 toAppend=(char*)malloc((strlen(tmpMap->value)+1)*sizeof(char)); 106 sprintf(toAppend,"%s",tmpMap->value); 107 }else{ 108 char *tmp3=zStrdup(toAppend); 109 toAppend=(char*)realloc(toAppend,(strlen(tmpMap->value)+strlen(tmp3)+2)*sizeof(char)); 110 sprintf(toAppend,"%s,%s",tmp3,tmpMap->value); 111 free(tmp3); 112 } 113 } 114 free(tmp1); 115 cnt+=1; 116 token = strtok_r (NULL, ",", &saveptr); 117 } 118 } 119 free(tmp); 120 } 121 if(toAppend!=NULL){ 122 char *tmp3=zStrdup(res); 123 res=(char*)realloc(res,(strlen(tmp3)+strlen(toAppend)+1)*sizeof(char)); 124 sprintf(res,"%s%s",tmp3,toAppend); 125 free(tmp3); 126 free(toAppend); 127 } 128 return res; 129 } 130 131 /** 66 132 * Cache a file for a given request. 67 133 * For each cached file, the are two files stored, a .zca and a .zcm containing … … 80 146 map* tmp=getMapFromMaps(conf,"main","cacheDir"); 81 147 if(tmp!=NULL){ 82 char* md5str=getMd5(request); 148 char* myRequest=getFilenameForRequest(conf,request); 149 char* md5str=getMd5(myRequest); 150 free(myRequest); 83 151 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 84 152 sprintf(fname,"%s/%s.zca",tmp->value,md5str); 153 zooLock* lck=lockFile(conf,fname,'w'); 154 if(lck!=NULL){ 85 155 #ifdef DEBUG 86 fprintf(stderr,"Cache list : %s\n",fname);87 fflush(stderr);156 fprintf(stderr,"Cache list : %s\n",fname); 157 fflush(stderr); 88 158 #endif 89 FILE* fo=fopen(fname,"w+");90 if(fo==NULL){159 FILE* fo=fopen(fname,"w+"); 160 if(fo==NULL){ 91 161 #ifdef DEBUG 92 162 fprintf (stderr, "Failed to open %s for writing: %s\n",fname, strerror(errno)); 93 163 #endif 94 filepath = NULL; 95 return; 96 } 97 fwrite(content,sizeof(char),length,fo); 98 fclose(fo); 164 filepath = NULL; 165 unlockFile(conf,lck); 166 return; 167 } 168 fwrite(content,sizeof(char),length,fo); 169 unlockFile(conf,lck); 170 fclose(fo); 99 171 100 101 102 } 103 104 sprintf(fname,"%s/%s.zcm",tmp->value,md5str);105 fo=fopen(fname,"w+");172 if (filepath != NULL) { 173 strncpy(filepath, fname, max_path); 174 } 175 176 sprintf(fname,"%s/%s.zcm",tmp->value,md5str); 177 fo=fopen(fname,"w+"); 106 178 #ifdef DEBUG 107 fprintf(stderr,"MIMETYPE: %s\n",mimeType);179 fprintf(stderr,"MIMETYPE: %s\n",mimeType); 108 180 #endif 109 fwrite(mimeType,sizeof(char),strlen(mimeType),fo); 110 fclose(fo); 111 112 free(md5str); 113 free(fname); 181 fwrite(mimeType,sizeof(char),strlen(mimeType),fo); 182 fclose(fo); 183 184 sprintf(fname,"%s/%s.zcp",tmp->value,md5str); 185 fo=fopen(fname,"w+"); 186 char* origin=getProvenance(conf,request); 187 #ifdef DEBUG 188 fprintf(stderr,"ORIGIN: %s\n",mimeType); 189 #endif 190 fwrite(origin,sizeof(char),strlen(origin),fo); 191 fclose(fo); 192 193 free(md5str); 194 free(fname); 195 } 114 196 } 115 197 else { 116 198 filepath = NULL; 117 199 } 118 200 } … … 129 211 map* tmpM=getMapFromMaps(conf,"main","cacheDir"); 130 212 if(tmpM!=NULL){ 131 char* md5str=getMd5(request); 213 char* myRequest=getFilenameForRequest(conf,request); 214 char* md5str=getMd5(myRequest); 215 free(myRequest); 132 216 #ifdef DEBUG 133 217 fprintf(stderr,"MD5STR : (%s)\n\n",md5str); … … 206 290 sprintf(xname,"Reference"); 207 291 sprintf(bname,"body"); 208 sprintf(hname,"headers" ,i);292 sprintf(hname,"headers"); 209 293 sprintf(oname,"Order"); 210 294 } … … 280 364 free(tmpStr); 281 365 }else{ 282 md5str=getMd5(tmp1->value); 366 char *myRequest=getFilenameForRequest(*m,tmp1->value); 367 md5str=getMd5(myRequest); 283 368 request=zStrdup(tmp1->value); 369 free(myRequest); 284 370 } 285 371 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); … … 387 473 388 474 if(cached!=NULL){ 389 390 475 struct stat f_status; 391 476 int s=stat(cached, &f_status); 392 477 if(s==0){ 478 zooLock* lck=lockFile(*m,cached,'r'); 479 if(lck==NULL) 480 return -1; 393 481 fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 394 482 FILE* f=fopen(cached,"rb"); … … 398 486 fclose(f); 399 487 addToMap(*content,"cache_file",cached); 488 unlockFile(*m,lck); 400 489 } 401 490 cached[strlen(cached)-1]='m'; 402 491 s=stat(cached, &f_status); 403 492 if(s==0){ 493 zooLock* lck=lockFile(*m,cached,'r'); 494 if(lck==NULL) 495 return -1; 404 496 mimeType=(char*)malloc(sizeof(char)*(f_status.st_size+1)); 405 497 FILE* f=fopen(cached,"rb"); … … 407 499 mimeType[f_status.st_size]=0; 408 500 fclose(f); 409 }410 501 unlockFile(*m,lck); 502 } 411 503 }else{ 412 504 addRequestToQueue(m,hInternet,url,true); … … 439 531 if(tmp!=NULL){ 440 532 map *c=getMap((*content),"xlink:href"); 441 char* md5str=getMd5(c->value); 533 char *myRequest=getFilenameForRequest(*m,c->value); 534 char* md5str=getMd5(myRequest); 535 free(myRequest); 442 536 char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6)); 443 537 sprintf(fname,"%s/%s.zca",tmp->value,md5str);
Note: See TracChangeset
for help on using the changeset viewer.