Ignore:
Timestamp:
Jul 27, 2011, 2:28:25 AM (13 years ago)
Author:
djay
Message:

Small code cleanup, add loadRemoteFile in service_internal to download file and take care of cached files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-kernel/service_internal.c

    r280 r281  
    22992299}
    23002300
     2301
    23012302/**
    23022303 * Cache a file for a given request
     
    23722373  return NULL;
    23732374}
     2375
     2376/**
     2377 * loadRemoteFile:
     2378 * Try to load file from cache or download a remote file if not in cache
     2379 */
     2380void loadRemoteFile(maps* m,map* content,HINTERNET hInternet,char *url){
     2381  HINTERNET res;
     2382  char* fcontent;
     2383  char* cached=isInCache(m,url);
     2384  int fsize;
     2385  if(cached!=NULL){
     2386    fprintf(stderr,"Use cached file: %s\n",cached);
     2387    struct stat f_status;
     2388    int s=stat(cached, &f_status);
     2389    if(s==0){
     2390      fprintf(stderr,"Use cached file: %s\n",cached);
     2391      fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
     2392      FILE* f=fopen(cached,"r");
     2393      fread(fcontent,sizeof(char),f_status.st_size,f);
     2394      fsize=f_status.st_size;
     2395    }
     2396  }else{
     2397    res=InternetOpenUrl(hInternet,url,NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0);
     2398    fcontent=(char*)calloc((res.nDataLen+1),sizeof(char));
     2399    if(fcontent == NULL){
     2400      return errorException(m, _("Unable to allocate memory."), "InternalError");
     2401    }
     2402    size_t dwRead;
     2403    InternetReadFile(res, (LPVOID)fcontent, res.nDataLen, &dwRead);
     2404    fcontent[res.nDataLen]=0;
     2405    fsize=res.nDataLen;
     2406  }
     2407  map* tmpMap=getMapOrEmpty(content,"value");
     2408  free(tmpMap->value);
     2409  tmpMap->value=(char*)malloc((fsize+1)*sizeof(char));
     2410  memcpy(tmpMap->value,fcontent,(fsize)*sizeof(char));
     2411  char ltmp1[256];
     2412  sprintf(ltmp1,"%d",fsize);
     2413  addToMap(content,"size",ltmp1);
     2414  if(cached==NULL)
     2415    addToCache(m,url,fcontent,fsize);
     2416  dumpMap(content);
     2417  free(fcontent);
     2418}
     2419
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