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

Last change on this file since 488 was 465, checked in by djay, 11 years ago

Add the optional YAML ZCFG support #4 and the zcfg2yaml converter. Return error messages that enable the service provider to quickly identify the root cause of errors due to configuration file syntax #90. Fix logic in addMapToMap #91. Enable multiple range definition using default and supported blocks. Add the lastest revision number in version.h (available from Python ZOO-API as zoo.VERSION).

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 10.2 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/**
29 * Specific includes
30 */
31#ifndef WIN32
32#include "fcgio.h"
33#include "fcgi_config.h"
34#include "fcgi_stdio.h"
35#endif
36#include <sys/types.h>
37#include <unistd.h>
38#include "service_internal.h"
39#ifdef WIN32
40#include "windows.h"
41#define strtok_r strtok_s
42#endif
43
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
53#include "service_internal.h"
54
55xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*);
56int runRequest(map*);
57
58using namespace std;
59
60#ifndef TRUE
61#define TRUE 1
62#endif
63#ifndef FALSE
64#define FALSE -1
65#endif
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
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); 
78  fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); 
79  fprintf (stderr, "Request: %s\n", cgiQueryString);
80  fprintf (stderr, "ContentType: %s\n", cgiContentType);
81  fprintf (stderr, "ContentLength: %d\n", cgiContentLength);
82  fflush(stderr);
83#endif
84
85 
86  char *strQuery=NULL;
87  if(cgiQueryString!=NULL)
88    strQuery=zStrdup(cgiQueryString);
89  map* tmpMap=NULL;
90
91  if(strncmp(cgiContentType,"text/xml",8)==0 || 
92     strncasecmp(cgiRequestMethod,"post",4)==0){
93    if(cgiContentLength==NULL){
94       cgiContentLength=0;
95       char *buffer=new char[2];
96       char *res=NULL;
97       int r=0;
98       while((r=fread(buffer,sizeof(char),1,cgiIn))){
99         cgiContentLength+=r;
100         if(res==NULL){
101           res=(char*)malloc(1*sizeof(char));
102           sprintf(res,"%s",buffer);
103         }
104         else{
105           res=(char*)realloc(res,(cgiContentLength+1)*sizeof(char));
106           char *tmp=zStrdup(res);
107           sprintf(res,"%s%s",tmp,buffer);
108           free(tmp);
109         }
110       }
111       if(res==NULL && (strQuery==NULL || strlen(strQuery)==0)){
112         return errorException(NULL,"ZOO-Kernel failed to process your request cause the request was emtpty.","InternalError",NULL);
113       }else{
114         if(strQuery==NULL || strlen(strQuery)==0)
115           tmpMap=createMap("request",res);
116            }
117    }else{
118      char *buffer=new char[cgiContentLength+1];
119      if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)>=0){
120        buffer[cgiContentLength]=0;
121        tmpMap=createMap("request",buffer);
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=zStrdup(buffer);
138            sprintf(buffer,"%s&%s",tmp,tmpValueFinal);
139            free(tmp);
140          }
141          free(tmpValueFinal);
142#ifdef DEBUG
143          fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue);
144#endif
145          delete[]ivalue;
146          arrayStep++;
147        }
148        tmpMap=createMap("request",buffer);
149      }
150      delete[]buffer;
151    }
152  }
153  else{
154#ifdef DEBUG
155    dumpMap(tmpMap);
156#endif
157    char **array, **arrayStep;
158    if (cgiFormEntries(&array) != cgiFormSuccess) {
159      return 1;
160    }
161    arrayStep = array;
162    while (*arrayStep) {
163      char *value=new char[cgiContentLength];
164      cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
165#ifdef DEBUG
166      fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
167#endif
168      if(tmpMap!=NULL)
169        addToMap(tmpMap,*arrayStep,value);
170      else
171        tmpMap=createMap(*arrayStep,value);
172      arrayStep++;
173      delete[]value;
174    }
175    cgiStringArrayFree(array);
176  }
177
178#ifdef WIN32
179  map *tmpReq=getMap(tmpMap,"rfile");
180  if(tmpReq!=NULL){
181    FILE *lf=fopen(tmpReq->value,"r");
182    fseek(lf,0,SEEK_END);
183    long flen=ftell(lf);
184    fseek(lf,0,SEEK_SET);
185    char *buffer=(char*)malloc((flen+1)*sizeof(char));
186    fread(buffer,flen,1,lf);
187    fclose(lf);
188    addToMap(tmpMap,"request",buffer);
189    free(buffer);
190    cgiContentLength=flen+9;
191  }
192#endif
193  /**
194   * In case that the POST method was used, then check if params came in XML
195   * format else try to use the attribute "request" which should be the only
196   * one.
197   */
198  if(strncasecmp(cgiRequestMethod,"post",4)==0 || 
199     (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0) 
200#ifdef WIN32
201     ||tmpReq!=NULL
202#endif
203     ){
204    /**
205     * First include the MetaPath and the ServiceProvider default parameters
206     * (which should be always available in GET params so in cgiQueryString)
207     */
208    char *str1;
209    str1=cgiQueryString;
210    /**
211     * Store the original XML request in xrequest map
212     */
213    map* t1=getMap(tmpMap,"request");
214    if(t1!=NULL && strncasecmp(t1->value,"<",1)==0){
215      addToMap(tmpMap,"xrequest",t1->value);
216      xmlInitParser();
217      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
218      {
219        xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*");
220        if(reqptr!=NULL){
221          xmlNodeSet* req=reqptr->nodesetval;
222          if(req!=NULL && req->nodeNr==1){
223            addToMap(tmpMap,"soap","true");
224            for(int k=0;k < req->nodeNr;k++){
225              //xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi");
226              xmlDocSetRootElement(doc, req->nodeTab[k]);
227              xmlChar *xmlbuff;
228              int buffersize;
229              xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1);
230              addToMap(tmpMap,"xrequest",(char*)xmlbuff);
231              xmlFree(xmlbuff);
232            }
233          }
234        }
235      }
236
237      xmlNodePtr cur = xmlDocGetRootElement(doc);
238      char *tval;
239      tval=NULL;
240      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
241      if(tval!=NULL)
242        addToMap(tmpMap,"service",tval);
243      tval=NULL;
244      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
245      if(tval!=NULL)
246        addToMap(tmpMap,"language",tval);
247      const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"};
248      for(int j=0;j<3;j++){
249        char tt[128];
250        sprintf(tt,"/*[local-name()='%s']",requests[j]);
251        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
252        if(reqptr!=NULL){
253          xmlNodeSet* req=reqptr->nodesetval;
254#ifdef DEBUG
255          fprintf(stderr,"%i",req->nodeNr);
256#endif
257          if(req!=NULL && req->nodeNr==1){
258            t1->value=zStrdup(requests[j]);
259            j=2;
260          }
261          xmlXPathFreeObject(reqptr);
262        }
263      }
264      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
265        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
266        xmlNodeSet* vers=versptr->nodesetval;
267        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
268        addToMap(tmpMap,"version",(char*)content);
269        xmlXPathFreeObject(versptr);
270        xmlFree(content);
271      }else{
272        tval=NULL;
273        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
274        if(tval!=NULL)
275          addToMap(tmpMap,"version",tval);
276        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
277        if(tval!=NULL)
278          addToMap(tmpMap,"language",tval);
279        xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
280        if(idptr!=NULL){
281          xmlNodeSet* id=idptr->nodesetval;
282          if(id!=NULL){
283            char* identifiers=NULL;
284            identifiers=(char*)calloc(cgiContentLength,sizeof(char));
285            identifiers[0]=0;
286            for(int k=0;k<id->nodeNr;k++){
287              xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
288              if(strlen(identifiers)>0){
289                char *tmp=zStrdup(identifiers);
290                snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
291                free(tmp);
292              }
293              else{
294                snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
295              }
296              xmlFree(content);
297            }
298            xmlXPathFreeObject(idptr);
299            addToMap(tmpMap,"Identifier",identifiers);
300            free(identifiers);
301          }
302        }
303      }
304      xmlFreeDoc(doc);
305      xmlCleanupParser();
306    }else{
307      freeMap(&tmpMap);
308      free(tmpMap);
309      tmpMap=createMap("not_valid","true");
310    }
311
312    char *token,*saveptr;
313    token=strtok_r(cgiQueryString,"&",&saveptr);
314    while(token!=NULL){
315      char *token1,*saveptr1;
316      char *name=NULL;
317      char *value=NULL;
318      token1=strtok_r(token,"=",&saveptr1);
319      while(token1!=NULL){
320        if(name==NULL)
321          name=zStrdup(token1);
322        else
323          value=zStrdup(token1);
324        token1=strtok_r(NULL,"=",&saveptr1);
325      }
326      addToMap(tmpMap,name,value);
327      free(name);
328      free(value);
329      name=NULL;
330      value=NULL;
331      token=strtok_r(NULL,"&",&saveptr);
332    }
333   
334  }
335
336  if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){
337    map* tmp=getMap(tmpMap,"dataInputs");
338    if(tmp!=NULL){
339      addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11);
340    }
341  }
342
343  if(strQuery!=NULL)
344    free(strQuery);
345  runRequest(tmpMap);
346
347  /**
348   * Required but can't be made after executing a process using POST requests.
349   */
350  if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){
351    freeMap(&tmpMap);
352    free(tmpMap);
353  }
354  return 0;
355
356}
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