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

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

Fix GetResult? and POST asynchronous requests on Windows. Prevent CONTENT_TYPE=text/xml before creating the process using CreateProcess?. Revert modifications made r587 for using url_decode directly inside zoo_loader.c, the url_decode call should be in kvpParseInputs (from request_parser.c), indeed, there should not be any decoding required in other cases than Execute requests. Fix issue in mapsFromPyDict, small changes in mapFromPyDict to fix parsing result value (only) for Python 3.

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