source: branches/branch-1.2/zoo-kernel/zoo_loader.c

Last change on this file was 301, checked in by djay, 13 years ago

Merge branch-1.2 r268:r296.

File size: 8.7 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 *  Copyright 2008-2011 GeoLabs SARL. All rights reserved.
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
25#define MALLOC_CHECK_ 0
26#define MALLOC_CHECK 0
27
28#ifdef WIN32
29#include "windows.h"
30#endif
31/**
32 * Specific includes
33 */
34#include "fcgio.h"
35#include "fcgi_config.h"
36#include "fcgi_stdio.h"
37#include <sys/types.h>
38#include <unistd.h>
39#include "service_internal.h"
40
41extern "C" {
42#include "cgic.h"
43#include <libxml/tree.h>
44#include <libxml/xmlmemory.h>
45#include <libxml/parser.h>
46#include <libxml/xpath.h>
47#include <libxml/xpathInternals.h>
48}
49
50#include "service_internal.h"
51
52xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*);
53int runRequest(map*);
54
55using namespace std;
56
57#define TRUE 1
58#define FALSE -1
59
60int cgiMain(){
61  /**
62   * We'll use cgiOut as the default output (stdout) to produce plain text
63   * response.
64   */
65  dup2(fileno(cgiOut),fileno(stdout));
66#ifdef DEBUG
67  fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
68  fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
69  fflush(cgiOut);
70#endif
71 
72#ifdef DEBUG
73  fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); 
74  fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); 
75  fprintf (stderr, "Request: %s\n", cgiQueryString);
76#endif
77
78  map* tmpMap=NULL;
79
80  if(strncmp(cgiContentType,"text/xml",8)==0 || 
81     strncasecmp(cgiRequestMethod,"post",4)==0){
82    if(cgiContentLength==NULL){
83       cgiContentLength=0;
84       char *buffer=new char[2];
85       char *res=NULL;
86       int r=0;
87       while(r=fread(buffer,sizeof(char),1,cgiIn)){
88         cgiContentLength+=r;
89         if(res==NULL){
90           res=(char*)malloc(1*sizeof(char));
91           sprintf(res,"%s",buffer);
92         }
93         else{
94           res=(char*)realloc(res,(cgiContentLength+1)*sizeof(char));
95           char *tmp=strdup(res);
96           sprintf(res,"%s%s",tmp,buffer);
97           free(tmp);
98         }
99       }
100       if(res==NULL){
101         return errorException(NULL,"ZOO-Kernel failed to process your request cause the request was emtpty.","InternalError");
102       }else
103         tmpMap=createMap("request",res);
104    }else{
105      char *buffer=new char[cgiContentLength+1];
106      if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)){
107        buffer[cgiContentLength]=0;
108        tmpMap=createMap("request",buffer);
109      }else{
110        buffer[0]=0;
111        char **array, **arrayStep;
112        if (cgiFormEntries(&array) != cgiFormSuccess) {
113          return 1;
114        }
115        arrayStep = array;
116        while (*arrayStep) {
117          char *ivalue=new char[cgiContentLength];
118          cgiFormStringNoNewlines(*arrayStep, ivalue, cgiContentLength);
119          char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+1)*sizeof(char));
120          sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
121          if(strlen(buffer)==0){
122            sprintf(buffer,"%s",tmpValueFinal);
123          }else{
124            char *tmp=strdup(buffer);
125            sprintf(buffer,"%s&%s",tmp,tmpValueFinal);
126            free(tmp);
127          }
128         
129          sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
130          free(tmpValueFinal);
131#ifdef DEBUG
132          fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue);
133#endif
134          delete[]ivalue;
135          arrayStep++;
136        }
137        tmpMap=createMap("request",buffer);
138      }
139      delete[]buffer;
140    }
141  }
142  else{
143    char **array, **arrayStep;
144    if (cgiFormEntries(&array) != cgiFormSuccess) {
145      return 1;
146    }
147    arrayStep = array;
148    while (*arrayStep) {
149      char *value=new char[cgiContentLength];
150      cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
151#ifdef DEBUG
152      fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
153#endif
154      if(tmpMap!=NULL)
155        addToMap(tmpMap,*arrayStep,value);
156      else
157        tmpMap=createMap(*arrayStep,value);
158      arrayStep++;
159      delete[]value;
160    }
161    cgiStringArrayFree(array);
162  }
163
164  /**
165   * In case that the POST method was used, then check if params came in XML
166   * format else try to use the attribute "request" which should be the only
167   * one.
168   */
169  if(strncasecmp(cgiRequestMethod,"post",4)==0 || 
170     (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){
171    /**
172     * First include the MetaPath and the ServiceProvider default parameters
173     * (which should be always available in GET params so in cgiQueryString)
174     */
175    char *str1;
176    str1=cgiQueryString;
177    /**
178     * Store the original XML request in xrequest map
179     */
180    map* t1=getMap(tmpMap,"request");
181    if(t1!=NULL){
182      addToMap(tmpMap,"xrequest",t1->value);
183      xmlInitParser();
184      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
185
186
187      {
188        xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*");
189        if(reqptr!=NULL){
190          xmlNodeSet* req=reqptr->nodesetval;
191          if(req!=NULL && req->nodeNr==1){
192            addToMap(tmpMap,"soap","true");
193            int k=0;
194            for(k;k < req->nodeNr;k++){
195              xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi");
196              xmlDocSetRootElement(doc, req->nodeTab[k]);
197              xmlChar *xmlbuff;
198              int buffersize;
199              xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1);
200              addToMap(tmpMap,"xrequest",(char*)xmlbuff);
201              char *tmp=(char*)xmlbuff;
202              fprintf(stderr,"%s\n",tmp);
203              xmlFree(xmlbuff);
204            }
205          }
206        }
207      }
208
209      xmlNodePtr cur = xmlDocGetRootElement(doc);
210      char *tval;
211      tval=NULL;
212      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
213      if(tval!=NULL)
214        addToMap(tmpMap,"service",tval);
215      tval=NULL;
216      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
217      if(tval!=NULL)
218        addToMap(tmpMap,"language",tval);
219      const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"};
220      for(int j=0;j<3;j++){
221        char tt[128];
222        sprintf(tt,"/*[local-name()='%s']",requests[j]);
223        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
224        if(reqptr!=NULL){
225          xmlNodeSet* req=reqptr->nodesetval;
226#ifdef DEBUG
227          fprintf(stderr,"%i",req->nodeNr);
228#endif
229          if(req!=NULL && req->nodeNr==1){
230            t1->value=strdup(requests[j]);
231            j=2;
232          }
233          xmlXPathFreeObject(reqptr);
234        }
235        //xmlFree(req);
236      }
237      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
238        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
239        xmlNodeSet* vers=versptr->nodesetval;
240        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
241        addToMap(tmpMap,"version",(char*)content);
242        xmlXPathFreeObject(versptr);
243        //xmlFree(vers);
244        xmlFree(content);
245      }else{
246        tval=NULL;
247        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
248        if(tval!=NULL)
249          addToMap(tmpMap,"version",tval);
250        xmlFree(tval);
251        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
252        if(tval!=NULL)
253          addToMap(tmpMap,"language",tval);
254        xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
255        if(idptr!=NULL){
256          xmlNodeSet* id=idptr->nodesetval;
257          if(id!=NULL){
258            char* identifiers=NULL;
259            identifiers=(char*)calloc(cgiContentLength,sizeof(char));
260            identifiers[0]=0;
261            for(int k=0;k<id->nodeNr;k++){
262              xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
263              if(strlen(identifiers)>0){
264                char *tmp=strdup(identifiers);
265                snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
266                free(tmp);
267              }
268              else{
269                snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
270              }
271              xmlFree(content);
272            }
273            xmlXPathFreeObject(idptr);
274            addToMap(tmpMap,"Identifier",identifiers);
275            free(identifiers);
276          }
277        }
278        //xmlFree(id);
279      }
280      xmlFree(tval);
281      xmlFreeDoc(doc);
282      xmlCleanupParser();
283    }
284  }
285
286  runRequest(tmpMap);
287
288  /**
289   * Required but can't be made after executing a process using POST requests.
290   */
291  if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){
292    freeMap(&tmpMap);
293    free(tmpMap);
294  }
295  return 0;
296
297}
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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