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
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#ifndef WIN32
32#include "fcgio.h"
33#include "fcgi_config.h"
34#include "fcgi_stdio.h"
35#endif
36#include <sys/types.h>
37#include <unistd.h>
38#include "service_internal.h"
39#ifdef WIN32
40#include "windows.h"
41#define strtok_r strtok_s
42#endif
43
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
53#include "service_internal.h"
54
55xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*);
56int runRequest(map*);
57
58using namespace std;
59
60#ifndef TRUE
61#define TRUE 1
62#endif
63#ifndef FALSE
64#define FALSE -1
65#endif
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
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); 
78  fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); 
79  fprintf (stderr, "Request: %s\n", cgiQueryString);
80  fprintf (stderr, "ContentType: %s\n", cgiContentType);
81  fprintf (stderr, "ContentLength: %d\n", cgiContentLength);
82  fflush(stderr);
83#endif
84
85  char *strQuery=strdup(cgiQueryString);
86  map* tmpMap=NULL;
87
88  if(strncmp(cgiContentType,"text/xml",8)==0 || 
89     strncasecmp(cgiRequestMethod,"post",4)==0){
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       }
108       if(res==NULL && (strQuery==NULL || strlen(strQuery)==0)){
109         return errorException(NULL,"ZOO-Kernel failed to process your request cause the request was emtpty.","InternalError");
110       }else{
111         if(strQuery==NULL || strlen(strQuery)==0)
112           tmpMap=createMap("request",res);
113       }
114    }else{
115      char *buffer=new char[cgiContentLength+1];
116      int r=0;
117      if((r=fread(buffer,sizeof(char),cgiContentLength,cgiIn))!=0){
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);
140#ifdef DEBUG
141          fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue);
142#endif
143          delete[]ivalue;
144          arrayStep++;
145        }
146        tmpMap=createMap("request",buffer);
147      }
148      delete[]buffer;
149    }
150  }
151  else{
152#ifdef DEBUG
153    dumpMap(tmpMap);
154#endif
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
166      if(tmpMap!=NULL)
167        addToMap(tmpMap,*arrayStep,value);
168      else
169        tmpMap=createMap(*arrayStep,value);
170      arrayStep++;
171      delete[]value;
172    }
173    cgiStringArrayFree(array);
174  }
175
176  /**
177   * In case that the POST method was used, then check if params came in XML
178   * format else try to use the attribute "request" which should be the only
179   * one.
180   */
181  if(strncasecmp(cgiRequestMethod,"post",4)==0 || 
182     (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){
183    /**
184     * First include the MetaPath and the ServiceProvider default parameters
185     * (which should be always available in GET params so in cgiQueryString)
186     */
187    char *str1;
188    str1=cgiQueryString;
189    /**
190     * Store the original XML request in xrequest map
191     */
192    map* t1=getMap(tmpMap,"request");
193    if(t1!=NULL && strncasecmp(t1->value,"<",1)==0){
194      addToMap(tmpMap,"xrequest",t1->value);
195      xmlInitParser();
196      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
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
220      xmlNodePtr cur = xmlDocGetRootElement(doc);
221      char *tval;
222      tval=NULL;
223      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
224      if(tval!=NULL)
225        addToMap(tmpMap,"service",tval);
226      tval=NULL;
227      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
228      if(tval!=NULL)
229        addToMap(tmpMap,"language",tval);
230      const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"};
231      for(int j=0;j<3;j++){
232        char tt[128];
233        sprintf(tt,"/*[local-name()='%s']",requests[j]);
234        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
235        if(reqptr!=NULL){
236          xmlNodeSet* req=reqptr->nodesetval;
237#ifdef DEBUG
238          fprintf(stderr,"%i",req->nodeNr);
239#endif
240          if(req!=NULL && req->nodeNr==1){
241            t1->value=strdup(requests[j]);
242            j=2;
243          }
244          xmlXPathFreeObject(reqptr);
245        }
246        //xmlFree(req);
247      }
248      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
249        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
250        xmlNodeSet* vers=versptr->nodesetval;
251        xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
252        addToMap(tmpMap,"version",(char*)content);
253        xmlXPathFreeObject(versptr);
254        //xmlFree(vers);
255        xmlFree(content);
256      }else{
257        tval=NULL;
258        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
259        if(tval!=NULL)
260          addToMap(tmpMap,"version",tval);
261        xmlFree(tval);
262        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
263        if(tval!=NULL)
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);
287          }
288        }
289        //xmlFree(id);
290      }
291      xmlFree(tval);
292      xmlFreeDoc(doc);
293      xmlCleanupParser();
294    }else{
295      freeMap(&tmpMap);
296      free(tmpMap);
297      tmpMap=createMap("not_valid","true");
298    }
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      }
314      addToMap(tmpMap,name,value);
315      free(name);
316      free(value);
317      name=NULL;
318      value=NULL;
319      token=strtok_r(NULL,"&",&saveptr);
320    }
321   
322  }
323
324  if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){
325    map* tmp=getMap(tmpMap,"dataInputs");
326    if(tmp!=NULL){
327      addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11);
328    }
329  }
330
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  }
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