[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" |
---|
[364] | 39 | #ifdef WIN32 |
---|
| 40 | #include "windows.h" |
---|
| 41 | #define strtok_r strtok_s |
---|
| 42 | #endif |
---|
[9] | 43 | |
---|
[1] | 44 | extern "C" { |
---|
| 45 | #include "cgic.h" |
---|
| 46 | #include <libxml/tree.h> |
---|
| 47 | #include <libxml/xmlmemory.h> |
---|
| 48 | #include <libxml/parser.h> |
---|
| 49 | #include <libxml/xpath.h> |
---|
| 50 | #include <libxml/xpathInternals.h> |
---|
| 51 | } |
---|
| 52 | |
---|
[280] | 53 | #include "service_internal.h" |
---|
| 54 | |
---|
[114] | 55 | xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*); |
---|
[490] | 56 | int runRequest(map**); |
---|
[9] | 57 | |
---|
| 58 | using namespace std; |
---|
| 59 | |
---|
[364] | 60 | #ifndef TRUE |
---|
[9] | 61 | #define TRUE 1 |
---|
[364] | 62 | #endif |
---|
| 63 | #ifndef FALSE |
---|
[1] | 64 | #define FALSE -1 |
---|
[364] | 65 | #endif |
---|
[1] | 66 | |
---|
| 67 | int cgiMain(){ |
---|
| 68 | /** |
---|
| 69 | * We'll use cgiOut as the default output (stdout) to produce plain text |
---|
| 70 | * response. |
---|
| 71 | */ |
---|
| 72 | dup2(fileno(cgiOut),fileno(stdout)); |
---|
| 73 | #ifdef DEBUG |
---|
[9] | 74 | fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n"); |
---|
| 75 | fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n"); |
---|
| 76 | fflush(cgiOut); |
---|
| 77 | fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); |
---|
[280] | 78 | fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); |
---|
[9] | 79 | fprintf (stderr, "Request: %s\n", cgiQueryString); |
---|
[380] | 80 | fprintf (stderr, "ContentType: %s\n", cgiContentType); |
---|
| 81 | fprintf (stderr, "ContentLength: %d\n", cgiContentLength); |
---|
[376] | 82 | fflush(stderr); |
---|
[1] | 83 | #endif |
---|
| 84 | |
---|
[381] | 85 | |
---|
| 86 | char *strQuery=NULL; |
---|
| 87 | if(cgiQueryString!=NULL) |
---|
[453] | 88 | strQuery=zStrdup(cgiQueryString); |
---|
[9] | 89 | map* tmpMap=NULL; |
---|
[69] | 90 | |
---|
[99] | 91 | if(strncmp(cgiContentType,"text/xml",8)==0 || |
---|
| 92 | strncasecmp(cgiRequestMethod,"post",4)==0){ |
---|
[490] | 93 | if(cgiContentLength==0){ |
---|
[280] | 94 | char *buffer=new char[2]; |
---|
| 95 | char *res=NULL; |
---|
| 96 | int r=0; |
---|
[381] | 97 | while((r=fread(buffer,sizeof(char),1,cgiIn))){ |
---|
[490] | 98 | buffer[1]=0; |
---|
[280] | 99 | if(res==NULL){ |
---|
[490] | 100 | res=(char*)malloc(2*sizeof(char)); |
---|
[280] | 101 | sprintf(res,"%s",buffer); |
---|
| 102 | } |
---|
| 103 | else{ |
---|
[510] | 104 | res=(char*)realloc(res,(cgiContentLength+2)*sizeof(char)); |
---|
| 105 | memcpy(res + cgiContentLength, buffer, sizeof(char)); |
---|
| 106 | res[cgiContentLength+1]=0; |
---|
[280] | 107 | } |
---|
[510] | 108 | cgiContentLength+=r; |
---|
[280] | 109 | } |
---|
[490] | 110 | delete[] buffer; |
---|
[376] | 111 | if(res==NULL && (strQuery==NULL || strlen(strQuery)==0)){ |
---|
[459] | 112 | return errorException(NULL,"ZOO-Kernel failed to process your request cause the request was emtpty.","InternalError",NULL); |
---|
[376] | 113 | }else{ |
---|
| 114 | if(strQuery==NULL || strlen(strQuery)==0) |
---|
| 115 | tmpMap=createMap("request",res); |
---|
[490] | 116 | } |
---|
| 117 | if(res!=NULL) |
---|
| 118 | free(res); |
---|
[1] | 119 | }else{ |
---|
[280] | 120 | char *buffer=new char[cgiContentLength+1]; |
---|
[490] | 121 | if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)>0){ |
---|
[280] | 122 | buffer[cgiContentLength]=0; |
---|
| 123 | tmpMap=createMap("request",buffer); |
---|
| 124 | }else{ |
---|
| 125 | buffer[0]=0; |
---|
| 126 | char **array, **arrayStep; |
---|
| 127 | if (cgiFormEntries(&array) != cgiFormSuccess) { |
---|
| 128 | return 1; |
---|
| 129 | } |
---|
| 130 | arrayStep = array; |
---|
| 131 | while (*arrayStep) { |
---|
| 132 | char *ivalue=new char[cgiContentLength]; |
---|
| 133 | cgiFormStringNoNewlines(*arrayStep, ivalue, cgiContentLength); |
---|
| 134 | char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+1)*sizeof(char)); |
---|
| 135 | sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue); |
---|
| 136 | if(strlen(buffer)==0){ |
---|
| 137 | sprintf(buffer,"%s",tmpValueFinal); |
---|
| 138 | }else{ |
---|
[453] | 139 | char *tmp=zStrdup(buffer); |
---|
[280] | 140 | sprintf(buffer,"%s&%s",tmp,tmpValueFinal); |
---|
| 141 | free(tmp); |
---|
| 142 | } |
---|
| 143 | free(tmpValueFinal); |
---|
[99] | 144 | #ifdef DEBUG |
---|
[280] | 145 | fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,ivalue); |
---|
[99] | 146 | #endif |
---|
[280] | 147 | delete[]ivalue; |
---|
| 148 | arrayStep++; |
---|
| 149 | } |
---|
[490] | 150 | if(tmpMap!=NULL) |
---|
| 151 | addToMap(tmpMap,"request",buffer); |
---|
| 152 | else |
---|
| 153 | tmpMap=createMap("request",buffer); |
---|
[99] | 154 | } |
---|
[280] | 155 | delete[]buffer; |
---|
[1] | 156 | } |
---|
| 157 | } |
---|
| 158 | else{ |
---|
[364] | 159 | #ifdef DEBUG |
---|
[331] | 160 | dumpMap(tmpMap); |
---|
[364] | 161 | #endif |
---|
[1] | 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 |
---|
[9] | 173 | if(tmpMap!=NULL) |
---|
| 174 | addToMap(tmpMap,*arrayStep,value); |
---|
[1] | 175 | else |
---|
[9] | 176 | tmpMap=createMap(*arrayStep,value); |
---|
[1] | 177 | arrayStep++; |
---|
[9] | 178 | delete[]value; |
---|
[1] | 179 | } |
---|
| 180 | cgiStringArrayFree(array); |
---|
| 181 | } |
---|
| 182 | |
---|
[458] | 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 | fclose(lf); |
---|
| 193 | addToMap(tmpMap,"request",buffer); |
---|
| 194 | free(buffer); |
---|
| 195 | cgiContentLength=flen+9; |
---|
| 196 | } |
---|
| 197 | #endif |
---|
[1] | 198 | /** |
---|
| 199 | * In case that the POST method was used, then check if params came in XML |
---|
[9] | 200 | * format else try to use the attribute "request" which should be the only |
---|
| 201 | * one. |
---|
[1] | 202 | */ |
---|
[10] | 203 | if(strncasecmp(cgiRequestMethod,"post",4)==0 || |
---|
[458] | 204 | (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0) |
---|
| 205 | #ifdef WIN32 |
---|
| 206 | ||tmpReq!=NULL |
---|
| 207 | #endif |
---|
| 208 | ){ |
---|
[1] | 209 | /** |
---|
| 210 | * Store the original XML request in xrequest map |
---|
| 211 | */ |
---|
[9] | 212 | map* t1=getMap(tmpMap,"request"); |
---|
[331] | 213 | if(t1!=NULL && strncasecmp(t1->value,"<",1)==0){ |
---|
[9] | 214 | addToMap(tmpMap,"xrequest",t1->value); |
---|
[1] | 215 | xmlInitParser(); |
---|
[9] | 216 | xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength); |
---|
[280] | 217 | { |
---|
| 218 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*"); |
---|
| 219 | if(reqptr!=NULL){ |
---|
| 220 | xmlNodeSet* req=reqptr->nodesetval; |
---|
| 221 | if(req!=NULL && req->nodeNr==1){ |
---|
| 222 | addToMap(tmpMap,"soap","true"); |
---|
[465] | 223 | for(int k=0;k < req->nodeNr;k++){ |
---|
| 224 | //xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi"); |
---|
[280] | 225 | xmlDocSetRootElement(doc, req->nodeTab[k]); |
---|
| 226 | xmlChar *xmlbuff; |
---|
| 227 | int buffersize; |
---|
| 228 | xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1); |
---|
| 229 | addToMap(tmpMap,"xrequest",(char*)xmlbuff); |
---|
| 230 | xmlFree(xmlbuff); |
---|
| 231 | } |
---|
| 232 | } |
---|
[490] | 233 | xmlXPathFreeObject(reqptr); |
---|
[280] | 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[1] | 237 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
| 238 | char *tval; |
---|
| 239 | tval=NULL; |
---|
| 240 | tval = (char*) xmlGetProp(cur,BAD_CAST "service"); |
---|
[490] | 241 | if(tval!=NULL){ |
---|
[9] | 242 | addToMap(tmpMap,"service",tval); |
---|
[490] | 243 | xmlFree(tval); |
---|
| 244 | } |
---|
[1] | 245 | tval=NULL; |
---|
| 246 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
[490] | 247 | if(tval!=NULL){ |
---|
[9] | 248 | addToMap(tmpMap,"language",tval); |
---|
[490] | 249 | xmlFree(tval); |
---|
| 250 | } |
---|
[280] | 251 | const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"}; |
---|
[1] | 252 | for(int j=0;j<3;j++){ |
---|
[280] | 253 | char tt[128]; |
---|
[1] | 254 | sprintf(tt,"/*[local-name()='%s']",requests[j]); |
---|
[9] | 255 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt); |
---|
| 256 | if(reqptr!=NULL){ |
---|
| 257 | xmlNodeSet* req=reqptr->nodesetval; |
---|
[1] | 258 | #ifdef DEBUG |
---|
[9] | 259 | fprintf(stderr,"%i",req->nodeNr); |
---|
[1] | 260 | #endif |
---|
[9] | 261 | if(req!=NULL && req->nodeNr==1){ |
---|
[490] | 262 | if(t1->value!=NULL) |
---|
| 263 | free(t1->value); |
---|
[453] | 264 | t1->value=zStrdup(requests[j]); |
---|
[9] | 265 | j=2; |
---|
| 266 | } |
---|
| 267 | xmlXPathFreeObject(reqptr); |
---|
[1] | 268 | } |
---|
| 269 | } |
---|
[9] | 270 | if(strncasecmp(t1->value,"GetCapabilities",15)==0){ |
---|
| 271 | xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']"); |
---|
| 272 | xmlNodeSet* vers=versptr->nodesetval; |
---|
[1] | 273 | xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1); |
---|
[9] | 274 | addToMap(tmpMap,"version",(char*)content); |
---|
| 275 | xmlXPathFreeObject(versptr); |
---|
[1] | 276 | xmlFree(content); |
---|
| 277 | }else{ |
---|
| 278 | tval=NULL; |
---|
| 279 | tval = (char*) xmlGetProp(cur,BAD_CAST "version"); |
---|
[490] | 280 | if(tval!=NULL){ |
---|
[9] | 281 | addToMap(tmpMap,"version",tval); |
---|
[490] | 282 | xmlFree(tval); |
---|
| 283 | } |
---|
[1] | 284 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
[490] | 285 | if(tval!=NULL){ |
---|
[9] | 286 | addToMap(tmpMap,"language",tval); |
---|
[490] | 287 | xmlFree(tval); |
---|
| 288 | } |
---|
[9] | 289 | xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']"); |
---|
| 290 | if(idptr!=NULL){ |
---|
| 291 | xmlNodeSet* id=idptr->nodesetval; |
---|
| 292 | if(id!=NULL){ |
---|
| 293 | char* identifiers=NULL; |
---|
| 294 | identifiers=(char*)calloc(cgiContentLength,sizeof(char)); |
---|
| 295 | identifiers[0]=0; |
---|
| 296 | for(int k=0;k<id->nodeNr;k++){ |
---|
| 297 | xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1); |
---|
| 298 | if(strlen(identifiers)>0){ |
---|
[453] | 299 | char *tmp=zStrdup(identifiers); |
---|
[9] | 300 | snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content); |
---|
| 301 | free(tmp); |
---|
| 302 | } |
---|
| 303 | else{ |
---|
| 304 | snprintf(identifiers,xmlStrlen(content)+1,"%s",content); |
---|
| 305 | } |
---|
| 306 | xmlFree(content); |
---|
| 307 | } |
---|
| 308 | xmlXPathFreeObject(idptr); |
---|
| 309 | addToMap(tmpMap,"Identifier",identifiers); |
---|
| 310 | free(identifiers); |
---|
[1] | 311 | } |
---|
| 312 | } |
---|
| 313 | } |
---|
[9] | 314 | xmlFreeDoc(doc); |
---|
| 315 | xmlCleanupParser(); |
---|
[331] | 316 | }else{ |
---|
| 317 | freeMap(&tmpMap); |
---|
| 318 | free(tmpMap); |
---|
| 319 | tmpMap=createMap("not_valid","true"); |
---|
[1] | 320 | } |
---|
[329] | 321 | |
---|
| 322 | char *token,*saveptr; |
---|
| 323 | token=strtok_r(cgiQueryString,"&",&saveptr); |
---|
| 324 | while(token!=NULL){ |
---|
| 325 | char *token1,*saveptr1; |
---|
| 326 | char *name=NULL; |
---|
| 327 | char *value=NULL; |
---|
| 328 | token1=strtok_r(token,"=",&saveptr1); |
---|
| 329 | while(token1!=NULL){ |
---|
| 330 | if(name==NULL) |
---|
[453] | 331 | name=zStrdup(token1); |
---|
[329] | 332 | else |
---|
[453] | 333 | value=zStrdup(token1); |
---|
[329] | 334 | token1=strtok_r(NULL,"=",&saveptr1); |
---|
| 335 | } |
---|
[331] | 336 | addToMap(tmpMap,name,value); |
---|
[329] | 337 | free(name); |
---|
| 338 | free(value); |
---|
[331] | 339 | name=NULL; |
---|
| 340 | value=NULL; |
---|
[329] | 341 | token=strtok_r(NULL,"&",&saveptr); |
---|
| 342 | } |
---|
| 343 | |
---|
[1] | 344 | } |
---|
| 345 | |
---|
[331] | 346 | if(strncasecmp(cgiContentType,"multipart/form-data",19)==0){ |
---|
[376] | 347 | map* tmp=getMap(tmpMap,"dataInputs"); |
---|
| 348 | if(tmp!=NULL){ |
---|
| 349 | addToMap(tmpMap,"dataInputs",strstr(strQuery,"dataInputs=")+11); |
---|
[331] | 350 | } |
---|
[376] | 351 | } |
---|
[331] | 352 | |
---|
[381] | 353 | if(strQuery!=NULL) |
---|
| 354 | free(strQuery); |
---|
[9] | 355 | |
---|
[490] | 356 | runRequest(&tmpMap); |
---|
| 357 | |
---|
[516] | 358 | if(tmpMap!=NULL){ |
---|
[9] | 359 | freeMap(&tmpMap); |
---|
| 360 | free(tmpMap); |
---|
| 361 | } |
---|
[1] | 362 | return 0; |
---|
| 363 | |
---|
| 364 | } |
---|