Index: trunk/zoo-kernel/service.h
===================================================================
--- trunk/zoo-kernel/service.h	(revision 280)
+++ trunk/zoo-kernel/service.h	(revision 281)
@@ -203,4 +203,5 @@
   }
 
+
   static map* getLastMap(map* m){
     map* tmp=m;
@@ -497,4 +498,14 @@
   }
 
+  static map* getMapOrEmpty(map* m,const char *key){
+    map* tmp=m;
+    map* tmpMap=getMap(tmp,key);
+    if(tmpMap==NULL){
+      addToMap(tmp,key,"");
+      tmpMap=getMap(tmp,key);
+    }
+    return tmpMap;
+  }
+
   static bool contains(map* m,map* i){
     while(i!=NULL){      
Index: trunk/zoo-kernel/service_internal.c
===================================================================
--- trunk/zoo-kernel/service_internal.c	(revision 280)
+++ trunk/zoo-kernel/service_internal.c	(revision 281)
@@ -2299,4 +2299,5 @@
 }
 
+
 /**
  * Cache a file for a given request
@@ -2372,2 +2373,47 @@
   return NULL;
 }
+
+/**
+ * loadRemoteFile:
+ * Try to load file from cache or download a remote file if not in cache
+ */
+void loadRemoteFile(maps* m,map* content,HINTERNET hInternet,char *url){
+  HINTERNET res;
+  char* fcontent;
+  char* cached=isInCache(m,url);
+  int fsize;
+  if(cached!=NULL){
+    fprintf(stderr,"Use cached file: %s\n",cached);
+    struct stat f_status;
+    int s=stat(cached, &f_status);
+    if(s==0){
+      fprintf(stderr,"Use cached file: %s\n",cached);
+      fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
+      FILE* f=fopen(cached,"r");
+      fread(fcontent,sizeof(char),f_status.st_size,f);
+      fsize=f_status.st_size;
+    }
+  }else{
+    res=InternetOpenUrl(hInternet,url,NULL,0,INTERNET_FLAG_NO_CACHE_WRITE,0);
+    fcontent=(char*)calloc((res.nDataLen+1),sizeof(char));
+    if(fcontent == NULL){
+      return errorException(m, _("Unable to allocate memory."), "InternalError");
+    }
+    size_t dwRead;
+    InternetReadFile(res, (LPVOID)fcontent, res.nDataLen, &dwRead);
+    fcontent[res.nDataLen]=0;
+    fsize=res.nDataLen;
+  }
+  map* tmpMap=getMapOrEmpty(content,"value");
+  free(tmpMap->value);
+  tmpMap->value=(char*)malloc((fsize+1)*sizeof(char));
+  memcpy(tmpMap->value,fcontent,(fsize)*sizeof(char)); 
+  char ltmp1[256];
+  sprintf(ltmp1,"%d",fsize);
+  addToMap(content,"size",ltmp1);
+  if(cached==NULL)
+    addToCache(m,url,fcontent,fsize);
+  dumpMap(content);
+  free(fcontent);
+}
+
Index: trunk/zoo-kernel/service_internal.h
===================================================================
--- trunk/zoo-kernel/service_internal.h	(revision 280)
+++ trunk/zoo-kernel/service_internal.h	(revision 281)
@@ -59,4 +59,5 @@
 
 #include "cgic.h"
+#include "ulinet.h"
 
 extern   int getServiceFromFile(const char*,service**);
@@ -127,4 +128,5 @@
   void addToCache(maps*,char*,char*,int);
   char* isInCache(maps*,char*);
+  void loadRemoteFile(maps*,map*,HINTERNET,char*);
   
 #ifdef __cplusplus
Index: trunk/zoo-kernel/zoo_service_loader.c
===================================================================
--- trunk/zoo-kernel/zoo_service_loader.c	(revision 280)
+++ trunk/zoo-kernel/zoo_service_loader.c	(revision 281)
@@ -1053,47 +1053,5 @@
 #endif
 		{
-		  char* cached=isInCache(m,tmpv1+1);
-		  if(cached!=NULL){
-		    fprintf(stderr,"Use cached file: %s\n",cached);
-		    struct stat f_status;
-		    int s=stat(cached, &f_status);
-		    if(s==0){
-		      map* tmpMap=getMap(tmpmaps->content,"value");
-		      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
-		      FILE* f=fopen(cached,"r");
-		      fread(fcontent,sizeof(char),f_status.st_size,f);
-		      free(tmpMap->value);
-		      tmpMap->value=(char*)malloc((f_status.st_size+1)*sizeof(char));
-		      memmove(tmpMap->value,fcontent,(f_status.st_size)*sizeof(char)); 
-		      free(fcontent);
-		    }
-		  }else{
-		    res=InternetOpenUrl(hInternet,tmpv1+1,NULL,0,
-					INTERNET_FLAG_NO_CACHE_WRITE,0);
-#ifdef DEBUG
-		    fprintf(stderr,"(%s) content-length : %d,,res.nDataAlloc %d \n",
-			    tmpv1+1,res.nDataAlloc,res.nDataLen);
-#endif
-		    char* tmpContent=(char*)calloc((res.nDataLen+1),sizeof(char));
-		    if(tmpContent == NULL){
-		      return errorException(m, _("Unable to allocate memory."), "InternalError");
-		    }
-		    size_t dwRead;
-		    InternetReadFile(res, (LPVOID)tmpContent,res.nDataLen, &dwRead);
-		    map* tmpMap=getMap(tmpmaps->content,"value");
-		    if(tmpMap!=NULL){
-		      free(tmpMap->value);
-		      tmpMap->value=(char*)malloc((res.nDataLen+1)*sizeof(char));
-		      memmove(tmpMap->value,tmpContent,(res.nDataLen)*sizeof(char));
-		      tmpMap->value[res.nDataLen]=0;
-		      if(strlen(tmpContent)!=res.nDataLen){
-			char tmp[256];
-			sprintf(tmp,"%d",res.nDataLen*sizeof(char));
-			addToMap(tmpmaps->content,"size",tmp);
-		      }
-		      addToCache(m,tmpv1+1,tmpContent,res.nDataLen);
-		    }
-		    free(tmpContent);
-		  }
+		  loadRemoteFile(m,tmpmaps->content,hInternet,tmpv1+1);
 		}
 	      char *tmpx1=url_encode(tmpv1+1);
@@ -1237,16 +1195,5 @@
 		  if(!(ltmp!=NULL && strcmp(ltmp->value,"POST")==0)
 		     && CHECK_INET_HANDLE(hInternet)){
-		    res=InternetOpenUrl(hInternet,(char*)val,NULL,0,
-					INTERNET_FLAG_NO_CACHE_WRITE,0);
-		    char* tmpContent=
-		      (char*)calloc((res.nDataLen+1),sizeof(char));
-		    if(tmpContent == NULL){
-		      return errorException(m, _("Unable to allocate memory."), "InternalError");
-		    }
-		    size_t dwRead;
-		    InternetReadFile(res, (LPVOID)tmpContent,
-				     res.nDataLen, &dwRead);
-		    tmpContent[res.nDataLen]=0;
-		    addToMap(tmpmaps->content,"value",tmpContent);
+		    loadRemoteFile(m,tmpmaps->content,hInternet,(char*)val);
 		  }
 		}
