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

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

Code cleanup to avoid most of the warning messages at compilation time.

File size: 7.7 KB
RevLine 
[1]1/**
2 * Author : Gérald FENOY
3 *
[69]4 *  Copyright 2008-2011 GeoLabs SARL. All rights reserved.
[1]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
[9]25#define MALLOC_CHECK_ 0
26#define MALLOC_CHECK 0
27
[1]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>
[9]36#include "service_internal.h"
37
[1]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
[114]47xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*);
[9]48int runRequest(map*);
49
50using namespace std;
51
52/* ************************************************************************* */
53
54int errorException(maps *m, const char *message, const char *errorcode) 
55{
[32]56  map* errormap = createMap("text", message);
[9]57  addToMap(errormap,"code", errorcode);
58  printExceptionReportResponse(m,errormap);
59  freeMap(&errormap);
60  free(errormap);
61  return -1;
62}
63
64/* ************************************************************************* */
65
[1]66#ifndef STRTOK_R
67char *
[9]68strtok_r (char *s1, const char *s2, char **lasts)
[1]69{
70  char *ret;
71
72  if (s1 == NULL)
73    s1 = *lasts;
[9]74  while (*s1 && strchr(s2, *s1))
[1]75    ++s1;
[9]76  if (*s1 == '\0')
[1]77    return NULL;
78  ret = s1;
[9]79  while (*s1 && !strchr(s2, *s1))
[1]80    ++s1;
[9]81  if (*s1)
[1]82    *s1++ = '\0';
83  *lasts = s1;
84  return ret;
85}
86
87#endif
88
[9]89#define TRUE 1
[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
[9]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);
[1]102#endif
103 
104#ifdef DEBUG
[9]105  fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); 
106  fprintf (stderr, "RequestMethod:%s\n", cgiRequestMethod); 
107  fprintf (stderr, "Request: %s\n", cgiQueryString);
[1]108#endif
109
[9]110  map* tmpMap=NULL;
[69]111
[99]112  if(strncmp(cgiContentType,"text/xml",8)==0 || 
113     strncasecmp(cgiRequestMethod,"post",4)==0){
[9]114    char *buffer=new char[cgiContentLength+1];
[1]115    if(fread(buffer,1,cgiContentLength,cgiIn)){
116      buffer[cgiContentLength]=0;
[9]117      tmpMap=createMap("request",buffer);
[1]118    }else{
[99]119      char **array, **arrayStep;
120      if (cgiFormEntries(&array) != cgiFormSuccess) {
121        return 1;
122      }
123      arrayStep = array;
124      while (*arrayStep) {
125        char *value=new char[cgiContentLength];
126        cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
127        char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(value)+1)*sizeof(char));
128        sprintf(tmpValueFinal,"%s=%s",*arrayStep,value);
129        tmpMap=createMap("request",tmpValueFinal);
130        free(tmpValueFinal);
131#ifdef DEBUG
132        fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
133#endif
134        delete[]value;
135        arrayStep++;
136      }
[1]137    }
[9]138    delete[]buffer;
[1]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
[9]152      if(tmpMap!=NULL)
153        addToMap(tmpMap,*arrayStep,value);
[1]154      else
[9]155        tmpMap=createMap(*arrayStep,value);
[1]156      arrayStep++;
[9]157      delete[]value;
[1]158    }
159    cgiStringArrayFree(array);
160  }
161
162  /**
163   * In case that the POST method was used, then check if params came in XML
[9]164   * format else try to use the attribute "request" which should be the only
165   * one.
[1]166   */
[10]167  if(strncasecmp(cgiRequestMethod,"post",4)==0 || 
168     (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){
[1]169    /**
170     * First include the MetaPath and the ServiceProvider default parameters
171     * (which should be always available in GET params so in cgiQueryString)
172     */
[19]173    char *str1;
[1]174    str1=cgiQueryString;
175    /**
176     * Store the original XML request in xrequest map
177     */
[9]178    map* t1=getMap(tmpMap,"request");
[1]179    if(t1!=NULL){
[9]180      addToMap(tmpMap,"xrequest",t1->value);
[1]181      xmlInitParser();
[9]182      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
[1]183      xmlNodePtr cur = xmlDocGetRootElement(doc);
184      char *tval;
185      tval=NULL;
186      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
187      if(tval!=NULL)
[9]188        addToMap(tmpMap,"service",tval);
[1]189      tval=NULL;
190      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
191      if(tval!=NULL)
[9]192        addToMap(tmpMap,"language",tval);
[1]193     
[114]194      const char* requests[3];
[1]195      requests[0]="GetCapabilities";
196      requests[1]="DescribeProcess";
197      requests[2]="Execute";
198      for(int j=0;j<3;j++){
199        char tt[35];
200        sprintf(tt,"/*[local-name()='%s']",requests[j]);
[9]201        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
202        if(reqptr!=NULL){
203          xmlNodeSet* req=reqptr->nodesetval;
[1]204#ifdef DEBUG
[9]205          fprintf(stderr,"%i",req->nodeNr);
[1]206#endif
[9]207          if(req!=NULL && req->nodeNr==1){
[114]208            t1->value=strdup(requests[j]);
[9]209            j=2;
210          }
211          xmlXPathFreeObject(reqptr);
[1]212        }
[9]213        //xmlFree(req);
[1]214      }
[9]215      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
216        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
217        xmlNodeSet* vers=versptr->nodesetval;
[1]218        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
[9]219        addToMap(tmpMap,"version",(char*)content);
220        xmlXPathFreeObject(versptr);
221        //xmlFree(vers);
[1]222        xmlFree(content);
223      }else{
224        tval=NULL;
225        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
226        if(tval!=NULL)
[9]227          addToMap(tmpMap,"version",tval);
[1]228        xmlFree(tval);
229        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
230        if(tval!=NULL)
[9]231          addToMap(tmpMap,"language",tval);
232        xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
233        if(idptr!=NULL){
234          xmlNodeSet* id=idptr->nodesetval;
235          if(id!=NULL){
236            char* identifiers=NULL;
237            identifiers=(char*)calloc(cgiContentLength,sizeof(char));
238            identifiers[0]=0;
239            for(int k=0;k<id->nodeNr;k++){
240              xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
241              if(strlen(identifiers)>0){
242                char *tmp=strdup(identifiers);
243                snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
244                free(tmp);
245              }
246              else{
247                snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
248              }
249              xmlFree(content);
250            }
251            xmlXPathFreeObject(idptr);
252            addToMap(tmpMap,"Identifier",identifiers);
253            free(identifiers);
[1]254          }
255        }
[9]256        //xmlFree(id);
[1]257      }
258      xmlFree(tval);
[9]259      xmlFreeDoc(doc);
260      xmlCleanupParser();
[1]261    }
262  }
263
[9]264  runRequest(tmpMap);
265
266  /**
267   * Required but can't be made after executing a process using POST requests.
268   */
269  if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){
270    freeMap(&tmpMap);
271    free(tmpMap);
272  }
[1]273  return 0;
274
275}
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