Ignore:
Timestamp:
Feb 5, 2018, 1:19:13 PM (6 years ago)
Author:
djay
Message:

Change the default ZOO-Kernel behavior, if an input has been passed by reference, the ZOO-Service will receive a cache_file map rather than the value field which was usually returned, same for array value apply. To use the previous behavior, one can add "memory=load" to the main section of the main.cfg file. Update ZOO-Services for using this new field if available.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/prototype-v0/zoo-project/zoo-kernel/caching.c

    r862 r863  
    142142 * @param max_path the size of the allocated filepath buffer 
    143143 */
     144void 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 */
    144222void addToCache(maps* conf,char* request,char* content,char* mimeType,int length,
    145223                char* filepath, size_t max_path){
     
    243321int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){
    244322 
     323  int shouldClean=-1;
    245324  map* tmp1;
    246325  char sindex[5];
    247326  maps* content=*in;
    248327  map* length=getMap(content->content,"length");
    249   int shouldClean=-1;
     328  map* memUse=getMapFromMaps(*m,"main","memory");
    250329  if(length==NULL){
    251330    length=createMap("length","1");
     
    266345    char hname[11];
    267346    char oname[12];
     347    char ufile[12];   
    268348    if(*index>0)
    269349      sprintf(vname1,"value_%d",*index);
     
    282362      sprintf(hname,"headers_%d",i);
    283363      sprintf(oname,"Order_%d",i);
     364      sprintf(ufile,"use_file_%d",i);
    284365    }else{
    285366      sprintf(cname,"cache_file");
     
    292373      sprintf(hname,"headers");
    293374      sprintf(oname,"Order");
     375      sprintf(ufile,"use_file");
    294376    }
    295377   
     
    298380    if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){
    299381
    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        }
    312396        fsize=hInternet->ihandle[*index].nDataLen;
    313397        if(hInternet->ihandle[*index].mimeType==NULL)
     
    317401       
    318402        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");
    325412        if(hInternet->ihandle[*index].code!=200){
    326413          char *error_rep_str=_("Unable to download the file for the input <%s>, response code was : %d.");
     
    376463        addToMap((*in)->content,sname,ltmp1);
    377464        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        }
    380472        free(mimeType);
    381473        free(request);
     
    388480    free(length);
    389481  }
     482  dumpMaps(*in);
    390483  return 0;
    391484}
     
    438531  hInternet->waitingRequests[hInternet->nb]=strdup(url);
    439532  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);
    441534  maps *oreq=getMaps(*m,"orequests");
    442535  if(oreq==NULL){
     
    465558  char *mimeType=NULL;
    466559  int fsize=0;
     560  map* memUse=getMapFromMaps(*m,"main","memory");
    467561
    468562  map* t=getMap(*content,"xlink:href");
     
    479573      if(lck==NULL)
    480574        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);
    484575      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      }
    487585      addToMap(*content,"cache_file",cached);
    488586      unlockFile(*m,lck);
     
    513611
    514612  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 
    521621  char ltmp1[256];
    522622  sprintf(ltmp1,"%d",fsize);
    523623  addToMap(*content,"size",ltmp1);
    524624  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);
    526629  }
    527630  else{
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