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

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

Remove the odd space by plus caracter replacement in input XML file.

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