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

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

Add the ZOOMakefile.opts file used from service providers' Makefiles.

File size: 8.6 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       tmpMap=createMap("request",res);
101    }else{
102      char *buffer=new char[cgiContentLength+1];
103      if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)){
104        buffer[cgiContentLength]=0;
105        tmpMap=createMap("request",buffer);
106        dumpMap(tmpMap);
107      }else{
108        buffer[0]=0;
109        char **array, **arrayStep;
110        if (cgiFormEntries(&array) != cgiFormSuccess) {
111          return 1;
112        }
113        arrayStep = array;
114        while (*arrayStep) {
115          char *ivalue=new char[cgiContentLength];
116          cgiFormStringNoNewlines(*arrayStep, ivalue, cgiContentLength);
117          char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+1)*sizeof(char));
118          sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
119          if(strlen(buffer)==0){
120            sprintf(buffer,"%s",tmpValueFinal);
121          }else{
122            char *tmp=strdup(buffer);
123            sprintf(buffer,"%s&%s",tmp,tmpValueFinal);
124            free(tmp);
125          }
126         
127          sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
128          free(tmpValueFinal);
129#ifdef DEBUG
130          fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue);
131#endif
132          delete[]ivalue;
133          arrayStep++;
134        }
135        tmpMap=createMap("request",buffer);
136      }
137      delete[]buffer;
138    }
139  }
140  else{
141    char **array, **arrayStep;
142    if (cgiFormEntries(&array) != cgiFormSuccess) {
143      return 1;
144    }
145    arrayStep = array;
146    while (*arrayStep) {
147      char *value=new char[cgiContentLength];
148      cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
149#ifdef DEBUG
150      fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
151#endif
152      if(tmpMap!=NULL)
153        addToMap(tmpMap,*arrayStep,value);
154      else
155        tmpMap=createMap(*arrayStep,value);
156      arrayStep++;
157      delete[]value;
158    }
159    cgiStringArrayFree(array);
160  }
161
162  /**
163   * In case that the POST method was used, then check if params came in XML
164   * format else try to use the attribute "request" which should be the only
165   * one.
166   */
167  if(strncasecmp(cgiRequestMethod,"post",4)==0 || 
168     (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){
169    /**
170     * First include the MetaPath and the ServiceProvider default parameters
171     * (which should be always available in GET params so in cgiQueryString)
172     */
173    char *str1;
174    str1=cgiQueryString;
175    /**
176     * Store the original XML request in xrequest map
177     */
178    map* t1=getMap(tmpMap,"request");
179    if(t1!=NULL){
180      addToMap(tmpMap,"xrequest",t1->value);
181      xmlInitParser();
182      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
183
184
185      {
186        xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*");
187        if(reqptr!=NULL){
188          xmlNodeSet* req=reqptr->nodesetval;
189          if(req!=NULL && req->nodeNr==1){
190            addToMap(tmpMap,"soap","true");
191            int k=0;
192            for(k;k < req->nodeNr;k++){
193              xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi");
194              xmlDocSetRootElement(doc, req->nodeTab[k]);
195              xmlChar *xmlbuff;
196              int buffersize;
197              xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1);
198              addToMap(tmpMap,"xrequest",(char*)xmlbuff);
199              char *tmp=(char*)xmlbuff;
200              fprintf(stderr,"%s\n",tmp);
201              xmlFree(xmlbuff);
202            }
203          }
204        }
205      }
206
207      xmlNodePtr cur = xmlDocGetRootElement(doc);
208      char *tval;
209      tval=NULL;
210      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
211      if(tval!=NULL)
212        addToMap(tmpMap,"service",tval);
213      tval=NULL;
214      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
215      if(tval!=NULL)
216        addToMap(tmpMap,"language",tval);
217      const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"};
218      for(int j=0;j<3;j++){
219        char tt[128];
220        sprintf(tt,"/*[local-name()='%s']",requests[j]);
221        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
222        if(reqptr!=NULL){
223          xmlNodeSet* req=reqptr->nodesetval;
224#ifdef DEBUG
225          fprintf(stderr,"%i",req->nodeNr);
226#endif
227          if(req!=NULL && req->nodeNr==1){
228            t1->value=strdup(requests[j]);
229            j=2;
230          }
231          xmlXPathFreeObject(reqptr);
232        }
233        //xmlFree(req);
234      }
235      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
236        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
237        xmlNodeSet* vers=versptr->nodesetval;
238        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
239        addToMap(tmpMap,"version",(char*)content);
240        xmlXPathFreeObject(versptr);
241        //xmlFree(vers);
242        xmlFree(content);
243      }else{
244        tval=NULL;
245        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
246        if(tval!=NULL)
247          addToMap(tmpMap,"version",tval);
248        xmlFree(tval);
249        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
250        if(tval!=NULL)
251          addToMap(tmpMap,"language",tval);
252        xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
253        if(idptr!=NULL){
254          xmlNodeSet* id=idptr->nodesetval;
255          if(id!=NULL){
256            char* identifiers=NULL;
257            identifiers=(char*)calloc(cgiContentLength,sizeof(char));
258            identifiers[0]=0;
259            for(int k=0;k<id->nodeNr;k++){
260              xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
261              if(strlen(identifiers)>0){
262                char *tmp=strdup(identifiers);
263                snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
264                free(tmp);
265              }
266              else{
267                snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
268              }
269              xmlFree(content);
270            }
271            xmlXPathFreeObject(idptr);
272            addToMap(tmpMap,"Identifier",identifiers);
273            free(identifiers);
274          }
275        }
276        //xmlFree(id);
277      }
278      xmlFree(tval);
279      xmlFreeDoc(doc);
280      xmlCleanupParser();
281    }
282  }
283
284  runRequest(tmpMap);
285
286  /**
287   * Required but can't be made after executing a process using POST requests.
288   */
289  if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){
290    freeMap(&tmpMap);
291    free(tmpMap);
292  }
293  return 0;
294
295}
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