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 | #ifndef WIN32 |
---|
32 | #include "fcgio.h" |
---|
33 | #include "fcgi_config.h" |
---|
34 | #include "fcgi_stdio.h" |
---|
35 | #endif |
---|
36 | #include <sys/types.h> |
---|
37 | #include <unistd.h> |
---|
38 | #include "service_internal.h" |
---|
39 | |
---|
40 | |
---|
41 | #ifdef WIN32 |
---|
42 | #include "windows.h" |
---|
43 | #define strtok_r strtok_s |
---|
44 | #endif |
---|
45 | |
---|
46 | extern "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 | |
---|
55 | #include "service_internal.h" |
---|
56 | |
---|
57 | xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*); |
---|
58 | int runRequest(map**); |
---|
59 | |
---|
60 | using namespace std; |
---|
61 | |
---|
62 | #ifndef TRUE |
---|
63 | #define TRUE 1 |
---|
64 | #endif |
---|
65 | #ifndef FALSE |
---|
66 | #define FALSE -1 |
---|
67 | #endif |
---|
68 | |
---|
69 | int 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 |
---|
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); |
---|
80 | fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); |
---|
81 | fprintf (stderr, "Request: %s\n", cgiQueryString); |
---|
82 | fprintf (stderr, "ContentType: %s\n", cgiContentType); |
---|
83 | fprintf (stderr, "ContentLength: %d\n", cgiContentLength); |
---|
84 | fflush(stderr); |
---|
85 | #endif |
---|
86 | |
---|
87 | |
---|
88 | char *strQuery=NULL; |
---|
89 | if(cgiQueryString!=NULL) |
---|
90 | strQuery=zStrdup(cgiQueryString); |
---|
91 | map* tmpMap=NULL; |
---|
92 | |
---|
93 | if(strncmp(cgiContentType,"text/xml",8)==0 || |
---|
94 | strncasecmp(cgiRequestMethod,"post",4)==0){ |
---|
95 | if(cgiContentLength==0){ |
---|
96 | char *buffer=new char[2]; |
---|
97 | char *res=NULL; |
---|
98 | int r=0; |
---|
99 | while((r=fread(buffer,sizeof(char),1,cgiIn))){ |
---|
100 | buffer[1]=0; |
---|
101 | if(res==NULL){ |
---|
102 | res=(char*)malloc(2*sizeof(char)); |
---|
103 | sprintf(res,"%s",buffer); |
---|
104 | } |
---|
105 | else{ |
---|
106 | res=(char*)realloc(res,(cgiContentLength+2)*sizeof(char)); |
---|
107 | memcpy(res + cgiContentLength, buffer, sizeof(char)); |
---|
108 | res[cgiContentLength+1]=0; |
---|
109 | } |
---|
110 | cgiContentLength+=r; |
---|
111 | } |
---|
112 | delete[] buffer; |
---|
113 | if(res==NULL && (strQuery==NULL || strlen(strQuery)==0)){ |
---|
114 | return errorException(NULL,"ZOO-Kernel failed to process your request cause the request was emtpty.","InternalError",NULL); |
---|
115 | }else{ |
---|
116 | if(strQuery==NULL || strlen(strQuery)==0) |
---|
117 | tmpMap=createMap("request",res); |
---|
118 | } |
---|
119 | if(res!=NULL) |
---|
120 | free(res); |
---|
121 | }else{ |
---|
122 | char *buffer=new char[cgiContentLength+1]; |
---|
123 | if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)>0){ |
---|
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{ |
---|
141 | char *tmp=zStrdup(buffer); |
---|
142 | sprintf(buffer,"%s&%s",tmp,tmpValueFinal); |
---|
143 | free(tmp); |
---|
144 | } |
---|
145 | free(tmpValueFinal); |
---|
146 | #ifdef DEBUG |
---|
147 | fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue); |
---|
148 | #endif |
---|
149 | delete[]ivalue; |
---|
150 | arrayStep++; |
---|
151 | } |
---|
152 | if(tmpMap!=NULL) |
---|
153 | addToMap(tmpMap,"request",buffer); |
---|
154 | else |
---|
155 | tmpMap=createMap("request",buffer); |
---|
156 | } |
---|
157 | delete[]buffer; |
---|
158 | } |
---|
159 | } |
---|
160 | else{ |
---|
161 | #ifdef DEBUG |
---|
162 | dumpMap(tmpMap); |
---|
163 | #endif |
---|
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 |
---|
175 | if(tmpMap!=NULL) |
---|
176 | addToMap(tmpMap,*arrayStep,value); |
---|
177 | else |
---|
178 | tmpMap=createMap(*arrayStep,value); |
---|
179 | arrayStep++; |
---|
180 | delete[]value; |
---|
181 | } |
---|
182 | cgiStringArrayFree(array); |
---|
183 | } |
---|
184 | |
---|
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 |
---|
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 | //xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi"); |
---|
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 | } |
---|
235 | xmlXPathFreeObject(reqptr); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
240 | char *tval; |
---|
241 | tval=NULL; |
---|
242 | tval = (char*) xmlGetProp(cur,BAD_CAST "service"); |
---|
243 | if(tval!=NULL){ |
---|
244 | addToMap(tmpMap,"service",tval); |
---|
245 | xmlFree(tval); |
---|
246 | } |
---|
247 | tval=NULL; |
---|
248 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
249 | if(tval!=NULL){ |
---|
250 | addToMap(tmpMap,"language",tval); |
---|
251 | xmlFree(tval); |
---|
252 | } |
---|
253 | const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"}; |
---|
254 | for(int j=0;j<3;j++){ |
---|
255 | char tt[128]; |
---|
256 | sprintf(tt,"/*[local-name()='%s']",requests[j]); |
---|
257 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt); |
---|
258 | if(reqptr!=NULL){ |
---|
259 | xmlNodeSet* req=reqptr->nodesetval; |
---|
260 | #ifdef DEBUG |
---|
261 | fprintf(stderr,"%i",req->nodeNr); |
---|
262 | #endif |
---|
263 | if(req!=NULL && req->nodeNr==1){ |
---|
264 | if(t1->value!=NULL) |
---|
265 | free(t1->value); |
---|
266 | t1->value=zStrdup(requests[j]); |
---|
267 | j=2; |
---|
268 | } |
---|
269 | xmlXPathFreeObject(reqptr); |
---|
270 | } |
---|
271 | } |
---|
272 | if(strncasecmp(t1->value,"GetCapabilities",15)==0){ |
---|
273 | xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']"); |
---|
274 | xmlNodeSet* vers=versptr->nodesetval; |
---|
275 | xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1); |
---|
276 | addToMap(tmpMap,"version",(char*)content); |
---|
277 | xmlXPathFreeObject(versptr); |
---|
278 | xmlFree(content); |
---|
279 | }else{ |
---|
280 | tval=NULL; |
---|
281 | tval = (char*) xmlGetProp(cur,BAD_CAST "version"); |
---|
282 | if(tval!=NULL){ |
---|
283 | addToMap(tmpMap,"version",tval); |
---|
284 | xmlFree(tval); |
---|
285 | } |
---|
286 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
287 | if(tval!=NULL){ |
---|
288 | addToMap(tmpMap,"language",tval); |
---|
289 | xmlFree(tval); |
---|
290 | } |
---|
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){ |
---|
301 | char *tmp=zStrdup(identifiers); |
---|
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); |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|
316 | xmlFreeDoc(doc); |
---|
317 | xmlCleanupParser(); |
---|
318 | }else{ |
---|
319 | freeMap(&tmpMap); |
---|
320 | free(tmpMap); |
---|
321 | tmpMap=createMap("not_valid","true"); |
---|
322 | } |
---|
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) |
---|
333 | name=zStrdup(token1); |
---|
334 | else |
---|
335 | value=zStrdup(token1); |
---|
336 | token1=strtok_r(NULL,"=",&saveptr1); |
---|
337 | } |
---|
338 | addToMap(tmpMap,name,value); |
---|
339 | free(name); |
---|
340 | free(value); |
---|
341 | name=NULL; |
---|
342 | value=NULL; |
---|
343 | token=strtok_r(NULL,"&",&saveptr); |
---|
344 | } |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){ |
---|
349 | map* tmp=getMap(tmpMap,"dataInputs"); |
---|
350 | if(tmp!=NULL){ |
---|
351 | addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11); |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | if(strQuery!=NULL) |
---|
356 | free(strQuery); |
---|
357 | |
---|
358 | runRequest(&tmpMap); |
---|
359 | |
---|
360 | if(tmpMap!=NULL){ |
---|
361 | freeMap(&tmpMap); |
---|
362 | free(tmpMap); |
---|
363 | } |
---|
364 | return 0; |
---|
365 | |
---|
366 | } |
---|