source: trunk/zoo-project/zoo-kernel/zoo_loader.c @ 364

Last change on this file since 364 was 364, checked in by djay, 12 years ago

Update to make ZOO-Kernel able to compile and run from Windows Platforms. A special thanks to Espen Messel, Knut Landmark and Benrd Härtwig for providing many patches that I can successfully apply on the SVN source tree and to Farkas for continuing requesting for ZOO-Kernel to run on Windows platforms privately and through the ZOO-Discuss mailing list.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 9.7 KB
RevLine 
[1]1/**
2 * Author : Gérald FENOY
3 *
[69]4 *  Copyright 2008-2011 GeoLabs SARL. All rights reserved.
[1]5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
[9]25#define MALLOC_CHECK_ 0
26#define MALLOC_CHECK 0
27
[1]28/**
29 * Specific includes
30 */
[364]31#ifndef WIN32
[1]32#include "fcgio.h"
33#include "fcgi_config.h"
34#include "fcgi_stdio.h"
[364]35#endif
[1]36#include <sys/types.h>
37#include <unistd.h>
[9]38#include "service_internal.h"
[364]39#ifdef WIN32
40#include "windows.h"
41#define strtok_r strtok_s
42#endif
[9]43
[1]44extern "C" {
45#include "cgic.h"
46#include <libxml/tree.h>
47#include <libxml/xmlmemory.h>
48#include <libxml/parser.h>
49#include <libxml/xpath.h>
50#include <libxml/xpathInternals.h>
51}
52
[280]53#include "service_internal.h"
54
[114]55xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*);
[9]56int runRequest(map*);
57
58using namespace std;
59
[364]60#ifndef TRUE
[9]61#define TRUE 1
[364]62#endif
63#ifndef FALSE
[1]64#define FALSE -1
[364]65#endif
[1]66
67int cgiMain(){
68  /**
69   * We'll use cgiOut as the default output (stdout) to produce plain text
70   * response.
71   */
72  dup2(fileno(cgiOut),fileno(stdout));
73#ifdef DEBUG
[9]74  fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
75  fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
76  fflush(cgiOut);
77  fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); 
[280]78  fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); 
[9]79  fprintf (stderr, "Request: %s\n", cgiQueryString);
[1]80#endif
81
[331]82  char *strQuery=strdup(cgiQueryString);
[9]83  map* tmpMap=NULL;
[69]84
[99]85  if(strncmp(cgiContentType,"text/xml",8)==0 || 
86     strncasecmp(cgiRequestMethod,"post",4)==0){
[280]87    if(cgiContentLength==NULL){
88       cgiContentLength=0;
89       char *buffer=new char[2];
90       char *res=NULL;
91       int r=0;
92       while(r=fread(buffer,sizeof(char),1,cgiIn)){
93         cgiContentLength+=r;
94         if(res==NULL){
95           res=(char*)malloc(1*sizeof(char));
96           sprintf(res,"%s",buffer);
97         }
98         else{
99           res=(char*)realloc(res,(cgiContentLength+1)*sizeof(char));
100           char *tmp=strdup(res);
101           sprintf(res,"%s%s",tmp,buffer);
102           free(tmp);
103         }
104       }
[286]105       if(res==NULL){
106         return errorException(NULL,"ZOO-Kernel failed to process your request cause the request was emtpty.","InternalError");
107       }else
108         tmpMap=createMap("request",res);
[1]109    }else{
[280]110      char *buffer=new char[cgiContentLength+1];
111      if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)){
112        buffer[cgiContentLength]=0;
113        tmpMap=createMap("request",buffer);
[331]114        fprintf(stderr,"%s\n",tmpMap->value);
[280]115      }else{
116        buffer[0]=0;
117        char **array, **arrayStep;
118        if (cgiFormEntries(&array) != cgiFormSuccess) {
119          return 1;
120        }
121        arrayStep = array;
122        while (*arrayStep) {
123          char *ivalue=new char[cgiContentLength];
124          cgiFormStringNoNewlines(*arrayStep, ivalue, cgiContentLength);
125          char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+1)*sizeof(char));
126          sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
127          if(strlen(buffer)==0){
128            sprintf(buffer,"%s",tmpValueFinal);
129          }else{
130            char *tmp=strdup(buffer);
131            sprintf(buffer,"%s&%s",tmp,tmpValueFinal);
132            free(tmp);
133          }
134         
135          sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
136          free(tmpValueFinal);
[99]137#ifdef DEBUG
[280]138          fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue);
[99]139#endif
[280]140          delete[]ivalue;
141          arrayStep++;
142        }
143        tmpMap=createMap("request",buffer);
[99]144      }
[280]145      delete[]buffer;
[1]146    }
147  }
148  else{
[364]149#ifdef DEBUG
[331]150    dumpMap(tmpMap);
[364]151#endif
[1]152    char **array, **arrayStep;
153    if (cgiFormEntries(&array) != cgiFormSuccess) {
154      return 1;
155    }
156    arrayStep = array;
157    while (*arrayStep) {
158      char *value=new char[cgiContentLength];
159      cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
160#ifdef DEBUG
161      fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
162#endif
[9]163      if(tmpMap!=NULL)
164        addToMap(tmpMap,*arrayStep,value);
[1]165      else
[9]166        tmpMap=createMap(*arrayStep,value);
[1]167      arrayStep++;
[9]168      delete[]value;
[1]169    }
170    cgiStringArrayFree(array);
171  }
172
173  /**
174   * In case that the POST method was used, then check if params came in XML
[9]175   * format else try to use the attribute "request" which should be the only
176   * one.
[1]177   */
[10]178  if(strncasecmp(cgiRequestMethod,"post",4)==0 || 
179     (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){
[1]180    /**
181     * First include the MetaPath and the ServiceProvider default parameters
182     * (which should be always available in GET params so in cgiQueryString)
183     */
[19]184    char *str1;
[1]185    str1=cgiQueryString;
186    /**
187     * Store the original XML request in xrequest map
188     */
[9]189    map* t1=getMap(tmpMap,"request");
[331]190    if(t1!=NULL && strncasecmp(t1->value,"<",1)==0){
[9]191      addToMap(tmpMap,"xrequest",t1->value);
[1]192      xmlInitParser();
[9]193      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
[280]194
195
196      {
197        xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*");
198        if(reqptr!=NULL){
199          xmlNodeSet* req=reqptr->nodesetval;
200          if(req!=NULL && req->nodeNr==1){
201            addToMap(tmpMap,"soap","true");
202            int k=0;
203            for(k;k < req->nodeNr;k++){
204              xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi");
205              xmlDocSetRootElement(doc, req->nodeTab[k]);
206              xmlChar *xmlbuff;
207              int buffersize;
208              xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1);
209              addToMap(tmpMap,"xrequest",(char*)xmlbuff);
210              char *tmp=(char*)xmlbuff;
211              fprintf(stderr,"%s\n",tmp);
212              xmlFree(xmlbuff);
213            }
214          }
215        }
216      }
217
[1]218      xmlNodePtr cur = xmlDocGetRootElement(doc);
219      char *tval;
220      tval=NULL;
221      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
222      if(tval!=NULL)
[9]223        addToMap(tmpMap,"service",tval);
[1]224      tval=NULL;
225      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
226      if(tval!=NULL)
[9]227        addToMap(tmpMap,"language",tval);
[280]228      const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"};
[1]229      for(int j=0;j<3;j++){
[280]230        char tt[128];
[1]231        sprintf(tt,"/*[local-name()='%s']",requests[j]);
[9]232        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
233        if(reqptr!=NULL){
234          xmlNodeSet* req=reqptr->nodesetval;
[1]235#ifdef DEBUG
[9]236          fprintf(stderr,"%i",req->nodeNr);
[1]237#endif
[9]238          if(req!=NULL && req->nodeNr==1){
[114]239            t1->value=strdup(requests[j]);
[9]240            j=2;
241          }
242          xmlXPathFreeObject(reqptr);
[1]243        }
[9]244        //xmlFree(req);
[1]245      }
[9]246      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
247        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
248        xmlNodeSet* vers=versptr->nodesetval;
[1]249        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
[9]250        addToMap(tmpMap,"version",(char*)content);
251        xmlXPathFreeObject(versptr);
252        //xmlFree(vers);
[1]253        xmlFree(content);
254      }else{
255        tval=NULL;
256        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
257        if(tval!=NULL)
[9]258          addToMap(tmpMap,"version",tval);
[1]259        xmlFree(tval);
260        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
261        if(tval!=NULL)
[9]262          addToMap(tmpMap,"language",tval);
263        xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
264        if(idptr!=NULL){
265          xmlNodeSet* id=idptr->nodesetval;
266          if(id!=NULL){
267            char* identifiers=NULL;
268            identifiers=(char*)calloc(cgiContentLength,sizeof(char));
269            identifiers[0]=0;
270            for(int k=0;k<id->nodeNr;k++){
271              xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
272              if(strlen(identifiers)>0){
273                char *tmp=strdup(identifiers);
274                snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
275                free(tmp);
276              }
277              else{
278                snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
279              }
280              xmlFree(content);
281            }
282            xmlXPathFreeObject(idptr);
283            addToMap(tmpMap,"Identifier",identifiers);
284            free(identifiers);
[1]285          }
286        }
[9]287        //xmlFree(id);
[1]288      }
289      xmlFree(tval);
[9]290      xmlFreeDoc(doc);
291      xmlCleanupParser();
[331]292    }else{
293      freeMap(&tmpMap);
294      free(tmpMap);
295      tmpMap=createMap("not_valid","true");
[1]296    }
[329]297
298    char *token,*saveptr;
299    token=strtok_r(cgiQueryString,"&",&saveptr);
300    while(token!=NULL){
301      char *token1,*saveptr1;
302      char *name=NULL;
303      char *value=NULL;
304      token1=strtok_r(token,"=",&saveptr1);
305      while(token1!=NULL){
306        if(name==NULL)
307          name=strdup(token1);
308        else
309          value=strdup(token1);
310        token1=strtok_r(NULL,"=",&saveptr1);
311      }
[331]312      addToMap(tmpMap,name,value);
[329]313      free(name);
314      free(value);
[331]315      name=NULL;
316      value=NULL;
[329]317      token=strtok_r(NULL,"&",&saveptr);
318    }
319   
[1]320  }
321
[331]322
323  if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){
324      map* tmp=getMap(tmpMap,"dataInputs");
325      if(tmp!=NULL){
326        addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11);
327      }
328    }
329
[9]330  runRequest(tmpMap);
331
332  /**
333   * Required but can't be made after executing a process using POST requests.
334   */
335  if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){
336    freeMap(&tmpMap);
337    free(tmpMap);
338  }
[1]339  return 0;
340
341}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png