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