[607] | 1 | /* |
---|
[1] | 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
[69] | 4 | * Copyright 2008-2011 GeoLabs SARL. All rights reserved. |
---|
[1] | 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 | |
---|
[9] | 25 | #define MALLOC_CHECK_ 0 |
---|
| 26 | #define MALLOC_CHECK 0 |
---|
| 27 | |
---|
[1] | 28 | /** |
---|
| 29 | * Specific includes |
---|
| 30 | */ |
---|
[364] | 31 | #ifndef WIN32 |
---|
[1] | 32 | #include "fcgio.h" |
---|
| 33 | #include "fcgi_config.h" |
---|
| 34 | #include "fcgi_stdio.h" |
---|
[364] | 35 | #endif |
---|
[1] | 36 | #include <sys/types.h> |
---|
| 37 | #include <unistd.h> |
---|
[9] | 38 | #include "service_internal.h" |
---|
[554] | 39 | |
---|
| 40 | |
---|
[364] | 41 | #ifdef WIN32 |
---|
| 42 | #include "windows.h" |
---|
| 43 | #define strtok_r strtok_s |
---|
| 44 | #endif |
---|
[9] | 45 | |
---|
[1] | 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 | |
---|
[280] | 55 | #include "service_internal.h" |
---|
| 56 | |
---|
[114] | 57 | xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*); |
---|
[490] | 58 | int runRequest(map**); |
---|
[9] | 59 | |
---|
| 60 | using namespace std; |
---|
| 61 | |
---|
[364] | 62 | #ifndef TRUE |
---|
[9] | 63 | #define TRUE 1 |
---|
[364] | 64 | #endif |
---|
| 65 | #ifndef FALSE |
---|
[1] | 66 | #define FALSE -1 |
---|
[364] | 67 | #endif |
---|
[1] | 68 | |
---|
[607] | 69 | /** |
---|
| 70 | * Main entry point for cgic. |
---|
| 71 | * @return 0 on sucess. |
---|
| 72 | */ |
---|
[1] | 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 |
---|
[9] | 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 | fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); |
---|
[280] | 84 | fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); |
---|
[9] | 85 | fprintf (stderr, "Request: %s\n", cgiQueryString); |
---|
[380] | 86 | fprintf (stderr, "ContentType: %s\n", cgiContentType); |
---|
| 87 | fprintf (stderr, "ContentLength: %d\n", cgiContentLength); |
---|
[376] | 88 | fflush(stderr); |
---|
[1] | 89 | #endif |
---|
[381] | 90 | |
---|
| 91 | char *strQuery=NULL; |
---|
| 92 | if(cgiQueryString!=NULL) |
---|
[453] | 93 | strQuery=zStrdup(cgiQueryString); |
---|
[9] | 94 | map* tmpMap=NULL; |
---|
[69] | 95 | |
---|
[99] | 96 | if(strncmp(cgiContentType,"text/xml",8)==0 || |
---|
| 97 | strncasecmp(cgiRequestMethod,"post",4)==0){ |
---|
[490] | 98 | if(cgiContentLength==0){ |
---|
[280] | 99 | char *buffer=new char[2]; |
---|
| 100 | char *res=NULL; |
---|
| 101 | int r=0; |
---|
[381] | 102 | while((r=fread(buffer,sizeof(char),1,cgiIn))){ |
---|
[490] | 103 | buffer[1]=0; |
---|
[280] | 104 | if(res==NULL){ |
---|
[490] | 105 | res=(char*)malloc(2*sizeof(char)); |
---|
[280] | 106 | sprintf(res,"%s",buffer); |
---|
| 107 | } |
---|
| 108 | else{ |
---|
[510] | 109 | res=(char*)realloc(res,(cgiContentLength+2)*sizeof(char)); |
---|
| 110 | memcpy(res + cgiContentLength, buffer, sizeof(char)); |
---|
| 111 | res[cgiContentLength+1]=0; |
---|
[280] | 112 | } |
---|
[510] | 113 | cgiContentLength+=r; |
---|
[280] | 114 | } |
---|
[490] | 115 | delete[] buffer; |
---|
[376] | 116 | if(res==NULL && (strQuery==NULL || strlen(strQuery)==0)){ |
---|
[605] | 117 | return errorException(NULL,"ZOO-Kernel failed to process your request because the request was empty.","InternalError",NULL); |
---|
[376] | 118 | }else{ |
---|
| 119 | if(strQuery==NULL || strlen(strQuery)==0) |
---|
| 120 | tmpMap=createMap("request",res); |
---|
[490] | 121 | } |
---|
| 122 | if(res!=NULL) |
---|
| 123 | free(res); |
---|
[1] | 124 | }else{ |
---|
[280] | 125 | char *buffer=new char[cgiContentLength+1]; |
---|
[490] | 126 | if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)>0){ |
---|
[280] | 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); |
---|
[601] | 139 | char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+2)*sizeof(char)); |
---|
[280] | 140 | sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue); |
---|
[601] | 141 | |
---|
| 142 | if(strlen(buffer)==0){ |
---|
[280] | 143 | sprintf(buffer,"%s",tmpValueFinal); |
---|
[601] | 144 | }else{ |
---|
[453] | 145 | char *tmp=zStrdup(buffer); |
---|
[280] | 146 | sprintf(buffer,"%s&%s",tmp,tmpValueFinal); |
---|
| 147 | free(tmp); |
---|
[601] | 148 | } |
---|
[280] | 149 | free(tmpValueFinal); |
---|
[99] | 150 | #ifdef DEBUG |
---|
[280] | 151 | fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue); |
---|
[99] | 152 | #endif |
---|
[280] | 153 | delete[]ivalue; |
---|
| 154 | arrayStep++; |
---|
[601] | 155 | } |
---|
[490] | 156 | if(tmpMap!=NULL) |
---|
| 157 | addToMap(tmpMap,"request",buffer); |
---|
| 158 | else |
---|
| 159 | tmpMap=createMap("request",buffer); |
---|
[99] | 160 | } |
---|
[280] | 161 | delete[]buffer; |
---|
[601] | 162 | } |
---|
[1] | 163 | } |
---|
| 164 | else{ |
---|
[364] | 165 | #ifdef DEBUG |
---|
[331] | 166 | dumpMap(tmpMap); |
---|
[364] | 167 | #endif |
---|
[1] | 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 |
---|
[587] | 179 | #ifdef WIN32 |
---|
| 180 | char* tmp = url_decode(value); |
---|
| 181 | if(tmpMap!=NULL) |
---|
| 182 | addToMap(tmpMap,*arrayStep,tmp); |
---|
| 183 | else |
---|
| 184 | tmpMap=createMap(*arrayStep,tmp); |
---|
| 185 | free(tmp); |
---|
| 186 | #else |
---|
[9] | 187 | if(tmpMap!=NULL) |
---|
[587] | 188 | addToMap(tmpMap,*arrayStep,value); |
---|
[1] | 189 | else |
---|
[587] | 190 | tmpMap=createMap(*arrayStep,value); |
---|
| 191 | #endif |
---|
[1] | 192 | arrayStep++; |
---|
[9] | 193 | delete[]value; |
---|
[1] | 194 | } |
---|
| 195 | cgiStringArrayFree(array); |
---|
| 196 | } |
---|
| 197 | |
---|
[458] | 198 | #ifdef WIN32 |
---|
| 199 | map *tmpReq=getMap(tmpMap,"rfile"); |
---|
| 200 | if(tmpReq!=NULL){ |
---|
| 201 | FILE *lf=fopen(tmpReq->value,"r"); |
---|
| 202 | fseek(lf,0,SEEK_END); |
---|
| 203 | long flen=ftell(lf); |
---|
| 204 | fseek(lf,0,SEEK_SET); |
---|
| 205 | char *buffer=(char*)malloc((flen+1)*sizeof(char)); |
---|
| 206 | fread(buffer,flen,1,lf); |
---|
| 207 | fclose(lf); |
---|
| 208 | addToMap(tmpMap,"request",buffer); |
---|
| 209 | free(buffer); |
---|
| 210 | cgiContentLength=flen+9; |
---|
| 211 | } |
---|
| 212 | #endif |
---|
[1] | 213 | /** |
---|
| 214 | * In case that the POST method was used, then check if params came in XML |
---|
[9] | 215 | * format else try to use the attribute "request" which should be the only |
---|
| 216 | * one. |
---|
[1] | 217 | */ |
---|
[10] | 218 | if(strncasecmp(cgiRequestMethod,"post",4)==0 || |
---|
[458] | 219 | (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0) |
---|
| 220 | #ifdef WIN32 |
---|
| 221 | ||tmpReq!=NULL |
---|
| 222 | #endif |
---|
| 223 | ){ |
---|
[1] | 224 | /** |
---|
| 225 | * Store the original XML request in xrequest map |
---|
| 226 | */ |
---|
[9] | 227 | map* t1=getMap(tmpMap,"request"); |
---|
[331] | 228 | if(t1!=NULL && strncasecmp(t1->value,"<",1)==0){ |
---|
[9] | 229 | addToMap(tmpMap,"xrequest",t1->value); |
---|
[1] | 230 | xmlInitParser(); |
---|
[9] | 231 | xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength); |
---|
[280] | 232 | { |
---|
| 233 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*"); |
---|
| 234 | if(reqptr!=NULL){ |
---|
| 235 | xmlNodeSet* req=reqptr->nodesetval; |
---|
| 236 | if(req!=NULL && req->nodeNr==1){ |
---|
| 237 | addToMap(tmpMap,"soap","true"); |
---|
[465] | 238 | for(int k=0;k < req->nodeNr;k++){ |
---|
| 239 | //xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi"); |
---|
[280] | 240 | xmlDocSetRootElement(doc, req->nodeTab[k]); |
---|
| 241 | xmlChar *xmlbuff; |
---|
| 242 | int buffersize; |
---|
| 243 | xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1); |
---|
| 244 | addToMap(tmpMap,"xrequest",(char*)xmlbuff); |
---|
| 245 | xmlFree(xmlbuff); |
---|
| 246 | } |
---|
| 247 | } |
---|
[490] | 248 | xmlXPathFreeObject(reqptr); |
---|
[280] | 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
[1] | 252 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
| 253 | char *tval; |
---|
| 254 | tval=NULL; |
---|
| 255 | tval = (char*) xmlGetProp(cur,BAD_CAST "service"); |
---|
[490] | 256 | if(tval!=NULL){ |
---|
[9] | 257 | addToMap(tmpMap,"service",tval); |
---|
[490] | 258 | xmlFree(tval); |
---|
| 259 | } |
---|
[1] | 260 | tval=NULL; |
---|
| 261 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
[490] | 262 | if(tval!=NULL){ |
---|
[9] | 263 | addToMap(tmpMap,"language",tval); |
---|
[490] | 264 | xmlFree(tval); |
---|
| 265 | } |
---|
[280] | 266 | const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"}; |
---|
[1] | 267 | for(int j=0;j<3;j++){ |
---|
[280] | 268 | char tt[128]; |
---|
[1] | 269 | sprintf(tt,"/*[local-name()='%s']",requests[j]); |
---|
[9] | 270 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt); |
---|
| 271 | if(reqptr!=NULL){ |
---|
| 272 | xmlNodeSet* req=reqptr->nodesetval; |
---|
[1] | 273 | #ifdef DEBUG |
---|
[9] | 274 | fprintf(stderr,"%i",req->nodeNr); |
---|
[1] | 275 | #endif |
---|
[9] | 276 | if(req!=NULL && req->nodeNr==1){ |
---|
[490] | 277 | if(t1->value!=NULL) |
---|
| 278 | free(t1->value); |
---|
[453] | 279 | t1->value=zStrdup(requests[j]); |
---|
[9] | 280 | j=2; |
---|
| 281 | } |
---|
| 282 | xmlXPathFreeObject(reqptr); |
---|
[1] | 283 | } |
---|
| 284 | } |
---|
[9] | 285 | if(strncasecmp(t1->value,"GetCapabilities",15)==0){ |
---|
| 286 | xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']"); |
---|
| 287 | xmlNodeSet* vers=versptr->nodesetval; |
---|
[1] | 288 | xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1); |
---|
[9] | 289 | addToMap(tmpMap,"version",(char*)content); |
---|
| 290 | xmlXPathFreeObject(versptr); |
---|
[1] | 291 | xmlFree(content); |
---|
| 292 | }else{ |
---|
| 293 | tval=NULL; |
---|
| 294 | tval = (char*) xmlGetProp(cur,BAD_CAST "version"); |
---|
[490] | 295 | if(tval!=NULL){ |
---|
[9] | 296 | addToMap(tmpMap,"version",tval); |
---|
[490] | 297 | xmlFree(tval); |
---|
| 298 | } |
---|
[1] | 299 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
[490] | 300 | if(tval!=NULL){ |
---|
[9] | 301 | addToMap(tmpMap,"language",tval); |
---|
[490] | 302 | xmlFree(tval); |
---|
| 303 | } |
---|
[9] | 304 | xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']"); |
---|
| 305 | if(idptr!=NULL){ |
---|
| 306 | xmlNodeSet* id=idptr->nodesetval; |
---|
| 307 | if(id!=NULL){ |
---|
| 308 | char* identifiers=NULL; |
---|
| 309 | identifiers=(char*)calloc(cgiContentLength,sizeof(char)); |
---|
| 310 | identifiers[0]=0; |
---|
| 311 | for(int k=0;k<id->nodeNr;k++){ |
---|
| 312 | xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1); |
---|
| 313 | if(strlen(identifiers)>0){ |
---|
[453] | 314 | char *tmp=zStrdup(identifiers); |
---|
[9] | 315 | snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content); |
---|
| 316 | free(tmp); |
---|
| 317 | } |
---|
| 318 | else{ |
---|
| 319 | snprintf(identifiers,xmlStrlen(content)+1,"%s",content); |
---|
| 320 | } |
---|
| 321 | xmlFree(content); |
---|
| 322 | } |
---|
| 323 | xmlXPathFreeObject(idptr); |
---|
| 324 | addToMap(tmpMap,"Identifier",identifiers); |
---|
| 325 | free(identifiers); |
---|
[1] | 326 | } |
---|
| 327 | } |
---|
| 328 | } |
---|
[9] | 329 | xmlFreeDoc(doc); |
---|
| 330 | xmlCleanupParser(); |
---|
[331] | 331 | }else{ |
---|
| 332 | freeMap(&tmpMap); |
---|
| 333 | free(tmpMap); |
---|
| 334 | tmpMap=createMap("not_valid","true"); |
---|
[1] | 335 | } |
---|
[329] | 336 | |
---|
| 337 | char *token,*saveptr; |
---|
| 338 | token=strtok_r(cgiQueryString,"&",&saveptr); |
---|
| 339 | while(token!=NULL){ |
---|
| 340 | char *token1,*saveptr1; |
---|
| 341 | char *name=NULL; |
---|
| 342 | char *value=NULL; |
---|
| 343 | token1=strtok_r(token,"=",&saveptr1); |
---|
| 344 | while(token1!=NULL){ |
---|
[587] | 345 | if(name==NULL) |
---|
| 346 | name=zStrdup(token1); |
---|
| 347 | else |
---|
| 348 | value=zStrdup(token1); |
---|
| 349 | token1=strtok_r(NULL,"=",&saveptr1); |
---|
[601] | 350 | } |
---|
[587] | 351 | //addToMap(tmpMap,name,value); |
---|
| 352 | /* knut: strtok(_r) ignores delimiter bytes at start and end of string; |
---|
| 353 | * it will return non-empty string or NULL, e.g. "metapath=" yields value=NULL. |
---|
| 354 | * This modification sets value="" instead of NULL. |
---|
| 355 | */ |
---|
| 356 | addToMap(tmpMap,name, value != NULL ? value : ""); |
---|
| 357 | |
---|
[329] | 358 | free(name); |
---|
| 359 | free(value); |
---|
[331] | 360 | name=NULL; |
---|
| 361 | value=NULL; |
---|
[329] | 362 | token=strtok_r(NULL,"&",&saveptr); |
---|
| 363 | } |
---|
| 364 | |
---|
[1] | 365 | } |
---|
| 366 | |
---|
[331] | 367 | if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){ |
---|
[376] | 368 | map* tmp=getMap(tmpMap,"dataInputs"); |
---|
| 369 | if(tmp!=NULL){ |
---|
| 370 | addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11); |
---|
[331] | 371 | } |
---|
[376] | 372 | } |
---|
[331] | 373 | |
---|
[381] | 374 | if(strQuery!=NULL) |
---|
| 375 | free(strQuery); |
---|
[9] | 376 | |
---|
[490] | 377 | runRequest(&tmpMap); |
---|
| 378 | |
---|
[516] | 379 | if(tmpMap!=NULL){ |
---|
[9] | 380 | freeMap(&tmpMap); |
---|
| 381 | free(tmpMap); |
---|
| 382 | } |
---|
[1] | 383 | return 0; |
---|
| 384 | |
---|
| 385 | } |
---|