Index: trunk/zoo-kernel/zoo_service_loader.c
===================================================================
--- trunk/zoo-kernel/zoo_service_loader.c	(revision 214)
+++ trunk/zoo-kernel/zoo_service_loader.c	(revision 216)
@@ -203,8 +203,4 @@
 #endif
       if(strncasecmp(r_inputs->value,"C-FORTRAN",9)==0){
-#ifdef WIN32
-	//Strange return value needed here !
-	return 1;
-#endif
 	r_inputs=getMap(request_inputs,"Identifier");
 	char fname[1024];
@@ -284,5 +280,10 @@
 #endif
       }
+#ifdef WIN32
+      *ioutputs=dupMaps(&request_output_real_format);
+      FreeLibrary(so);
+#else
       dlclose(so);
+#endif
     } else {
       /**
@@ -345,6 +346,80 @@
 	      }
   *myMap=m;
+#ifndef WIN32
   *ioutputs=request_output_real_format;
+#endif
 }
+
+#ifdef WIN32
+/**
+ * createProcess function: create a new process after setting some env variables
+ */
+void createProcess(maps* m,map* request_inputs,service* s1,char* opts,int cpid, maps* inputs,maps* outputs){
+  STARTUPINFO si;
+  PROCESS_INFORMATION pi;
+  ZeroMemory( &si, sizeof(si) );
+  si.cb = sizeof(si);
+  ZeroMemory( &pi, sizeof(pi) );
+  char *tmp=(char *)malloc((1024+cgiContentLength)*sizeof(char));
+  char *tmpq=(char *)malloc((1024+cgiContentLength)*sizeof(char));
+  map *req=getMap(request_inputs,"request");
+  map *id=getMap(request_inputs,"identifier");
+  map *di=getMap(request_inputs,"DataInputs");
+
+  char *dataInputsKVP=getMapsAsKVP(inputs,cgiContentLength,0);
+  char *dataOutputsKVP=getMapsAsKVP(outputs,cgiContentLength,1);
+  fprintf(stderr,"DATAINPUTSKVP %s\n",dataInputsKVP);
+  fprintf(stderr,"DATAOUTPUTSKVP %s\n",dataOutputsKVP);
+  map *sid=getMapFromMaps(m,"lenv","sid");
+  map* r_inputs=getMapFromMaps(m,"main","tmpPath");
+  map* r_inputs1=getMap(s1->content,"ServiceProvider");
+  map* r_inputs2=getMap(s1->content,"ResponseDocument");
+  if(r_inputs2==NULL)
+    r_inputs2=getMap(s1->content,"RawDataOutput");
+  map *tmpPath=getMapFromMaps(m,"lenv","cwd");
+
+  if(r_inputs2!=NULL){
+    sprintf(tmp,"\"request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s&cgiSid=%s\"",req->value,id->value,dataInputsKVP,r_inputs2->name,r_inputs2->value,sid->value);
+	sprintf(tmpq,"request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s",req->value,id->value,dataInputsKVP,r_inputs2->name,dataOutputsKVP);
+  }
+  else{
+    sprintf(tmp,"\"request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&cgiSid=%s\"",req->value,id->value,dataInputsKVP,sid->value);
+    sprintf(tmpq,"request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s",req->value,id->value,dataInputsKVP,sid->value);
+  }
+
+  char *tmp1=strdup(tmp);
+  sprintf(tmp,"zoo_loader.cgi %s \"%s\"",tmp1,sid->value);
+
+  free(dataInputsKVP);
+  free(dataOutputsKVP);
+  fprintf(stderr,"REQUEST IS : %s \n",tmp);
+  SetEnvironmentVariable("CGISID",TEXT(sid->value));
+  SetEnvironmentVariable("QUERY_STRING",TEXT(tmpq));
+  char clen[1000];
+  sprintf(clen,"%d",strlen(tmpq));
+  SetEnvironmentVariable("CONTENT_LENGTH",TEXT(clen));
+
+  if( !CreateProcess( NULL,             // No module name (use command line)
+		      TEXT(tmp),        // Command line
+		      NULL,             // Process handle not inheritable
+		      NULL,             // Thread handle not inheritable
+		      FALSE,            // Set handle inheritance to FALSE
+		      CREATE_NO_WINDOW, // Apache won't wait until the end
+		      NULL,             // Use parent's environment block
+		      NULL,             // Use parent's starting directory 
+		      &si,              // Pointer to STARTUPINFO struct
+		      &pi )             // Pointer to PROCESS_INFORMATION struct
+      ) 
+    { 
+      fprintf( stderr, "CreateProcess failed (%d).\n", GetLastError() );
+      return ;
+    }else{
+    fprintf( stderr, "CreateProcess successfull (%d).\n\n\n\n", GetLastError() );
+  }
+  CloseHandle( pi.hProcess );
+  CloseHandle( pi.hThread );
+  fprintf(stderr,"CreateProcess finished !\n");
+}
+#endif
 
 int runRequest(map* request_inputs)
@@ -671,12 +746,14 @@
 #endif
   int saved_stdout = dup(fileno(stdout));
-  dup2(fileno(stderr),fileno(stdout));
+    dup2(fileno(stderr),fileno(stdout));
   t=getServiceFromFile(tmps1,&s1);
   fflush(stdout);
   dup2(saved_stdout,fileno(stdout));
   if(t<0){
-    char tmpMsg[2048+strlen(r_inputs->value)];
+    char *tmpMsg=(char*)malloc(2048+strlen(r_inputs->value));
+    
     sprintf(tmpMsg,_("The value for <indetifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."),r_inputs->value);
     errorException(m, tmpMsg, "InvalidParameterValue");
+    free(tmpMsg);
     freeService(&s1);
     free(s1);
@@ -687,5 +764,5 @@
     return 0;
   }
-  close(saved_stdout);
+  //close(saved_stdout);
 
 #ifdef DEBUG
@@ -887,6 +964,12 @@
 	char tmpn[256];
 	memset(tmpn,0,256);
-	strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
-	tmpn[strlen(tmpc)-strlen(tmpv)]=0;
+	if(tmpv!=NULL){
+	  strncpy(tmpn,tmpc,(strlen(tmpc)-strlen(tmpv))*sizeof(char));
+	  tmpn[strlen(tmpc)-strlen(tmpv)]=0;
+	}
+	else{
+	  strncpy(tmpn,tmpc,strlen(tmpc)*sizeof(char));
+	  tmpn[strlen(tmpc)]=0;
+	}
 #ifdef DEBUG
 	fprintf(stderr,"***\n*** %s = %s ***\n",tmpn,tmpv+1);
@@ -898,5 +981,8 @@
 	  }
 	  tmpmaps->name=strdup(tmpn);
-	  tmpmaps->content=createMap("value",tmpv+1);
+	  if(tmpv!=NULL)
+	    tmpmaps->content=createMap("value",tmpv+1);
+	  else
+	    tmpmaps->content=createMap("value","Reference");
 	  tmpmaps->next=NULL;
 	}
@@ -912,6 +998,22 @@
 	  char tmpn1[1024];
 	  memset(tmpn1,0,1024);
-	  strncpy(tmpn1,tmpc,strlen(tmpc)-strlen(tmpv1));
-	  tmpn1[strlen(tmpc)-strlen(tmpv1)]=0;
+	  if(tmpv1!=NULL){
+	    strncpy(tmpn1,tmpc,strlen(tmpc)-strlen(tmpv1));
+	    tmpn1[strlen(tmpc)-strlen(tmpv1)]=0;
+	    addToMap(tmpmaps->content,tmpn1,tmpv1+1);
+	  }
+	  else{
+	    strncpy(tmpn1,tmpc,strlen(tmpc));
+	    tmpn1[strlen(tmpc)]=0;
+	    map* lmap=getLastMap(tmpmaps->content);
+	    char *tmpValue=(char*)calloc((strlen(lmap->value)+strlen(tmpc)+1),sizeof(char));
+	    sprintf(tmpValue,"%s@%s",lmap->value,tmpc);
+	    free(lmap->value);
+	    lmap->value=strdup(tmpValue);
+	    free(tmpValue);
+	    dumpMap(tmpmaps->content);
+	    tmpc=strtok(NULL,"@");
+	    continue;
+	  }
 #ifdef DEBUG
 	  fprintf(stderr,"*** NAME NON URL-ENCODED \n***%s***\n",tmpn1);
@@ -920,55 +1022,59 @@
 	  if(strcmp(tmpn1,"xlink:href")!=0)
 	    addToMap(tmpmaps->content,tmpn1,tmpv1+1);
-	  else{
-	    if(strncasecmp(tmpv1+1,"http://",7)!=0 &&
-	       strncasecmp(tmpv1+1,"ftp://",6)!=0){
-	      char emsg[1024];
-	      sprintf(emsg,_("Unable to find a valid protocol to download the remote file %s"),tmpv1+1);
-	      errorException(m,emsg,"InternalError");
-	      freeMaps(&m);
-	      free(m);
-	      free(REQUEST);
-	      free(SERVICE_URL);
-	      InternetCloseHandle(hInternet);
-	      freeService(&s1);
-	      free(s1);
-	      return 0;
+	  else
+	    if(tmpv1!=NULL){
+	      if(strncasecmp(tmpv1+1,"http://",7)!=0 &&
+		 strncasecmp(tmpv1+1,"ftp://",6)!=0){
+		char emsg[1024];
+		sprintf(emsg,_("Unable to find a valid protocol to download the remote file %s"),tmpv1+1);
+		errorException(m,emsg,"InternalError");
+		freeMaps(&m);
+		free(m);
+		free(REQUEST);
+		free(SERVICE_URL);
+		InternetCloseHandle(hInternet);
+		freeService(&s1);
+		free(s1);
+		return 0;
+	      }
+#ifdef DEBUG
+	      fprintf(stderr,"REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",tmpv1+1);
+#endif
+#ifndef WIN32
+	      if(CHECK_INET_HANDLE(hInternet))
+#endif
+		{
+		  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);
+		    }
+		  }
+		  free(tmpContent);
+		}
+	      char *tmpx=url_encode(tmpv1+1);
+	      addToMap(tmpmaps->content,tmpn1,tmpx);
+	      free(tmpx);
+	      addToMap(tmpmaps->content,"Reference",tmpv1+1);
+	      dumpMap(tmpmaps->content);
 	    }
-#ifdef DEBUG
-	    fprintf(stderr,"REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n",tmpv1+1);
-#endif
-#ifndef WIN32
-	    if(CHECK_INET_HANDLE(hInternet))
-#endif
-	      {
-		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);
-		  }
-		}
-		free(tmpContent);
-	      }
-	    addToMap(tmpmaps->content,tmpn1,tmpv1+1);
-	    addToMap(tmpmaps->content,"Reference",tmpv1+1);
-	  }
 	  tmpc=strtok(NULL,"@");
 	}
@@ -1132,4 +1238,6 @@
 	    hInternet.header=NULL;
 	    while(cur3){
+	      while(cur3!=NULL && cur3->type!=XML_ELEMENT_NODE)
+		cur2=cur3->next;
 	      if(xmlStrcasecmp(cur3->name,BAD_CAST "Header")==0 ){
 		const char *ha[2];
@@ -1697,4 +1805,5 @@
   _tmpMaps->next=NULL;
   addToMap(_tmpMaps->content,"status","0");
+  addToMap(_tmpMaps->content,"cwd",ntmp);
   if(cgiCookie!=NULL && strlen(cgiCookie)>0){
     addToMap(_tmpMaps->content,"sessid",strstr(cgiCookie,"=")+1);
@@ -1722,5 +1831,20 @@
   dumpMap(request_inputs);
 #endif
-
+#ifdef WIN32
+  char *cgiSidL=NULL;
+  if(getenv("CGISID")!=NULL)
+	addToMap(request_inputs,"cgiSid",getenv("CGISID"));
+  map* test1=getMap(request_inputs,"cgiSid");
+  if(test1!=NULL){
+    cgiSid=test1->value;
+  }
+  if(cgiSid!=NULL){
+    addToMap(request_inputs,"storeExecuteResponse","true");
+    addToMap(request_inputs,"status","true");
+    status=getMap(request_inputs,"status");
+    dumpMap(request_inputs);
+    fprintf(stderr,"cgiSID : %s",cgiSid);
+  }
+#endif
   if(status!=NULL)
     if(strcasecmp(status->value,"false")==0)
@@ -1738,5 +1862,13 @@
     pid = fork ();
 #else
-    pid = 0;
+    if(cgiSid==NULL){
+      addToMap(request_inputs,"cgSid",cgiSid);
+      createProcess(m,request_inputs,s1,NULL,cpid,request_input_real_format,request_output_real_format);
+      pid = cpid;
+    }else{
+      pid=0;
+      cpid=atoi(cgiSid);
+    }
+    fflush(stderr);
 #endif
     if (pid > 0) {
@@ -1765,7 +1897,7 @@
       fprintf(stderr,"\nFILE TO STORE DATA %s\n",r_inputs->value);
 #endif
+      freopen(flog,"w+",stderr);
       freopen(fbkp , "w+", stdout);
       fclose(stdin);
-      freopen(flog,"w+",stderr);
       free(fbkp);
       free(flog);
@@ -1778,9 +1910,11 @@
       updateStatus(m);
       printProcessResponse(m,request_inputs,cpid,
-			    s1,r_inputs1->value,SERVICE_STARTED,
-			    request_input_real_format,
-			    request_output_real_format);
+			   s1,r_inputs1->value,SERVICE_STARTED,
+			   request_input_real_format,
+			   request_output_real_format);
+#ifndef WIN32
       fflush(stdout);
       rewind(stdout);
+#endif
 
       loadServiceAndRun(&m,s1,request_inputs,&request_input_real_format,&request_output_real_format,&eres);
@@ -1798,5 +1932,5 @@
 #ifdef DEBUG
   dumpMaps(request_output_real_format);
-  fprintf(stderr,"Function loaded and returned %d\n",eres);
+  fprintf(stderr,"Function loaded and returned %d\n",*eres);
   fflush(stderr);
 #endif
@@ -1805,4 +1939,5 @@
 		   request_output_real_format,request_inputs,
 		   cpid,m,eres);
+  fflush(stdout);
   /**
    * Ensure that if error occurs when freeing memory, no signal will return
