source: trunk/zoo-kernel/zoo_loader.c @ 280

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

Base of SOAP Envelope support. Primitive cache system. Solving bugs #45, #46, #47, #48, #50

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