Index: branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c	(revision 860)
+++ branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c	(revision 862)
@@ -29,4 +29,5 @@
 #define MAX_WAIT_MSECS 180*1000 /* Wait max. 180 seconds */
 #include "ulinet.h"
+#include "server_internal.h"
 #include <assert.h>
 #include <ctype.h>
@@ -67,4 +68,34 @@
   return realsize;
 }
+
+/**
+ * Write the downloaded content to a _HINTERNET structure
+ *
+ * @param buffer the buffer to read
+ * @param size size of each member
+ * @param nmemb number of element to read
+ * @param data the _HINTERNET structure to write in
+ * @return the size red, -1 if buffer is NULL
+ */
+size_t write_data_into_file(void *buffer, size_t size, size_t nmemb, void *data) 
+{ 
+   size_t realsize = size * nmemb;
+   int writen=0;
+   _HINTERNET *psInternet;
+   if(buffer==NULL){
+     buffer=NULL;
+     return -1;
+   }
+   psInternet=(_HINTERNET *)data;
+   writen+=fwrite(buffer, size, nmemb, psInternet->file);
+   if(psInternet->nDataLen>0){
+     psInternet->nDataAlloc+=psInternet->nDataLen+writen+1;
+     psInternet->nDataLen += realsize;
+   }else
+     psInternet->nDataLen=realsize+1;
+   buffer=NULL;
+   return realsize;
+}
+
 
 /**
@@ -363,4 +394,5 @@
 	fclose(handle.file);
 	unlink(handle.filename);
+	free(handle.filename);
       }
       else{
@@ -411,4 +443,5 @@
  * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file)
  * @param dwContext not used
+ * @return the updated HINTERNET 
  */
 HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){
@@ -447,26 +480,22 @@
   curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1);
 #endif
-
       
   switch(dwFlags)
     {
     case INTERNET_FLAG_NO_CACHE_WRITE:
-      hInternet->ihandle[hInternet->nb].hasCacheFile=-1;
       curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into);
       curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]);
+      hInternet->ihandle[hInternet->nb].hasCacheFile=-1;
       break;
     default:
-      sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL));
-      hInternet->ihandle[hInternet->nb].filename[24]=0;
-#ifdef MSG_LAF_VERBOSE
-      fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename);
-#endif
-      hInternet->ihandle[hInternet->nb].filename=filename;
+      memset(filename,0,255);
+      char* tmpUuid=get_uuid();
+      sprintf(filename,"/tmp/ZOO_Cache%s", tmpUuid);
+      free(tmpUuid);
+      hInternet->ihandle[hInternet->nb].filename=strdup(filename);
       hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+");
-    
       hInternet->ihandle[hInternet->nb].hasCacheFile=1;
-      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL);
-      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file);
-      hInternet->ihandle[hInternet->nb].nDataLen=0;
+      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into_file);
+      curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]);
       break;
     }
@@ -514,12 +543,17 @@
  */
 int processDownloads(HINTERNET* hInternet){
-  int still_running=0;
+  int still_running=0,numfds;
   int msgs_left=0;
   int i=0;
   do{
-    if(curl_multi_perform(hInternet->handle, &still_running)==CURLM_OK)
-      if(still_running){
-	zSleep(10);
-      }
+    CURLMcode mc;
+    mc = curl_multi_perform(hInternet->handle, &still_running);
+    if(mc==CURLM_OK){
+      mc = curl_multi_wait(hInternet->handle, NULL, 0, 1000, &numfds);
+    }
+    if(mc != CURLM_OK) {
+      fprintf(stderr, "curl_multi failed, code %d.n", mc);
+      break;
+    }
   }while(still_running);  
   for(i=0;i<hInternet->nb;i++){
@@ -565,5 +599,7 @@
     fseek (hInternet.file , 0 , SEEK_END);
     dwDataSize=ftell(hInternet.file); //taille du ficher
-    rewind (hInternet.file);
+    //dwDataSize=hInternet.nDataLen;
+    //rewind (hInternet.file);
+    fseek(hInternet.file, 0, SEEK_SET);
   }
   else{
@@ -575,13 +611,15 @@
   }
 
-  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize )
+  if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ){
     return 0;
+  }
 
 #ifdef MSG_LAF_VERBOSE
-  printf("\nfile size : %dko\n",dwDataSize/1024);
+  fprintf(stderr,"\nfile size : %dko\n",dwDataSize/1024);
 #endif
 
   if(hInternet.hasCacheFile>0){
-    *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 
+    int freadRes = fread(lpBuffer,dwDataSize+1,1,hInternet.file); 
+    *lpdwNumberOfBytesRead = hInternet.nDataLen;
   }
   else{
