[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 | |
---|
[216] | 28 | #ifdef WIN32 |
---|
| 29 | #include "windows.h" |
---|
| 30 | #endif |
---|
[1] | 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> |
---|
[9] | 39 | #include "service_internal.h" |
---|
| 40 | |
---|
[1] | 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 | |
---|
[280] | 50 | #include "service_internal.h" |
---|
| 51 | |
---|
[114] | 52 | xmlXPathObjectPtr extractFromDoc(xmlDocPtr,const char*); |
---|
[9] | 53 | int runRequest(map*); |
---|
| 54 | |
---|
| 55 | using namespace std; |
---|
| 56 | |
---|
| 57 | /* ************************************************************************* */ |
---|
| 58 | |
---|
| 59 | int errorException(maps *m, const char *message, const char *errorcode) |
---|
| 60 | { |
---|
[32] | 61 | map* errormap = createMap("text", message); |
---|
[9] | 62 | addToMap(errormap,"code", errorcode); |
---|
| 63 | printExceptionReportResponse(m,errormap); |
---|
| 64 | freeMap(&errormap); |
---|
| 65 | free(errormap); |
---|
| 66 | return -1; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /* ************************************************************************* */ |
---|
| 70 | |
---|
[1] | 71 | |
---|
[9] | 72 | #define TRUE 1 |
---|
[1] | 73 | #define FALSE -1 |
---|
| 74 | |
---|
| 75 | int cgiMain(){ |
---|
| 76 | /** |
---|
| 77 | * We'll use cgiOut as the default output (stdout) to produce plain text |
---|
| 78 | * response. |
---|
| 79 | */ |
---|
| 80 | dup2(fileno(cgiOut),fileno(stdout)); |
---|
| 81 | #ifdef DEBUG |
---|
[9] | 82 | fprintf(cgiOut,"Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n"); |
---|
| 83 | fprintf(cgiOut,"Welcome on ZOO verbose debuging mode \r\n\r\n"); |
---|
| 84 | fflush(cgiOut); |
---|
[1] | 85 | #endif |
---|
| 86 | |
---|
| 87 | #ifdef DEBUG |
---|
[9] | 88 | fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); |
---|
[280] | 89 | fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,strncasecmp(cgiRequestMethod,"post",4),strncmp(cgiContentType,"text/xml",8)==0 || strncasecmp(cgiRequestMethod,"post",4)==0); |
---|
[9] | 90 | fprintf (stderr, "Request: %s\n", cgiQueryString); |
---|
[1] | 91 | #endif |
---|
| 92 | |
---|
[9] | 93 | map* tmpMap=NULL; |
---|
[69] | 94 | |
---|
[99] | 95 | if(strncmp(cgiContentType,"text/xml",8)==0 || |
---|
| 96 | strncasecmp(cgiRequestMethod,"post",4)==0){ |
---|
[280] | 97 | if(cgiContentLength==NULL){ |
---|
| 98 | cgiContentLength=0; |
---|
| 99 | char *buffer=new char[2]; |
---|
| 100 | char *res=NULL; |
---|
| 101 | int r=0; |
---|
| 102 | while(r=fread(buffer,sizeof(char),1,cgiIn)){ |
---|
| 103 | cgiContentLength+=r; |
---|
| 104 | if(res==NULL){ |
---|
| 105 | res=(char*)malloc(1*sizeof(char)); |
---|
| 106 | sprintf(res,"%s",buffer); |
---|
| 107 | } |
---|
| 108 | else{ |
---|
| 109 | res=(char*)realloc(res,(cgiContentLength+1)*sizeof(char)); |
---|
| 110 | char *tmp=strdup(res); |
---|
| 111 | sprintf(res,"%s%s",tmp,buffer); |
---|
| 112 | free(tmp); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | tmpMap=createMap("request",res); |
---|
[1] | 116 | }else{ |
---|
[280] | 117 | char *buffer=new char[cgiContentLength+1]; |
---|
| 118 | if(fread(buffer,sizeof(char),cgiContentLength,cgiIn)){ |
---|
| 119 | buffer[cgiContentLength]=0; |
---|
| 120 | tmpMap=createMap("request",buffer); |
---|
| 121 | dumpMap(tmpMap); |
---|
| 122 | }else{ |
---|
| 123 | buffer[0]=0; |
---|
| 124 | char **array, **arrayStep; |
---|
| 125 | if (cgiFormEntries(&array) != cgiFormSuccess) { |
---|
| 126 | return 1; |
---|
| 127 | } |
---|
| 128 | arrayStep = array; |
---|
| 129 | while (*arrayStep) { |
---|
| 130 | char *ivalue=new char[cgiContentLength]; |
---|
| 131 | cgiFormStringNoNewlines(*arrayStep, ivalue, cgiContentLength); |
---|
| 132 | char* tmpValueFinal=(char*) malloc((strlen(*arrayStep)+strlen(ivalue)+1)*sizeof(char)); |
---|
| 133 | sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue); |
---|
| 134 | if(strlen(buffer)==0){ |
---|
| 135 | sprintf(buffer,"%s",tmpValueFinal); |
---|
| 136 | }else{ |
---|
| 137 | char *tmp=strdup(buffer); |
---|
| 138 | sprintf(buffer,"%s&%s",tmp,tmpValueFinal); |
---|
| 139 | free(tmp); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | sprintf(tmpValueFinal,"%s=%s",*arrayStep,ivalue); |
---|
| 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 | } |
---|
| 150 | tmpMap=createMap("request",buffer); |
---|
[99] | 151 | } |
---|
[280] | 152 | delete[]buffer; |
---|
[1] | 153 | } |
---|
| 154 | } |
---|
| 155 | else{ |
---|
| 156 | char **array, **arrayStep; |
---|
| 157 | if (cgiFormEntries(&array) != cgiFormSuccess) { |
---|
| 158 | return 1; |
---|
| 159 | } |
---|
| 160 | arrayStep = array; |
---|
| 161 | while (*arrayStep) { |
---|
| 162 | char *value=new char[cgiContentLength]; |
---|
| 163 | cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength); |
---|
| 164 | #ifdef DEBUG |
---|
| 165 | fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value); |
---|
| 166 | #endif |
---|
[9] | 167 | if(tmpMap!=NULL) |
---|
| 168 | addToMap(tmpMap,*arrayStep,value); |
---|
[1] | 169 | else |
---|
[9] | 170 | tmpMap=createMap(*arrayStep,value); |
---|
[1] | 171 | arrayStep++; |
---|
[9] | 172 | delete[]value; |
---|
[1] | 173 | } |
---|
| 174 | cgiStringArrayFree(array); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * In case that the POST method was used, then check if params came in XML |
---|
[9] | 179 | * format else try to use the attribute "request" which should be the only |
---|
| 180 | * one. |
---|
[1] | 181 | */ |
---|
[10] | 182 | if(strncasecmp(cgiRequestMethod,"post",4)==0 || |
---|
| 183 | (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){ |
---|
[1] | 184 | /** |
---|
| 185 | * First include the MetaPath and the ServiceProvider default parameters |
---|
| 186 | * (which should be always available in GET params so in cgiQueryString) |
---|
| 187 | */ |
---|
[19] | 188 | char *str1; |
---|
[1] | 189 | str1=cgiQueryString; |
---|
| 190 | /** |
---|
| 191 | * Store the original XML request in xrequest map |
---|
| 192 | */ |
---|
[9] | 193 | map* t1=getMap(tmpMap,"request"); |
---|
[1] | 194 | if(t1!=NULL){ |
---|
[9] | 195 | addToMap(tmpMap,"xrequest",t1->value); |
---|
[1] | 196 | xmlInitParser(); |
---|
[9] | 197 | xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength); |
---|
[280] | 198 | |
---|
| 199 | |
---|
| 200 | { |
---|
| 201 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,"/*[local-name()='Envelope']/*[local-name()='Body']/*"); |
---|
| 202 | if(reqptr!=NULL){ |
---|
| 203 | xmlNodeSet* req=reqptr->nodesetval; |
---|
| 204 | if(req!=NULL && req->nodeNr==1){ |
---|
| 205 | addToMap(tmpMap,"soap","true"); |
---|
| 206 | int k=0; |
---|
| 207 | for(k;k < req->nodeNr;k++){ |
---|
| 208 | xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi"); |
---|
| 209 | xmlDocSetRootElement(doc, req->nodeTab[k]); |
---|
| 210 | xmlChar *xmlbuff; |
---|
| 211 | int buffersize; |
---|
| 212 | xmlDocDumpFormatMemoryEnc(doc, &xmlbuff, &buffersize, "utf-8", 1); |
---|
| 213 | addToMap(tmpMap,"xrequest",(char*)xmlbuff); |
---|
| 214 | char *tmp=(char*)xmlbuff; |
---|
| 215 | fprintf(stderr,"%s\n",tmp); |
---|
| 216 | xmlFree(xmlbuff); |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
[1] | 222 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
| 223 | char *tval; |
---|
| 224 | tval=NULL; |
---|
| 225 | tval = (char*) xmlGetProp(cur,BAD_CAST "service"); |
---|
| 226 | if(tval!=NULL) |
---|
[9] | 227 | addToMap(tmpMap,"service",tval); |
---|
[1] | 228 | tval=NULL; |
---|
| 229 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
| 230 | if(tval!=NULL) |
---|
[9] | 231 | addToMap(tmpMap,"language",tval); |
---|
[280] | 232 | const char* requests[3]={"GetCapabilities","DescribeProcess","Execute"}; |
---|
[1] | 233 | for(int j=0;j<3;j++){ |
---|
[280] | 234 | char tt[128]; |
---|
[1] | 235 | sprintf(tt,"/*[local-name()='%s']",requests[j]); |
---|
[9] | 236 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt); |
---|
| 237 | if(reqptr!=NULL){ |
---|
| 238 | xmlNodeSet* req=reqptr->nodesetval; |
---|
[1] | 239 | #ifdef DEBUG |
---|
[9] | 240 | fprintf(stderr,"%i",req->nodeNr); |
---|
[1] | 241 | #endif |
---|
[9] | 242 | if(req!=NULL && req->nodeNr==1){ |
---|
[114] | 243 | t1->value=strdup(requests[j]); |
---|
[9] | 244 | j=2; |
---|
| 245 | } |
---|
| 246 | xmlXPathFreeObject(reqptr); |
---|
[1] | 247 | } |
---|
[9] | 248 | //xmlFree(req); |
---|
[1] | 249 | } |
---|
[9] | 250 | if(strncasecmp(t1->value,"GetCapabilities",15)==0){ |
---|
| 251 | xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']"); |
---|
| 252 | xmlNodeSet* vers=versptr->nodesetval; |
---|
[1] | 253 | xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1); |
---|
[9] | 254 | addToMap(tmpMap,"version",(char*)content); |
---|
| 255 | xmlXPathFreeObject(versptr); |
---|
| 256 | //xmlFree(vers); |
---|
[1] | 257 | xmlFree(content); |
---|
| 258 | }else{ |
---|
| 259 | tval=NULL; |
---|
| 260 | tval = (char*) xmlGetProp(cur,BAD_CAST "version"); |
---|
| 261 | if(tval!=NULL) |
---|
[9] | 262 | addToMap(tmpMap,"version",tval); |
---|
[1] | 263 | xmlFree(tval); |
---|
| 264 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
| 265 | if(tval!=NULL) |
---|
[9] | 266 | addToMap(tmpMap,"language",tval); |
---|
| 267 | xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']"); |
---|
| 268 | if(idptr!=NULL){ |
---|
| 269 | xmlNodeSet* id=idptr->nodesetval; |
---|
| 270 | if(id!=NULL){ |
---|
| 271 | char* identifiers=NULL; |
---|
| 272 | identifiers=(char*)calloc(cgiContentLength,sizeof(char)); |
---|
| 273 | identifiers[0]=0; |
---|
| 274 | for(int k=0;k<id->nodeNr;k++){ |
---|
| 275 | xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1); |
---|
| 276 | if(strlen(identifiers)>0){ |
---|
| 277 | char *tmp=strdup(identifiers); |
---|
| 278 | snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content); |
---|
| 279 | free(tmp); |
---|
| 280 | } |
---|
| 281 | else{ |
---|
| 282 | snprintf(identifiers,xmlStrlen(content)+1,"%s",content); |
---|
| 283 | } |
---|
| 284 | xmlFree(content); |
---|
| 285 | } |
---|
| 286 | xmlXPathFreeObject(idptr); |
---|
| 287 | addToMap(tmpMap,"Identifier",identifiers); |
---|
| 288 | free(identifiers); |
---|
[1] | 289 | } |
---|
| 290 | } |
---|
[9] | 291 | //xmlFree(id); |
---|
[1] | 292 | } |
---|
| 293 | xmlFree(tval); |
---|
[9] | 294 | xmlFreeDoc(doc); |
---|
| 295 | xmlCleanupParser(); |
---|
[1] | 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
[9] | 299 | runRequest(tmpMap); |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | * Required but can't be made after executing a process using POST requests. |
---|
| 303 | */ |
---|
| 304 | if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){ |
---|
| 305 | freeMap(&tmpMap); |
---|
| 306 | free(tmpMap); |
---|
| 307 | } |
---|
[1] | 308 | return 0; |
---|
| 309 | |
---|
| 310 | } |
---|