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

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

Initial ZOO SVN Repository Import.

File size: 7.9 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/**
26 * Specific includes
27 */
28#include "fcgio.h"
29#include "fcgi_config.h"
30#include "fcgi_stdio.h"
31#include <sys/types.h>
32#include <unistd.h>
33extern "C" {
34#include "cgic.h"
35}
36
37#include "service_internal.h"
38extern "C" {
39#include <libxml/tree.h>
40#include <libxml/xmlmemory.h>
41#include <libxml/parser.h>
42#include <libxml/xpath.h>
43#include <libxml/xpathInternals.h>
44}
45
46#ifndef STRTOK_R
47char *
48strtok_r(char *s1, const char *s2, char **lasts)
49{
50  char *ret;
51
52  if (s1 == NULL)
53    s1 = *lasts;
54  while(*s1 && strchr(s2, *s1))
55    ++s1;
56  if(*s1 == '\0')
57    return NULL;
58  ret = s1;
59  while(*s1 && !strchr(s2, *s1))
60    ++s1;
61  if(*s1)
62    *s1++ = '\0';
63  *lasts = s1;
64  return ret;
65}
66
67#endif
68
69xmlNodeSet* extractFromDoc(xmlDocPtr,char*);
70int runRequest(map*);
71
72using namespace std;
73
74char* strtoupper(char* str)
75{
76  int leng=strlen(str);
77  for(int i=0; i<leng; i++)
78    if (97<=str[i]&&str[i]<=122)//a-z
79      str[i]-=32;
80  return str;
81}
82
83char* strtolower(char* str)
84{
85  int leng=strlen(str);
86  for(int i=0; i<leng; i++)
87    if (65<=str[i]&&str[i]<=90)//A-Z
88      str[i]+=32;
89  return str;
90}
91
92char* remplace(char* delim,char* rep,char* source){
93  if(strcmp(source,"")==0){
94#ifdef DEBUG
95    //char *tmp="RETURN NULL !\n";
96    //printf((void*)tmp);
97#endif
98    return "NULL";
99  }
100
101  char *_token; 
102  char *origin=strdup(source);
103  char result[1024]="";
104  _token = strtok(source, delim);
105#ifdef DEBUG
106  fprintf(stderr,"\nREPLACE TOKEN (%s == %s => %d)\n ",_token,origin,strcmp(_token,origin));
107#endif
108  if(strcmp(_token,origin)!=0)
109    while(_token!=NULL){
110      sprintf(result,"%s%s%s",result,_token,rep);
111      _token = strtok (NULL, delim);
112      if(_token==NULL)
113        result[strlen(result)-strlen(rep)]=0;
114#ifdef DEBUG
115      fprintf(stderr,"\n\nRESULT(%s)\n\n",result);
116#endif
117    }
118  else
119    return origin;
120 
121  return strdup(result);
122}
123
124
125char *colors[] = {
126  "red", "green", "blue"
127};
128
129#define colorsTotal 3
130#define TRUE -1
131#define FALSE -1
132static bool started=FALSE;
133static bool isStarted=FALSE;
134
135int cgiMain(){
136  /**
137   * We'll use cgiOut as the default output (stdout) to produce plain text
138   * response.
139   */
140  dup2(fileno(cgiOut),fileno(stdout));
141#ifdef DEBUG
142    fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
143    fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
144    fflush(cgiOut);
145#endif
146 
147  /**
148   * Read the main configuration file
149   */
150  if(!isStarted){
151    maps* m;
152    m=(maps*)malloc(sizeof(maps*)); 
153    conf_read("main.cfg",m);
154#ifdef REAL_FCGI
155#ifdef DEBUG
156    printf("ok passed");
157#endif   
158#endif   
159    isStarted=TRUE;
160  }
161
162#ifdef DEBUG
163  char textb[23];
164  sprintf(textb,"\n%s\n%s\n",cgiRemoteAddr,cgiRequestMethod);
165  fprintf(stderr,"****%s\n%s\n***",textb,cgiQueryString);
166#endif
167
168  map* tmp=NULL;
169   
170  if(strncmp(cgiContentType,"text/xml",8)==0){
171    char *buffer=new char[cgiContentLength];
172    if(fread(buffer,1,cgiContentLength,cgiIn)){
173      buffer[cgiContentLength]=0;
174      tmp=createMap("request",buffer);
175    }else{
176      /* Here we have to return an error message ... */
177      return 1; 
178    }
179  }
180  else{
181    char **array, **arrayStep;
182    if (cgiFormEntries(&array) != cgiFormSuccess) {
183      return 1;
184    }
185    arrayStep = array;
186    while (*arrayStep) {
187      char *value=new char[cgiContentLength];
188      cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
189#ifdef DEBUG
190      fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
191#endif
192      if(tmp!=NULL)
193        addToMap(tmp,*arrayStep,value);
194      else
195        tmp=createMap(*arrayStep,value);
196      arrayStep++;
197    }
198    cgiStringArrayFree(array);
199  }
200
201  /**
202   * In case that the POST method was used, then check if params came in XML
203   * format (attribute request should be the once).
204   */
205  if(strcmp(cgiRequestMethod,mtoupper("post"))==0 && count(tmp)==1){
206    /**
207     * First include the MetaPath and the ServiceProvider default parameters
208     * (which should be always available in GET params so in cgiQueryString)
209     */
210    char *saveptr1, *saveptr2;
211    char *str1, *str2, *token, *subtoken;
212    str1=cgiQueryString;
213    token=strtok_r(str1,"&",&saveptr1);
214    while(token!=NULL){
215#ifdef DEBUG
216      fprintf(stderr,"%s",token);
217#endif
218      str2=token;
219      subtoken=strtok_r(str2,"=",&saveptr2);
220#ifdef DEBUG
221      fprintf(stderr,"%s\n",subtoken);
222#endif
223      char* tmp_name;
224      if(subtoken!=NULL){
225        tmp_name=subtoken;
226#ifdef DEBUG
227        fprintf(stderr,"%s",subtoken);
228#endif
229        subtoken=strtok_r(NULL,"=",&saveptr2);
230        if(subtoken!=NULL)
231          addToMap(tmp,tmp_name,subtoken);
232        else
233          addToMap(tmp,tmp_name,"");
234      }
235      token=strtok_r(NULL,"&",&saveptr1);
236    }
237    /**
238     * Store the original XML request in xrequest map
239     */
240    map* t1=getMap(tmp,"request");
241    if(t1!=NULL){
242      addToMap(tmp,"xrequest",t1->value);
243      xmlInitParser();
244      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);     
245      xmlNodePtr cur = xmlDocGetRootElement(doc);
246      char *tval;
247      tval=NULL;
248      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
249      if(tval!=NULL)
250        addToMap(tmp,"service",tval);
251      tval=NULL;
252      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
253      if(tval!=NULL)
254        addToMap(tmp,"language",tval);
255     
256      char* requests[3];
257      requests[0]="GetCapabilities";
258      requests[1]="DescribeProcess";
259      requests[2]="Execute";
260      for(int j=0;j<3;j++){
261        char tt[35];
262        sprintf(tt,"/*[local-name()='%s']",requests[j]);
263        xmlNodeSet* req=extractFromDoc(doc,tt);
264#ifdef DEBUG
265        fprintf(stderr,"%i",req->nodeNr);
266#endif
267        if(req->nodeNr==1){
268          t1->value=requests[j];
269          j=2;
270        }
271        xmlFree(req);
272      }
273      if(strcmp(mtoupper(t1->value),mtoupper("GetCapabilities"))==0){   
274        xmlNodeSet* vers=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
275        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
276        addToMap(tmp,"version",(char*)content);
277        xmlFree(vers);
278        xmlFree(content);
279      }else{
280        tval=NULL;
281        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
282        if(tval!=NULL)
283          addToMap(tmp,"version",tval);
284        xmlFree(tval);
285        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
286        if(tval!=NULL)
287          addToMap(tmp,"language",tval);
288        xmlNodeSet* id=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
289        char* identifiers=NULL;
290        identifiers=(char*)malloc(cgiContentLength);
291        identifiers[0]=0;
292        for(int k=0;k<id->nodeNr;k++){
293          xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
294          if(strlen(identifiers)>0){
295            sprintf(identifiers,"%s,%s",identifiers,content);
296            identifiers[strlen(identifiers)]=0;
297          }
298          else{
299            sprintf(identifiers,"%s",content);
300            identifiers[strlen(identifiers)]=0;
301          }
302          xmlFree(content);
303        }
304        xmlFree(id);
305        addToMap(tmp,"Identifier",identifiers);
306      }
307      //xmlCleanupParser();
308      xmlFree(tval);
309    }
310  }
311  //dumpMap(tmp);
312  runRequest(tmp);
313  //dumpMap(tmp);
314
315  return 0;
316
317}
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