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

Last change on this file since 580 was 554, checked in by knut, 9 years ago

Changed the WIN32 version of function zGettimeofday. Changed return type for getShmLockId (WIN32). Changed type of _HINTERNET.mimeType from unsigned char* to char*. Fixed interconnected memory issues in functions getKeyValue and getShmLockId (WIN32). Added code to transfer the correct unique process identifier (usid) to background processes (applies to WIN32 version).

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