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

Last change on this file since 9 was 9, checked in by djay, 14 years ago

Update of both ZOO Kernel and ZOO Services (ogr base-vect-ops ServicesProvider?).
All the ZCFG files have been corrected to remove all references to wrong metadata (Test = Demo) to avoid validation issues.
Main Memory leaks has been removed from this version.
Addition of the Simplify Service in the C ogr base-vect-ops ServicesProvider? and addition of the Python version (without Simplify).
Update of the configure.ac and Makefile.in to follow dicussions on the mailing list and ensure to use our cgic206 and not another one, path to our cgic library is now directly in the Makefile.in file.
Accept the "-" character to name inputs, to solve issue on GRASS 7 integration.
Addition of the extension keyword for ZCFG file to be able to store resulting outputs in a file name using the extension suffix.
This version after a testing period shall be considerate as 1.0.1 version of the ZOO Project.

File size: 7.5 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 *  Copyright 2008-2009 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#include "fcgio.h"
32#include "fcgi_config.h"
33#include "fcgi_stdio.h"
34#include <sys/types.h>
35#include <unistd.h>
36#include "service_internal.h"
37
38extern "C" {
39#include "cgic.h"
40#include <libxml/tree.h>
41#include <libxml/xmlmemory.h>
42#include <libxml/parser.h>
43#include <libxml/xpath.h>
44#include <libxml/xpathInternals.h>
45}
46
47xmlXPathObjectPtr extractFromDoc(xmlDocPtr,char*);
48int runRequest(map*);
49
50using namespace std;
51
52/* ************************************************************************* */
53
54int errorException(maps *m, const char *message, const char *errorcode) 
55{
56  map * errormap = createMap("text", message);
57  addToMap(errormap,"code", errorcode);
58  printExceptionReportResponse(m,errormap);
59  freeMap(&errormap);
60  free(errormap);
61  return -1;
62}
63
64/* ************************************************************************* */
65
66#ifndef STRTOK_R
67char *
68strtok_r (char *s1, const char *s2, char **lasts)
69{
70  char *ret;
71
72  if (s1 == NULL)
73    s1 = *lasts;
74  while (*s1 && strchr(s2, *s1))
75    ++s1;
76  if (*s1 == '\0')
77    return NULL;
78  ret = s1;
79  while (*s1 && !strchr(s2, *s1))
80    ++s1;
81  if (*s1)
82    *s1++ = '\0';
83  *lasts = s1;
84  return ret;
85}
86
87#endif
88
89#define TRUE 1
90#define FALSE -1
91
92int cgiMain(){
93  /**
94   * We'll use cgiOut as the default output (stdout) to produce plain text
95   * response.
96   */
97  dup2(fileno(cgiOut),fileno(stdout));
98#ifdef DEBUG
99  fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
100  fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
101  fflush(cgiOut);
102#endif
103 
104  /**
105   * Read the main configuration file
106  if(!isStarted){
107    maps* m;
108    m=(maps*)malloc(sizeof(maps*)); 
109    conf_read("main.cfg",m);
110#ifdef REAL_FCGI
111#ifdef DEBUG
112    printf("ok passed");
113#endif   
114#endif   
115    isStarted=TRUE;
116  }
117   */
118
119#ifdef DEBUG
120  fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); 
121  fprintf (stderr, "RequestMethod:%s\n", cgiRequestMethod); 
122  fprintf (stderr, "Request: %s\n", cgiQueryString);
123#endif
124
125  map* tmpMap=NULL;
126   
127  if(strncmp(cgiContentType,"text/xml",8)==0){
128    char *buffer=new char[cgiContentLength+1];
129    if(fread(buffer,1,cgiContentLength,cgiIn)){
130      buffer[cgiContentLength]=0;
131      tmpMap=createMap("request",buffer);
132    }else{
133      /* Here we have to return an error message ... */
134      fprintf(stderr, "Unable to read cgi content in zoo_loader.c line %i\n", __LINE__);     
135      return 1; 
136    }
137    delete[]buffer;
138  }
139  else{
140    char **array, **arrayStep;
141    if (cgiFormEntries(&array) != cgiFormSuccess) {
142      return 1;
143    }
144    arrayStep = array;
145    while (*arrayStep) {
146      char *value=new char[cgiContentLength];
147      cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
148#ifdef DEBUG
149      fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
150#endif
151      if(tmpMap!=NULL)
152        addToMap(tmpMap,*arrayStep,value);
153      else
154        tmpMap=createMap(*arrayStep,value);
155      arrayStep++;
156      delete[]value;
157    }
158    cgiStringArrayFree(array);
159  }
160
161  /**
162   * In case that the POST method was used, then check if params came in XML
163   * format else try to use the attribute "request" which should be the only
164   * one.
165   */
166  if(strncasecmp(cgiRequestMethod,"post",4)==0 || count(tmpMap)==1){
167    /**
168     * First include the MetaPath and the ServiceProvider default parameters
169     * (which should be always available in GET params so in cgiQueryString)
170     */
171    //char *saveptr1, *saveptr2;
172    char *str1/*, str2, *token, *subtoken*/;
173    str1=cgiQueryString;
174    /**
175     * Store the original XML request in xrequest map
176     */
177    map* t1=getMap(tmpMap,"request");
178    if(t1!=NULL){
179      addToMap(tmpMap,"xrequest",t1->value);
180      xmlInitParser();
181      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
182      xmlNodePtr cur = xmlDocGetRootElement(doc);
183      char *tval;
184      tval=NULL;
185      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
186      if(tval!=NULL)
187        addToMap(tmpMap,"service",tval);
188      tval=NULL;
189      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
190      if(tval!=NULL)
191        addToMap(tmpMap,"language",tval);
192     
193      char* requests[3];
194      requests[0]="GetCapabilities";
195      requests[1]="DescribeProcess";
196      requests[2]="Execute";
197      for(int j=0;j<3;j++){
198        char tt[35];
199        sprintf(tt,"/*[local-name()='%s']",requests[j]);
200        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
201        if(reqptr!=NULL){
202          xmlNodeSet* req=reqptr->nodesetval;
203#ifdef DEBUG
204          fprintf(stderr,"%i",req->nodeNr);
205#endif
206          if(req!=NULL && req->nodeNr==1){
207            t1->value=requests[j];
208            j=2;
209          }
210          xmlXPathFreeObject(reqptr);
211        }
212        //xmlFree(req);
213      }
214      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
215        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
216        xmlNodeSet* vers=versptr->nodesetval;
217        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
218        addToMap(tmpMap,"version",(char*)content);
219        xmlXPathFreeObject(versptr);
220        //xmlFree(vers);
221        xmlFree(content);
222      }else{
223        tval=NULL;
224        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
225        if(tval!=NULL)
226          addToMap(tmpMap,"version",tval);
227        xmlFree(tval);
228        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
229        if(tval!=NULL)
230          addToMap(tmpMap,"language",tval);
231        xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
232        if(idptr!=NULL){
233          xmlNodeSet* id=idptr->nodesetval;
234          if(id!=NULL){
235            char* identifiers=NULL;
236            identifiers=(char*)calloc(cgiContentLength,sizeof(char));
237            identifiers[0]=0;
238            for(int k=0;k<id->nodeNr;k++){
239              xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
240              if(strlen(identifiers)>0){
241                char *tmp=strdup(identifiers);
242                snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
243                free(tmp);
244              }
245              else{
246                snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
247              }
248              xmlFree(content);
249            }
250            xmlXPathFreeObject(idptr);
251            addToMap(tmpMap,"Identifier",identifiers);
252            free(identifiers);
253          }
254        }
255        //xmlFree(id);
256      }
257      xmlFree(tval);
258      xmlFreeDoc(doc);
259      xmlCleanupParser();
260    }
261  }
262
263  runRequest(tmpMap);
264
265  /**
266   * Required but can't be made after executing a process using POST requests.
267   */
268  if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){
269    freeMap(&tmpMap);
270    free(tmpMap);
271  }
272  return 0;
273
274}
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