source: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c @ 512

Last change on this file since 512 was 512, checked in by david, 9 years ago

-loading files configuration on startup
-devel version with probably a lot of leak
-using temporarily glib to store services

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 16.8 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 *  Copyright 2008-2011 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
25#define MALLOC_CHECK_ 0
26#define MALLOC_CHECK 0
27
28/**
29 * Specific includes
30 */
31#ifndef WIN32
32#include "fcgio.h"
33#include "fcgi_config.h"
34#include "fcgi_stdio.h"
35#endif
36#include <sys/types.h>
37#include <unistd.h>
38#include "service_internal.h"
39#ifdef WIN32
40#include "windows.h"
41#define strtok_r strtok_s
42#endif
43
44extern "C"
45{
46#include "cgic.h"
47#include <libxml/tree.h>
48#include <libxml/xmlmemory.h>
49#include <libxml/parser.h>
50#include <libxml/xpath.h>
51#include <libxml/xpathInternals.h>
52
53#include <string.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <glib.h>
57#include <sys/stat.h>
58#include "service_zcfg.h"
59}
60
61#include "service_internal.h"
62
63xmlXPathObjectPtr extractFromDoc (xmlDocPtr, const char *);
64int runRequest (map **);
65
66using namespace std;
67
68#ifndef TRUE
69#define TRUE 1
70#endif
71#ifndef FALSE
72#define FALSE -1
73#endif
74
75
76int
77main (int argc, char *argv[])
78{
79  maps *m;
80  m = (maps *) malloc (MAP_SIZE);
81  conf_read ("main.cfg", m);
82  char ntmp[1024];
83#ifndef WIN32
84  getcwd (ntmp, 1024);
85#else
86  _getcwd (ntmp, 1024);
87#endif
88  char *rootDir = "/var/www/zoo-wps/cgi-bin";
89  init_services_conf (rootDir);
90
91
92  while (FCGI_Accept () >= 0)
93    {
94      cgiMain_init (argc, argv);
95/**
96   * We'll use cgiOut as the default output (stdout) to produce plain text
97   * response.
98   */
99      dup2 (fileno (cgiOut), fileno (stdout));
100
101#ifdef DEBUG
102      fprintf (cgiOut,
103               "Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n");
104      fprintf (cgiOut, "Welcome on ZOO verbose debuging mode \r\n\r\n");
105      fflush (cgiOut);
106      fprintf (stderr, "Addr:%s\n", cgiRemoteAddr);
107      fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod,
108               strncasecmp (cgiRequestMethod, "post", 4),
109               strncmp (cgiContentType, "text/xml", 8) == 0
110               || strncasecmp (cgiRequestMethod, "post", 4) == 0);
111      fprintf (stderr, "Request: %s\n", cgiQueryString);
112      fprintf (stderr, "ContentType: %s\n", cgiContentType);
113      fprintf (stderr, "ContentLength: %d\n", cgiContentLength);
114      fflush (stderr);
115#endif
116
117
118      char *strQuery = NULL;
119      if (cgiQueryString != NULL)
120        strQuery = zStrdup (cgiQueryString);
121      map *tmpMap = NULL;
122
123      if (strncmp (cgiContentType, "text/xml", 8) == 0 ||
124          strncasecmp (cgiRequestMethod, "post", 4) == 0)
125        {
126          if (cgiContentLength == 0)
127            {
128              char *buffer = new char[2];
129              char *res = NULL;
130              int r = 0;
131              while ((r = fread (buffer, sizeof (char), 1, cgiIn)))
132                {
133                  buffer[1] = 0;
134                  cgiContentLength += r;
135                  if (res == NULL)
136                    {
137                      res = (char *) malloc (2 * sizeof (char));
138                      sprintf (res, "%s", buffer);
139                    }
140                  else
141                    {
142                      char *tmp = zStrdup (res);
143                      res =
144                        (char *) realloc (res,
145                                          (strlen (tmp) + 2) * sizeof (char));
146                      sprintf (res, "%s%s", tmp, buffer);
147                      free (tmp);
148                    }
149                }
150              delete[]buffer;
151              if (res == NULL && (strQuery == NULL || strlen (strQuery) == 0))
152                {
153                  return errorException (NULL,
154                                         "ZOO-Kernel failed to process your request cause the request was emtpty.",
155                                         "InternalError", NULL);
156                }
157              else
158                {
159                  if (strQuery == NULL || strlen (strQuery) == 0)
160                    tmpMap = createMap ("request", res);
161                }
162              if (res != NULL)
163                free (res);
164            }
165          else
166            {
167              char *buffer = new char[cgiContentLength + 1];
168              if (fread (buffer, sizeof (char), cgiContentLength, cgiIn) > 0)
169                {
170                  buffer[cgiContentLength] = 0;
171                  tmpMap = createMap ("request", buffer);
172                }
173              else
174                {
175                  buffer[0] = 0;
176                  char **array, **arrayStep;
177                  if (cgiFormEntries (&array) != cgiFormSuccess)
178                    {
179                      return 1;
180                    }
181                  arrayStep = array;
182                  while (*arrayStep)
183                    {
184                      char *ivalue = new char[cgiContentLength];
185                      cgiFormStringNoNewlines (*arrayStep, ivalue,
186                                               cgiContentLength);
187                      char *tmpValueFinal =
188                        (char *)
189                        malloc ((strlen (*arrayStep) + strlen (ivalue) +
190                                 1) * sizeof (char));
191                      sprintf (tmpValueFinal, "%s=%s", *arrayStep, ivalue);
192                      if (strlen (buffer) == 0)
193                        {
194                          sprintf (buffer, "%s", tmpValueFinal);
195                        }
196                      else
197                        {
198                          char *tmp = zStrdup (buffer);
199                          sprintf (buffer, "%s&%s", tmp, tmpValueFinal);
200                          free (tmp);
201                        }
202                      free (tmpValueFinal);
203#ifdef DEBUG
204                      fprintf (stderr, "(( \n %s \n %s \n ))", *arrayStep,
205                               ivalue);
206#endif
207                      delete[]ivalue;
208                      arrayStep++;
209                    }
210                  if (tmpMap != NULL)
211                    addToMap (tmpMap, "request", buffer);
212                  else
213                    tmpMap = createMap ("request", buffer);
214                }
215              delete[]buffer;
216            }
217        }
218      else
219        {
220#ifdef DEBUG
221          dumpMap (tmpMap);
222#endif
223          char **array, **arrayStep;
224          if (cgiFormEntries (&array) != cgiFormSuccess)
225            {
226              return 1;
227            }
228          arrayStep = array;
229          while (*arrayStep)
230            {
231              char *value = new char[cgiContentLength];
232              cgiFormStringNoNewlines (*arrayStep, value, cgiContentLength);
233#ifdef DEBUG
234              fprintf (stderr, "(( \n %s \n %s \n ))", *arrayStep, value);
235#endif
236              if (tmpMap != NULL)
237                addToMap (tmpMap, *arrayStep, value);
238              else
239                tmpMap = createMap (*arrayStep, value);
240              arrayStep++;
241              delete[]value;
242            }
243          cgiStringArrayFree (array);
244        }
245
246#ifdef WIN32
247      map *tmpReq = getMap (tmpMap, "rfile");
248      if (tmpReq != NULL)
249        {
250          FILE *lf = fopen (tmpReq->value, "r");
251          fseek (lf, 0, SEEK_END);
252          long flen = ftell (lf);
253          fseek (lf, 0, SEEK_SET);
254          char *buffer = (char *) malloc ((flen + 1) * sizeof (char));
255          fread (buffer, flen, 1, lf);
256          fclose (lf);
257          addToMap (tmpMap, "request", buffer);
258          free (buffer);
259          cgiContentLength = flen + 9;
260        }
261#endif
262  /**
263   * In case that the POST method was used, then check if params came in XML
264   * format else try to use the attribute "request" which should be the only
265   * one.
266   */
267      if (strncasecmp (cgiRequestMethod, "post", 4) == 0 ||
268          (count (tmpMap) == 1 && strncmp (tmpMap->value, "<", 1) == 0)
269#ifdef WIN32
270          || tmpReq != NULL
271#endif
272        )
273        {
274    /**
275     * Store the original XML request in xrequest map
276     */
277          map *t1 = getMap (tmpMap, "request");
278          if (t1 != NULL && strncasecmp (t1->value, "<", 1) == 0)
279            {
280              addToMap (tmpMap, "xrequest", t1->value);
281              xmlInitParser ();
282              xmlDocPtr doc = xmlParseMemory (t1->value, cgiContentLength);
283              {
284                xmlXPathObjectPtr reqptr = extractFromDoc (doc,
285                                                           "/*[local-name()='Envelope']/*[local-name()='Body']/*");
286                if (reqptr != NULL)
287                  {
288                    xmlNodeSet *req = reqptr->nodesetval;
289                    if (req != NULL && req->nodeNr == 1)
290                      {
291                        addToMap (tmpMap, "soap", "true");
292                        for (int k = 0; k < req->nodeNr; k++)
293                          {
294                            //xmlNsPtr ns=xmlNewNs(req->nodeTab[k],BAD_CAST "http://www.w3.org/2001/XMLSchema-instance",BAD_CAST "xsi");
295                            xmlDocSetRootElement (doc, req->nodeTab[k]);
296                            xmlChar *xmlbuff;
297                            int buffersize;
298                            xmlDocDumpFormatMemoryEnc (doc, &xmlbuff,
299                                                       &buffersize, "utf-8",
300                                                       1);
301                            addToMap (tmpMap, "xrequest", (char *) xmlbuff);
302                            xmlFree (xmlbuff);
303                          }
304                      }
305                    xmlXPathFreeObject (reqptr);
306                  }
307              }
308
309              xmlNodePtr cur = xmlDocGetRootElement (doc);
310              char *tval;
311              tval = NULL;
312              tval = (char *) xmlGetProp (cur, BAD_CAST "service");
313              if (tval != NULL)
314                {
315                  addToMap (tmpMap, "service", tval);
316                  xmlFree (tval);
317                }
318              tval = NULL;
319              tval = (char *) xmlGetProp (cur, BAD_CAST "language");
320              if (tval != NULL)
321                {
322                  addToMap (tmpMap, "language", tval);
323                  xmlFree (tval);
324                }
325              const char *requests[3] =
326                { "GetCapabilities", "DescribeProcess", "Execute" };
327              for (int j = 0; j < 3; j++)
328                {
329                  char tt[128];
330                  sprintf (tt, "/*[local-name()='%s']", requests[j]);
331                  xmlXPathObjectPtr reqptr = extractFromDoc (doc, tt);
332                  if (reqptr != NULL)
333                    {
334                      xmlNodeSet *req = reqptr->nodesetval;
335#ifdef DEBUG
336                      fprintf (stderr, "%i", req->nodeNr);
337#endif
338                      if (req != NULL && req->nodeNr == 1)
339                        {
340                          if (t1->value != NULL)
341                            free (t1->value);
342                          t1->value = zStrdup (requests[j]);
343                          j = 2;
344                        }
345                      xmlXPathFreeObject (reqptr);
346                    }
347                }
348              if (strncasecmp (t1->value, "GetCapabilities", 15) == 0)
349                {
350                  xmlXPathObjectPtr versptr =
351                    extractFromDoc (doc, "/*/*/*[local-name()='Version']");
352                  xmlNodeSet *vers = versptr->nodesetval;
353                  xmlChar *content = xmlNodeListGetString (doc,
354                                                           vers->
355                                                           nodeTab
356                                                           [0]->xmlChildrenNode,
357                                                           1);
358                  addToMap (tmpMap, "version", (char *) content);
359                  xmlXPathFreeObject (versptr);
360                  xmlFree (content);
361                }
362              else
363                {
364                  tval = NULL;
365                  tval = (char *) xmlGetProp (cur, BAD_CAST "version");
366                  if (tval != NULL)
367                    {
368                      addToMap (tmpMap, "version", tval);
369                      xmlFree (tval);
370                    }
371                  tval = (char *) xmlGetProp (cur, BAD_CAST "language");
372                  if (tval != NULL)
373                    {
374                      addToMap (tmpMap, "language", tval);
375                      xmlFree (tval);
376                    }
377                  xmlXPathObjectPtr idptr =
378                    extractFromDoc (doc, "/*/*[local-name()='Identifier']");
379                  if (idptr != NULL)
380                    {
381                      xmlNodeSet *id = idptr->nodesetval;
382                      if (id != NULL)
383                        {
384                          char *identifiers = NULL;
385                          identifiers =
386                            (char *) calloc (cgiContentLength, sizeof (char));
387                          identifiers[0] = 0;
388                          for (int k = 0; k < id->nodeNr; k++)
389                            {
390                              xmlChar *content = xmlNodeListGetString (doc,
391                                                                       id->nodeTab
392                                                                       [k]->
393                                                                       xmlChildrenNode,
394                                                                       1);
395                              if (strlen (identifiers) > 0)
396                                {
397                                  char *tmp = zStrdup (identifiers);
398                                  snprintf (identifiers,
399                                            strlen (tmp) +
400                                            xmlStrlen (content) + 2, "%s,%s",
401                                            tmp, content);
402                                  free (tmp);
403                                }
404                              else
405                                {
406                                  snprintf (identifiers,
407                                            xmlStrlen (content) + 1, "%s",
408                                            content);
409                                }
410                              xmlFree (content);
411                            }
412                          xmlXPathFreeObject (idptr);
413                          addToMap (tmpMap, "Identifier", identifiers);
414                          free (identifiers);
415                        }
416                    }
417                }
418              xmlFreeDoc (doc);
419              xmlCleanupParser ();
420            }
421          else
422            {
423              freeMap (&tmpMap);
424              free (tmpMap);
425              tmpMap = createMap ("not_valid", "true");
426            }
427
428          char *token, *saveptr;
429          token = strtok_r (cgiQueryString, "&", &saveptr);
430          while (token != NULL)
431            {
432              char *token1, *saveptr1;
433              char *name = NULL;
434              char *value = NULL;
435              token1 = strtok_r (token, "=", &saveptr1);
436              while (token1 != NULL)
437                {
438                  if (name == NULL)
439                    name = zStrdup (token1);
440                  else
441                    value = zStrdup (token1);
442                  token1 = strtok_r (NULL, "=", &saveptr1);
443                }
444              addToMap (tmpMap, name, value);
445              free (name);
446              free (value);
447              name = NULL;
448              value = NULL;
449              token = strtok_r (NULL, "&", &saveptr);
450            }
451
452        }
453
454      if (strncasecmp (cgiContentType, "multipart/form-data", 19) == 0)
455        {
456          map *tmp = getMap (tmpMap, "dataInputs");
457          if (tmp != NULL)
458            {
459              addToMap (tmpMap, "dataInputs",
460                        strstr (strQuery, "dataInputs=") + 11);
461            }
462        }
463
464      if (strQuery != NULL)
465        free (strQuery);
466
467      runRequest (&tmpMap);
468
469  /**
470   * Required but can't be made after executing a process using POST requests.
471   */
472      if ( /*strncasecmp(cgiRequestMethod,"post",4)!=0 && count(tmpMap)!=1 && */ tmpMap != NULL)
473        {
474          freeMap (&tmpMap);
475          free (tmpMap);
476        }
477      cgiFreeResources ();
478    }
479  FCGI_Finish ();
480  return 0;
481
482}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png