Index: trunk/zoo-project/zoo-kernel/nmake.opt
===================================================================
--- trunk/zoo-project/zoo-kernel/nmake.opt	(revision 391)
+++ trunk/zoo-project/zoo-kernel/nmake.opt	(revision 392)
@@ -34,5 +34,5 @@
 TPATH=$(ZOOK_DIR)\..\..\..\tools
 LIBINTL_CPATH=$(ZOOK_DIR)\..\..\..\
-PYTHON_CPATH=$(ZOOK_DIR)\..\..\..\Python-2.7.3
+PYTHON_CPATH=$(PY_DIR)
 
 CFLAGS=$(CFLAGS1) /nologo /MD /W3 /EHsc /Ox /D_CRT_SECURE_NO_WARNINGS /DWIN32 $(CJFLAGS) -I$(ZOOK_DIR) -I./ $(JAVA_CFLAGS) $(MS_CFLAGS) -I$(TROOT)\..\..\thirds\dirent-win32 -I$(INTL_DIR) -I$(CURL_DIR)\include -I$(XML2_DIR)\include -I$(GDAL_DIR)\port $(JS_CFLAGS) -I$(GDAL_DIR)\ogr -I$(GDAL_DIR)\gcore -I$(GD_DIR) -I$(ICONV_DIR) -I$(TROOT)\..\..\thirds\include -I$(TROOT)\..\..\thirds\cgic206 -I$(PYTHON_CPATH)\include -I$(SSL_DIR)/inc32 -I$(FCGI_DIR)\include $(PY_CFLAGS) -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
Index: trunk/zoo-project/zoo-kernel/service_internal.c
===================================================================
--- trunk/zoo-project/zoo-kernel/service_internal.c	(revision 391)
+++ trunk/zoo-project/zoo-kernel/service_internal.c	(revision 392)
@@ -2,5 +2,5 @@
  * Author : Gérald FENOY
  *
- * Copyright (c) 2009-2012 GeoLabs SARL
+ * Copyright (c) 2009-2013 GeoLabs SARL
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -2518,5 +2518,5 @@
     fprintf(stderr,"MD5STR : (%s)\n\n",md5str);
 #endif
-    char* fname=(char*)malloc(sizeof(char)*(strlen(tmpM->value)+50));
+    char* fname=(char*)malloc(sizeof(char)*(strlen(tmpM->value)+strlen(md5str)+6));
     sprintf(fname,"%s/%s.zca",tmpM->value,md5str);
     struct stat f_status;
@@ -2548,7 +2548,7 @@
       fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
       FILE* f=fopen(cached,"rb");
-      fread(fcontent,sizeof(char),f_status.st_size,f);
+      int len=fread(fcontent,f_status.st_size,1,f);
       fsize=f_status.st_size;
-      hasF=1;
+      fcontent[fsize]=0;
     }
   }else{
@@ -2558,5 +2558,4 @@
       return errorException(m, _("Unable to allocate memory."), "InternalError");
     }
-    hasF=1;
     size_t dwRead;
     InternetReadFile(res, (LPVOID)fcontent, res.nDataLen, &dwRead);
@@ -2574,5 +2573,6 @@
   if(tmpMap->value==NULL)
     fprintf(stderr,"Unable to allocate memory!\n");
-  memcpy(tmpMap->value,fcontent,(fsize)*sizeof(char));
+  //snprintf(tmpMap->value,(fsize+1)*sizeof(char),fcontent);
+  memcpy(tmpMap->value,fcontent,(fsize+1)*sizeof(char));
   
   char ltmp1[256];
@@ -2582,6 +2582,4 @@
     addToCache(m,url,fcontent,fsize);
   else{
-    if(hasF)
-      free(fcontent);
     free(cached);
   }
Index: trunk/zoo-project/zoo-kernel/service_internal.h
===================================================================
--- trunk/zoo-project/zoo-kernel/service_internal.h	(revision 391)
+++ trunk/zoo-project/zoo-kernel/service_internal.h	(revision 392)
@@ -2,5 +2,5 @@
  * Author : Gérald FENOY
  *
- * Copyright (c) 2009-2011 GeoLabs SARL
+ * Copyright (c) 2009-2013 GeoLabs SARL
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -59,5 +59,4 @@
 #include <openssl/buffer.h>
 
-#include "cgic.h"
 #include "ulinet.h"
 
@@ -135,5 +134,5 @@
   char* isInCache(maps*,char*);
   int loadRemoteFile(maps*,map*,HINTERNET,char*);
-  
+
 #ifdef __cplusplus
 }
Index: trunk/zoo-project/zoo-kernel/service_internal_python.c
===================================================================
--- trunk/zoo-project/zoo-kernel/service_internal_python.c	(revision 391)
+++ trunk/zoo-project/zoo-kernel/service_internal_python.c	(revision 392)
@@ -2,5 +2,5 @@
  * Author : Gérald FENOY
  *
- * Copyright (c) 2009-2012 GeoLabs SARL
+ * Copyright (c) 2009-2013 GeoLabs SARL
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -25,4 +25,15 @@
 #include "service_internal_python.h"
 
+struct module_state {
+    PyObject *error;
+};
+
+#if PY_MAJOR_VERSION >= 3
+#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
+#else
+#define GETSTATE(m) (&_state)
+static struct module_state _state;
+#endif
+
 static PyObject* ZooError;
 
@@ -33,16 +44,61 @@
 };
 
+#if PY_MAJOR_VERSION >= 3
+
+static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
+  Py_VISIT(GETSTATE(m)->error);
+  return 0;
+}
+
+static int myextension_clear(PyObject *m) {
+  Py_CLEAR(GETSTATE(m)->error);
+  return 0;
+}
+
+static struct PyModuleDef moduledef = {
+  PyModuleDef_HEAD_INIT,
+  "zoo",
+  NULL,
+  sizeof(struct module_state),
+  zooMethods,
+  NULL,
+  myextension_traverse,
+  myextension_clear,
+  NULL
+};
+#endif
+
 PyMODINIT_FUNC init_zoo(){
   PyObject *tmp,*d;
-  PyObject* module = Py_InitModule("zoo", zooMethods);
-  if (module == NULL)
+  PyObject *module = 
+#if PY_MAJOR_VERSION >= 3
+    PyModule_Create(&moduledef);
+#else
+    Py_InitModule("zoo", zooMethods);
+#endif
+  if (module == NULL){
+#if PY_MAJOR_VERSION >= 3
+    return NULL;
+#else
     return;
-  
+#endif
+  }
+
+  struct module_state *st = GETSTATE(module);
+
   d = PyModule_GetDict(module);
+#if PY_MAJOR_VERSION >= 3
+  tmp = PyLong_FromLong(3);
+#else
   tmp = PyInt_FromLong(3);
+#endif
   PyDict_SetItemString(d, "SERVICE_SUCCEEDED", tmp);
   Py_DECREF(tmp);
 
+#if PY_MAJOR_VERSION >= 3
+  tmp = PyLong_FromLong(4);
+#else
   tmp = PyInt_FromLong(4);
+#endif
   PyDict_SetItemString(d, "SERVICE_FAILED", tmp);
   Py_DECREF(tmp);
@@ -51,4 +107,7 @@
   Py_INCREF(ZooError);
   PyModule_AddObject(module, "error", ZooError);
+#if PY_MAJOR_VERSION >= 3
+  return module;
+#endif
 }
 
@@ -57,6 +116,6 @@
   maps* inputs=*real_inputs;
   maps* outputs=*real_outputs;
-  char ntmp[1024];
-  getcwd(ntmp,1024);
+  map* tmp0=getMapFromMaps(*main_conf,"lenv","cwd");
+  char *ntmp=tmp0->value;
   map* tmp=NULL;
   tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH");
@@ -78,17 +137,21 @@
   tmp=NULL;
   tmp=getMap(request,"metapath");
-  char *pythonpath=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char));
-  if(tmp!=NULL && strcmp(tmp->value,"")!=0)
+  char *pythonpath;//=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char));
+  if(tmp!=NULL && strcmp(tmp->value,"")!=0){
+    pythonpath=(char*)malloc((4+strlen(python_path)+strlen(ntmp)+strlen(tmp->value))*sizeof(char));
 #ifdef WIN32
     sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
 #else
-  sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
-#endif
-  else
+    sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
+#endif
+  }
+  else{
+    pythonpath=(char*)malloc((2+strlen(python_path)+strlen(ntmp))*sizeof(char));
 #ifdef WIN32
     sprintf(pythonpath,"%s;%s",ntmp,python_path);
 #else
-  sprintf(pythonpath,"%s:%s",ntmp,python_path);
-#endif
+    sprintf(pythonpath,"%s:%s",ntmp,python_path);
+#endif
+  }
 #ifdef DEBUG
     fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);
@@ -106,7 +169,16 @@
 
   PyThreadState *mainstate;
+#if PY_MAJOR_VERSION >= 3
+  PyImport_AppendInittab("zoo", init_zoo);
+#else
   PyEval_InitThreads();
+#endif
   Py_Initialize();
+#if PY_MAJOR_VERSION >= 3
+  PyEval_InitThreads();
+  PyImport_ImportModule("zoo");
+#else
   init_zoo();
+#endif
   mainstate = PyThreadState_Swap(NULL);
   PyEval_ReleaseLock();
@@ -116,5 +188,10 @@
   tmp=getMap(s->content,"serviceProvider");
   if(tmp!=NULL)
-    pName = PyString_FromString(tmp->value);
+    pName = 
+#if PY_MAJOR_VERSION >= 3
+      PyUnicode_FromString(tmp->value);
+#else
+      PyString_FromString(tmp->value);
+#endif
   else{
     map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file.");
@@ -145,5 +222,9 @@
       pValue = PyObject_CallObject(pFunc, pArgs);
       if (pValue != NULL) {
+#if PY_MAJOR_VERSION >= 3
+	res=PyLong_AsLong(pValue);
+#else
 	res=PyInt_AsLong(pValue);
+#endif
 	freeMaps(real_outputs);
 	free(*real_outputs);
@@ -153,5 +234,9 @@
 	*real_outputs=mapsFromPyDict(arg3);
 #ifdef DEBUG
+#if PY_MAJOR_VERSION >= 3
+	fprintf(stderr,"Result of call: %i\n", PyLong_AsLong(pValue));
+#else
 	fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
+#endif
 	dumpMaps(inputs);
 	dumpMaps(*real_outputs);
@@ -162,13 +247,26 @@
 	PyObject *trace=PyObject_Str(pvalue);
 	char pbt[10240];
+#if PY_MAJOR_VERSION >= 3
+	if(PyUnicode_Check(trace))
+	  sprintf(pbt,"TRACE : %s",_PyUnicode_AsString(trace));
+#else
 	if(PyString_Check(trace))
 	  sprintf(pbt,"TRACE : %s",PyString_AsString(trace));
+#endif
 	else
 	  fprintf(stderr,"EMPTY TRACE ?");
 	trace=NULL;
 	trace=PyObject_Str(ptype);
+#if PY_MAJOR_VERSION >= 3
+	if(PyUnicode_Check(trace)){
+#else
 	if(PyString_Check(trace)){
+#endif
 	  char *tpbt=strdup(pbt);
+#if PY_MAJOR_VERSION >= 3
+	  sprintf(pbt,"%s\n%s\0",tpbt,_PyUnicode_AsString(trace));
+#else
 	  sprintf(pbt,"%s\n%s\0",tpbt,PyString_AsString(trace));
+#endif
 	  free(tpbt);
 	}
@@ -177,5 +275,9 @@
 	
 	char *tpbt=strdup(pbt);
+#if PY_MAJOR_VERSION >= 3
+	pName = PyUnicode_FromString("traceback");
+#else
 	pName = PyString_FromString("traceback");
+#endif
 	pModule = PyImport_Import(pName);
 	pArgs = PyTuple_New(1);
@@ -185,6 +287,11 @@
 	trace=NULL;
 	trace=PyObject_Str(pValue);
+#if PY_MAJOR_VERSION >= 3
+	if(PyUnicode_Check(trace))
+	  sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,_PyUnicode_AsString(trace));
+#else
 	if(PyString_Check(trace))
 	  sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",tpbt,PyString_AsString(trace));
+#endif
 	else
 	  sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations. %s",tpbt);
@@ -214,6 +321,8 @@
     //exit(-1);
   } 
+#if PY_MAJOR_VERSION < 3
   PyGILState_Release(gstate);
   PyEval_AcquireLock();
+#endif
   PyThreadState_Swap(mainstate);
   Py_Finalize();
@@ -226,5 +335,10 @@
   while(tmp!=NULL){
     PyObject* value=(PyObject*)PyDict_FromMap(tmp->content);
-    PyObject* name=PyString_FromString(tmp->name);
+    PyObject* name=
+#if PY_MAJOR_VERSION >= 3
+      PyUnicode_FromString(tmp->name);
+#else
+      PyString_FromString(tmp->name);
+#endif
     if(PyDict_SetItem(res,name,value)<0){
       fprintf(stderr,"Unable to set map value ...");
@@ -245,5 +359,10 @@
   map* tmap=getMapType(tmp);
   while(tmp!=NULL){
-    PyObject* name=PyString_FromString(tmp->name);
+    PyObject* name=
+#if PY_MAJOR_VERSION >= 3
+      PyUnicode_FromString(tmp->name);
+#else
+      PyString_FromString(tmp->name);
+#endif
     if(strcasecmp(tmp->name,"value")==0) {
       if(isArray!=NULL){
@@ -264,10 +383,19 @@
 	    PyObject* lsvalue;
 	    if(sMap==NULL){
+#if PY_MAJOR_VERSION >= 3
+	      lvalue=PyUnicode_FromString(vMap->value);
+#else
 	      lvalue=PyString_FromString(vMap->value);
+#endif
 	      lsvalue=Py_None;
 	    }
 	    else{
+#if PY_MAJOR_VERSION >= 3
+	      lvalue=PyUnicode_FromStringAndSize(vMap->value,atoi(sMap->value));
+	      lsvalue=PyUnicode_FromString(sMap->value);
+#else
 	      lvalue=PyString_FromStringAndSize(vMap->value,atoi(sMap->value));
 	      lsvalue=PyString_FromString(sMap->value);
+#endif
 	      hasSize=1;
 	    }
@@ -286,5 +414,9 @@
 	  PyObject* lmvalue;
 	  if(mMap!=NULL){
+#if PY_MAJOR_VERSION >= 3
+	    lmvalue=PyUnicode_FromString(mMap->value);
+#else
 	    lmvalue=PyString_FromString(mMap->value);
+#endif
 	  }else
 	    lmvalue=Py_None;
@@ -301,10 +433,18 @@
 	  return NULL;
 	}
+#if PY_MAJOR_VERSION >= 3
+	if(PyDict_SetItem(res,PyUnicode_FromString(tmap->name),mvalue)<0){
+#else
 	if(PyDict_SetItem(res,PyString_FromString(tmap->name),mvalue)<0){
+#endif
 	  fprintf(stderr,"Unable to set key value pair...");
 	  return NULL;
 	}
 	if(hasSize>0)
+#if PY_MAJOR_VERSION >= 3
+	  if(PyDict_SetItem(res,PyUnicode_FromString("size"),svalue)<0){
+#else
 	  if(PyDict_SetItem(res,PyString_FromString("size"),svalue)<0){
+#endif
 	    fprintf(stderr,"Unable to set key value pair...");
 	    return NULL;
@@ -312,5 +452,10 @@
       }
       else if(size!=NULL){
-	PyObject* value=PyString_FromStringAndSize(tmp->value,atoi(size->value));
+	PyObject* value=
+#if PY_MAJOR_VERSION >= 3
+	  PyUnicode_FromStringAndSize(tmp->value,atoi(size->value));
+#else
+	  PyString_FromStringAndSize(tmp->value,atoi(size->value));
+#endif
 	if(PyDict_SetItem(res,name,value)<0){
 	  fprintf(stderr,"Unable to set key value pair...");
@@ -319,5 +464,10 @@
       }
       else{
-	PyObject* value=PyString_FromString(tmp->value);
+	PyObject* value=
+#if PY_MAJOR_VERSION >= 3
+	  PyUnicode_FromString(tmp->value);
+#else
+	  PyString_FromString(tmp->value);
+#endif
 	if(PyDict_SetItem(res,name,value)<0){
 	  fprintf(stderr,"Unable to set key value pair...");
@@ -328,5 +478,10 @@
     else{
       if(PyDict_GetItem(res,name)==NULL){
-	PyObject* value=PyString_FromString(tmp->value);
+	PyObject* value=
+#if PY_MAJOR_VERSION >= 3
+	  PyUnicode_FromString(tmp->value);
+#else
+	  PyString_FromString(tmp->value);
+#endif
 	if(PyDict_SetItem(res,name,value)<0){
 	  fprintf(stderr,"Unable to set key value pair...");
@@ -358,5 +513,9 @@
 #endif
     cursor=(maps*)malloc(MAPS_SIZE);
+#if PY_MAJOR_VERSION >= 3
+    cursor->name=_PyUnicode_AsString(key);
+#else
     cursor->name=PyString_AsString(key);
+#endif
     cursor->content=mapFromPyDict((PyDictObject*)value);
 #ifdef DEBUG
@@ -391,12 +550,28 @@
 	    PyString_AsString(key),PyString_AsString(value));
 #endif
+#if PY_MAJOR_VERSION >= 3
+    if(strcmp(_PyUnicode_AsString(key),"value")==0){
+#else
     if(strcmp(PyString_AsString(key),"value")==0){
+#endif
       char *buffer=NULL;
       Py_ssize_t size;
+#if PY_MAJOR_VERSION >= 3
+      buffer=_PyUnicode_AsStringAndSize(value,&size);
+#else
       PyString_AsStringAndSize(value,&buffer,&size);
+#endif
       if(res!=NULL){
+#if PY_MAJOR_VERSION >= 3
+	addToMap(res,_PyUnicode_AsString(key),"");
+#else
 	addToMap(res,PyString_AsString(key),"");
+#endif
       }else{
+#if PY_MAJOR_VERSION >= 3
+	res=createMap(_PyUnicode_AsString(key),"");
+#else
 	res=createMap(PyString_AsString(key),"");
+#endif
       }
       map* tmpR=getMap(res,"value");
@@ -409,8 +584,19 @@
       addToMap(res,"size",sin);
     }else{
-      if(res!=NULL)
+      if(res!=NULL){
+#if PY_MAJOR_VERSION >= 3
+	addToMap(res,_PyUnicode_AsString(key),_PyUnicode_AsString(value));
+#else
 	addToMap(res,PyString_AsString(key),PyString_AsString(value));
-      else
-	res=createMap(PyString_AsString(key),PyString_AsString(value));
+#endif
+      }
+      else{
+	res=
+#if PY_MAJOR_VERSION >= 3
+	  createMap(_PyUnicode_AsString(key),_PyUnicode_AsString(value));
+#else
+	  createMap(PyString_AsString(key),PyString_AsString(value));
+#endif
+      }
     }
   }
@@ -428,5 +614,9 @@
     return NULL;
   }
+#if PY_MAJOR_VERSION >= 3
+  return PyUnicode_FromString(_ss(str));
+#else
   return PyString_FromString(_ss(str));
+#endif
 }
 
@@ -456,5 +646,10 @@
     PyObject* lenv = PyMapping_GetItemString(confdict, "lenv");
     if (lenv && PyMapping_Check(lenv)){
-      PyObject* valobj = PyString_FromString(status);
+      PyObject* valobj = 
+#if PY_MAJOR_VERSION >= 3
+	PyUnicode_FromString(status);
+#else
+	PyString_FromString(status);
+#endif
       PyMapping_SetItemString(lenv, "status", valobj);
       Py_DECREF(valobj);
Index: trunk/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 391)
+++ trunk/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 392)
@@ -2,5 +2,5 @@
  * Author : Gérald FENOY
  *
- *  Copyright 2008-2012 GeoLabs SARL. All rights reserved.
+ *  Copyright 2008-2013 GeoLabs SARL. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -272,5 +272,4 @@
 #endif
 #endif
-
     if( so != NULL ) {
 #ifdef DEBUG
@@ -378,5 +377,5 @@
       char* errstr = dlerror();
 #endif
-      sprintf(tmps,_("C Library can't be loaded %s \n"),errstr);
+      sprintf(tmps,_("C Library can't be loaded %s"),errstr);
       map* tmps1=createMap("text",tmps);
       printExceptionReportResponse(m,tmps1);
@@ -1896,5 +1895,6 @@
   }
   
-  //if(CHECK_INET_HANDLE(hInternet))
+
+  //  if(CHECK_INET_HANDLE(hInternet))
   InternetCloseHandle(hInternet);
 
@@ -2039,7 +2039,7 @@
       ;
 #endif
-	  char* toto=(char*)malloc((strlen(mapcs->name)+strlen(mapcs->value)+2)*sizeof(char));
+      char* toto=(char*)malloc((strlen(mapcs->name)+strlen(mapcs->value)+2)*sizeof(char));
       sprintf(toto,"%s=%s",mapcs->name,mapcs->value);
-	  putenv(toto);
+      putenv(toto);
 #ifdef DEBUG
       fflush(stderr);
@@ -2111,5 +2111,5 @@
       token=strtok_r(cgiCookie,";",&saveptr);
       while(token!=NULL){
-	if(strcasestr(token,"ID")!=NULL){
+	if(strcasestr(token,"ID=")!=NULL){
 	  if(tcook!=NULL)
 	    free(tcook);
@@ -2120,5 +2120,5 @@
       }
     }else{
-      if(strstr(cgiCookie,"=")!=NULL && strcasestr(cgiCookie,"ID")!=NULL){
+      if(strstr(cgiCookie,"=")!=NULL && strcasestr(cgiCookie,"ID=")!=NULL){
 	tcook=strdup(cgiCookie);
 	hasValidCookie=1;
