Index: branches/prototype-v0/zoo-project/zoo-kernel/caching.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/caching.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/caching.c	(revision 862)
@@ -298,5 +298,5 @@
     if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){
 
-      if(getMap(content->content,icname)==NULL){	
+      if(getMap(content->content,icname)==NULL){
 	fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char));
 	if(fcontent == NULL){
@@ -513,5 +513,4 @@
 
   map* tmpMap=getMapOrFill(content,"value","");
-    
   free(tmpMap->value);
   tmpMap->value=(char*)malloc((fsize+1)*sizeof(char));
Index: branches/prototype-v0/zoo-project/zoo-kernel/response_print.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/response_print.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/response_print.c	(revision 862)
@@ -2562,4 +2562,6 @@
       xmlNodePtr txt=xmlNewText(BAD_CAST tmp->value);
       xmlAddChild(nc1,txt);
+      if(cnt==0)
+	setMapInMaps(m,"lenv","message",tmp->value);
     }
     else{
@@ -2718,7 +2720,5 @@
       toto=getMap(tmpI->content,"asReference");
 #ifdef USE_MS
-    restartNoMS:
       map* geodatatype=getMap(tmpI->content,"geodatatype");
-      
       if(toto!=NULL && strcasecmp(toto->value,"true")==0 &&
 	 (testMap==NULL ||
@@ -2749,5 +2749,5 @@
 	    map *ext=getMap(tmpI->content,"extension");
 	    char file_ext[32];
-
+	    
 	    if( ext != NULL && ext->value != NULL) {
 	      strncpy(file_ext, ext->value, 32);
@@ -2812,14 +2812,4 @@
 	  }
 	  addToMap(tmpI->content,"Reference",file_url);
-	  /*maps* curs=tmpI;
-	  curs=curs->next;
-	  map* test=getMap(tmpI->content,"replicateStorageNext");
-	  if(test!=NULL && strncasecmp(test->value,"true",4)==0)
-	    while(curs!=NULL){
-	      addToMap(curs->content,"storage",file_path);
-	      curs=curs->next;
-	      }
-	  if(file_path!=NULL)
-	  free(file_path);*/
 	  if(file_name!=NULL)
 	    free(file_name);
@@ -2830,12 +2820,12 @@
 #ifdef USE_MS
       else{
-	if(testMap!=NULL){
-	  setMapInMaps(m,"lenv","state","out");
-	  setReferenceUrl(m,tmpI);
-	  geodatatype=getMap(tmpI->content,"geodatatype");
-	  if(geodatatype!=NULL && strcasecmp(geodatatype->value,"other")==0)
-	    goto restartNoMS;
-	}
-      }
+	  if(testMap!=NULL){
+	    setMapInMaps(m,"lenv","state","out");
+	    setReferenceUrl(m,tmpI);
+	    geodatatype=getMap(tmpI->content,"geodatatype");
+	    if(geodatatype!=NULL && strcasecmp(geodatatype->value,"other")==0)
+	      res=SERVICE_FAILED;
+	  }
+	}
 #endif
       if(file_name!=NULL){
Index: branches/prototype-v0/zoo-project/zoo-kernel/server_internal.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/server_internal.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/server_internal.c	(revision 862)
@@ -377,13 +377,18 @@
   struct stat file_status; 
   stat(filename, &file_status);
-  map* tmpMap1=getMap(content,"value");
-  if(tmpMap1==NULL){
-    addToMap(content,"value","");
-    tmpMap1=getMap(content,"value");
-  }
-  free(tmpMap1->value);
-  tmpMap1->value=(char*) malloc((count+1)*sizeof(char));  
-  fread(tmpMap1->value,1,count,file);
-  tmpMap1->value[count]=0;
+  if(getMap(content,"storage")==NULL){
+    map* tmpMap1=getMap(content,"value");
+    if(tmpMap1==NULL){
+      addToMap(content,"value","");
+      tmpMap1=getMap(content,"value");
+    }
+    free(tmpMap1->value);
+    tmpMap1->value=(char*) malloc((count+1)*sizeof(char));
+    if(tmpMap1->value==NULL){
+      setMapInMaps(m,"lenv","message","Unable to allocate the memory required to read the produced file.");
+    }
+    fread(tmpMap1->value,1,count,file);
+    tmpMap1->value[count]=0;
+  }
   fclose(file);
   char rsize[1000];
@@ -423,4 +428,5 @@
 void dumpMapsValuesToFiles(maps** main_conf,maps** in){
   map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
+  map* tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
   map* tmpSid=getMapFromMaps(*main_conf,"lenv","usid");
   maps* inputs=*in;
@@ -450,4 +456,9 @@
 	  setMapArray(cMap,"cache_file",k,val);
 	  free(val);
+	  val=(char*)malloc((strlen(tmpUrl->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char));
+	  sprintf(val,"%s/Input_%s_%s_%d.%s",tmpUrl->value,inputs->name,tmpSid->value,k,file_ext);
+	  setMapArray(cMap,"cache_url",k,val);
+	  setMapArray(cMap,"byValue",k,"true");
+	  free(val);
 	}
       }else{
@@ -467,6 +478,10 @@
 	addToMap(cMap,"cache_file",val);
 	free(val);
-      }
-      addToMap(inputs->content,"byValue","true");
+	val=(char*)malloc((strlen(tmpUrl->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char));
+	sprintf(val,"%s/Input_%s_%s_%d.%s",tmpUrl->value,inputs->name,tmpSid->value,0,file_ext);
+	addToMap(cMap,"cache_url",val);
+	addToMap(cMap,"byValue",val);
+	free(val);
+      }
     }
     inputs=inputs->next;
@@ -641,4 +656,11 @@
 	}
 	if(res==NULL){
+	  map* tmpMaxO0=getMap(tmpInputs->content,"useMapserver");
+	  if(tmpMaxO0!=NULL){
+	    if(tmpMaps2->content==NULL)
+	      tmpMaps2->content=createMap("useMapserver",tmpMaxO0->value);
+	    else
+	      addToMap(tmpMaps2->content,"useMapserver",tmpMaxO0->value);
+	  }
 	  map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs");
 	  if(tmpMaxO!=NULL){
@@ -714,24 +736,16 @@
 	   * content map.
 	   */
-	  map* tmpMap1=getMap(tmpInputs->content,"minOccurs");
-	  if(tmpMap1!=NULL){
-	    if(tmpMaps->content==NULL)
-	      tmpMaps->content=createMap("minOccurs",tmpMap1->value);
-	    else
-	      addToMap(tmpMaps->content,"minOccurs",tmpMap1->value);
-	  }
-	  map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs");
-	  if(tmpMaxO!=NULL){
-	    if(tmpMaps->content==NULL)
-	      tmpMaps->content=createMap("maxOccurs",tmpMaxO->value);
-	    else
-	      addToMap(tmpMaps->content,"maxOccurs",tmpMaxO->value);
-	  }
-	  map* tmpMaxMB=getMap(tmpInputs->content,"maximumMegabytes");
-	  if(tmpMaxMB!=NULL){
-	    if(tmpMaps->content==NULL)
-	      tmpMaps->content=createMap("maximumMegabytes",tmpMaxMB->value);
-	    else
-	      addToMap(tmpMaps->content,"maximumMegabytes",tmpMaxMB->value);
+	  char* keys[4]={
+	    "minOccurs",
+	    "maxOccurs",
+	    "maximumMegabytes",
+	    "useMapserver"
+	  };
+	  int i=0;
+	  for(i=0;i<4;i++){
+	    map* tmpMap1=getMap(tmpInputs->content,keys[i]);
+	    if(tmpMap1!=NULL){
+	      addToMap(tmpMaps->content,keys[i],tmpMap1->value);
+	    }
 	  }
 	  /**
@@ -812,4 +826,5 @@
 	  if(tmpCheck!=NULL && strncasecmp(tmpCheck->value,"true",4)==0){
 	    // Get the default value
+	    addToMap(tmpMaps->content,"useMapserver","true");
 	    tmpIoType=getIoTypeFromElement(tmpInputs,tmpInputs->name,NULL);
 	    tmpCheck=getMap(tmpMaps->content,"mimeType");
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_callback.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_callback.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_callback.c	(revision 862)
@@ -112,5 +112,4 @@
   }
 
-
   /**
    * Practically invoke the callback, meaning sending the HTTP POST request.
@@ -121,4 +120,9 @@
     local_params* arg=(local_params*)args;
     HINTERNET hInternet,res1;
+    const struct tm *tm;
+    size_t len;
+    time_t now;
+    char *tmp1;
+    map *tmpStatus;
     hInternet=InternetOpen("ZooWPSClient\0",
 			   INTERNET_OPEN_TYPE_PRECONFIG,
@@ -134,8 +138,14 @@
     free(URL);
 #ifdef CALLBACK_DEBUG
+    now = time ( NULL );
+    tm = localtime ( &now );
+    tmp1 = (char*)malloc((TIME_SIZE+1)*sizeof(char));
+    len = strftime ( tmp1, TIME_SIZE, "%Y-%m-%dT%I:%M:%SZ", tm );
     fprintf(stderr,"************************* From thread %d %s %d: REQUEST PARAMETERS cStep %d %d\n",pthread_self(),__FILE__,__LINE__,cStep,isOngoing);
     fprintf(stderr," * JSON: [%s] \n",jsonStr);
     fprintf(stderr," * URL: %s/ \n\n",hInternet.waitingRequests[0]);
+    fprintf(stderr," * DATE: %s/ \n\n",tmp1);
     fprintf(stderr,"************************* From thread %d %s %d: REQUEST PARAMETERS\n",pthread_self(),__FILE__,__LINE__);
+    free(tmp1);
 #endif
     while( (arg->step!=7 || isOngoing>0) &&
@@ -152,9 +162,4 @@
     }
 #endif
-    const struct tm *tm;
-    size_t len;
-    time_t now;
-    char *tmp1;
-    map *tmpStatus;
     
     now = time ( NULL );
@@ -184,5 +189,5 @@
 #ifdef CALLBACK_DEBUG    
     fprintf(stderr,"************************* From thread %d %s %d: REQUEST END (%s)\n\n",pthread_self(),__FILE__,__LINE__,tmp1);
-#endif    
+#endif
     free(tmp1);
     char *tmp = (char *) malloc ((hInternet.ihandle[0].nDataLen + 1)
@@ -206,6 +211,10 @@
     if(cStep==0 || cStep==6 || arg->state==1)
       cStep=arg->step+1;
-#ifdef CALLBACK_DEBUG    
-    fprintf(stderr,"************************* From thread %d %s %d: RESPONSE CONTENT\n",pthread_self(),__FILE__,__LINE__);
+#ifdef CALLBACK_DEBUG
+    now = time ( NULL );
+    tm = localtime ( &now );
+    tmp1 = (char*)malloc((TIME_SIZE+1)*sizeof(char));
+    len = strftime ( tmp1, TIME_SIZE, "%Y-%m-%dT%I:%M:%SZ", tm );
+    fprintf(stderr,"************************* From thread %d %s %d: RESPONSE CONTENT (%s)\n",pthread_self(),__FILE__,__LINE__,,tmp1);
     for(i=0;i<7;i++){
       fprintf(stderr,"%d) %d %d\n",i,steps[i][0],steps[i][1]);
@@ -214,10 +223,12 @@
     fprintf(stderr,"************************* From thread %d %s %d\n\n",pthread_self(),__FILE__,__LINE__);
     fflush(stderr);
+    free(tmp1);
 #endif
     steps[arg->step][arg->state]=true;
     free(tmp);
-    //free(args);
+#ifdef CALLBACK_DEBUG
     fprintf(stderr,"************************* From thread %d %s %d: EXIT\n\n",pthread_self(),__FILE__,__LINE__);
     fflush(stderr);
+#endif
     pthread_exit(NULL);
   }
@@ -305,111 +316,93 @@
       // Update the execute request stored on disk at step 0,0 to modify the references used.
       if(state==1){
-	fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	fflush(stderr);
 	maps* curs=inputs;
-	fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	fflush(stderr);
 	xmlInitParser();
-	fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	fflush(stderr);
 	map* xmlPath=getMapFromMaps(conf,"lenv","execute_file");
-	dumpMap(xmlPath);
-	fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	fflush(stderr);
 	while(curs!=NULL){
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
-	  dumpMap(curs->content);
-	  //map* bvMap=getMap(curs->content,"byValue");
-	  // TODO handle mapArray
-	  //if(bvMap!=NULL && strncasecmp(bvMap->value,"true",4)==0){
-	  if(getMap(curs->content,"href")==NULL && getMap(curs->content,"mimeType")!=NULL){
-	    map* tmpMap=getMap(curs->content,"value");
-	    char tmpStr[100];
-	    sprintf(tmpStr,"%d",strlen(tmpMap->value));
-	    addToMap(curs->content,"size",tmpStr);
-	    tmpMap=getMap(curs->content,"mimeType");
-	    addToMap(curs->content,"fmimeType",tmpMap->value);
-	    tmpMap=getMap(curs->content,"cache_file");
-	    addToMap(curs->content,"generated_file",tmpMap->value);
-	    addToMap(curs->content,"storage",tmpMap->value);
-	    setReferenceUrl(conf,curs);
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    dumpMap(curs->content);
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    const char *params[5];
-	    int xmlLoadExtDtdDefaultValue;
-	    int hasFile=-1;
-	    map* xslPath=getMapFromMaps(conf,"callback","template");
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    dumpMap(xslPath);
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    map* filePath=getMap(curs->content,"ref_wfs_link");
-	    if(filePath==NULL)
-	      filePath=getMap(curs->content,"ref_wcs_link");
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    dumpMap(filePath);
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    char* inputName=curs->name;
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    if(xslPath==NULL || xmlPath==NULL || filePath==NULL)
-	      break;
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    char *tmpParam=(char*)malloc((strlen(curs->name)+11)*sizeof(char));
-	    char *tmpParam1=(char*)malloc((strlen(filePath->value)+11)*sizeof(char));
-	    addToMap(curs->content,"href",filePath->value);
-	    addToMap(curs->content,"xlink:href",filePath->value);
-	    sprintf(tmpParam,"string(\"%s\")",curs->name);
-	    sprintf(tmpParam1,"string(\"%s\")",filePath->value);
-	    params[0]="attr";
-	    params[1]=tmpParam;
-	    params[2]="value";
-	    params[3]=tmpParam1;//filePath->value;
-	    params[4]=NULL;
-	    fprintf(stderr, "## XSLT PARAMETERS ATTR: %s VALUE: %s \n",
-		    tmpParam,tmpParam1);
-	    xmlSubstituteEntitiesDefault(1);
-	    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	    fflush(stderr);
-	    xmlLoadExtDtdDefaultValue = 0;
-	    xsltStylesheetPtr cur = NULL;
-	    xmlDocPtr doc, res;
-	    cur = xsltParseStylesheetFile(BAD_CAST xslPath->value);
-	    doc = xmlParseFile(xmlPath->value);
-	    fflush(stderr);
-	    res = xsltApplyStylesheet(cur, doc, params);
-	    xmlChar *xmlbuff;
-	    int buffersize;
-	    xmlDocDumpFormatMemory(res, &xmlbuff, &buffersize, 1);
-	    // Store the executeRequest in file again
-	    free(tmpParam);
-	    free(tmpParam1);
-	    fprintf(stderr," # Request / XSLT: %s\n",xmlbuff);
-	    fflush(stderr);
-	    FILE* saveExecute=fopen(xmlPath->value,"wb");
-	    fwrite(xmlbuff,1,buffersize,saveExecute);
-	    fflush(saveExecute);
-	    fclose(saveExecute);
-	    xmlFree(xmlbuff);
-	    xmlFreeDoc(doc);
-	    xsltFreeStylesheet(cur);
+	  map* length=getMap(curs->content,"length");
+	  map* useMS=getMap(curs->content,"useMapserver");
+	  if(length==NULL){
+	    addToMap(curs->content,"length","1");
+	    length=getMap(curs->content,"length");
 	  }
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
+	  int len=atoi(length->value);
+	  for(int ii=0;ii<len;ii++){
+	    if(getMapArray(curs->content,"byValue",ii)!=NULL && getMapArray(curs->content,"mimeType",ii)!=NULL && useMS!=NULL && strncasecmp(useMS->value,"true",4)==0){
+	      map* tmpMap=getMapArray(curs->content,"value",ii);
+	      char tmpStr[100];
+	      sprintf(tmpStr,"%d",strlen(tmpMap->value));
+	      setMapArray(curs->content,"size",ii,tmpStr);
+	      tmpMap=getMapArray(curs->content,"mimeType",ii);
+	      setMapArray(curs->content,"fmimeType",ii,tmpMap->value);
+	      tmpMap=getMapArray(curs->content,"cache_file",ii);
+	      setMapArray(curs->content,"generated_file",ii,tmpMap->value);
+	      setMapArray(curs->content,"storage",ii,tmpMap->value);
+	      setReferenceUrl(conf,curs);
+	      addIntToMap(curs->content,"published_id",ii+1);
+	      const char *params[7];
+	      int xmlLoadExtDtdDefaultValue;
+	      int hasFile=-1;
+	      map* xslPath=getMapFromMaps(conf,"callback","template");
+	      map* filePath=getMapArray(curs->content,"ref_wfs_link",ii);
+	      if(filePath==NULL)
+		filePath=getMap(curs->content,"ref_wcs_link");
+	      char* inputName=curs->name;
+	      if(xslPath==NULL || xmlPath==NULL || filePath==NULL)
+		break;
+	      char *tmpParam=(char*)malloc((strlen(curs->name)+11)*sizeof(char));
+	      char *tmpParam1=(char*)malloc((strlen(filePath->value)+11)*sizeof(char));
+	      char tmpParam2[16];
+	      sprintf(tmpParam2,"string(\"%d\")",ii);
+	      setMapArray(curs->content,"href",ii,filePath->value);
+	      setMapArray(curs->content,"xlink:href",ii,filePath->value);
+	      tmpMap=getMapArray(curs->content,"cache_url",ii);
+	      if(tmpMap!=NULL)
+		setMapArray(curs->content,"xlink:href",ii,tmpMap->value);
+	      else
+		setMapArray(curs->content,"xlink:href",ii,filePath->value);
+	      sprintf(tmpParam,"string(\"%s\")",curs->name);
+	      sprintf(tmpParam1,"string(\"%s\")",filePath->value);
+	      sprintf(tmpParam2,"string(\"%d\")",ii);
+	      params[0]="attr";
+	      params[1]=tmpParam;
+	      params[2]="value";
+	      params[3]=tmpParam1;//filePath->value;
+	      params[4]="cnt";
+	      params[5]=tmpParam2;
+	      params[6]=NULL;
+	      fprintf(stderr, "## XSLT PARAMETERS ATTR: %s VALUE: %s INDEX: %s\n",
+		      tmpParam,tmpParam1,tmpParam2);
+	      fflush(stderr);
+	      xmlSubstituteEntitiesDefault(1);
+	      xmlLoadExtDtdDefaultValue = 0;
+	      xsltStylesheetPtr cur = NULL;
+	      xmlDocPtr doc, res;
+	      cur = xsltParseStylesheetFile(BAD_CAST xslPath->value);
+	      doc = xmlParseFile(xmlPath->value);
+	      fflush(stderr);
+	      res = xsltApplyStylesheet(cur, doc, params);
+	      xmlChar *xmlbuff;
+	      int buffersize;
+	      xmlDocDumpFormatMemory(res, &xmlbuff, &buffersize, 1);
+	      // Store the executeRequest in file again
+	      free(tmpParam);
+	      free(tmpParam1);
+	      fprintf(stderr," # Request / XSLT: %s\n",xmlbuff);
+	      fflush(stderr);
+	      FILE* saveExecute=fopen(xmlPath->value,"wb");
+	      if(saveExecute!=NULL){
+		fwrite(xmlbuff,1,buffersize,saveExecute);
+		fflush(saveExecute);
+		fclose(saveExecute);
+	      }
+	      xmlFree(xmlbuff);
+	      xmlFreeDoc(doc);
+	      xsltFreeStylesheet(cur);
+	    }
+	  }
+	  addIntToMap(curs->content,"published_id",0);
 	  curs=curs->next;
 	}
-	fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	fflush(stderr);
 	xmlCleanupParser();
-	fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	fflush(stderr);
 	FILE* f0=fopen(xmlPath->value,"rb");
 	if(f0!=NULL){
@@ -423,25 +416,11 @@
 	  fcontent[flen]=0;
 	  fclose(f0);
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
 	  map *schema=getMapFromMaps(conf,"database","schema");
 	  map* sid=getMapFromMaps(conf,"lenv","usid");
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
 	  char *req=(char*)malloc((flen+strlen(schema->value)+strlen(sid->value)+66)*sizeof(char));
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
 	  sprintf(req,"UPDATE %s.services set request_execute_content=$$%s$$ WHERE uuid=$$%s$$",schema->value,fcontent,sid->value);
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
 	  execSql(conf,1,req);
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
 	  free(fcontent);
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
 	  free(req);
-	  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	  fflush(stderr);
 	}
       }
@@ -453,5 +432,5 @@
 	{
 	  "xlink:href",
-	  "ref"
+	  "ref_download_link"
 	},
 	{
@@ -514,6 +493,4 @@
 	      setMapArray(curs->content,"storage",ii,tmpMap->value);
 	    }
-	    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
-	    dumpMap(curs->content);
 	    struct stat buf;
 	    char timeStr[ 100 ] = "";
@@ -553,6 +530,8 @@
 	if(hasRef<0)
 	  json_object_put(res3);
-	else
-	  json_object_object_add(res1,curs->name,res3);
+	else{
+	  json_object_object_add(res1,curs->name,json_object_get(res3));
+	  json_object_put(res3);
+	}
 	addIntToMap(curs->content,"published_id",0);
 	curs=curs->next;
@@ -570,5 +549,4 @@
 	if(length!=NULL){
 	  json_object *res1=json_object_new_object();
-	  json_object *res3=json_object_new_array();
 	  int limit=atoi(length->value);
 	  int i=0;
@@ -589,20 +567,17 @@
 		json_object_object_add(res1,tmp0->value,res2);
 	      }else{
-		if(json_object_is_type(res4,json_type_object)){
-		  json_object_array_add(res3,res4);
-		}
-		json_object_array_add(res3,res2);
-		if(json_object_is_type(res4,json_type_object)){
+		if(json_object_is_type(res4,json_type_object) && !json_object_is_type(res4, json_type_array)){
+		  json_object *res3=json_object_new_array();
+		  json_object_array_add(res3,json_object_get(res4));
+		  json_object_array_add(res3,res2);
 		  json_object_object_del(res1,tmp0->value);
 		  json_object_object_add(res1,tmp0->value,res3);
-		}
+		}else
+		  json_object_array_add(res4,res2);
 	      }
 	    }
 	  }
-	  if(json_object_array_length(res3)==0)
-	    json_object_put(res3);
 	  json_object_object_add(res,"inputs",res1);
 	}
-	//json_object_object_add(res,"inputs",in);
       }
       break;
@@ -631,5 +606,4 @@
     case 5: {
       // Downloading process outputs from cluster
-      //json_object* in=mapsToJson(outputs);
       maps* curs=outputs;
       dumpMaps(curs);
@@ -708,6 +682,17 @@
 	    json_object *jsStr=json_object_new_string(sid->value);
 	    json_object_object_add(res2,keys[i][1],jsStr);
-	    if(i==0)
+	    if(i==0){
 	      hasRef=1;
+	      json_object_object_add(res2,"ref_download_link",jsStr);
+	    }
+	    if(i==1){
+	      struct stat buf;
+	      char timeStr[ 100 ] = "";
+	      if (stat(sid->value, &buf)==0){
+		strftime(timeStr, 100, "%d-%m-%Y %H:%M:%S", localtime( &buf.st_mtime));
+		json_object *jsStr=json_object_new_string(timeStr);
+		json_object_object_add(res2,"creation_date",jsStr);
+	      }
+	    }
 	  }
 	}
@@ -724,8 +709,5 @@
 	      int i0=0;
 	      for(;i0<6;i0++){
-		if(i0==0)
-		  sid=getMap(specificMaps->content,specifics[i][1]);
-		else
-		  sid=getMap(specificMaps->content,keys[i0][0]);
+		sid=getMap(specificMaps->content,keys[i0][0]);
 		if(sid!=NULL){
 		  json_object *jsStr=json_object_new_string(sid->value);
@@ -760,5 +742,4 @@
 		  json_object *jsStr=json_object_new_string(sid->value);
 		  json_object_object_add(res3,keys[i0][1],jsStr);
-		  //if(i0==0)
 		  hasRef0=1;
 		}
@@ -858,5 +839,4 @@
     local_arguments[nbThreads]->step=step;
     local_arguments[nbThreads]->state=state;
-    //pthread_t p1;
     if(myThreads==NULL)
       myThreads=(pthread_t*)malloc((nbThreads+1)*sizeof(pthread_t));
@@ -867,5 +847,4 @@
       return false;
     }
-    //free(argumentsA);
     nbThreads++;
     return true;
@@ -878,6 +857,4 @@
     int i=0;
     for(i=0;i<nbThreads;i++){
-      fprintf(stderr,"%s %d %d \n",__FILE__,__LINE__,i);
-      fflush(stderr);
       pthread_join(myThreads[i],NULL);
       free(local_arguments[i]);
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal.c	(revision 862)
@@ -62,4 +62,6 @@
     if(itn<ZOO_LOCK_MAX_RETRY){
       itn++;
+      fprintf(stderr,"(%d) Wait for write lock on %s, tried %d times (sleep) ... \n",getpid(),myLock->filename,itn);
+      fflush(stderr);
       sleep(5);
       free(myLock->filename);
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c	(revision 862)
@@ -60,5 +60,6 @@
   while(cur!=NULL && cur->defaults!=NULL){
     map* mimeType=getMap(cur->defaults->content,"mimeType");
-    if(mimeType!=NULL){
+    map* useMS=getMap(cur->defaults->content,"useMapserver");
+    if(mimeType!=NULL && useMS!=NULL && strncasecmp(useMS->value,"true",4)==0){
       int geo=isGeographic(mimeType->value);
       if(geo>0){
@@ -159,5 +160,5 @@
 	free(tmp[0]);
 	//addToMap(cur->content,"internal","true");
-      }
+      }      
     }
     cur=cur->next;
@@ -332,5 +333,9 @@
   int parameters_cnt=0;
   while(input!=NULL && input->content!=NULL){
-    if(getMaps(*real_outputs,input->name)==NULL){
+    map* isInRequest=getMap(input->content,"inRequest");
+    map* minNb=getMap(input->content,"minoccurs");
+    if(getMaps(*real_outputs,input->name)==NULL &&
+       ( (isInRequest!=NULL && strncasecmp(isInRequest->value,"true",4)==0)
+	 || (minNb!=NULL && atoi(minNb->value)>0) ) ){
       parameters_cnt+=1;
       if(parameters_cnt==1)
@@ -345,5 +350,5 @@
 	  dumpMapsValuesToFiles(main_conf,&input);
 	  addToMap(input->content,"toPublish","true");
-	  addToMap(input->content,"useMapserver","true");
+	  //addToMap(input->content,"useMapserver","true");
 	}
 	if(getMap(input->content,"cache_file")!=NULL){
@@ -362,16 +367,25 @@
 	    setMapArray(input->content,"targetPath",i,targetPath);
 	    setMapArray(input->content,"localPath",i,tmp->value);
-	    addToUploadQueue(main_conf,input);
+	    map* tmp1=getMapArray(input->content,"value",i);
+	    if(tmp1!=NULL){
+	      free(tmp1->value);
+	      tmp1->value=strdup("empty");
+	    }
 	    if(i==0){
 	      parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
 	      sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
 	    }else{
+	      fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+	      fflush(stderr);
 	      char *tmpStr=zStrdup(parameters[parameters_cnt-1]);
 	      parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char));
 	      sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath);
 	      free(tmpStr);
+	      fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+	      fflush(stderr);
 	    }
 	    free(targetPath);
 	  }
+	  addToUploadQueue(main_conf,input);
 	}else{
 	  // ???
@@ -396,9 +410,9 @@
 #endif
   invokeCallback(m,inputs,NULL,1,1);
+  invokeCallback(m,inputs,NULL,2,0);
   if(getMapFromMaps(m,"lenv","mapError")!=NULL){
-    invokeCallback(m,inputs,NULL,7,0);
+    invokeCallback(*main_conf,inputs,NULL,7,0);
     return -1;
   }
-  invokeCallback(m,inputs,NULL,2,0);
 #ifdef HPC_DEBUG
   fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -408,10 +422,10 @@
   // Upload data on HPC
   if(runUpload(main_conf)==false){
-    errorException (m, _("Unable to lock the file for upload!"),
+    errorException (*main_conf, _("Unable to lock the file for upload!"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 #endif
-    invokeCallback(m,inputs,NULL,7,0);
+    invokeCallback(*main_conf,inputs,NULL,7,0);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -429,11 +443,9 @@
   // Add the filename to generate for every output to parameters
   input=*real_outputs;
-  // TODO: fix appendOutputParameters
-  //appendOutputParameters(input,parameters,&parameters_cnt,s,uuid,targetPathMap);
 #ifdef HPC_DEBUG
   dumpMaps(input);
 #endif
   while(input!=NULL){
-    // TODO: parse all outputs including inner outputs if required.
+    // Parse all outputs including inner outputs if required.
     if(input->child==NULL){
       // Name every files that should be produced by the service execution
@@ -453,9 +465,12 @@
       char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
       sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
+      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
+      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
+      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
       free(targetName);
       setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
-      if(strcasecmp(input->name,"wms_link")!=0&&
-	 strcasecmp(input->name,"wcs_link")!=0 &&
-	 strcasecmp(input->name,"wfs_link")!=0){
+      addToMap(input->content,"generated_url",targetUrl);
+      free(targetUrl);
+      {
 	parameters_cnt+=1;
 	if(parameters_cnt==1)
@@ -494,7 +509,12 @@
       char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
       sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
+      map *tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
+      char *targetUrl=(char*)malloc((strlen(tmpUrl->value)+strlen(targetName)+2)*sizeof(char));
+      sprintf(targetUrl,"%s/%s",tmpUrl->value,targetName);
       free(targetName);
       addToMap(input->content,"generated_file",targetPath);
       addToMap(input->content,"storage",targetPath);
+      addToMap(input->content,"generated_url",targetUrl);
+      free(targetUrl);
       if(strcasecmp(input->name,"wms_link")!=0&&
 	 strcasecmp(input->name,"wcs_link")!=0 &&
@@ -623,5 +643,5 @@
     setMapInMaps(*main_conf,"lenv","message",_("There is no remote_work_path defined in your section!"));
     setMapInMaps(*main_conf,"lenv","status","failed");
-    errorException (m, _("There is no remote_work_path defined in your section!"),
+    errorException (*main_conf, _("There is no remote_work_path defined in your section!"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -629,5 +649,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -645,7 +665,7 @@
   free(scriptPath);
   if(copy0!=true){
-    setMapInMaps(m,"lenv","message",_("Unable to upload the script"));
-    invokeCallback(m,NULL,NULL,7,0);
-    errorException(m,_("Unable to upload the script"),"NoApplicableCode",NULL);
+    setMapInMaps(*main_conf,"lenv","message",_("Unable to upload the script"));
+    invokeCallback(*main_conf,NULL,NULL,7,0);
+    errorException(*main_conf,_("Unable to upload the script"),"NoApplicableCode",NULL);
     return -1;
   }
@@ -688,5 +708,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
     fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -694,10 +714,9 @@
 #endif
     sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s using %s: %s", s->name, configurationId, tmpPath->value);
-    errorException(m,tmpS,"NoApplicableCode",NULL);
+    errorException(*main_conf,tmpS,"NoApplicableCode",NULL);
     free(command);
     free(targetPath);
     ssh_close(*main_conf);
     removeReadLocks(main_conf);
-    sleep(1);
     return -1;
   }
@@ -728,6 +747,6 @@
   if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
     perror("bind error");
-    setMapInMaps(m,"lenv","message",_("Unable to bind socket!"));
-    errorException (m, _("Unable to bind socket!"),
+    setMapInMaps(*main_conf,"lenv","message",_("Unable to bind socket!"));
+    errorException (*main_conf, _("Unable to bind socket!"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -735,5 +754,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
     removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -749,5 +768,5 @@
   if (listen(fd, 5) == -1) {
     setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
-    errorException (m, _("Listen error"),
+    errorException (*main_conf, _("Listen error"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -755,5 +774,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
     removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -765,5 +784,5 @@
   if ( (cl = accept(fd, NULL, NULL)) == -1) {
     setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
-    errorException (m, _("Accept error"),
+    errorException (*main_conf, _("Accept error"),
 		    "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -771,5 +790,5 @@
     fflush(stderr);
 #endif
-    invokeCallback(m,NULL,NULL,7,0);
+    invokeCallback(*main_conf,NULL,NULL,7,0);
     removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -788,7 +807,6 @@
     while ( (rc=read(cl,buf,10)) ) {	  
       if(rc==0){
-	sleep(1);
 	setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
-	errorException (m, _("Read closed"),
+	errorException (*main_conf, _("Read closed"),
 			"InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -796,5 +814,5 @@
 	fflush(stderr);
 #endif
-	invokeCallback(m,NULL,NULL,7,0);
+	invokeCallback(*main_conf,NULL,NULL,7,0);
 	removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -806,5 +824,5 @@
 	if(rc<0){
 	  setMapInMaps(*main_conf,"lenv","message",_("Read error"));
-	  errorException (m, _("Read error"),
+	  errorException (*main_conf, _("Read error"),
 			  "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -812,5 +830,5 @@
 	  fflush(stderr);
 #endif
-	  invokeCallback(m,NULL,NULL,7,0);
+	  invokeCallback(*main_conf,NULL,NULL,7,0);
 	  removeReadLocks(main_conf);
 #ifdef HPC_DEBUG
@@ -872,9 +890,31 @@
 		free(targetPath);
 	      }else{
-		char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
-		sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
-		setMapInMaps(m,"lenv","message",tmpStr);
-		free(tmpStr);
-		invokeCallback(m,NULL,NULL,7,0);
+                map* hpcStdErr=getMapFromMaps(*main_conf,"henv","StdErr");
+                if(hpcStdErr!=NULL && ssh_fetch(*main_conf,targetPath,hpcStdErr->value,ssh_get_cnt(m))==0){
+		  struct stat f_status;
+		  int ts=stat(targetPath, &f_status);
+		  if(ts==0) {
+		    char* fcontent = NULL;
+		    fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
+		    FILE* f=fopen(targetPath,"rb");
+		    fread(fcontent,f_status.st_size,1,f);
+		    int fsize=f_status.st_size;
+		    fcontent[fsize]=0;
+		    fclose(f);
+		    setMapInMaps(*main_conf,"lenv","message",fcontent);
+		    free(fcontent);
+		  }else{
+		    char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
+		    sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
+		    setMapInMaps(*main_conf,"lenv","message",tmpStr);
+		    free(tmpStr);
+		  }
+                }else{
+                  char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
+                  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
+                  setMapInMaps(*main_conf,"lenv","message",tmpStr);
+                  free(tmpStr);
+                }
+		invokeCallback(*main_conf,NULL,NULL,7,0);
 		return SERVICE_FAILED;
 	      }
@@ -882,4 +922,5 @@
 	  }else{
 	    map* generatedFile=getMap(input->content,"generated_file");
+	    map* generatedUrl=getMap(input->content,"generated_url");
 	    if(generatedFile!=NULL){
 	      char* filename=strrchr(generatedFile->value,'/');
@@ -895,4 +936,5 @@
 		maps* output=getMaps(*real_outputs,input->name);
 		setMapInMaps(output->child,"download_link","generated_file",targetPath);
+		setMapInMaps(output->child,"download_link","generated_url",generatedUrl->value);
 		setMapInMaps(output->child,"download_link","storage",targetPath);
 		setMapInMaps(output->child,"download_link","useMapserver","false");
@@ -908,12 +950,9 @@
 		if(getMaps(output->child,"wcs_link")!=NULL){
 		  sprintf(serviceName,"wcs_link");
-		  setMapInMaps(output->child,serviceName,"msOgc","WCS");
-		  setMapInMaps(output->child,serviceName,"requestedMimeType","image/tiff");
-		}
-		else{
+		  setMapInMaps(output->child,"wcs_link","msOgc","WCS");
+		}else{
 		  sprintf(serviceName,"wfs_link");
-		  setMapInMaps(output->child,serviceName,"msOgc","WFS");
-		  setMapInMaps(output->child,serviceName,"requestedMimeType","text/xml");
-		}
+		  setMapInMaps(output->child,"wfs_link","msOgc","WFS");
+                }
 		setMapInMaps(output->child,serviceName,"storage",targetPath);
 		setMapInMaps(output->child,serviceName,"generated_file",targetPath);
@@ -958,6 +997,6 @@
       }else{
 	// Try to access remotely to the log file and return a more relevant error message
-	setMapInMaps(m,"lenv","message",_("HPC Execution failed!"));
-	errorException (m, _("HPC Execution failed!"),
+	setMapInMaps(*main_conf,"lenv","message",_("HPC Execution failed!"));
+	errorException (*main_conf, _("HPC Execution failed!"),
 			"InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -965,5 +1004,5 @@
 	fflush(stderr);
 #endif
-	invokeCallback(m,NULL,NULL,7,0);
+	invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
 	fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
@@ -971,10 +1010,9 @@
 #endif
       }
-      //free(buf);
     }
     if(hasPassed<0){
       perror("Failed to read");
       setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
-      errorException (m, _("Unable to parse the value returned by remote execution"),
+      errorException (*main_conf, _("Unable to parse the value returned by remote execution"),
 		      "InternalError", NULL);
 #ifdef HPC_DEBUG
@@ -982,5 +1020,5 @@
       fflush(stderr);
 #endif
-      invokeCallback(m,NULL,NULL,7,0);
+      invokeCallback(*main_conf,NULL,NULL,7,0);
 #ifdef HPC_DEBUG
       fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c	(revision 862)
@@ -144,4 +144,8 @@
  */
 void setReferenceUrl(maps* m,maps* tmpI){
+  int imyIndex=getPublishedId(tmpI);
+  if(getMapArray(tmpI->content,"ref_wms_link",imyIndex)!=NULL)
+    return;
+  outputMapfile(m,tmpI);
   map *msUrl=getMapFromMaps(m,"main","mapserverAddress");
   if(msUrl==NULL){
@@ -150,8 +154,6 @@
     exit(-1);
   }
-  int imyIndex=getPublishedId(tmpI);
   if(getMapArray(tmpI->content,"ref_wms_link",imyIndex)!=NULL)
     return;
-  outputMapfile(m,tmpI);
   int finalProto=-1;
   map *msOgcVersion=getMapFromMaps(m,"main","msOgcVersion");
@@ -165,16 +167,24 @@
   map* versionMap=getMapArray(tmpI->content,"msOgcVersion",imyIndex);
   map* datatype=getMapArray(tmpI->content,"geodatatype",imyIndex);
+  map* layerName=getMapArray(tmpI->content,"msLayer",imyIndex);
   char options[4][5][25]={
     {"WMS","1.3.0","GetMap","layers=%s","wms_extent"},
-    {"WFS","1.1.0","GetFeature","typename=%s","wcs_extent"},
+    {"WFS","1.0.0","GetFeature","typename=%s","wcs_extent"},
     {"WCS","2.0.0","GetCoverage","coverageid=%s","wcs_extent"},
-    {"WCS","1.1.0","GetCoverage","coverage=%s","wcs_extent"}
+    {"WCS","1.0.0","GetCoverage","coverage=%s","wcs_extent"}
   };
-  if(datatype==NULL || strncmp(datatype->value,"other",5)==0){
+  map *nbElements=getMapArray(tmpI->content,"nb_features",imyIndex);
+  if(nbElements==NULL)
+    nbElements=getMapArray(tmpI->content,"nb_pixels",imyIndex);
+  if(datatype==NULL || strncmp(datatype->value,"other",5)==0 || (nbElements!=NULL && atoi(nbElements->value)==0)){
     map* minNb=getMap(tmpI->content,"minoccurs");
-    if(minNb==NULL || atoi(minNb->value)>=1){
+    map* useMs=getMap(tmpI->content,"useMapserver");
+    if((minNb==NULL || atoi(minNb->value)>=1) && useMs!=NULL && strncasecmp(useMs->value,"true",4)==0){
       setMapInMaps(m,"lenv","mapError","true");
       setMapInMaps(m,"lenv","locator",tmpI->name);
-      setMapInMaps(m,"lenv","message",_("The ZOO-Kernel was able to retrieve the data but could not read it as geographic data."));
+      if(nbElements==NULL)
+	setMapInMaps(m,"lenv","message",_("The ZOO-Kernel was able to retrieve the data but could not read it as geographic data."));
+      else
+	setMapInMaps(m,"lenv","message",_("The ZOO-Kernel was able to retrieve the data but could not access any feature or pixel in te resulting file."));
       if(getMapFromMaps(m,"lenv","state")==NULL)
 	errorException (m, _("Unable to find any geographic data"), "WrongInputData", tmpI->name);
@@ -229,6 +239,9 @@
   }
   char layers[128];
-  sprintf(layers,options[proto][3],tmpI->name);
-
+  if(layerName==NULL)
+    sprintf(layers,options[proto][3],tmpI->name);
+  else
+    sprintf(layers,options[proto][3],layerName->value);
+  
   if(format==NULL || width==NULL || height==NULL || extent==NULL){
     char tmpStr[1024];
@@ -525,4 +538,5 @@
   int imyIndex=getPublishedId(output);
   msMapSetExtent(m,minX,minY,maxX,maxY);
+  m->maxsize=4096;
 #ifdef DEBUGMS
   fprintf(stderr,"Extent %.15f %.15f %.15f %.15f\n",minX,minY,maxX,maxY);
@@ -743,12 +757,17 @@
      * Add a new layer set name, data
      */
-    if(msGrowMapLayers(m)==NULL){
-      return -1;
-    }
-    if(initLayer((m->layers[m->numlayers]), m) == -1){
-      return -1;
-    }
-
-    layerObj* myLayer=m->layers[m->numlayers];
+    layerObj* myLayer=NULL;
+    if(getMapArray(output->content,"msInclude",imyIndex)==NULL){
+      if(msGrowMapLayers(m)==NULL){
+	return -1;
+      }
+      if(initLayer((m->layers[m->numlayers]), m) == -1){
+	return -1;
+      }
+      myLayer=m->layers[m->numlayers];
+    }else{
+      myLayer=m->layers[m->numlayers-1];
+    }
+    
 #ifdef DEBUGMS
     dumpMaps(output);
@@ -852,53 +871,54 @@
     else
       msInsertHashTable(&(myLayer->metadata), "ows_title", "Default Title");
+
+    if(getMapArray(output->content,"msInclude",imyIndex)==NULL){
+      if(msGrowLayerClasses(myLayer) == NULL)
+	return -1;
+      if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
+	return -1;
+      if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
+	return -1;
+      if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
+	return -1;
+      /**
+       * Apply msStyle else fallback to the default style
+       */
+      tmpMap=getMap(output->content,"msStyle");
+      if(tmpMap!=NULL)
+	msUpdateStyleFromString(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles],tmpMap->value,0);
+      else{
+	/**
+	 * Set style
+	 */
+	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=125;
+	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=125;
+	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=255;
+	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.red=80;
+	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.green=80;
+	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.blue=80;
+	
+	/**
+	 * Set specific style depending on type
+	 */
+	if(myLayer->type == MS_LAYER_POLYGON)
+	  myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
+	if(myLayer->type == MS_LAYER_LINE){
+	  myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
+	  myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinewidth=1.5;
+	}
+	if(myLayer->type == MS_LAYER_POINT){
+	  myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->symbol=1;
+	  myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->size=15;
+	}
+	
+      }
+      myLayer->CLASS[myLayer->numclasses]->numstyles++;
+      myLayer->numclasses++;
     
-    if(msGrowLayerClasses(myLayer) == NULL)
-      return -1;
-    if(initClass((myLayer->CLASS[myLayer->numclasses])) == -1)
-      return -1;
-    if(msGrowClassStyles(myLayer->CLASS[myLayer->numclasses]) == NULL)
-      return -1;
-    if(initStyle(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]) == -1)
-      return -1;
-    /**
-     * Apply msStyle else fallback to the default style
-     */
-    tmpMap=getMap(output->content,"msStyle");
-    if(tmpMap!=NULL)
-      msUpdateStyleFromString(myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles],tmpMap->value,0);
-    else{
-      /**
-       * Set style
-       */
-      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.red=125;
-      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.green=125;
-      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->color.blue=255;
-      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.red=80;
-      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.green=80;
-      myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinecolor.blue=80;
-      
-      /**
-       * Set specific style depending on type
-       */
-      if(myLayer->type == MS_LAYER_POLYGON)
-	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
-      if(myLayer->type == MS_LAYER_LINE){
-	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->width=3;
-	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->outlinewidth=1.5;
-      }
-      if(myLayer->type == MS_LAYER_POINT){
-	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->symbol=1;
-	myLayer->CLASS[myLayer->numclasses]->styles[myLayer->CLASS[myLayer->numclasses]->numstyles]->size=15;
-      }
-      
-    }
-    myLayer->CLASS[myLayer->numclasses]->numstyles++;
-    myLayer->numclasses++;
-    
-    m->layerorder[m->numlayers] = m->numlayers;
-    m->numlayers++;
-
-  }
-
+      m->layerorder[m->numlayers] = m->numlayers;
+      m->numlayers++;
+
+    }
+  }
   OGR_DS_Destroy(poDS);
   //OGRCleanupAll();
@@ -982,6 +1002,23 @@
   m->width=GDALGetRasterXSize( hDataset );
   m->height=GDALGetRasterYSize( hDataset );
+  if(m->width>4096 || m->height>4096){
+    if(m->width>m->height)
+      m->maxsize=m->width;
+    else  
+      m->maxsize=m->height;
+  }else
+    m->maxsize=4096;
   addIntToMapArray(output->content,"nb_pixels",imyIndex,GDALGetRasterXSize( hDataset )*GDALGetRasterYSize( hDataset ));
-  
+  int pixel_type=GDALGetRasterDataType( hDataset );
+  addIntToMapArray(output->content,"pixel_data_type",imyIndex,pixel_type);
+
+  int outputIndex=msGetOutputFormatIndex(m,"tiff");
+  if(outputIndex>=0){
+    m->outputformatlist[outputIndex]->imagemode=((pixel_type==GDT_Byte)?MS_IMAGEMODE_BYTE:((pixel_type==GDT_Int16 || pixel_type==GDT_UInt16)?MS_IMAGEMODE_INT16:((pixel_type!=GDT_Float32)?MS_IMAGEMODE_FLOAT32:MS_IMAGEMODE_BYTE)));
+    outputIndex=msGetOutputFormatIndex(m,"geotiff");
+    m->outputformatlist[outputIndex]->imagemode=((pixel_type==GDT_Byte)?MS_IMAGEMODE_BYTE:((pixel_type==GDT_Int16 || pixel_type==GDT_UInt16)?MS_IMAGEMODE_INT16:((pixel_type!=GDT_Float32)?MS_IMAGEMODE_FLOAT32:MS_IMAGEMODE_BYTE)));    
+  }
+  //
+    
   /**
    * Set projection using Authority Code and Name if available or fallback to 
@@ -1241,5 +1278,20 @@
    * Create an empty map, set name, default size and extent
    */
-  mapObj *myMap=msNewMapObj();
+  map* mapfileTemplate=getMapArray(outputs->content,"msInclude",imyIndex);
+  mapObj *myMap=NULL;
+  if(mapfileTemplate==NULL){
+    myMap=msNewMapObj();
+  }
+  else{
+    map* dataPath=getMapFromMaps(conf,"main","dataPath");
+    map* sid=getMapFromMaps(conf,"lenv","sid");
+    char *mapfileTemplatePath=(char*)malloc(((strlen(dataPath->value)+strlen(sid->value)+strlen(outputs->name)+10)*sizeof(char)));
+    sprintf(mapfileTemplatePath,"%s/%s_%s.map",dataPath->value,outputs->name,sid->value);
+    myMap=msLoadMap(mapfileTemplate->value,mapfileTemplatePath);
+    if(myMap==NULL){
+      setMapInMaps(conf,"lenv","message",_("Unable to open your template mapfile!"));
+      return ;
+    }
+  }
   free(myMap->name);
   myMap->name=zStrdup("ZOO-Project_WXS_Server");
@@ -1281,5 +1333,5 @@
     fprintf(stderr,"Unable to initialize GDAL driver !\n");
   else{
-    o3->imagemode=MS_IMAGEMODE_BYTE;
+    o3->imagemode=MS_IMAGEMODE_INT16;
     o3->inmapfile=MS_TRUE;  
     msAppendOutputFormat(myMap,msCloneOutputFormat(o3));
@@ -1291,5 +1343,5 @@
     fprintf(stderr,"Unable to initialize GDAL driver !\n");
   else{
-    o4->imagemode=MS_IMAGEMODE_INT16;
+    o4->imagemode=MS_IMAGEMODE_BYTE;
     o4->inmapfile=MS_TRUE;  
     msAppendOutputFormat(myMap,msCloneOutputFormat(o4));
@@ -1310,4 +1362,5 @@
 #endif
 
+  
   outputFormatObj *o6=msCreateDefaultOutputFormat(NULL,"GDAL/GTiff","geotiff");
   if(!o6)
@@ -1321,4 +1374,5 @@
   }
 
+  
   /*
    * Set default projection to EPSG:4326
@@ -1408,5 +1462,7 @@
   sprintf(mapPath,"%s/%s_%d_%s.map",tmp1->value,outputs->name,imyIndex,sid->value);
   msSaveMap(myMap,mapPath);
+  saveMapNames(conf,outputs,mapPath);
   free(mapPath);
+  //free(myMap->symbolset.filename);
   //msFreeSymbolSet(&myMap->symbolset);
   msFreeMap(myMap);
@@ -1415,2 +1471,20 @@
 }
 
+/**
+ * Save the map fullpath in a text file (.maps)
+ * @param conf the main configuration map pointer
+ * @param output the current output for which a mapfile has been generated
+ * @param mapfile the mapfile saved to store in the text file
+ */
+void saveMapNames(maps* conf,maps* output,char* mapfile){
+  map* storage=getMap(output->content,"storage");
+  char *tmp=zStrdup(storage->value);
+  tmp[strlen(tmp)-strlen(strrchr(tmp,'.'))]=0;
+  char* mapName=(char*)malloc((strlen(tmp)+6)*sizeof(char*));
+  sprintf(mapName,"%s.maps",tmp);
+  FILE* myMaps=fopen(mapName,"a");
+  if(myMaps!=NULL){
+    fprintf(myMaps,"%s\n",mapfile);
+    fclose(myMaps);
+  } 
+}
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.h	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.h	(revision 862)
@@ -55,4 +55,6 @@
   int tryGdal(maps* conf,maps* output,mapObj* m);
   void outputMapfile(maps* conf,maps* outputs);
+  void saveMapNames(maps*,maps*,char*);
+
 #ifdef __cplusplus
 }
Index: branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c	(revision 862)
@@ -205,5 +205,5 @@
     return NULL;
 
-  libssh2_session_set_blocking(result->session, 0);
+  libssh2_session_set_blocking(result->session, 1);
 
   while ((rc = libssh2_session_handshake(result->session, result->sock_id))
@@ -418,5 +418,4 @@
  */
 int ssh_fetch(maps* conf,const char* localPath,const char* targetPath,int cnt){
-  char mem[1024];
   size_t nread;
   size_t memuse=0;
@@ -436,5 +435,4 @@
     if (!sessions[cnt]->sftp_session &&
 	(libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
-      
       fprintf(stderr, "Unable to init SFTP session\n");
       return -1;
@@ -452,8 +450,9 @@
       }
       else {
-	//non-blocking open
 	waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 
       }
     }
+    if(!sftp_handle)
+      zSleep(100);
   } while (!sftp_handle);
  
@@ -461,11 +460,10 @@
   do {
     do {
-      rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
-      /*fprintf(stderr, "libssh2_sftp_read returned %d\n",
-	rc);*/
+      char* mem=(char*)malloc(16*1024*1024);
+      rc = libssh2_sftp_read(sftp_handle, mem,16*1024*1024);
       if(rc > 0) {
-	//write(2, mem, rc);
 	fwrite(mem, rc, 1, local);
       }
+      free(mem);
     } while (rc > 0);
     
@@ -585,4 +583,6 @@
  */
 bool ssh_close_session(maps* conf,SSHCON* con){
+  if(con==NULL)
+    return true;
   while (libssh2_session_disconnect(con->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
 	 == LIBSSH2_ERROR_EAGAIN);
@@ -593,4 +593,5 @@
 #endif
   libssh2_session_free(con->session);
+  con=NULL;
   return true;
 }
@@ -639,21 +640,11 @@
 		     getMapArray(input->content,"targetPath",i)};
 
-	setMapArray(queueMaps->content,"input",queueIndex,input->name);
-	setMapArray(queueMaps->content,"localPath",queueIndex,tmp[0]->value);
-	setMapArray(queueMaps->content,"targetPath",queueIndex,tmp[1]->value);
-	queueIndex+=1;
+	setMapArray(queueMaps->content,"input",queueIndex+i,input->name);
+	setMapArray(queueMaps->content,"localPath",queueIndex+i,tmp[0]->value);
+	setMapArray(queueMaps->content,"targetPath",queueIndex+i,tmp[1]->value);
+
       }
     }
   }
-#ifdef SSH_DEBUG  
-  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
-  fflush(stderr);
-  dumpMaps(queueMaps);
-  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
-  fflush(stderr);
-  dumpMaps(*conf);
-  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
-  fflush(stderr);
-#endif
   return true;
 }
@@ -664,11 +655,4 @@
     return false;
   }
-#ifdef SSH_DEBUG
-  fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__);
-  fflush(stderr);
-  dumpMaps(getMaps(*conf,"uploadQueue"));
-  fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__);
-  fflush(stderr);
-#endif
   map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
   maps* queueMaps=getMaps(*conf,"uploadQueue");
@@ -682,7 +666,4 @@
 	getMapArray(queueMaps->content,"targetPath",i)
       };
-#ifdef SSH_DEBUG      
-      fprintf(stderr,"*** %s %d %s %s\n",__FILE__,__LINE__,argv[1]->value,argv[2]->value);
-#endif      
       /**/zooLock* lck;
       if((lck=lockFile(*conf,argv[1]->value,'w'))!=NULL){/**/
@@ -703,4 +684,18 @@
     }    
   }
+  while (libssh2_session_disconnect(test->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
+         == LIBSSH2_ERROR_EAGAIN);
+#ifdef WIN32
+  closesocket(test->sock_id);
+#else
+  close(test->sock_id);
+#endif
+  libssh2_session_free(test->session);
+  free(test);
+  test=NULL;
+  sessions[ssh_get_cnt(*conf)-1]=NULL;
+  maps* tmp=getMaps(*conf,"lenv");
+  addIntToMap(tmp->content,"nb_sessions",ssh_get_cnt(*conf)-1);  
+
   return true; 
 }
Index: branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c	(revision 861)
+++ 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{
Index: branches/prototype-v0/zoo-project/zoo-kernel/zoo_loader.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/zoo_loader.c	(revision 861)
+++ branches/prototype-v0/zoo-project/zoo-kernel/zoo_loader.c	(revision 862)
@@ -64,4 +64,9 @@
 #define FALSE -1
 #endif
+
+int cgiInit(){
+  fprintf(FCGI_stderr,"ZOO-Kernel initialization %s %d ... \n",__FILE__,__LINE__);
+  fflush(FCGI_stderr);
+}
 
 /**
