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