source: branches/prototype-v0/zoo-project/zoo-kernel/zoo_loader.c @ 862

Last change on this file since 862 was 862, checked in by djay, 6 years ago

Add the capability to publish heatmap or any templated mapfile using the epecific msInclude and msLayer keys for an output. For MapServer? published output, define 4096 as the default maxsize and use pixel width or height for raster files. use the correct MapServer? imagemode depending on GDALGetRasterDataType (MS_IMAGEMODE_BYTE for GDT_Byte, MS_IMAGEMODE_INT16 for GDT_Int16 and MS_IMAGEMODE_FLOAT32 for GDT_Float32). Create a text file (.maps) listing every mapfiles created for a MapServer? published output (or inputs) using saveMapNames function. Fixes in ulinet, use uuid for naming temporary files. Add dialect input to the ogr2ogr service. Use the .maps file for removing a file from the DeleteData? service

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