Index: trunk/zoo-kernel/zoo_loader.c
===================================================================
--- trunk/zoo-kernel/zoo_loader.c	(revision 1)
+++ trunk/zoo-kernel/zoo_loader.c	(revision 9)
@@ -23,4 +23,7 @@
  */
 
+#define MALLOC_CHECK_ 0
+#define MALLOC_CHECK 0
+
 /**
  * Specific includes
@@ -31,10 +34,8 @@
 #include <sys/types.h>
 #include <unistd.h>
+#include "service_internal.h"
+
 extern "C" {
 #include "cgic.h"
-}
-
-#include "service_internal.h"
-extern "C" {
 #include <libxml/tree.h>
 #include <libxml/xmlmemory.h>
@@ -44,7 +45,26 @@
 }
 
+xmlXPathObjectPtr extractFromDoc(xmlDocPtr,char*);
+int runRequest(map*);
+
+using namespace std;
+
+/* ************************************************************************* */
+
+int errorException(maps *m, const char *message, const char *errorcode) 
+{
+  map * errormap = createMap("text", message);
+  addToMap(errormap,"code", errorcode);
+  printExceptionReportResponse(m,errormap);
+  freeMap(&errormap);
+  free(errormap);
+  return -1;
+}
+
+/* ************************************************************************* */
+
 #ifndef STRTOK_R
 char *
-strtok_r(char *s1, const char *s2, char **lasts)
+strtok_r (char *s1, const char *s2, char **lasts)
 {
   char *ret;
@@ -52,12 +72,12 @@
   if (s1 == NULL)
     s1 = *lasts;
-  while(*s1 && strchr(s2, *s1))
+  while (*s1 && strchr(s2, *s1))
     ++s1;
-  if(*s1 == '\0')
+  if (*s1 == '\0')
     return NULL;
   ret = s1;
-  while(*s1 && !strchr(s2, *s1))
+  while (*s1 && !strchr(s2, *s1))
     ++s1;
-  if(*s1)
+  if (*s1)
     *s1++ = '\0';
   *lasts = s1;
@@ -67,69 +87,6 @@
 #endif
 
-xmlNodeSet* extractFromDoc(xmlDocPtr,char*);
-int runRequest(map*);
-
-using namespace std;
-
-char* strtoupper(char* str)
-{
-  int leng=strlen(str);
-  for(int i=0; i<leng; i++)
-    if (97<=str[i]&&str[i]<=122)//a-z
-      str[i]-=32;
-  return str;
-}
-
-char* strtolower(char* str)
-{
-  int leng=strlen(str);
-  for(int i=0; i<leng; i++)
-    if (65<=str[i]&&str[i]<=90)//A-Z
-      str[i]+=32;
-  return str;
-}
-
-char* remplace(char* delim,char* rep,char* source){
-  if(strcmp(source,"")==0){
-#ifdef DEBUG
-    //char *tmp="RETURN NULL !\n";
-    //printf((void*)tmp);
-#endif
-    return "NULL";
-  }
-
-  char *_token; 
-  char *origin=strdup(source);
-  char result[1024]="";
-  _token = strtok(source, delim);
-#ifdef DEBUG
-  fprintf(stderr,"\nREPLACE TOKEN (%s == %s => %d)\n ",_token,origin,strcmp(_token,origin));
-#endif
-  if(strcmp(_token,origin)!=0)
-    while(_token!=NULL){
-      sprintf(result,"%s%s%s",result,_token,rep);
-      _token = strtok (NULL, delim);
-      if(_token==NULL)
-	result[strlen(result)-strlen(rep)]=0;
-#ifdef DEBUG
-      fprintf(stderr,"\n\nRESULT(%s)\n\n",result);
-#endif
-    }
-  else
-    return origin;
-  
-  return strdup(result);
-}
-
-
-char *colors[] = {
-  "red", "green", "blue"
-};
-
-#define colorsTotal 3
-#define TRUE -1
+#define TRUE 1
 #define FALSE -1
-static bool started=FALSE;
-static bool isStarted=FALSE;
 
 int cgiMain(){
@@ -140,12 +97,11 @@
   dup2(fileno(cgiOut),fileno(stdout));
 #ifdef DEBUG
-    fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
-    fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
-    fflush(cgiOut);
+  fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
+  fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
+  fflush(cgiOut);
 #endif
   
   /**
    * Read the main configuration file
-   */
   if(!isStarted){
     maps* m;
@@ -159,22 +115,25 @@
     isStarted=TRUE;
   }
-
-#ifdef DEBUG
-  char textb[23];
-  sprintf(textb,"\n%s\n%s\n",cgiRemoteAddr,cgiRequestMethod);
-  fprintf(stderr,"****%s\n%s\n***",textb,cgiQueryString);
-#endif
-
-  map* tmp=NULL;
+   */
+
+#ifdef DEBUG
+  fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); 
+  fprintf (stderr, "RequestMethod:%s\n", cgiRequestMethod); 
+  fprintf (stderr, "Request: %s\n", cgiQueryString);
+#endif
+
+  map* tmpMap=NULL;
     
   if(strncmp(cgiContentType,"text/xml",8)==0){
-    char *buffer=new char[cgiContentLength];
+    char *buffer=new char[cgiContentLength+1];
     if(fread(buffer,1,cgiContentLength,cgiIn)){
       buffer[cgiContentLength]=0;
-      tmp=createMap("request",buffer);
+      tmpMap=createMap("request",buffer);
     }else{
       /* Here we have to return an error message ... */
+      fprintf(stderr, "Unable to read cgi content in zoo_loader.c line %i\n", __LINE__);      
       return 1; 
     }
+    delete[]buffer;
   }
   else{
@@ -190,9 +149,10 @@
       fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
 #endif
-      if(tmp!=NULL)
-	addToMap(tmp,*arrayStep,value);
+      if(tmpMap!=NULL)
+	addToMap(tmpMap,*arrayStep,value);
       else
-	tmp=createMap(*arrayStep,value);
+	tmpMap=createMap(*arrayStep,value);
       arrayStep++;
+      delete[]value;
     }
     cgiStringArrayFree(array);
@@ -201,46 +161,23 @@
   /**
    * In case that the POST method was used, then check if params came in XML
-   * format (attribute request should be the once).
-   */
-  if(strcmp(cgiRequestMethod,mtoupper("post"))==0 && count(tmp)==1){
+   * format else try to use the attribute "request" which should be the only 
+   * one.
+   */
+  if(strncasecmp(cgiRequestMethod,"post",4)==0 || count(tmpMap)==1){
     /**
      * First include the MetaPath and the ServiceProvider default parameters
      * (which should be always available in GET params so in cgiQueryString)
      */
-    char *saveptr1, *saveptr2;
-    char *str1, *str2, *token, *subtoken;
+    //char *saveptr1, *saveptr2;
+    char *str1/*, str2, *token, *subtoken*/;
     str1=cgiQueryString;
-    token=strtok_r(str1,"&",&saveptr1);
-    while(token!=NULL){
-#ifdef DEBUG
-      fprintf(stderr,"%s",token);
-#endif
-      str2=token;
-      subtoken=strtok_r(str2,"=",&saveptr2);
-#ifdef DEBUG
-      fprintf(stderr,"%s\n",subtoken);
-#endif
-      char* tmp_name;
-      if(subtoken!=NULL){
-	tmp_name=subtoken;
-#ifdef DEBUG
-	fprintf(stderr,"%s",subtoken);
-#endif
-	subtoken=strtok_r(NULL,"=",&saveptr2);
-	if(subtoken!=NULL)
-	  addToMap(tmp,tmp_name,subtoken);
-	else
-	  addToMap(tmp,tmp_name,"");
-      }
-      token=strtok_r(NULL,"&",&saveptr1);
-    }
     /**
      * Store the original XML request in xrequest map
      */
-    map* t1=getMap(tmp,"request");
+    map* t1=getMap(tmpMap,"request");
     if(t1!=NULL){
-      addToMap(tmp,"xrequest",t1->value);
+      addToMap(tmpMap,"xrequest",t1->value);
       xmlInitParser();
-      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);      
+      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
       xmlNodePtr cur = xmlDocGetRootElement(doc);
       char *tval;
@@ -248,9 +185,9 @@
       tval = (char*) xmlGetProp(cur,BAD_CAST "service");
       if(tval!=NULL)
-	addToMap(tmp,"service",tval);
+	addToMap(tmpMap,"service",tval);
       tval=NULL;
       tval = (char*) xmlGetProp(cur,BAD_CAST "language");
       if(tval!=NULL)
-	addToMap(tmp,"language",tval);
+	addToMap(tmpMap,"language",tval);
       
       char* requests[3];
@@ -261,19 +198,25 @@
 	char tt[35];
 	sprintf(tt,"/*[local-name()='%s']",requests[j]);
-	xmlNodeSet* req=extractFromDoc(doc,tt);
-#ifdef DEBUG
-	fprintf(stderr,"%i",req->nodeNr);
-#endif
-	if(req->nodeNr==1){
-	  t1->value=requests[j];
-	  j=2;
+	xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
+	if(reqptr!=NULL){
+	  xmlNodeSet* req=reqptr->nodesetval;
+#ifdef DEBUG
+	  fprintf(stderr,"%i",req->nodeNr);
+#endif
+	  if(req!=NULL && req->nodeNr==1){
+	    t1->value=requests[j];
+	    j=2;
+	  }
+	  xmlXPathFreeObject(reqptr);
 	}
-	xmlFree(req);
+	//xmlFree(req);
       }
-      if(strcmp(mtoupper(t1->value),mtoupper("GetCapabilities"))==0){	
-	xmlNodeSet* vers=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
+      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
+	xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
+	xmlNodeSet* vers=versptr->nodesetval;
 	xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
-	addToMap(tmp,"version",(char*)content);
-	xmlFree(vers);
+	addToMap(tmpMap,"version",(char*)content);
+	xmlXPathFreeObject(versptr);
+	//xmlFree(vers);
 	xmlFree(content);
       }else{
@@ -281,36 +224,50 @@
 	tval = (char*) xmlGetProp(cur,BAD_CAST "version");
 	if(tval!=NULL)
-	  addToMap(tmp,"version",tval);
+	  addToMap(tmpMap,"version",tval);
 	xmlFree(tval);
 	tval = (char*) xmlGetProp(cur,BAD_CAST "language");
 	if(tval!=NULL)
-	  addToMap(tmp,"language",tval);
-	xmlNodeSet* id=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
-	char* identifiers=NULL;
-	identifiers=(char*)malloc(cgiContentLength);
-	identifiers[0]=0;
-	for(int k=0;k<id->nodeNr;k++){
-	  xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
-	  if(strlen(identifiers)>0){
-	    sprintf(identifiers,"%s,%s",identifiers,content);
-	    identifiers[strlen(identifiers)]=0;
+	  addToMap(tmpMap,"language",tval);
+	xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
+	if(idptr!=NULL){
+	  xmlNodeSet* id=idptr->nodesetval;
+	  if(id!=NULL){
+	    char* identifiers=NULL;
+	    identifiers=(char*)calloc(cgiContentLength,sizeof(char));
+	    identifiers[0]=0;
+	    for(int k=0;k<id->nodeNr;k++){
+	      xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
+	      if(strlen(identifiers)>0){
+		char *tmp=strdup(identifiers);
+		snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
+		free(tmp);
+	      }
+	      else{
+		snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
+	      }
+	      xmlFree(content);
+	    }
+	    xmlXPathFreeObject(idptr);
+	    addToMap(tmpMap,"Identifier",identifiers);
+	    free(identifiers);
 	  }
-	  else{
-	    sprintf(identifiers,"%s",content);
-	    identifiers[strlen(identifiers)]=0;
-	  }
-	  xmlFree(content);
 	}
-	xmlFree(id);
-	addToMap(tmp,"Identifier",identifiers);
+	//xmlFree(id);
       }
-      //xmlCleanupParser();
       xmlFree(tval);
-    }
-  }
-  //dumpMap(tmp);
-  runRequest(tmp);
-  //dumpMap(tmp);
-
+      xmlFreeDoc(doc);
+      xmlCleanupParser();
+    }
+  }
+
+  runRequest(tmpMap);
+
+  /** 
+   * Required but can't be made after executing a process using POST requests.
+   */
+  if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){
+    freeMap(&tmpMap);
+    free(tmpMap);
+  }
   return 0;
 
