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

Last change on this file since 648 was 648, checked in by djay, 9 years ago

Remove uneeded debug messages

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 11.1 KB
RevLine 
[607]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"
[640]39#include "response_print.h"
[554]40
41
[364]42#ifdef WIN32
43#include "windows.h"
44#define strtok_r strtok_s
45#endif
[9]46
[1]47extern "C" {
48#include "cgic.h"
49#include <libxml/tree.h>
50#include <libxml/xmlmemory.h>
51#include <libxml/parser.h>
52#include <libxml/xpath.h>
53#include <libxml/xpathInternals.h>
54}
55
[280]56#include "service_internal.h"
[621]57#include "request_parser.h"
[280]58
[490]59int runRequest(map**);
[9]60
61using namespace std;
62
[364]63#ifndef TRUE
[9]64#define TRUE 1
[364]65#endif
66#ifndef FALSE
[1]67#define FALSE -1
[364]68#endif
[1]69
[607]70/**
71 * Main entry point for cgic.
72 * @return 0 on sucess.
73 */
[1]74int cgiMain(){
75  /**
76   * We'll use cgiOut as the default output (stdout) to produce plain text
77   * response.
78   */
79  dup2(fileno(cgiOut),fileno(stdout));
80#ifdef DEBUG
[9]81  fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
82  fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n");
83  fflush(cgiOut);
84  fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); 
[280]85  fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); 
[9]86  fprintf (stderr, "Request: %s\n", cgiQueryString);
[380]87  fprintf (stderr, "ContentType: %s\n", cgiContentType);
88  fprintf (stderr, "ContentLength: %d\n", cgiContentLength);
[376]89  fflush(stderr);
[1]90#endif
[381]91 
92  char *strQuery=NULL;
93  if(cgiQueryString!=NULL)
[453]94    strQuery=zStrdup(cgiQueryString);
[9]95  map* tmpMap=NULL;
[69]96
[99]97  if(strncmp(cgiContentType,"text/xml",8)==0 || 
98     strncasecmp(cgiRequestMethod,"post",4)==0){
[490]99    if(cgiContentLength==0){
[280]100       char *buffer=new char[2];
101       char *res=NULL;
102       int r=0;
[381]103       while((r=fread(buffer,sizeof(char),1,cgiIn))){
[490]104         buffer[1]=0;
[280]105         if(res==NULL){
[490]106           res=(char*)malloc(2*sizeof(char));
[280]107           sprintf(res,"%s",buffer);
108         }
109         else{
[510]110           res=(char*)realloc(res,(cgiContentLength+2)*sizeof(char));
111           memcpy(res + cgiContentLength, buffer, sizeof(char));
112           res[cgiContentLength+1]=0;
[280]113         }
[510]114         cgiContentLength+=r;
[280]115       }
[490]116       delete[] buffer;
[376]117       if(res==NULL && (strQuery==NULL || strlen(strQuery)==0)){
[605]118         return errorException(NULL,"ZOO-Kernel failed to process your request because the request was empty.","InternalError",NULL);
[376]119       }else{
120         if(strQuery==NULL || strlen(strQuery)==0)
121           tmpMap=createMap("request",res);
[490]122       }
123       if(res!=NULL)
124         free(res);
[1]125    }else{
[280]126      char *buffer=new char[cgiContentLength+1];
[490]127      if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)>0){
[280]128        buffer[cgiContentLength]=0;
129        tmpMap=createMap("request",buffer);
130      }else{
131        buffer[0]=0;
132        char **array, **arrayStep;
133        if (cgiFormEntries(&array) != cgiFormSuccess) {
134          return 1;
135        }
136        arrayStep = array;
137        while (*arrayStep) {
138          char *ivalue=new char[cgiContentLength];
139          cgiFormStringNoNewlines(*arrayStep, ivalue, cgiContentLength);
[601]140          char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+2)*sizeof(char));       
[280]141          sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue);
[601]142
143          if(strlen(buffer)==0){               
[280]144            sprintf(buffer,"%s",tmpValueFinal);
[601]145          }else{               
[453]146            char *tmp=zStrdup(buffer);
[280]147            sprintf(buffer,"%s&%s",tmp,tmpValueFinal);
148            free(tmp);
[601]149          }       
[280]150          free(tmpValueFinal);
[99]151#ifdef DEBUG
[280]152          fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue);
[99]153#endif
[280]154          delete[]ivalue;
155          arrayStep++;
[601]156        }       
[490]157        if(tmpMap!=NULL)
158          addToMap(tmpMap,"request",buffer);
159        else
160          tmpMap=createMap("request",buffer);
[99]161      }
[280]162      delete[]buffer;
[601]163    }   
[1]164  }
165  else{
[364]166#ifdef DEBUG
[331]167    dumpMap(tmpMap);
[364]168#endif
[1]169    char **array, **arrayStep;
170    if (cgiFormEntries(&array) != cgiFormSuccess) {
171      return 1;
172    }
173    arrayStep = array;
174    while (*arrayStep) {
175      char *value=new char[cgiContentLength];
176      cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength);
177#ifdef DEBUG
178      fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value);
179#endif
[587]180#ifdef WIN32
181      char* tmp = url_decode(value); 
182      if(tmpMap!=NULL)         
183        addToMap(tmpMap,*arrayStep,tmp);
184      else             
185        tmpMap=createMap(*arrayStep,tmp);
186      free(tmp);
187#else
[9]188      if(tmpMap!=NULL)
[587]189        addToMap(tmpMap,*arrayStep,value);
[1]190      else
[587]191        tmpMap=createMap(*arrayStep,value);
192#endif 
[1]193      arrayStep++;
[9]194      delete[]value;
[1]195    }
196    cgiStringArrayFree(array);
197  }
198
[458]199#ifdef WIN32
200  map *tmpReq=getMap(tmpMap,"rfile");
201  if(tmpReq!=NULL){
202    FILE *lf=fopen(tmpReq->value,"r");
203    fseek(lf,0,SEEK_END);
204    long flen=ftell(lf);
205    fseek(lf,0,SEEK_SET);
206    char *buffer=(char*)malloc((flen+1)*sizeof(char));
207    fread(buffer,flen,1,lf);
208    fclose(lf);
209    addToMap(tmpMap,"request",buffer);
210    free(buffer);
211    cgiContentLength=flen+9;
212  }
213#endif
[1]214  /**
215   * In case that the POST method was used, then check if params came in XML
[9]216   * format else try to use the attribute "request" which should be the only
217   * one.
[1]218   */
[10]219  if(strncasecmp(cgiRequestMethod,"post",4)==0 || 
[458]220     (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0) 
221#ifdef WIN32
222     ||tmpReq!=NULL
223#endif
224     ){
[1]225    /**
226     * Store the original XML request in xrequest map
227     */
[9]228    map* t1=getMap(tmpMap,"request");
[621]229    if(t1!=NULL && strncasecmp(t1->value,"<",1)==0) {
[9]230      addToMap(tmpMap,"xrequest",t1->value);
[1]231      xmlInitParser();
[9]232      xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength);
[280]233      {
234        xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*");
235        if(reqptr!=NULL){
236          xmlNodeSet* req=reqptr->nodesetval;
237          if(req!=NULL && req->nodeNr==1){
238            addToMap(tmpMap,"soap","true");
[465]239            for(int k=0;k < req->nodeNr;k++){
240              //xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi");
[280]241              xmlDocSetRootElement(doc, req->nodeTab[k]);
242              xmlChar *xmlbuff;
243              int buffersize;
244              xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1);
245              addToMap(tmpMap,"xrequest",(char*)xmlbuff);
246              xmlFree(xmlbuff);
247            }
248          }
[490]249          xmlXPathFreeObject(reqptr);
[280]250        }
251      }
252
[1]253      xmlNodePtr cur = xmlDocGetRootElement(doc);
254      char *tval;
255      tval=NULL;
256      tval = (char*) xmlGetProp(cur,BAD_CAST "service");
[490]257      if(tval!=NULL){
[9]258        addToMap(tmpMap,"service",tval);
[490]259        xmlFree(tval);
260      }
[1]261      tval=NULL;
262      tval = (char*) xmlGetProp(cur,BAD_CAST "language");
[490]263      if(tval!=NULL){
[9]264        addToMap(tmpMap,"language",tval);
[490]265        xmlFree(tval);
266      }
[280]267      const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"};
[1]268      for(int j=0;j<3;j++){
[280]269        char tt[128];
[1]270        sprintf(tt,"/*[local-name()='%s']",requests[j]);
[9]271        xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt);
272        if(reqptr!=NULL){
273          xmlNodeSet* req=reqptr->nodesetval;
[1]274#ifdef DEBUG
[9]275          fprintf(stderr,"%i",req->nodeNr);
[1]276#endif
[9]277          if(req!=NULL && req->nodeNr==1){
[490]278            if(t1->value!=NULL)
279              free(t1->value);
[453]280            t1->value=zStrdup(requests[j]);
[9]281            j=2;
282          }
283          xmlXPathFreeObject(reqptr);
[1]284        }
285      }
[9]286      if(strncasecmp(t1->value,"GetCapabilities",15)==0){
287        xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']");
288        xmlNodeSet* vers=versptr->nodesetval;
[640]289        if(vers!=NULL && vers->nodeTab!=NULL && vers->nodeTab[0]!=NULL){
290          xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1);
291          addToMap(tmpMap,"version",(char*)content);
292          xmlFree(content);
293        }
294        if(cur->ns){
295          addToMap(tmpMap,"wps_schemas",(char*)cur->ns->href);
296          xmlFree(tval);
297          int j=0;
298          for(j=0;j<2;j++)
299            if(strncasecmp(schemas[j][2],(char*)cur->ns->href,strlen(schemas[j][2]))==0){
300              char vers[6];
301              sprintf(vers,"%d.0.0",j+1);
302              addToMap(tmpMap,"version",(char*)vers);
303            }
304        }
[9]305        xmlXPathFreeObject(versptr);
[1]306      }else{
307        tval=NULL;
308        tval = (char*) xmlGetProp(cur,BAD_CAST "version");
[490]309        if(tval!=NULL){
[9]310          addToMap(tmpMap,"version",tval);
[490]311          xmlFree(tval);
312        }
[1]313        tval = (char*) xmlGetProp(cur,BAD_CAST "language");
[490]314        if(tval!=NULL){
[9]315          addToMap(tmpMap,"language",tval);
[490]316          xmlFree(tval);
317        }
[9]318        xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']");
319        if(idptr!=NULL){
320          xmlNodeSet* id=idptr->nodesetval;
321          if(id!=NULL){
322            char* identifiers=NULL;
323            identifiers=(char*)calloc(cgiContentLength,sizeof(char));
324            identifiers[0]=0;
325            for(int k=0;k<id->nodeNr;k++){
326              xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1);
327              if(strlen(identifiers)>0){
[453]328                char *tmp=zStrdup(identifiers);
[9]329                snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content);
330                free(tmp);
331              }
332              else{
333                snprintf(identifiers,xmlStrlen(content)+1,"%s",content);
334              }
335              xmlFree(content);
336            }
337            xmlXPathFreeObject(idptr);
338            addToMap(tmpMap,"Identifier",identifiers);
339            free(identifiers);
[1]340          }
341        }
342      }
[9]343      xmlFreeDoc(doc);
344      xmlCleanupParser();
[331]345    }else{
346      freeMap(&tmpMap);
347      free(tmpMap);
348      tmpMap=createMap("not_valid","true");
[1]349    }
[329]350
351    char *token,*saveptr;
352    token=strtok_r(cgiQueryString,"&",&saveptr);
353    while(token!=NULL){
354      char *token1,*saveptr1;
355      char *name=NULL;
356      char *value=NULL;
357      token1=strtok_r(token,"=",&saveptr1);
358      while(token1!=NULL){
[587]359        if(name==NULL)
360          name=zStrdup(token1);
361        else
362          value=zStrdup(token1);
363        token1=strtok_r(NULL,"=",&saveptr1);
[601]364      }   
[587]365      //addToMap(tmpMap,name,value);
366          /* knut: strtok(_r) ignores delimiter bytes at start and end of string;
367           * it will return non-empty string or NULL, e.g. "metapath=" yields value=NULL.
368           * This modification sets value="" instead of NULL.
369           */
370          addToMap(tmpMap,name, value != NULL ? value : "");
371
[329]372      free(name);
373      free(value);
[331]374      name=NULL;
375      value=NULL;
[329]376      token=strtok_r(NULL,"&",&saveptr);
377    }
378   
[1]379  }
380
[331]381  if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){
[376]382    map* tmp=getMap(tmpMap,"dataInputs");
383    if(tmp!=NULL){
384      addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11);
[331]385    }
[376]386  }
[331]387
[381]388  if(strQuery!=NULL)
389    free(strQuery);
[9]390
[640]391 
[490]392  runRequest(&tmpMap);
393
[516]394  if(tmpMap!=NULL){
[9]395    freeMap(&tmpMap);
396    free(tmpMap);
397  }
[1]398  return 0;
399
400}
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