Index: /trunk/zoo-project/HISTORY.txt
===================================================================
--- /trunk/zoo-project/HISTORY.txt	(revision 681)
+++ /trunk/zoo-project/HISTORY.txt	(revision 682)
@@ -1,3 +1,4 @@
 Version 1.5.0-dev
+  * Fix asynchronous POST request on WIN32 platforms
   * Add utils/registry service
   * Initial support for WPS 2.0.0 including the Dismiss extension
Index: /trunk/zoo-project/zoo-kernel/response_print.c
===================================================================
--- /trunk/zoo-project/zoo-kernel/response_print.c	(revision 681)
+++ /trunk/zoo-project/zoo-kernel/response_print.c	(revision 682)
@@ -1745,5 +1745,11 @@
   }
   
-  if(vid==0 && hasStoredExecuteResponse==true && status!=SERVICE_STARTED && status!=SERVICE_ACCEPTED){
+  if(vid==0 && 
+     hasStoredExecuteResponse==true 
+     && status!=SERVICE_STARTED 
+#ifndef WIN32
+     && status!=SERVICE_ACCEPTED
+#endif
+     ){
 #ifndef RELY_ON_DB
     semid lid=acquireLock(m);//,1);
@@ -2334,5 +2340,5 @@
   map* version=getMapFromMaps(m,"main","rversion");
   int vid=getVersionId(version->value);
-  
+
   maps* tmpSess=getMaps(m,"senv");
   if(tmpSess!=NULL){
@@ -2375,5 +2381,5 @@
     else{
       fclose(teste);
-      dumpMapsToFile(tmpSess,session_file_path);
+      dumpMapsToFile(tmpSess,session_file_path,1);
     }
   }
@@ -2441,8 +2447,8 @@
 	  elements* in=getElements(s->outputs,tmpI->name);
 	  char *format=NULL;
-	  if(in!=NULL){
-	    format=strdup(in->format);
+	  if(in!=NULL && in->format!=NULL){
+	    format=zStrdup(in->format);
 	  }else
-	    format=strdup("LiteralData");
+	    format=zStrdup("LiteralData");
 	  if(strcasecmp(format,"BoundingBoxData")==0){
 	    addToMap(tmpI->content,"extension","xml");
@@ -2466,5 +2472,5 @@
 	      getFileExtension(mtype != NULL ? mtype->value : NULL, file_ext, 32);
 	    }
-		
+
 	    file_name=(char*)malloc((strlen(s->name)+strlen(usid->value)+strlen(file_ext)+strlen(tmpI->name)+45)*sizeof(char));
 	    sprintf(file_name,"%s_%s_%s_%d.%s",s->name,tmpI->name,usid->value,itn,file_ext);
@@ -2472,5 +2478,5 @@
 	    file_path=(char*)malloc((strlen(tmp1->value)+strlen(file_name)+2)*sizeof(char));
 	    sprintf(file_path,"%s/%s",tmp1->value,file_name);
-    
+
 	    FILE *ofile=fopen(file_path,"wb");
 	    if(ofile==NULL){
@@ -2498,4 +2504,5 @@
 
 	  }
+
 	  map *tmp2=getMapFromMaps(m,"main","tmpUrl");
 	  map *tmp3=getMapFromMaps(m,"main","serverAddress");
@@ -2509,8 +2516,9 @@
 	    sprintf(file_url,"%s/%s/%s",tmp3->value,tmp2->value,file_name);
 	  }
+
 	  addToMap(tmpI->content,"Reference",file_url);
 	  free(format);
 	  free(file_name);
-	  free(file_url);	
+	  free(file_url);
 	  
 	}
Index: /trunk/zoo-project/zoo-kernel/server_internal.c
===================================================================
--- /trunk/zoo-project/zoo-kernel/server_internal.c	(revision 681)
+++ /trunk/zoo-project/zoo-kernel/server_internal.c	(revision 682)
@@ -72,4 +72,7 @@
 #endif
   sprintf(res,"%s",rest);
+#ifdef WIN32
+  RpcStringFree(&rest);
+#endif
   return res;
 }
@@ -895,6 +898,6 @@
 	if(result!=NULL){
 	  char *encoding=getEncoding(conf);
-	  fprintf(stdout,"Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
-	  fprintf(stdout,"%s",result);
+	  printf("Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding);
+	  printf("%s",result);
 	  fflush(stdout);
 	  freeMap(&statusInfo);
Index: /trunk/zoo-project/zoo-kernel/service.c
===================================================================
--- /trunk/zoo-project/zoo-kernel/service.c	(revision 681)
+++ /trunk/zoo-project/zoo-kernel/service.c	(revision 682)
@@ -87,7 +87,8 @@
  * @param file_path the full path to the file name to store the map
  */
-void dumpMapsToFile(maps* m,char* file_path){
+void dumpMapsToFile(maps* m,char* file_path,int limit){
   FILE* file=fopen(file_path,"w+");
   maps* tmp=m;
+  int cnt=0;
   while(tmp!=NULL){
     fprintf(file,"[%s]\n",tmp->name);
@@ -95,4 +96,7 @@
     fflush(file);
     tmp=tmp->next;
+    cnt++;
+    if(limit>=0 && cnt==limit)
+      tmp=NULL;
   }
   fflush(file);
Index: /trunk/zoo-project/zoo-kernel/service.h
===================================================================
--- /trunk/zoo-project/zoo-kernel/service.h	(revision 681)
+++ /trunk/zoo-project/zoo-kernel/service.h	(revision 682)
@@ -271,6 +271,6 @@
   ZOO_DLL_EXPORT void dumpMap(map*);
   ZOO_DLL_EXPORT void dumpMaps(maps* m);
-  ZOO_DLL_EXPORT void dumpMapToFile(map*,FILE*); // Not required
-  ZOO_DLL_EXPORT void dumpMapsToFile(maps*,char*);
+  ZOO_DLL_EXPORT void dumpMapToFile(map*,FILE*); // (used only internally)
+  ZOO_DLL_EXPORT void dumpMapsToFile(maps*,char*,int);
   ZOO_DLL_EXPORT map* createMap(const char*,const char*);
   ZOO_DLL_EXPORT int count(map*);
Index: /trunk/zoo-project/zoo-kernel/service_internal_python.c
===================================================================
--- /trunk/zoo-project/zoo-kernel/service_internal_python.c	(revision 681)
+++ /trunk/zoo-project/zoo-kernel/service_internal_python.c	(revision 682)
@@ -523,14 +523,16 @@
 maps* mapsFromPyDict(PyDictObject* t){
   maps* res=NULL;
-  maps* cursor=res;
+  maps* cursor=NULL;
   PyObject* list=PyDict_Keys((PyObject*)t);
   int nb=PyList_Size(list);
   int i;
+  PyObject* key;
+  PyObject* value;
   for(i=0;i<nb;i++){
 #ifdef DEBUG
     fprintf(stderr,">> parsing maps %d\n",i);
 #endif
-    PyObject* key=PyList_GetItem(list,i);
-    PyObject* value=PyDict_GetItem((PyObject*)t,key);
+    key=PyList_GetItem(list,i);
+    value=PyDict_GetItem((PyObject*)t,key);
 #ifdef DEBUG
     fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
@@ -538,5 +540,5 @@
 #endif
     cursor=(maps*)malloc(MAPS_SIZE);
-    cursor->name=PyString_AsString(key);
+    cursor->name=zStrdup(PyString_AsString(key));
     cursor->content=mapFromPyDict((PyDictObject*)value);
 #ifdef DEBUG
@@ -572,7 +574,9 @@
   int nb=PyList_Size(list);
   int i;
+  PyObject* key;
+  PyObject* value;
   for(i=0;i<nb;i++){
-    PyObject* key=PyList_GetItem(list,i);
-    PyObject* value=PyDict_GetItem((PyObject*)t,key);
+    key=PyList_GetItem(list,i);
+    value=PyDict_GetItem((PyObject*)t,key);
 #ifdef DEBUG
     fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
@@ -584,5 +588,16 @@
       Py_ssize_t size;
 #if PY_MAJOR_VERSION >= 3
-      buffer=_PyUnicode_AsStringAndSize(value,&size);
+      if(PyBytes_Check(value)){
+	size=PyBytes_Size(value);
+	buffer=PyBytes_AsString(value);
+      }
+      else
+	if(PyUnicode_Check(value) && PyUnicode_READY(value) == 0){
+	  buffer=PyUnicode_AsUTF8AndSize(value,&size);
+	}
+	else{
+	  fprintf(stderr,"Unsupported return value.");
+	  return NULL;
+	}
 #else
       PyString_AsStringAndSize(value,&buffer,&size);
Index: /trunk/zoo-project/zoo-kernel/zoo_loader.c
===================================================================
--- /trunk/zoo-project/zoo-kernel/zoo_loader.c	(revision 681)
+++ /trunk/zoo-project/zoo-kernel/zoo_loader.c	(revision 682)
@@ -29,19 +29,11 @@
  * Specific includes
  */
-#ifndef WIN32
 #include "fcgio.h"
 #include "fcgi_config.h" 
 #include "fcgi_stdio.h"
-#endif
 #include <sys/types.h>
 #include <unistd.h>
 #include "service_internal.h"
 #include "response_print.h"
-
-
-#ifdef WIN32
-#include "windows.h"
-#define strtok_r strtok_s
-#endif
 
 extern "C" {
@@ -54,10 +46,11 @@
 }
 
+#ifdef WIN32
+#include "windows.h"
+#define strtok_r strtok_s
+#endif
+
 #include "service_internal.h"
 #include "request_parser.h"
-
-#ifdef WIN32
-#include "server_internal.h"
-#endif
 
 int runRequest(map**);
@@ -178,17 +171,8 @@
       fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
 #endif
-#ifdef WIN32
-      char* tmp = url_decode(value); 
-      if(tmpMap!=NULL)		
-        addToMap(tmpMap,*arrayStep,tmp);
-      else		
-        tmpMap=createMap(*arrayStep,tmp);
-      free(tmp);
-#else
       if(tmpMap!=NULL)
         addToMap(tmpMap,*arrayStep,value);
       else
         tmpMap=createMap(*arrayStep,value);
-#endif	
       arrayStep++;
       delete[]value;
@@ -206,8 +190,10 @@
     char *buffer=(char*)malloc((flen+1)*sizeof(char));
     fread(buffer,flen,1,lf);
+    char *pchr=strrchr(buffer,'>');
+    cgiContentLength=strlen(buffer)-strlen(pchr)+1;
+    buffer[cgiContentLength]=0;
     fclose(lf);
     addToMap(tmpMap,"request",buffer);
     free(buffer);
-    cgiContentLength=flen+9;
   }
 #endif
@@ -387,10 +373,9 @@
       }	  
       //addToMap(tmpMap,name,value);
-	  /* knut: strtok(_r) ignores delimiter bytes at start and end of string; 
-	   * it will return non-empty string or NULL, e.g. "metapath=" yields value=NULL.
-	   * This modification sets value="" instead of NULL.
-	   */
-	  addToMap(tmpMap,name, value != NULL ? value : "");
-
+      /* knut: strtok(_r) ignores delimiter bytes at start and end of string; 
+       * it will return non-empty string or NULL, e.g. "metapath=" yields value=NULL.
+       * This modification sets value="" instead of NULL.
+       */
+      addToMap(tmpMap,name, value != NULL ? value : "");
       free(name);
       free(value);
@@ -412,5 +397,4 @@
     free(strQuery);
 
-  
   runRequest(&tmpMap);
 
Index: /trunk/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- /trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 681)
+++ /trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 682)
@@ -734,5 +734,5 @@
     strncpy(kvp, key, strlen(key));
     addToCache(m, tmpReq->value, tmpReq->value, "text/xml", strlen(tmpReq->value), 
-	       filepath, FILENAME_MAX);				   
+	       filepath, FILENAME_MAX);
     if (filepath == NULL) {
       errorException( m, _("Unable to cache HTTP POST Execute request."), "InternalError", NULL);  
@@ -787,6 +787,8 @@
   SetEnvironmentVariable ("CGISID", TEXT (sid->value));
   SetEnvironmentVariable ("QUERY_STRING", TEXT (tmpq));
-  // knut: Prevent REQUEST_METHOD=POST in background process call to cgic:main (process hangs when reading cgiIn):
+  // knut: Prevent REQUEST_METHOD=POST in background process call to cgic:main 
+  // (process hangs when reading cgiIn):
   SetEnvironmentVariable("REQUEST_METHOD", "GET");
+  SetEnvironmentVariable("CONTENT_TYPE", "text/plain");
   
   char clen[1000];
@@ -794,4 +796,5 @@
   SetEnvironmentVariable ("CONTENT_LENGTH", TEXT (clen));
 
+  // ref. https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863%28v=vs.85%29.aspx
   if (!CreateProcess (NULL,     // No module name (use command line)
                       TEXT (tmp),       // Command line
@@ -1798,7 +1801,8 @@
    * Initialize the specific [lenv] section which contains runtime variables:
    * 
-   *  - usid : it is an unique identification number 
+   *  - usid : it is an universally unique identifier  
+   *  - osid : it is an idenfitication number 
    *  - sid : it is the process idenfitication number (OS)
-   *  - uusid : it is an universally unique identification number 
+   *  - uusid : it is an universally unique identifier 
    *  - status : value between 0 and 100 to express the  completude of 
    * the operations of the running service 
@@ -1931,10 +1935,10 @@
   map *test1 = getMap (request_inputs, "cgiSid");
   if (test1 != NULL){
-      cgiSid = test1->value;
-      addToMap (request_inputs, "storeExecuteResponse", "true");
-      addToMap (request_inputs, "status", "true");
-      setMapInMaps (m, "lenv", "sid", test1->value);
-      status = getMap (request_inputs, "status");
-    }
+    cgiSid = zStrdup(test1->value);
+    addToMap (request_inputs, "storeExecuteResponse", "true");
+    addToMap (request_inputs, "status", "true");
+    setMapInMaps (m, "lenv", "osid", test1->value);
+    status = getMap (request_inputs, "status");
+  }
   test1 = getMap (request_inputs, "usid");
   if (test1 != NULL){
@@ -2129,5 +2133,4 @@
                              &request_input_real_format,
                              &request_output_real_format, &eres);
-
         }
       else
@@ -2182,5 +2185,4 @@
       lockShm (lid);
 #endif
-      fclose(f0);
       FILE *f3 = fopen (fbkp, "wb+");
       free (fbkp);
@@ -2206,5 +2208,5 @@
       }
 #ifndef RELY_ON_DB
-      dumpMapsToFile(bmap,fbkpres);
+      dumpMapsToFile(bmap,fbkpres,1);
       removeShmLock (m, 1);
 #else
@@ -2221,4 +2223,6 @@
       free (fbkp1);
       free (tmps1);
+      if(cgiSid!=NULL)
+	free(cgiSid);
     }
 
