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