Ignore:
Timestamp:
May 7, 2019, 2:17:08 PM (5 years ago)
Author:
djay
Message:

Merge prototype-v0 branch in trunk

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/zoo-project/zoo-kernel/caching.c

    r837 r917  
    2323 */
    2424
     25#include <openssl/md5.h>
     26#include <openssl/evp.h>
    2527#include "caching.h"
    2628#include "service.h"
    2729#include "service_internal.h"
    2830#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
    3535/**
    3636 * Compute md5
     
    4141 */
    4242char* getMd5(char* url){
    43   EVP_MD_CTX md5ctx;
     43  EVP_MD_CTX *md5ctx=EVP_MD_CTX_create();
    4444  char* fresult=(char*)malloc((EVP_MAX_MD_SIZE+1)*sizeof(char));
    4545  unsigned char result[EVP_MAX_MD_SIZE];
    4646  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);
    5151  int i;
    5252  for(i = 0; i < len; i++){
    5353    if(i>0){
    54       char *tmp=strdup(fresult);
     54      char *tmp=zStrdup(fresult);
    5555      sprintf(fresult,"%s%02x", tmp,result[i]);
    5656      free(tmp);
     
    6262}
    6363
     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 */
     71char* 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 */
     112char* 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 */
     171int 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}
    64185
    65186/**
     
    76197 * @param max_path the size of the allocated filepath buffer 
    77198 */
     199void 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 */
    78293void addToCache(maps* conf,char* request,char* content,char* mimeType,int length,
    79294                char* filepath, size_t max_path){
    80295  map* tmp=getMapFromMaps(conf,"main","cacheDir");
    81296  if(tmp!=NULL){
    82     char* md5str=getMd5(request);
     297    char* myRequest=getFilenameForRequest(conf,request);
     298    char* md5str=getMd5(myRequest);
     299    free(myRequest);
    83300    char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6));
    84301    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);
    99320       
    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    }
    114345  }
    115346  else {
    116           filepath = NULL;
     347    filepath = NULL;
    117348  }       
    118349}
     
    127358 */
    128359char* isInCache(maps* conf,char* request){
     360  map* tmpUrl=getMapFromMaps(conf,"main","tmpUrl");
    129361  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);
    132412#ifdef DEBUG
    133413    fprintf(stderr,"MD5STR : (%s)\n\n",md5str);
     
    135415    char* fname=(char*)malloc(sizeof(char)*(strlen(tmpM->value)+strlen(md5str)+6));
    136416    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);
    139419    if(s==0 && f_status.st_size>0){
    140420      free(md5str);
     
    158438 */
    159439int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){
     440 
     441  int shouldClean=-1;
    160442  map* tmp1;
    161443  char sindex[5];
    162444  maps* content=*in;
    163445  map* length=getMap(content->content,"length");
    164   int shouldClean=-1;
     446  map* memUse=getMapFromMaps(*m,"main","memory");
    165447  if(length==NULL){
    166448    length=createMap("length","1");
     
    171453    char *mimeType=NULL;
    172454    int fsize=0;
     455    char oriname[12];
    173456    char cname[15];
    174457    char vname[11];
     
    178461    char icname[14];
    179462    char xname[16];
     463    char bname[8];
     464    char hname[11];
    180465    char oname[12];
     466    char ufile[12];   
    181467    if(*index>0)
    182468      sprintf(vname1,"value_%d",*index);
     
    185471   
    186472    if(i>0){
     473      sprintf(cname,"cache_file_%d",i);
    187474      tmp1=getMap(content->content,cname);
    188       sprintf(cname,"cache_file_%d",i);
     475      sprintf(oriname,"origin_%d",i);
    189476      sprintf(vname,"value_%d",i);
    190477      sprintf(sname,"size_%d",i);
     
    192479      sprintf(icname,"isCached_%d",i);
    193480      sprintf(xname,"Reference_%d",i);
     481      sprintf(bname,"body_%d",i);
     482      sprintf(hname,"headers_%d",i);
    194483      sprintf(oname,"Order_%d",i);
     484      sprintf(ufile,"use_file_%d",i);
    195485    }else{
    196486      sprintf(cname,"cache_file");
     487      sprintf(oriname,"origin");
    197488      sprintf(vname,"value");
    198489      sprintf(sname,"size");
     
    200491      sprintf(icname,"isCached");
    201492      sprintf(xname,"Reference");
     493      sprintf(bname,"body");
     494      sprintf(hname,"headers");
    202495      sprintf(oname,"Order");
     496      sprintf(ufile,"use_file");
    203497    }
    204498   
     
    206500    sprintf(sindex,"%d",*index+1);
    207501    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;
    214516        }
    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;
    221517        fsize=hInternet->ihandle[*index].nDataLen;
    222518        if(hInternet->ihandle[*index].mimeType==NULL)
     
    226522       
    227523        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");
    234533        if(hInternet->ihandle[*index].code!=200){
    235534          const char *error_rep_str=_("Unable to download the file for the input <%s>, response code was : %d.");
     
    255554        sprintf(ltmp1,"%d",fsize);
    256555        map* tmp=getMapFromMaps(*m,"main","cacheDir");
     556        char *request=NULL;
    257557        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          }
    259579          char* fname=(char*)malloc(sizeof(char)*(strlen(tmp->value)+strlen(md5str)+6));
    260580          sprintf(fname,"%s/%s.zca",tmp->value,md5str);
     
    264584        addToMap((*in)->content,sname,ltmp1);
    265585        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        }
    268595        free(mimeType);
    269         *index++;
    270        
     596        free(request);
     597        (*index)++;
    271598      }
    272599    }
     
    324651 */
    325652void addRequestToQueue(maps** m,HINTERNET* hInternet,const char* url,bool req){
    326   hInternet->waitingRequests[hInternet->nb]=strdup(url);
     653  hInternet->waitingRequests[hInternet->nb]=zStrdup(url);
    327654  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);
    329656  maps *oreq=getMaps(*m,"orequests");
    330657  if(oreq==NULL){
     
    335662    free(oreq);
    336663  }else{
    337     setMapArray(oreq->content,"value",hInternet->nb-1,url);
     664    setMapArray(oreq->content,"value",hInternet->nb,url);
    338665  }
    339666}
     
    352679  char* cached=isInCache(*m,url);
    353680  char *mimeType=NULL;
    354   int fsize=0;
     681  char *origin=NULL;
     682  long long fsize=0;
     683  map* memUse=getMapFromMaps(*m,"main","memory");
    355684
    356685  map* t=getMap(*content,"xlink:href");
     
    361690
    362691  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);
    366694    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;
    370698      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      }
    373708      addToMap(*content,"cache_file",cached);
     709      unlockFile(*m,lck);
    374710    }
    375711    cached[strlen(cached)-1]='m';
    376     s=stat(cached, &f_status);
     712    s=zStat(cached, &f_status);
    377713    if(s==0){
     714      zooLock* lck=lockFile(*m,cached,'r');
     715      if(lck==NULL)
     716        return -1;
    378717      mimeType=(char*)malloc(sizeof(char)*(f_status.st_size+1));
    379718      FILE* f=fopen(cached,"rb");
     
    381720      mimeType[f_status.st_size]=0;
    382721      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    }
    385737  }else{   
    386738    addRequestToQueue(m,hInternet,url,true);
     
    393745    addToMap(*content,"fmimeType",mimeType);
    394746  }
     747  if(origin!=NULL){
     748    addToMap(*content,"origin",origin);
     749  }
    395750
    396751  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 
    404760  char ltmp1[256];
    405   sprintf(ltmp1,"%d",fsize);
     761  sprintf(ltmp1,"%ld",fsize);
    406762  addToMap(*content,"size",ltmp1);
    407763  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);
    409768  }
    410769  else{
    411770    addToMap(*content,"isCached","true");
    412771    map* tmp=getMapFromMaps(*m,"main","cacheDir");
    413     if(tmp!=NULL){
     772    map* tmp1=getMap((*content),"cache_file");
     773    if(tmp!=NULL && tmp1==NULL){
    414774      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);
    425793  return 0;
    426794}
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png