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

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

Update to support multiple outputs provided in a XML POST request.

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