[1] | 1 | /** |
---|
| 2 | * Author : Gérald FENOY |
---|
| 3 | * |
---|
| 4 | * Copyright 2008-2009 GeoLabs SARL. All rights reserved. |
---|
| 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 | { |
---|
| 56 | map * errormap = createMap("text", message); |
---|
| 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 | /** |
---|
| 105 | * Read the main configuration file |
---|
| 106 | if(!isStarted){ |
---|
| 107 | maps* m; |
---|
| 108 | m=(maps*)malloc(sizeof(maps*)); |
---|
| 109 | conf_read("main.cfg",m); |
---|
| 110 | #ifdef REAL_FCGI |
---|
| 111 | #ifdef DEBUG |
---|
| 112 | printf("ok passed"); |
---|
| 113 | #endif |
---|
| 114 | #endif |
---|
| 115 | isStarted=TRUE; |
---|
| 116 | } |
---|
[9] | 117 | */ |
---|
[1] | 118 | |
---|
| 119 | #ifdef DEBUG |
---|
[9] | 120 | fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); |
---|
| 121 | fprintf (stderr, "RequestMethod:%s\n", cgiRequestMethod); |
---|
| 122 | fprintf (stderr, "Request: %s\n", cgiQueryString); |
---|
[1] | 123 | #endif |
---|
| 124 | |
---|
[9] | 125 | map* tmpMap=NULL; |
---|
[1] | 126 | |
---|
| 127 | if(strncmp(cgiContentType,"text/xml",8)==0){ |
---|
[9] | 128 | char *buffer=new char[cgiContentLength+1]; |
---|
[1] | 129 | if(fread(buffer,1,cgiContentLength,cgiIn)){ |
---|
| 130 | buffer[cgiContentLength]=0; |
---|
[9] | 131 | tmpMap=createMap("request",buffer); |
---|
[1] | 132 | }else{ |
---|
| 133 | /* Here we have to return an error message ... */ |
---|
[9] | 134 | fprintf(stderr, "Unable to read cgi content in zoo_loader.c line %i\n", __LINE__); |
---|
[1] | 135 | return 1; |
---|
| 136 | } |
---|
[9] | 137 | delete[]buffer; |
---|
[1] | 138 | } |
---|
| 139 | else{ |
---|
| 140 | char **array, **arrayStep; |
---|
| 141 | if (cgiFormEntries(&array) != cgiFormSuccess) { |
---|
| 142 | return 1; |
---|
| 143 | } |
---|
| 144 | arrayStep = array; |
---|
| 145 | while (*arrayStep) { |
---|
| 146 | char *value=new char[cgiContentLength]; |
---|
| 147 | cgiFormStringNoNewlines(*arrayStep, value, cgiContentLength); |
---|
| 148 | #ifdef DEBUG |
---|
| 149 | fprintf(stderr,"(( \n %s \n %s \n ))",*arrayStep,value); |
---|
| 150 | #endif |
---|
[9] | 151 | if(tmpMap!=NULL) |
---|
| 152 | addToMap(tmpMap,*arrayStep,value); |
---|
[1] | 153 | else |
---|
[9] | 154 | tmpMap=createMap(*arrayStep,value); |
---|
[1] | 155 | arrayStep++; |
---|
[9] | 156 | delete[]value; |
---|
[1] | 157 | } |
---|
| 158 | cgiStringArrayFree(array); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * In case that the POST method was used, then check if params came in XML |
---|
[9] | 163 | * format else try to use the attribute "request" which should be the only |
---|
| 164 | * one. |
---|
[1] | 165 | */ |
---|
[10] | 166 | fprintf(stderr,"%i %i",tmpMap->value[0],'<'); |
---|
| 167 | if(strncasecmp(cgiRequestMethod,"post",4)==0 || |
---|
| 168 | (count(tmpMap)==1 && strncmp(tmpMap->value,"<",1)==0)){ |
---|
[1] | 169 | /** |
---|
| 170 | * First include the MetaPath and the ServiceProvider default parameters |
---|
| 171 | * (which should be always available in GET params so in cgiQueryString) |
---|
| 172 | */ |
---|
[9] | 173 | //char *saveptr1, *saveptr2; |
---|
| 174 | char *str1/*, str2, *token, *subtoken*/; |
---|
[1] | 175 | str1=cgiQueryString; |
---|
| 176 | /** |
---|
| 177 | * Store the original XML request in xrequest map |
---|
| 178 | */ |
---|
[9] | 179 | map* t1=getMap(tmpMap,"request"); |
---|
[1] | 180 | if(t1!=NULL){ |
---|
[9] | 181 | addToMap(tmpMap,"xrequest",t1->value); |
---|
[1] | 182 | xmlInitParser(); |
---|
[9] | 183 | xmlDocPtr doc = xmlParseMemory(t1->value,cgiContentLength); |
---|
[1] | 184 | xmlNodePtr cur = xmlDocGetRootElement(doc); |
---|
| 185 | char *tval; |
---|
| 186 | tval=NULL; |
---|
| 187 | tval = (char*) xmlGetProp(cur,BAD_CAST "service"); |
---|
| 188 | if(tval!=NULL) |
---|
[9] | 189 | addToMap(tmpMap,"service",tval); |
---|
[1] | 190 | tval=NULL; |
---|
| 191 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
| 192 | if(tval!=NULL) |
---|
[9] | 193 | addToMap(tmpMap,"language",tval); |
---|
[1] | 194 | |
---|
| 195 | char* requests[3]; |
---|
| 196 | requests[0]="GetCapabilities"; |
---|
| 197 | requests[1]="DescribeProcess"; |
---|
| 198 | requests[2]="Execute"; |
---|
| 199 | for(int j=0;j<3;j++){ |
---|
| 200 | char tt[35]; |
---|
| 201 | sprintf(tt,"/*[local-name()='%s']",requests[j]); |
---|
[9] | 202 | xmlXPathObjectPtr reqptr=extractFromDoc(doc,tt); |
---|
| 203 | if(reqptr!=NULL){ |
---|
| 204 | xmlNodeSet* req=reqptr->nodesetval; |
---|
[1] | 205 | #ifdef DEBUG |
---|
[9] | 206 | fprintf(stderr,"%i",req->nodeNr); |
---|
[1] | 207 | #endif |
---|
[9] | 208 | if(req!=NULL && req->nodeNr==1){ |
---|
| 209 | t1->value=requests[j]; |
---|
| 210 | j=2; |
---|
| 211 | } |
---|
| 212 | xmlXPathFreeObject(reqptr); |
---|
[1] | 213 | } |
---|
[9] | 214 | //xmlFree(req); |
---|
[1] | 215 | } |
---|
[9] | 216 | if(strncasecmp(t1->value,"GetCapabilities",15)==0){ |
---|
| 217 | xmlXPathObjectPtr versptr=extractFromDoc(doc,"/*/*/*[local-name()='Version']"); |
---|
| 218 | xmlNodeSet* vers=versptr->nodesetval; |
---|
[1] | 219 | xmlChar* content=xmlNodeListGetString(doc, vers->nodeTab[0]->xmlChildrenNode,1); |
---|
[9] | 220 | addToMap(tmpMap,"version",(char*)content); |
---|
| 221 | xmlXPathFreeObject(versptr); |
---|
| 222 | //xmlFree(vers); |
---|
[1] | 223 | xmlFree(content); |
---|
| 224 | }else{ |
---|
| 225 | tval=NULL; |
---|
| 226 | tval = (char*) xmlGetProp(cur,BAD_CAST "version"); |
---|
| 227 | if(tval!=NULL) |
---|
[9] | 228 | addToMap(tmpMap,"version",tval); |
---|
[1] | 229 | xmlFree(tval); |
---|
| 230 | tval = (char*) xmlGetProp(cur,BAD_CAST "language"); |
---|
| 231 | if(tval!=NULL) |
---|
[9] | 232 | addToMap(tmpMap,"language",tval); |
---|
| 233 | xmlXPathObjectPtr idptr=extractFromDoc(doc,"/*/*[local-name()='Identifier']"); |
---|
| 234 | if(idptr!=NULL){ |
---|
| 235 | xmlNodeSet* id=idptr->nodesetval; |
---|
| 236 | if(id!=NULL){ |
---|
| 237 | char* identifiers=NULL; |
---|
| 238 | identifiers=(char*)calloc(cgiContentLength,sizeof(char)); |
---|
| 239 | identifiers[0]=0; |
---|
| 240 | for(int k=0;k<id->nodeNr;k++){ |
---|
| 241 | xmlChar* content=xmlNodeListGetString(doc, id->nodeTab[k]->xmlChildrenNode,1); |
---|
| 242 | if(strlen(identifiers)>0){ |
---|
| 243 | char *tmp=strdup(identifiers); |
---|
| 244 | snprintf(identifiers,strlen(tmp)+xmlStrlen(content)+2,"%s,%s",tmp,content); |
---|
| 245 | free(tmp); |
---|
| 246 | } |
---|
| 247 | else{ |
---|
| 248 | snprintf(identifiers,xmlStrlen(content)+1,"%s",content); |
---|
| 249 | } |
---|
| 250 | xmlFree(content); |
---|
| 251 | } |
---|
| 252 | xmlXPathFreeObject(idptr); |
---|
| 253 | addToMap(tmpMap,"Identifier",identifiers); |
---|
| 254 | free(identifiers); |
---|
[1] | 255 | } |
---|
| 256 | } |
---|
[9] | 257 | //xmlFree(id); |
---|
[1] | 258 | } |
---|
| 259 | xmlFree(tval); |
---|
[9] | 260 | xmlFreeDoc(doc); |
---|
| 261 | xmlCleanupParser(); |
---|
[1] | 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[9] | 265 | runRequest(tmpMap); |
---|
| 266 | |
---|
| 267 | /** |
---|
| 268 | * Required but can't be made after executing a process using POST requests. |
---|
| 269 | */ |
---|
| 270 | if(strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && tmpMap!=NULL){ |
---|
| 271 | freeMap(&tmpMap); |
---|
| 272 | free(tmpMap); |
---|
| 273 | } |
---|
[1] | 274 | return 0; |
---|
| 275 | |
---|
| 276 | } |
---|