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 | #ifdef WIN32 |
---|
29 | #include "windows.h" |
---|
30 | #endif |
---|
31 | /** |
---|
32 | * Specific includes |
---|
33 | */ |
---|
34 | #include "fcgio.h" |
---|
35 | #include "fcgi_config.h" |
---|
36 | #include "fcgi_stdio.h" |
---|
37 | #include <sys/types.h> |
---|
38 | #include <unistd.h> |
---|
39 | #include "service_internal.h" |
---|
40 | |
---|
41 | extern "C" { |
---|
42 | #include "cgic.h" |
---|
43 | #include <libxml/tree.h> |
---|
44 | #include <libxml/xmlmemory.h> |
---|
45 | #include <libxml/parser.h> |
---|
46 | #include <libxml/xpath.h> |
---|
47 | #include <libxml/xpathInternals.h> |
---|
48 | } |
---|
49 | |
---|
50 | xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*); |
---|
51 | int runRequest(map*); |
---|
52 | |
---|
53 | using namespace std; |
---|
54 | |
---|
55 | /* ************************************************************************* */ |
---|
56 | |
---|
57 | int errorException(maps *m, const char *message, const char *errorcode) |
---|
58 | { |
---|
59 | map* errormap = createMap("text", message); |
---|
60 | addToMap(errormap,"code", errorcode); |
---|
61 | printExceptionReportResponse(m,errormap); |
---|
62 | freeMap(&errormap); |
---|
63 | free(errormap); |
---|
64 | return -1; |
---|
65 | } |
---|
66 | |
---|
67 | /* ************************************************************************* */ |
---|
68 | |
---|
69 | |
---|
70 | #define TRUE 1 |
---|
71 | #define FALSE -1 |
---|
72 | |
---|
73 | int cgiMain(){ |
---|
74 | /** |
---|
75 | * We'll use cgiOut as the default output (stdout) to produce plain text |
---|
76 | * response. |
---|
77 | */ |
---|
78 | dup2(fileno(cgiOut),fileno(stdout)); |
---|
79 | #ifdef DEBUG |
---|
80 | fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n"); |
---|
81 | fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n"); |
---|
82 | fflush(cgiOut); |
---|
83 | #endif |
---|
84 | |
---|
85 | #ifdef DEBUG |
---|
86 | fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); |
---|
87 | fprintf (stderr, "RequestMethod:%s\n", cgiRequestMethod); |
---|
88 | fprintf (stderr, "Request: %s\n", cgiQueryString); |
---|
89 | #endif |
---|
90 | |
---|
91 | map* tmpMap=NULL; |
---|
92 | |
---|
93 | if(strncmp(cgiContentType,"text/xml",8)==0 || |
---|
94 | strncasecmp(cgiRequestMethod,"post",4)==0){ |
---|
95 | char *buffer=new char[cgiContentLength+1]; |
---|
96 | if(fread(buffer,1,cgiContentLength,cgiIn)){ |
---|
97 | buffer[cgiContentLength]=0; |
---|
98 | tmpMap=createMap("request",buffer); |
---|
99 | }else{ |
---|
100 | char **array, **arrayStep; |
---|
101 | if (cgiFormEntries(&array) != cgiFormSuccess) { |
---|
102 | return 1; |
---|
103 | } |
---|
104 | arrayStep = array; |
---|
105 | while (*arrayStep) { |
---|
106 | char *value=new char[cgiContentLength]; |
---|
107 | cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength); |
---|
108 | char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(value)+1)*sizeof(char)); |
---|
109 | sprintf(tmpValueFinal,"%s=%s",*arrayStep,value); |
---|
110 | tmpMap=createMap("request",tmpValueFinal); |
---|
111 | free(tmpValueFinal); |
---|
112 | #ifdef DEBUG |
---|
113 | fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value); |
---|
114 | #endif |
---|
115 | delete[]value; |
---|
116 | arrayStep++; |
---|
117 | } |
---|
118 | } |
---|
119 | delete[]buffer; |
---|
120 | } |
---|
121 | else{ |
---|
122 | char **array, **arrayStep; |
---|
123 | if (cgiFormEntries(&array) != cgiFormSuccess) { |
---|
124 | return 1; |
---|
125 | } |
---|
126 | arrayStep = array; |
---|
127 | while (*arrayStep) { |
---|
128 | char *value=new char[cgiContentLength]; |
---|
129 | cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength); |
---|
130 | #ifdef DEBUG |
---|
131 | fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value); |
---|
132 | #endif |
---|
133 | if(tmpMap!=NULL) |
---|
134 | addToMap(tmpMap,*arrayStep,value); |
---|
135 | else |
---|
136 | tmpMap=createMap(*arrayStep,value); |
---|
137 | arrayStep++; |
---|
138 | delete[]value; |
---|
139 | } |
---|
140 | cgiStringArrayFree(array); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * In case that the POST method was used, then check if params came in XML |
---|
145 | * format else try to use the attribute "request" which should be the only |
---|
146 | * one. |
---|
147 | */ |
---|
148 | if(strncasecmp(cgiRequestMethod,"post",4)==0 || |
---|
149 | (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){ |
---|
150 | /** |
---|
151 | * First include the MetaPath and the ServiceProvider default parameters |
---|
152 | * (which should be always available in GET params so in cgiQueryString) |
---|
153 | */ |
---|
154 | char *str1; |
---|
155 | str1=cgiQueryString; |
---|
156 | /** |
---|
157 | * Store the original XML request in xrequest map |
---|
158 | */ |
---|
159 | map* t1=getMap(tmpMap,"request"); |
---|
160 | if(t1!=NULL){ |
---|
161 | addToMap(tmpMap,"xrequest",t1->value); |
---|
162 | xmlInitParser(); |
---|
163 | xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength); |
---|
164 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
165 | char *tval; |
---|
166 | tval=NULL; |
---|
167 | tval = (char*) xmlGetProp(cur,BAD_CAST "service"); |
---|
168 | if(tval!=NULL) |
---|
169 | addToMap(tmpMap,"service",tval); |
---|
170 | tval=NULL; |
---|
171 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
172 | if(tval!=NULL) |
---|
173 | addToMap(tmpMap,"language",tval); |
---|
174 | |
---|
175 | const char* requests[3]; |
---|
176 | requests[0]="GetCapabilities"; |
---|
177 | requests[1]="DescribeProcess"; |
---|
178 | requests[2]="Execute"; |
---|
179 | for(int j=0;j<3;j++){ |
---|
180 | char tt[35]; |
---|
181 | sprintf(tt,"/*[local-name()='%s']",requests[j]); |
---|
182 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt); |
---|
183 | if(reqptr!=NULL){ |
---|
184 | xmlNodeSet* req=reqptr->nodesetval; |
---|
185 | #ifdef DEBUG |
---|
186 | fprintf(stderr,"%i",req->nodeNr); |
---|
187 | #endif |
---|
188 | if(req!=NULL && req->nodeNr==1){ |
---|
189 | t1->value=strdup(requests[j]); |
---|
190 | j=2; |
---|
191 | } |
---|
192 | xmlXPathFreeObject(reqptr); |
---|
193 | } |
---|
194 | //xmlFree(req); |
---|
195 | } |
---|
196 | if(strncasecmp(t1->value,"GetCapabilities",15)==0){ |
---|
197 | xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']"); |
---|
198 | xmlNodeSet* vers=versptr->nodesetval; |
---|
199 | xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1); |
---|
200 | addToMap(tmpMap,"version",(char*)content); |
---|
201 | xmlXPathFreeObject(versptr); |
---|
202 | //xmlFree(vers); |
---|
203 | xmlFree(content); |
---|
204 | }else{ |
---|
205 | tval=NULL; |
---|
206 | tval = (char*) xmlGetProp(cur,BAD_CAST "version"); |
---|
207 | if(tval!=NULL) |
---|
208 | addToMap(tmpMap,"version",tval); |
---|
209 | xmlFree(tval); |
---|
210 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
211 | if(tval!=NULL) |
---|
212 | addToMap(tmpMap,"language",tval); |
---|
213 | xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']"); |
---|
214 | if(idptr!=NULL){ |
---|
215 | xmlNodeSet* id=idptr->nodesetval; |
---|
216 | if(id!=NULL){ |
---|
217 | char* identifiers=NULL; |
---|
218 | identifiers=(char*)calloc(cgiContentLength,sizeof(char)); |
---|
219 | identifiers[0]=0; |
---|
220 | for(int k=0;k<id->nodeNr;k++){ |
---|
221 | xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1); |
---|
222 | if(strlen(identifiers)>0){ |
---|
223 | char *tmp=strdup(identifiers); |
---|
224 | snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content); |
---|
225 | free(tmp); |
---|
226 | } |
---|
227 | else{ |
---|
228 | snprintf(identifiers,xmlStrlen(content)+1,"%s",content); |
---|
229 | } |
---|
230 | xmlFree(content); |
---|
231 | } |
---|
232 | xmlXPathFreeObject(idptr); |
---|
233 | addToMap(tmpMap,"Identifier",identifiers); |
---|
234 | free(identifiers); |
---|
235 | } |
---|
236 | } |
---|
237 | //xmlFree(id); |
---|
238 | } |
---|
239 | xmlFree(tval); |
---|
240 | xmlFreeDoc(doc); |
---|
241 | xmlCleanupParser(); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | runRequest(tmpMap); |
---|
246 | |
---|
247 | /** |
---|
248 | * Required but can't be made after executing a process using POST requests. |
---|
249 | */ |
---|
250 | if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){ |
---|
251 | freeMap(&tmpMap); |
---|
252 | free(tmpMap); |
---|
253 | } |
---|
254 | return 0; |
---|
255 | |
---|
256 | } |
---|