source: trunk/zoo-project/zoo-kernel/request_parser.c @ 654

Last change on this file since 654 was 654, checked in by djay, 9 years ago

Initial support for WPS 2.0.0 including the Dismiss extension.

  • Property svn:keywords set to Id
File size: 49.7 KB
Line 
1/*
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2015 GeoLabs SARL
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#include "request_parser.h"
26#include "service_internal.h"
27#include "server_internal.h"
28#include "response_print.h"
29#include "caching.h"
30
31/**
32 * Apply XPath Expression on XML document.
33 *
34 * @param doc the XML Document
35 * @param search the XPath expression
36 * @return xmlXPathObjectPtr containing the resulting nodes set
37 */
38xmlXPathObjectPtr
39extractFromDoc (xmlDocPtr doc, const char *search)
40{
41  xmlXPathContextPtr xpathCtx;
42  xmlXPathObjectPtr xpathObj;
43  xpathCtx = xmlXPathNewContext (doc);
44  xpathObj = xmlXPathEvalExpression (BAD_CAST search, xpathCtx);
45  xmlXPathFreeContext (xpathCtx);
46  return xpathObj;
47}
48
49/**
50 * Create (or append to) an array valued maps value = "["",""]"
51 *
52 * @param m the conf maps containing the main.cfg settings
53 * @param mo the map to update
54 * @param mi the map to append
55 * @param elem the elements containing default definitions
56 * @return 0 on success, -1 on failure
57 */
58int appendMapsToMaps (maps * m, maps * mo, maps * mi, elements * elem){
59  maps *tmpMaps = getMaps (mo, mi->name);
60  map *tmap = getMapType (tmpMaps->content);
61  elements *el = getElements (elem, mi->name);
62  int hasEl = 1;
63  if (el == NULL)
64    hasEl = -1;
65  if (tmap == NULL)
66    {
67      if (hasEl > 0)
68        tmap = getMapType (el->defaults->content);
69    }
70 
71  map *testMap = NULL;
72  if (hasEl > 0)
73    {
74      testMap = getMap (el->content, "maxOccurs");
75    }
76  else
77    {
78      testMap = createMap ("maxOccurs", "unbounded");
79    }
80   
81  if (testMap != NULL)
82    {
83      if (strncasecmp (testMap->value, "unbounded", 9) != 0
84          && atoi (testMap->value) > 1)
85        {
86          addMapsArrayToMaps (&mo, mi, tmap->name);
87          map* nb=getMapFromMaps(mo,mi->name,"length");
88          if (nb!=NULL && atoi(nb->value)>atoi(testMap->value))
89            {
90              char emsg[1024];
91              sprintf (emsg,
92                       _("The maximum allowed occurrences for <%s> (%i) was exceeded."),
93                       mi->name, atoi (testMap->value));
94              errorException (m, emsg, "InternalError", NULL);
95              return -1;
96            }
97        }
98      else
99        {
100          if (strncasecmp (testMap->value, "unbounded", 9) == 0)
101            {
102              if (hasEl < 0)
103                {
104                  freeMap (&testMap);
105                  free (testMap);
106                }
107              if (addMapsArrayToMaps (&mo, mi, tmap->name) < 0)
108                {
109                  char emsg[1024];
110                  map *tmpMap = getMap (mi->content, "length");
111                  sprintf (emsg,
112                           _
113                           ("ZOO-Kernel was unable to load your data for %s position %s."),
114                           mi->name, tmpMap->value);
115                  errorException (m, emsg, "InternalError", NULL);
116                  return -1;
117                }
118            }
119          else
120            {
121              char emsg[1024];
122              sprintf (emsg,
123                       _
124                       ("The maximum allowed occurrences for <%s> is one."),
125                       mi->name);
126              errorException (m, emsg, "InternalError", NULL);
127              return -1;
128            }
129        }
130    }
131  return 0;
132}
133
134/**
135 * Make sure that each value encoded in base64 in a maps is decoded.
136 *
137 * @param in the maps containing the values
138 * @see readBase64
139 */
140void ensureDecodedBase64(maps **in){
141  maps* cursor=*in;
142  while(cursor!=NULL){
143    map *tmp=getMap(cursor->content,"encoding");
144    if(tmp!=NULL && strncasecmp(tmp->value,"base64",6)==0){
145      tmp=getMap(cursor->content,"value");
146      readBase64(&tmp);
147      addToMap(cursor->content,"base64_value",tmp->value);
148      int size=0;
149      char *s=strdup(tmp->value);
150      free(tmp->value);
151      tmp->value=base64d(s,strlen(s),&size);
152      free(s);
153      char sizes[1024];
154      sprintf(sizes,"%d",size);
155      addToMap(cursor->content,"size",sizes);
156    }
157    map* length=getMap(cursor->content,"length");
158    if(length!=NULL){
159      int len=atoi(length->value);
160      for(int i=1;i<len;i++){
161        tmp=getMapArray(cursor->content,"encoding",i);
162        if(tmp!=NULL && strncasecmp(tmp->value,"base64",6)==0){
163          char key[17];
164          sprintf(key,"base64_value_%d",i);
165          tmp=getMapArray(cursor->content,"value",i);
166          readBase64(&tmp);
167          addToMap(cursor->content,key,tmp->value);
168          int size=0;
169          char *s=strdup(tmp->value);
170          free(tmp->value);
171          tmp->value=base64d(s,strlen(s),&size);
172          free(s);
173          char sizes[1024];
174          sprintf(sizes,"%d",size);
175          sprintf(key,"size_%d",i);
176          addToMap(cursor->content,key,sizes);
177        }
178      }
179    }
180    cursor=cursor->next;
181  }
182}
183
184/**
185 * Parse inputs provided as KVP and store them in a maps.
186 *
187 * @param main_conf the conf maps containing the main.cfg settings
188 * @param s the service
189 * @param request_inputs the map storing KVP raw value
190 * @param request_output the maps to store the KVP pairs
191 * @param hInternet the HINTERNET queue to add potential requests
192 * @return 0 on success, -1 on failure
193 */
194int kvpParseInputs(maps** main_conf,service* s,map *request_inputs,maps** request_output,HINTERNET* hInternet){
195  // Parsing inputs provided as KVP
196  maps *tmpmaps = *request_output;
197  map* r_inputs = getMap (request_inputs, "DataInputs");
198  char* cursor_input;
199  if (r_inputs != NULL){
200    //snprintf (cursor_input, 40960, "%s", r_inputs->value);
201    if(strstr(r_inputs->value,"=")==NULL)
202      cursor_input = url_decode (r_inputs->value);
203    else
204      cursor_input = zStrdup (r_inputs->value);
205    int j = 0;
206
207    // Put each DataInputs into the inputs_as_text array
208    char *pToken;
209    pToken = strtok (cursor_input, ";");
210    char **inputs_as_text = (char **) malloc (100 * sizeof (char *));
211    if (inputs_as_text == NULL)
212      {
213        free(cursor_input);
214        return errorException (*main_conf, _("Unable to allocate memory."),
215                               "InternalError", NULL);
216      }
217    int i = 0;
218    while (pToken != NULL)
219      {
220        inputs_as_text[i] =
221          (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
222        if (inputs_as_text[i] == NULL)
223          {
224            free(cursor_input);
225            return errorException (*main_conf, _("Unable to allocate memory."),
226                                   "InternalError", NULL);
227          }
228        snprintf (inputs_as_text[i], strlen (pToken) + 1, "%s", pToken);
229        pToken = strtok (NULL, ";");
230        i++;
231      }
232       
233    for (j = 0; j < i; j++)
234      {
235        char *tmp = zStrdup (inputs_as_text[j]);
236        free (inputs_as_text[j]);
237        char *tmpc;
238        tmpc = strtok (tmp, "@");
239        while (tmpc != NULL)
240          {
241            char *tmpv = strstr (tmpc, "=");
242            char tmpn[256];
243            memset (tmpn, 0, 256);
244            if (tmpv != NULL)
245              {
246                strncpy (tmpn, tmpc,
247                         (strlen (tmpc) - strlen (tmpv)) * sizeof (char));
248                tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
249              }
250            else
251              {
252                strncpy (tmpn, tmpc, strlen (tmpc) * sizeof (char));
253                tmpn[strlen (tmpc)] = 0;
254              }
255            if (tmpmaps == NULL)
256              {
257                tmpmaps = (maps *) malloc (MAPS_SIZE);
258                if (tmpmaps == NULL)
259                  {
260                    free(cursor_input);
261                    return errorException (*main_conf,
262                                           _("Unable to allocate memory."),
263                                           "InternalError", NULL);
264                  }
265                tmpmaps->name = zStrdup (tmpn);
266                if (tmpv != NULL)
267                  {
268                    char *tmpvf = url_decode (tmpv + 1);
269                    tmpmaps->content = createMap ("value", tmpvf);
270                    free (tmpvf);
271                  }
272                else
273                  tmpmaps->content = createMap ("value", "Reference");
274                tmpmaps->next = NULL;
275              }
276            tmpc = strtok (NULL, "@");
277            while (tmpc != NULL)
278              {
279                char *tmpv1 = strstr (tmpc, "=");
280                char tmpn1[1024];
281                memset (tmpn1, 0, 1024);
282                if (tmpv1 != NULL)
283                  {
284                    strncpy (tmpn1, tmpc, strlen (tmpc) - strlen (tmpv1));
285                    tmpn1[strlen (tmpc) - strlen (tmpv1)] = 0;
286                    addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
287                  }
288                else
289                  {
290                    strncpy (tmpn1, tmpc, strlen (tmpc));
291                    tmpn1[strlen (tmpc)] = 0;
292                    map *lmap = getLastMap (tmpmaps->content);
293                    char *tmpValue =
294                      (char *) malloc ((strlen (tmpv) + strlen (tmpc) + 1) *
295                                       sizeof (char));
296                    sprintf (tmpValue, "%s@%s", tmpv + 1, tmpc);
297                    free (lmap->value);
298                    lmap->value = zStrdup (tmpValue);
299                    free (tmpValue);
300                    tmpc = strtok (NULL, "@");
301                    continue;
302                  }
303                if (strcmp (tmpn1, "xlink:href") != 0)
304                  addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
305                else if (tmpv1 != NULL)
306                  {
307                    char *tmpx2 = url_decode (tmpv1 + 1);
308                    if (strncasecmp (tmpx2, "http://", 7) != 0 &&
309                        strncasecmp (tmpx2, "ftp://", 6) != 0 &&
310                        strncasecmp (tmpx2, "https://", 8) != 0)
311                      {
312                        char emsg[1024];
313                        sprintf (emsg,
314                                 _
315                                 ("Unable to find a valid protocol to download the remote file %s"),
316                                 tmpv1 + 1);
317                        free(cursor_input);
318                        return errorException (*main_conf, emsg, "InternalError", NULL);
319                      }
320                    addToMap (tmpmaps->content, tmpn1, tmpx2);
321                    {
322                      if (loadRemoteFile
323                          (&*main_conf, &tmpmaps->content, hInternet, tmpx2) < 0)
324                        {
325                          free(cursor_input);
326                          return errorException (*main_conf, "Unable to fetch any ressource", "InternalError", NULL);
327                        }
328                      }
329                    free (tmpx2);
330                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
331                    addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
332                  }
333                tmpc = strtok (NULL, "@");
334              }
335            if (*request_output == NULL)
336              *request_output = dupMaps (&tmpmaps);
337            else
338              {
339                maps *testPresence =
340                  getMaps (*request_output, tmpmaps->name);
341                if (testPresence != NULL)
342                  {
343                    elements *elem =
344                      getElements (s->inputs, tmpmaps->name);
345                    if (elem != NULL)
346                      {
347                        if (appendMapsToMaps
348                            (*main_conf, *request_output, tmpmaps,
349                             elem) < 0)
350                          {
351                            free(cursor_input);
352                            return errorException (*main_conf, "Unable to append maps", "InternalError", NULL);
353                          }
354                      }
355                  }
356                else
357                  addMapsToMaps (request_output, tmpmaps);
358              }
359            freeMaps (&tmpmaps);
360            free (tmpmaps);
361            tmpmaps = NULL;
362            free (tmp);
363          }
364      }
365    free (inputs_as_text);
366    free(cursor_input);
367  }
368  return 1;
369}
370
371/**
372 * Parse outputs provided as KVP and store them in a maps.
373 *
374 * @param main_conf the conf maps containing the main.cfg settings
375 * @param request_inputs the map storing KVP raw value
376 * @param request_output the maps to store the KVP pairs
377 * @return 0 on success, -1 on failure
378 */
379int kvpParseOutputs(maps** main_conf,map *request_inputs,maps** request_output){
380  /**
381   * Parsing outputs provided as KVP
382   */
383  map *r_inputs = NULL;
384  r_inputs = getMap (request_inputs, "ResponseDocument");
385  if (r_inputs == NULL)
386    r_inputs = getMap (request_inputs, "RawDataOutput");
387
388  if (r_inputs != NULL)
389    {
390      char *cursor_output = zStrdup (r_inputs->value);
391      int j = 0;
392
393      /**
394       * Put each Output into the outputs_as_text array
395       */
396      char *pToken;
397      maps *tmp_output = NULL;
398      pToken = strtok (cursor_output, ";");
399      char **outputs_as_text = (char **) malloc (128 * sizeof (char *));
400      if (outputs_as_text == NULL)
401        {
402          free(cursor_output);
403          return errorException (*main_conf, _("Unable to allocate memory"),
404                                 "InternalError", NULL);
405        }
406      int i = 0;
407      while (pToken != NULL)
408        {
409          outputs_as_text[i] =
410            (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
411          if (outputs_as_text[i] == NULL)
412            {
413              free(cursor_output);
414              return errorException (*main_conf, _("Unable to allocate memory"),
415                                     "InternalError", NULL);
416            }
417          snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s",
418                    pToken);
419          pToken = strtok (NULL, ";");
420          i++;
421        }
422      for (j = 0; j < i; j++)
423        {
424          char *tmp = zStrdup (outputs_as_text[j]);
425          free (outputs_as_text[j]);
426          char *tmpc;
427          tmpc = strtok (tmp, "@");
428          int k = 0;
429          while (tmpc != NULL)
430            {
431              if (k == 0)
432                {
433                  if (tmp_output == NULL)
434                    {
435                      tmp_output = (maps *) malloc (MAPS_SIZE);
436                      if (tmp_output == NULL)
437                        {
438                          free(cursor_output);
439                          return errorException (*main_conf,
440                                                 _
441                                                 ("Unable to allocate memory."),
442                                                 "InternalError", NULL);
443                        }
444                      tmp_output->name = zStrdup (tmpc);
445                      tmp_output->content = NULL;
446                      tmp_output->next = NULL;
447                    }
448                }
449              else
450                {
451                  char *tmpv = strstr (tmpc, "=");
452                  char tmpn[256];
453                  memset (tmpn, 0, 256);
454                  strncpy (tmpn, tmpc,
455                           (strlen (tmpc) -
456                            strlen (tmpv)) * sizeof (char));
457                  tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
458                  if (tmp_output->content == NULL)
459                    {
460                      tmp_output->content = createMap (tmpn, tmpv + 1);
461                      tmp_output->content->next = NULL;
462                    }
463                  else
464                    addToMap (tmp_output->content, tmpn, tmpv + 1);
465                }
466              k++;
467              tmpc = strtok (NULL, "@");
468            }
469          if (*request_output == NULL)
470            *request_output = dupMaps (&tmp_output);
471          else
472            addMapsToMaps (request_output, tmp_output);
473          freeMaps (&tmp_output);
474          free (tmp_output);
475          tmp_output = NULL;
476          free (tmp);
477        }
478      free (outputs_as_text);
479      free(cursor_output);
480    }
481  return 1;
482}
483
484/**
485 * Parse inputs from XML nodes and store them in a maps.
486 *
487 * @param main_conf the conf maps containing the main.cfg settings
488 * @param s the service
489 * @param request_output the maps to store the KVP pairs
490 * @param doc the xmlDocPtr containing the original request
491 * @param nodes the input nodes array
492 * @param hInternet the HINTERNET queue to add potential requests
493 * @return 0 on success, -1 on failure
494 */
495int xmlParseInputs(maps** main_conf,service* s,maps** request_output,xmlDocPtr doc,xmlNodeSet* nodes,HINTERNET* hInternet){
496  int k = 0;
497  int l = 0;
498  map* version=getMapFromMaps(*main_conf,"main","rversion");
499  int vid=getVersionId(version->value);
500  for (k=0; k < nodes->nodeNr; k++)
501    {
502      maps *tmpmaps = NULL;
503      xmlNodePtr cur = nodes->nodeTab[k];
504
505      if (nodes->nodeTab[k]->type == XML_ELEMENT_NODE)
506        {
507          // A specific Input node.
508          if(vid==1){
509            tmpmaps = (maps *) malloc (MAPS_SIZE);
510            xmlChar *val = xmlGetProp (cur, BAD_CAST "id");
511            tmpmaps->name = zStrdup ((char *) val);
512            tmpmaps->content = NULL;
513            tmpmaps->next = NULL;
514          }
515
516          xmlNodePtr cur2 = cur->children;
517          while (cur2 != NULL)
518            {
519              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
520                cur2 = cur2->next;
521              if (cur2 == NULL)
522                break;
523              // Indentifier
524              if (xmlStrncasecmp
525                  (cur2->name, BAD_CAST "Identifier",
526                   xmlStrlen (cur2->name)) == 0)
527                {
528                  xmlChar *val =
529                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
530                  if (tmpmaps == NULL)
531                    {
532                      tmpmaps = (maps *) malloc (MAPS_SIZE);
533                      if (tmpmaps == NULL)
534                        {
535                          return errorException (*main_conf,
536                                                 _
537                                                 ("Unable to allocate memory."),
538                                                 "InternalError", NULL);
539                        }
540                      tmpmaps->name = zStrdup ((char *) val);
541                      tmpmaps->content = NULL;
542                      tmpmaps->next = NULL;
543                    }
544                  xmlFree (val);
545                }
546              // Title, Asbtract
547              if (xmlStrncasecmp
548                  (cur2->name, BAD_CAST "Title",
549                   xmlStrlen (cur2->name)) == 0
550                  || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract",
551                                     xmlStrlen (cur2->name)) == 0)
552                {
553                  xmlChar *val =
554                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
555                  if (tmpmaps == NULL)
556                    {
557                      tmpmaps = (maps *) malloc (MAPS_SIZE);
558                      if (tmpmaps == NULL)
559                        {
560                          return errorException (*main_conf,
561                                                 _
562                                                 ("Unable to allocate memory."),
563                                                 "InternalError", NULL);
564                        }
565                      tmpmaps->name = zStrdup ("missingIndetifier");
566                      tmpmaps->content =
567                        createMap ((char *) cur2->name, (char *) val);
568                      tmpmaps->next = NULL;
569                    }
570                  else
571                    {
572                      if (tmpmaps->content != NULL)
573                        addToMap (tmpmaps->content,
574                                  (char *) cur2->name, (char *) val);
575                      else
576                        tmpmaps->content =
577                          createMap ((char *) cur2->name, (char *) val);
578                    }
579                  xmlFree (val);
580                }
581              // InputDataFormChoice (Reference or Data ?)
582              if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0)
583                {
584                  // Get every attribute from a Reference node
585                  // mimeType, encoding, schema, href, method
586                  // Header and Body gesture should be added here
587                  const char *refs[5] =
588                    { "mimeType", "encoding", "schema", "method",
589                      "href"
590                    };
591                  for (l = 0; l < 5; l++)
592                    {
593                      xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]);
594                      if (val != NULL && xmlStrlen (val) > 0)
595                        {
596                          if (tmpmaps->content != NULL)
597                            addToMap (tmpmaps->content, refs[l],
598                                      (char *) val);
599                          else
600                            tmpmaps->content =
601                              createMap (refs[l], (char *) val);
602
603                          map *ltmp = getMap (tmpmaps->content, "method");
604                          if (l == 4 )
605                            {
606                              if ((ltmp==NULL || strncmp (ltmp->value, "POST",4) != 0))
607                                {
608                                  if (loadRemoteFile
609                                      (main_conf, &tmpmaps->content, hInternet,
610                                       (char *) val) != 0)
611                                    {
612                                      return errorException (*main_conf,
613                                                             _("Unable to add a request in the queue."),
614                                                             "InternalError",
615                                                             NULL);
616                                    }
617                                  addIntToMap (tmpmaps->content, "Order", hInternet->nb);
618                                }
619                              addToMap (tmpmaps->content, "Reference", (char*) val);
620                            }
621                        }
622                      xmlFree (val);
623                    }
624                  // Parse Header and Body from Reference
625                  xmlNodePtr cur3 = cur2->children;
626                  while (cur3 != NULL)
627                    {
628                      while (cur3 != NULL
629                             && cur3->type != XML_ELEMENT_NODE)
630                        cur3 = cur3->next;
631                      if (cur3 == NULL)
632                        break;
633                      if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
634                          0)
635                        {
636                          const char *ha[2];
637                          ha[0] = "key";
638                          ha[1] = "value";
639                          int hai;
640                          char *has;
641                          char *key;
642                          for (hai = 0; hai < 2; hai++)
643                            {
644                              xmlChar *val =
645                                xmlGetProp (cur3, BAD_CAST ha[hai]);
646#ifdef POST_DEBUG
647                              fprintf (stderr, "%s = %s\n", ha[hai],
648                                       (char *) val);
649#endif
650                              if (hai == 0)
651                                {
652                                  key = zStrdup ((char *) val);
653                                }
654                              else
655                                {
656                                  has =
657                                    (char *)
658                                    malloc ((4 + xmlStrlen (val) +
659                                             strlen (key)) *
660                                            sizeof (char));
661                                  if (has == NULL)
662                                    {
663                                      return errorException (*main_conf,
664                                                             _
665                                                             ("Unable to allocate memory."),
666                                                             "InternalError",
667                                                             NULL);
668                                    }
669                                  snprintf (has,
670                                            (3 + xmlStrlen (val) +
671                                             strlen (key)), "%s: %s", key,
672                                            (char *) val);
673                                  free (key);
674                                }
675                              xmlFree (val);
676                            }
677                          hInternet->ihandle[hInternet->nb].header = NULL;
678                          hInternet->ihandle[hInternet->nb].header =
679                            curl_slist_append (hInternet->ihandle
680                                               [hInternet->nb].header,
681                                               has);
682                          if (has != NULL)
683                            free (has);
684                        }
685                      else
686                        {
687#ifdef POST_DEBUG
688                          fprintf (stderr,
689                                   "Try to fetch the body part of the request ...\n");
690#endif
691                          if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
692                              == 0)
693                            {
694#ifdef POST_DEBUG
695                              fprintf (stderr, "Body part found (%s) !!!\n",
696                                       (char *) cur3->content);
697#endif
698                              char *tmp = NULL;
699                              xmlNodePtr cur4 = cur3->children;
700                              while (cur4 != NULL)
701                                {
702                                  while (cur4 && cur4 != NULL && cur4->type && cur4->type != XML_ELEMENT_NODE){
703                                    if(cur4->next)
704                                      cur4 = cur4->next;
705                                    else
706                                      cur4 = NULL;
707                                  }
708                                  if(cur4 != NULL) {
709                                    xmlDocPtr bdoc =
710                                      xmlNewDoc (BAD_CAST "1.0");
711                                    bdoc->encoding =
712                                      xmlCharStrdup ("UTF-8");
713                                    xmlDocSetRootElement (bdoc, cur4);
714                                    xmlChar *btmps;
715                                    int bsize;
716                                    // TODO : check for encoding defined in the input
717                                    xmlDocDumpFormatMemoryEnc(bdoc, &btmps, &bsize, "UTF-8", 0);
718                                    if (btmps != NULL){
719                                      tmp = (char *) malloc ((bsize + 1) * sizeof (char));
720
721                                      sprintf (tmp, "%s", (char*) btmps);
722                                         
723                                      xmlFreeDoc (bdoc);
724                                         
725                                      map *btmp =
726                                        getMap (tmpmaps->content, "Reference");
727                                      if (btmp != NULL)
728                                        {
729                                          addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
730                                          InternetOpenUrl (hInternet,
731                                                           btmp->value,
732                                                           tmp,
733                                                           xmlStrlen(btmps),
734                                                           INTERNET_FLAG_NO_CACHE_WRITE,
735                                                           0);
736                                          addIntToMap (tmpmaps->content, "Order", hInternet->nb);
737                                        }
738                                      xmlFree (btmps);
739                                      free (tmp);
740                                      break;
741                                    }
742                                  }
743                                  cur4 = cur4->next;
744                                }
745                            }
746                          else
747                            if (xmlStrcasecmp
748                                (cur3->name,
749                                 BAD_CAST "BodyReference") == 0)
750                              {
751                                xmlChar *val =
752                                  xmlGetProp (cur3, BAD_CAST "href");
753                                HINTERNET bInternet, res1, res;
754                                bInternet = InternetOpen (
755#ifndef WIN32
756                                                          (LPCTSTR)
757#endif
758                                                          "ZooWPSClient\0",
759                                                          INTERNET_OPEN_TYPE_PRECONFIG,
760                                                          NULL, NULL, 0);
761                                if (!CHECK_INET_HANDLE (bInternet))
762                                  fprintf (stderr,
763                                           "WARNING : bInternet handle failed to initialize");
764                                bInternet.waitingRequests[0] =
765                                  strdup ((char *) val);
766                                res1 =
767                                  InternetOpenUrl (&bInternet,
768                                                   bInternet.waitingRequests
769                                                   [0], NULL, 0,
770                                                   INTERNET_FLAG_NO_CACHE_WRITE,
771                                                   0);
772                                processDownloads (&bInternet);
773                                char *tmp =
774                                  (char *)
775                                  malloc ((bInternet.ihandle[0].nDataLen +
776                                           1) * sizeof (char));
777                                if (tmp == NULL)
778                                  {
779                                    return errorException (*main_conf,
780                                                           _
781                                                           ("Unable to allocate memory."),
782                                                           "InternalError",
783                                                           NULL);
784                                  }
785                                size_t bRead;
786                                InternetReadFile (bInternet.ihandle[0],
787                                                  (LPVOID) tmp,
788                                                  bInternet.
789                                                  ihandle[0].nDataLen,
790                                                  &bRead);
791                                tmp[bInternet.ihandle[0].nDataLen] = 0;
792                                InternetCloseHandle (&bInternet);
793                                map *btmp =
794                                  getMap (tmpmaps->content, "href");
795                                if (btmp != NULL)
796                                  {
797                                    addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
798
799                                    res =
800                                      InternetOpenUrl (hInternet,
801                                                       btmp->value,
802                                                       tmp,
803                                                       strlen(tmp),
804                                                       INTERNET_FLAG_NO_CACHE_WRITE,
805                                                       0);
806                                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
807                                  }
808                                free (tmp);
809                              }
810                        }
811                      cur3 = cur3->next;
812                    }
813                }
814              else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
815                {
816                  xmlNodePtr cur4 = cur2->children;
817                  if(vid==1){
818                    // Get every dataEncodingAttributes from a Data node:
819                    // mimeType, encoding, schema
820                    const char *coms[3] =
821                      { "mimeType", "encoding", "schema" };
822                    for (l = 0; l < 3; l++){
823                      xmlChar *val =
824                          xmlGetProp (cur4, BAD_CAST coms[l]);
825                        if (val != NULL && strlen ((char *) val) > 0){
826                          if (tmpmaps->content != NULL)
827                            addToMap (tmpmaps->content,coms[l],(char *) val);
828                          else
829                            tmpmaps->content =
830                              createMap (coms[l],(char *) val);
831                        }
832                        xmlFree (val);
833                    }
834                    while (cur4 != NULL){
835                      while(cur4 != NULL && 
836                            cur4->type != XML_CDATA_SECTION_NODE &&
837                            cur4->type != XML_TEXT_NODE)
838                        cur4=cur4->next;
839                      if(cur4!=NULL){
840                        if(cur4->content!=NULL)
841                          if (tmpmaps->content != NULL)
842                            addToMap (tmpmaps->content, "value",
843                                      (char *) cur4->content);
844                          else
845                            tmpmaps->content =
846                              createMap ("value", (char *) cur4->content);
847                        cur4=cur4->next;
848                      }
849                    }
850                  }
851
852
853                  while (cur4 != NULL)
854                    {
855                      while (cur4 != NULL
856                             && cur4->type != XML_ELEMENT_NODE)
857                        cur4 = cur4->next;
858                      if (cur4 == NULL)
859                        break;
860                      if (xmlStrcasecmp
861                          (cur4->name, BAD_CAST "LiteralData") == 0)
862                        {
863                          // Get every attribute from a LiteralData node
864                          // dataType , uom
865                          char *list[2];
866                          list[0] = zStrdup ("dataType");
867                          list[1] = zStrdup ("uom");
868                          for (l = 0; l < 2; l++)
869                            {
870                              xmlChar *val =
871                                xmlGetProp (cur4, BAD_CAST list[l]);
872                              if (val != NULL
873                                  && strlen ((char *) val) > 0)
874                                {
875                                  if (tmpmaps->content != NULL)
876                                    addToMap (tmpmaps->content, list[l],
877                                              (char *) val);
878                                  else
879                                    tmpmaps->content =
880                                      createMap (list[l], (char *) val);
881                                }
882                              xmlFree (val);
883                              free (list[l]);
884                            }
885                        }
886                      else
887                        if (xmlStrcasecmp
888                            (cur4->name, BAD_CAST "ComplexData") == 0)
889                          {
890                            // Get every attribute from a Reference node
891                            // mimeType, encoding, schema
892                            const char *coms[3] =
893                              { "mimeType", "encoding", "schema" };
894                            for (l = 0; l < 3; l++)
895                              {
896                                xmlChar *val =
897                                  xmlGetProp (cur4, BAD_CAST coms[l]);
898                                if (val != NULL
899                                    && strlen ((char *) val) > 0)
900                                  {
901                                    if (tmpmaps->content != NULL)
902                                      addToMap (tmpmaps->content, coms[l],
903                                                (char *) val);
904                                    else
905                                      tmpmaps->content =
906                                        createMap (coms[l], (char *) val);
907                                  }
908                                xmlFree (val);
909                              }
910                          }
911
912                      map *test = getMap (tmpmaps->content, "encoding");
913
914                      if (test == NULL)
915                        { 
916                          if (tmpmaps->content != NULL)
917                            addToMap (tmpmaps->content, "encoding",
918                                      "utf-8");
919                          else
920                            tmpmaps->content =
921                              createMap ("encoding", "utf-8");
922                          test = getMap (tmpmaps->content, "encoding");
923                        }
924
925                      if (strcasecmp (test->value, "base64") != 0)
926                        { 
927                          xmlChar *mv = xmlNodeListGetString (doc,
928                                                              cur4->xmlChildrenNode,
929                                                              1);
930                          map *ltmp =
931                            getMap (tmpmaps->content, "mimeType");
932                          if (mv == NULL
933                              ||
934                              (xmlStrcasecmp
935                               (cur4->name, BAD_CAST "ComplexData") == 0
936                               && (ltmp == NULL
937                                   || strncasecmp (ltmp->value,
938                                                   "text/xml", 8) == 0)))
939                            {
940                              xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
941                              int buffersize;
942                              xmlNodePtr cur5 = cur4->children;
943                              while (cur5 != NULL
944                                     && cur5->type != XML_ELEMENT_NODE
945                                     && cur5->type != XML_CDATA_SECTION_NODE)
946                                cur5 = cur5->next;
947                              if (cur5 != NULL
948                                  && cur5->type != XML_CDATA_SECTION_NODE)
949                                {
950                                  xmlDocSetRootElement (doc1, cur5);
951                                  xmlDocDumpFormatMemoryEnc (doc1, &mv,
952                                                             &buffersize,
953                                                             "utf-8", 1);
954                                  char size[1024];
955                                  sprintf (size, "%d", buffersize);
956                                  addToMap (tmpmaps->content, "size",
957                                            size);
958                                  xmlFreeDoc (doc1);
959                                }
960                              else
961                                {
962                                  if (cur5 != NULL
963                                      && cur5->type == XML_CDATA_SECTION_NODE){
964                                    xmlDocPtr doc2 = xmlParseMemory((const char*)cur5->content,xmlStrlen(cur5->content));
965                                    xmlDocSetRootElement (doc1,xmlDocGetRootElement(doc2));
966                                    xmlDocDumpFormatMemoryEnc (doc1, &mv,
967                                                               &buffersize,
968                                                               "utf-8", 1);
969                                    char size[1024];
970                                    sprintf (size, "%d", buffersize);
971                                    addToMap (tmpmaps->content, "size",
972                                              size);
973                                    xmlFreeDoc (doc2);
974                                    xmlFreeDoc (doc1);
975                                  }
976                                }
977                            }else{
978                            xmlNodePtr cur5 = cur4->children;
979                            while (cur5 != NULL
980                                   && cur5->type != XML_CDATA_SECTION_NODE)
981                              cur5 = cur5->next;
982                            if (cur5 != NULL
983                                && cur5->type == XML_CDATA_SECTION_NODE){
984                              xmlFree(mv);
985                              mv=xmlStrdup(cur5->content);
986                            }
987                          }
988                          if (mv != NULL)
989                            {
990                              addToMap (tmpmaps->content, "value",
991                                        (char *) mv);
992                              xmlFree (mv);
993                            }
994                        }
995                      else
996                        {
997                          xmlChar *tmp = xmlNodeListGetRawString (doc,
998                                                                  cur4->xmlChildrenNode,
999                                                                  0);
1000                          addToMap (tmpmaps->content, "value",
1001                                    (char *) tmp);
1002                          xmlFree (tmp);
1003                        }
1004                      cur4 = cur4->next;
1005                    }
1006                }
1007              cur2 = cur2->next;
1008              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE){
1009                cur2 = cur2->next;
1010              }
1011            }
1012          {
1013            maps *testPresence =
1014              getMaps (*request_output, tmpmaps->name);
1015            if (testPresence != NULL)
1016              {
1017                elements *elem = getElements (s->inputs, tmpmaps->name);
1018                if (elem != NULL)
1019                  {
1020                    if (appendMapsToMaps
1021                        (*main_conf, *request_output, tmpmaps, elem) < 0)
1022                      {
1023                        return errorException (*main_conf,
1024                                               _("Unable to append maps to maps."),
1025                                               "InternalError",
1026                                               NULL);
1027                      }
1028                  }
1029              }
1030            else
1031              addMapsToMaps (request_output, tmpmaps);
1032          }
1033          freeMaps (&tmpmaps);
1034          free (tmpmaps);
1035          tmpmaps = NULL;
1036        }
1037    }
1038  return 1;
1039}
1040
1041/**
1042 * Parse outputs from XML nodes and store them in a maps (WPS version 2.0.0).
1043 *
1044 * @param main_conf the conf maps containing the main.cfg settings
1045 * @param request_inputs the map storing KVP raw value
1046 * @param request_output the maps to store the KVP pairs
1047 * @param doc the xmlDocPtr containing the original request
1048 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
1049 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
1050 * @return 0 on success, -1 on failure
1051 */
1052int xmlParseOutputs2(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodeSet* nodes){
1053  int k = 0;
1054  int l = 0;
1055  for (k=0; k < nodes->nodeNr; k++){
1056    maps *tmpmaps = NULL;
1057    xmlNodePtr cur = nodes->nodeTab[k];
1058    if (cur->type == XML_ELEMENT_NODE){
1059      maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
1060      xmlChar *val = xmlGetProp (cur, BAD_CAST "id");
1061      if(val!=NULL)
1062        tmpmaps->name = zStrdup ((char*)val);
1063      else
1064        tmpmaps->name = zStrdup ("unknownIdentifier");
1065      tmpmaps->content = NULL;
1066      tmpmaps->next = NULL;
1067      const char ress[4][13] =
1068        { "mimeType", "encoding", "schema", "transmission" };
1069      for (l = 0; l < 4; l++){
1070        val = xmlGetProp (cur, BAD_CAST ress[l]);
1071        if (val != NULL && strlen ((char *) val) > 0)
1072          {
1073            if (tmpmaps->content != NULL)
1074              addToMap (tmpmaps->content, ress[l],
1075                        (char *) val);
1076            else
1077              tmpmaps->content =
1078                createMap (ress[l], (char *) val);
1079            if(l==3 && strncasecmp((char*)val,"reference",xmlStrlen(val))==0)
1080              addToMap (tmpmaps->content,"asReference","true");
1081          }
1082        xmlFree (val);
1083      }
1084      if (*request_output == NULL)
1085        *request_output = dupMaps(&tmpmaps);
1086      else
1087        addMapsToMaps(request_output,tmpmaps);
1088    }
1089  }
1090}
1091
1092/**
1093 * Parse outputs from XML nodes and store them in a maps.
1094 *
1095 * @param main_conf the conf maps containing the main.cfg settings
1096 * @param request_inputs the map storing KVP raw value
1097 * @param request_output the maps to store the KVP pairs
1098 * @param doc the xmlDocPtr containing the original request
1099 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
1100 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
1101 * @return 0 on success, -1 on failure
1102 */
1103int xmlParseOutputs(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodePtr cur,bool raw){
1104  int l=0;
1105  if( raw == true)
1106    {
1107      addToMap (*request_inputs, "RawDataOutput", "");
1108      if (cur->type == XML_ELEMENT_NODE)
1109        {
1110
1111          maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
1112          if (tmpmaps == NULL)
1113            {
1114              return errorException (*main_conf, _("Unable to allocate memory."),
1115                                     "InternalError", NULL);
1116            }
1117          tmpmaps->name = zStrdup ("unknownIdentifier");
1118          tmpmaps->content = NULL;
1119          tmpmaps->next = NULL;
1120
1121          // Get every attribute from a RawDataOutput node
1122          // mimeType, encoding, schema, uom
1123          const char *outs[4] =
1124            { "mimeType", "encoding", "schema", "uom" };
1125          for (l = 0; l < 4; l++)
1126            {
1127              xmlChar *val = xmlGetProp (cur, BAD_CAST outs[l]);
1128              if (val != NULL)
1129                {
1130                  if (strlen ((char *) val) > 0)
1131                    {
1132                      if (tmpmaps->content != NULL)
1133                        addToMap (tmpmaps->content, outs[l],
1134                                  (char *) val);
1135                      else
1136                        tmpmaps->content =
1137                          createMap (outs[l], (char *) val);
1138                    }
1139                  xmlFree (val);
1140                }
1141            }
1142          xmlNodePtr cur2 = cur->children;
1143          while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1144            cur2 = cur2->next;
1145          while (cur2 != NULL)
1146            {
1147              if (xmlStrncasecmp
1148                  (cur2->name, BAD_CAST "Identifier",
1149                   xmlStrlen (cur2->name)) == 0)
1150                {
1151                  xmlChar *val =
1152                    xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
1153                  free (tmpmaps->name);
1154                  tmpmaps->name = zStrdup ((char *) val);
1155                  xmlFree (val);
1156                }
1157              cur2 = cur2->next;
1158              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1159                cur2 = cur2->next;
1160            }
1161          if (*request_output == NULL)
1162            *request_output = dupMaps (&tmpmaps);
1163          else
1164            addMapsToMaps (request_output, tmpmaps);
1165          if (tmpmaps != NULL)
1166            {
1167              freeMaps (&tmpmaps);
1168              free (tmpmaps);
1169              tmpmaps = NULL;
1170            }
1171        }
1172    }
1173  else
1174    {
1175      addToMap (*request_inputs, "ResponseDocument", "");
1176
1177      if (cur->type == XML_ELEMENT_NODE) {
1178        // Get every attribute: storeExecuteResponse, lineage, status
1179        const char *ress[3] =
1180          { "storeExecuteResponse", "lineage", "status" };
1181        xmlChar *val;
1182        for (l = 0; l < 3; l++)
1183          {
1184            val = xmlGetProp (cur, BAD_CAST ress[l]);
1185            if (val != NULL && strlen ((char *) val) > 0)
1186              {
1187                addToMap (*request_inputs, ress[l], (char *) val);
1188              }
1189            xmlFree (val);
1190          }
1191                       
1192        xmlNodePtr cur1 = cur->children;               
1193        while (cur1 != NULL) // iterate over Output nodes
1194          {
1195            if (cur1->type != XML_ELEMENT_NODE || 
1196                xmlStrncasecmp(cur1->name, BAD_CAST "Output", 
1197                               xmlStrlen (cur1->name)) != 0) {
1198              cur1 = cur1->next;
1199              continue;
1200            }
1201                               
1202            maps *tmpmaps = (maps *) malloc (MAPS_SIZE); // one per Output node
1203            if (tmpmaps == NULL) {
1204              return errorException (*main_conf,
1205                                     _
1206                                     ("Unable to allocate memory."),
1207                                     "InternalError", NULL);
1208            }
1209            tmpmaps->name = zStrdup ("unknownIdentifier");
1210            tmpmaps->content = NULL;
1211            tmpmaps->next = NULL;
1212                               
1213            xmlNodePtr elems = cur1->children;
1214                               
1215            while (elems != NULL) {
1216
1217              // Identifier
1218              if (xmlStrncasecmp
1219                  (elems->name, BAD_CAST "Identifier",
1220                   xmlStrlen (elems->name)) == 0)
1221                {
1222                  xmlChar *val =
1223                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1224               
1225                  free(tmpmaps->name);
1226                  tmpmaps->name = zStrdup ((char *) val);
1227                  if (tmpmaps->content == NULL) {
1228                    tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
1229                  }
1230                  else {
1231                    addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
1232                  }
1233
1234                  map* tt = getMap (*request_inputs, "ResponseDocument");
1235                  if (strlen(tt->value) == 0) {
1236                    addToMap (*request_inputs, "ResponseDocument",
1237                              (char *) val);
1238                  }
1239                  else {
1240                    char* tmp = (char*) malloc((strlen(tt->value) + 1 
1241                                                + strlen((char*) val) + 1) * sizeof(char));
1242                    sprintf (tmp, "%s;%s", tt->value, (char *) val);
1243                    free(tt->value);
1244                    tt->value = tmp;
1245                  }
1246                  xmlFree (val);
1247                }
1248             
1249              // Title, Abstract
1250              else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
1251                                      xmlStrlen (elems->name)) == 0
1252                       || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
1253                                         xmlStrlen (elems->name)) == 0)
1254                {
1255                  xmlChar *val =
1256                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1257                                                       
1258                  if (tmpmaps->content == NULL) {
1259                    tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
1260                  }
1261                  else {
1262                    addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
1263                  }
1264                  xmlFree (val);
1265                }
1266              elems = elems->next;
1267            }
1268                               
1269            // Get every attribute from an Output node:
1270            // mimeType, encoding, schema, uom, asReference
1271            const char *outs[5] =
1272              { "mimeType", "encoding", "schema", "uom", "asReference" };
1273                                         
1274            for (l = 0; l < 5; l++) {
1275              xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                               
1276              if (val != NULL && xmlStrlen(val) > 0) {
1277                if (tmpmaps->content != NULL) {
1278                  addToMap (tmpmaps->content, outs[l], (char *) val);
1279                }                         
1280                else {
1281                  tmpmaps->content = createMap (outs[l], (char *) val);
1282                }       
1283              }
1284              xmlFree (val);
1285            }
1286                               
1287            if (*request_output == NULL) {
1288              *request_output = tmpmaps;
1289            }   
1290            else if (getMaps(*request_output, tmpmaps->name) != NULL) {
1291              return errorException (*main_conf,
1292                                     _
1293                                     ("Duplicate <Output> elements in WPS Execute request"),
1294                                     "InternalError", NULL);
1295            }
1296            else {
1297              maps* mptr = *request_output;
1298              while (mptr->next != NULL) {
1299                mptr = mptr->next;
1300              }
1301              mptr->next = tmpmaps;     
1302            }                                   
1303            cur1 = cur1->next;
1304          }                     
1305      }
1306    }
1307  return 1;
1308}
1309
1310/**
1311 * Parse XML request and store informations in maps.
1312 *
1313 * @param main_conf the conf maps containing the main.cfg settings
1314 * @param post the string containing the XML request
1315 * @param request_inputs the map storing KVP raw value
1316 * @param s the service
1317 * @param inputs the maps to store the KVP pairs
1318 * @param outputs the maps to store the KVP pairs
1319 * @param hInternet the HINTERNET queue to add potential requests
1320 * @return 0 on success, -1 on failure
1321 */
1322int xmlParseRequest(maps** main_conf,const char* post,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1323
1324  map* version=getMapFromMaps(*main_conf,"main","rversion");
1325  int vid=getVersionId(version->value);
1326
1327  xmlInitParser ();
1328  xmlDocPtr doc = xmlParseMemory (post, cgiContentLength);
1329
1330  /**
1331   * Extract Input nodes from the XML Request.
1332   */
1333  xmlXPathObjectPtr tmpsptr =
1334    extractFromDoc (doc, (vid==0?"/*/*/*[local-name()='Input']":"/*/*[local-name()='Input']"));
1335  xmlNodeSet *tmps = tmpsptr->nodesetval;
1336  if(tmps==NULL || xmlParseInputs(main_conf,s,inputs,doc,tmps,hInternet)<0){
1337    xmlXPathFreeObject (tmpsptr);
1338    xmlFreeDoc (doc);
1339    xmlCleanupParser ();
1340    return -1;
1341  }
1342  xmlXPathFreeObject (tmpsptr);
1343
1344  if(vid==1){
1345    tmpsptr =
1346      extractFromDoc (doc, "/*[local-name()='Execute']");
1347    bool asRaw = false;
1348    tmps = tmpsptr->nodesetval;
1349    if(tmps->nodeNr > 0){
1350      int k = 0;
1351      for (k=0; k < tmps->nodeNr; k++){
1352        maps *tmpmaps = NULL;
1353        xmlNodePtr cur = tmps->nodeTab[k];
1354        if (cur->type == XML_ELEMENT_NODE){
1355          xmlChar *val = xmlGetProp (cur, BAD_CAST "mode");
1356          if(val!=NULL)
1357            addToMap(*request_inputs,"mode",(char*)val);
1358          else
1359            addToMap(*request_inputs,"mode","auto");
1360          val = xmlGetProp (cur, BAD_CAST "response");
1361          if(val!=NULL){
1362            addToMap(*request_inputs,"response",(char*)val);
1363            if(strncasecmp((char*)val,"raw",xmlStrlen(val))==0)
1364              addToMap(*request_inputs,"RawDataOutput","");
1365            else
1366              addToMap(*request_inputs,"ResponseDocument","");
1367          }
1368          else{
1369            addToMap(*request_inputs,"response","document");
1370            addToMap(*request_inputs,"ResponseDocument","");
1371          }
1372        }
1373      }
1374    }
1375    xmlXPathFreeObject (tmpsptr);
1376    tmpsptr =
1377      extractFromDoc (doc, "/*/*[local-name()='Output']");
1378    tmps = tmpsptr->nodesetval;
1379    if(tmps->nodeNr > 0){
1380      if(xmlParseOutputs2(main_conf,request_inputs,outputs,doc,tmps)<0){
1381        xmlXPathFreeObject (tmpsptr);
1382        xmlFreeDoc (doc);
1383        xmlCleanupParser ();
1384        return -1;
1385      }
1386    }
1387  }
1388  else{
1389    // Extract ResponseDocument / RawDataOutput from the XML Request
1390    tmpsptr =
1391      extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
1392    bool asRaw = false;
1393    tmps = tmpsptr->nodesetval;
1394    if (tmps->nodeNr == 0)
1395      {
1396        xmlXPathFreeObject (tmpsptr);
1397        tmpsptr =
1398          extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
1399        tmps = tmpsptr->nodesetval;
1400        asRaw = true;
1401      }
1402    if(tmps->nodeNr != 0){
1403      if(xmlParseOutputs(main_conf,request_inputs,outputs,doc,tmps->nodeTab[0],asRaw)<0){
1404        xmlXPathFreeObject (tmpsptr);
1405        xmlFreeDoc (doc);
1406        xmlCleanupParser ();
1407        return -1;
1408      }
1409    }
1410  }
1411  xmlXPathFreeObject (tmpsptr);
1412  xmlFreeDoc (doc);
1413  xmlCleanupParser ();
1414  return 1;
1415}
1416
1417/**
1418 * Parse request and store informations in maps.
1419 *
1420 * @param main_conf the conf maps containing the main.cfg settings
1421 * @param post the string containing the XML request
1422 * @param request_inputs the map storing KVP raw value
1423 * @param s the service
1424 * @param inputs the maps to store the KVP pairs
1425 * @param outputs the maps to store the KVP pairs
1426 * @param hInternet the HINTERNET queue to add potential requests
1427 * @return 0 on success, -1 on failure
1428 * @see kvpParseOutputs,kvpParseInputs,xmlParseRequest
1429 */
1430int parseRequest(maps** main_conf,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1431  map *postRequest = NULL;
1432  postRequest = getMap (*request_inputs, "xrequest");
1433  if (postRequest == NULLMAP)
1434    {
1435      if(kvpParseOutputs(main_conf,*request_inputs,outputs)<0){
1436        return -1;
1437      }
1438      if(kvpParseInputs(main_conf,s,*request_inputs,inputs,hInternet)<0){
1439        return -1;
1440      }
1441    }
1442  else
1443    {
1444      //Parse XML request
1445      if(xmlParseRequest(main_conf,postRequest->value,request_inputs,s,inputs,outputs,hInternet)<0){
1446        return -1;
1447      }
1448    }
1449  return 1;
1450}
1451
1452/**
1453 * Ensure that each requested arguments are present in the request
1454 * DataInputs and ResponseDocument / RawDataOutput. Potentially run
1455 * http requests from the queue in parallel.
1456 * For optional inputs add default values defined in the ZCFG file.
1457 *
1458 * @param main_conf
1459 * @param s
1460 * @param original_request
1461 * @param request_inputs
1462 * @param request_outputs
1463 * @param hInternet
1464 *
1465 * @see runHttpRequests
1466 */
1467int validateRequest(maps** main_conf,service* s,map* original_request, maps** request_inputs,maps** request_outputs,HINTERNET* hInternet){
1468
1469  runHttpRequests (main_conf, request_inputs, hInternet);
1470  InternetCloseHandle (hInternet);
1471
1472  map* errI=NULL;
1473  char *dfv = addDefaultValues (request_inputs, s->inputs, *main_conf, 0,&errI);
1474
1475  maps *ptr = *request_inputs;
1476  while (ptr != NULL)
1477    {
1478      map *tmp0 = getMap (ptr->content, "size");
1479      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
1480      if (tmp1 != NULL && tmp0 != NULL)
1481        {
1482          float i = atof (tmp0->value) / 1048576.0;
1483          if (i >= atoi (tmp1->value))
1484            {
1485              char tmps[1024];
1486              map *tmpe = createMap ("code", "FileSizeExceeded");
1487              snprintf (tmps, 1024,
1488                        _
1489                        ("The <%s> parameter has a size limit (%s MB) defined in the ZOO ServicesProvider configuration file, but the reference you provided exceeds this limit (%f MB)."),
1490                        ptr->name, tmp1->value, i);
1491              addToMap (tmpe, "locator", ptr->name);
1492              addToMap (tmpe, "text", tmps);
1493              printExceptionReportResponse (*main_conf, tmpe);
1494              freeMap (&tmpe);
1495              free (tmpe);
1496              return -1;
1497            }
1498        }
1499      ptr = ptr->next;
1500    }
1501
1502  map* errO=NULL;
1503  char *dfv1 =
1504    addDefaultValues (request_outputs, s->outputs, *main_conf, 1,&errO);
1505  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
1506    {
1507      char tmps[1024];
1508      map *tmpe = NULL;
1509      if (strcmp (dfv, "") != 0)
1510        {
1511          tmpe = createMap ("code", "MissingParameterValue");
1512          int nb=0;
1513          int length=1;
1514          map* len=getMap(errI,"length");
1515          if(len!=NULL)
1516            length=atoi(len->value);
1517          for(nb=0;nb<length;nb++){
1518            map* errp=getMapArray(errI,"value",nb);
1519            snprintf (tmps, 1024,
1520                      _
1521                      ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
1522                      errp->value);
1523            setMapArray (tmpe, "locator", nb , errp->value);
1524            setMapArray (tmpe, "text", nb , tmps);
1525            setMapArray (tmpe, "code", nb , "MissingParameterValue");
1526          }
1527        }
1528      if (strcmp (dfv1, "") != 0)
1529        {
1530          int ilength=0;
1531          if(tmpe==NULL)
1532            tmpe = createMap ("code", "InvalidParameterValue");
1533          else{
1534            map* len=getMap(tmpe,"length");
1535            if(len!=NULL)
1536              ilength=atoi(len->value);
1537          }
1538          int nb=0;
1539          int length=1;
1540          map* len=getMap(errO,"length");
1541          if(len!=NULL)
1542            length=atoi(len->value);
1543          for(nb=0;nb<length;nb++){
1544            map* errp=getMapArray(errO,"value",nb);
1545            snprintf (tmps, 1024,
1546                      _
1547                      ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
1548                      errp->value,
1549                      ((getMap(original_request,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
1550            setMapArray (tmpe, "locator", nb+ilength , errp->value);
1551            setMapArray (tmpe, "text", nb+ilength , tmps);
1552            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
1553          }
1554        }
1555      printExceptionReportResponse (*main_conf, tmpe);
1556      if(errI!=NULL){
1557        freeMap(&errI);
1558        free(errI);
1559      }
1560      if(errO!=NULL){
1561        freeMap(&errO);
1562        free(errO);
1563      }
1564      freeMap (&tmpe);
1565      free (tmpe);
1566      return -1;
1567    }
1568  maps *tmpReqI = *request_inputs;
1569  while (tmpReqI != NULL)
1570    {
1571      char name[1024];
1572      if (getMap (tmpReqI->content, "isFile") != NULL)
1573        {
1574          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
1575              cgiFormSuccess)
1576            {
1577              int BufferLen = 1024;
1578              cgiFilePtr file;
1579              int targetFile;
1580              char storageNameOnServer[2048];
1581              char fileNameOnServer[64];
1582              char contentType[1024];
1583              char buffer[1024];
1584              char *tmpStr = NULL;
1585              int size;
1586              int got, t;
1587              map *path = getMapFromMaps (*main_conf, "main", "tmpPath");
1588              cgiFormFileSize (tmpReqI->name, &size);
1589              cgiFormFileContentType (tmpReqI->name, contentType,
1590                                      sizeof (contentType));
1591              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
1592                {
1593                  t = -1;
1594                  while (1)
1595                    {
1596                      tmpStr = strstr (name + t + 1, "\\");
1597                      if (NULL == tmpStr)
1598                        tmpStr = strstr (name + t + 1, "/");
1599                      if (NULL != tmpStr)
1600                        t = (int) (tmpStr - name);
1601                      else
1602                        break;
1603                    }
1604                  strcpy (fileNameOnServer, name + t + 1);
1605
1606                  sprintf (storageNameOnServer, "%s/%s", path->value,
1607                           fileNameOnServer);
1608#ifdef DEBUG
1609                  fprintf (stderr, "Name on server %s\n",
1610                           storageNameOnServer);
1611                  fprintf (stderr, "fileNameOnServer: %s\n",
1612                           fileNameOnServer);
1613#endif
1614                  targetFile =
1615                    open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
1616                          S_IRWXU | S_IRGRP | S_IROTH);
1617                  if (targetFile < 0)
1618                    {
1619#ifdef DEBUG
1620                      fprintf (stderr, "could not create the new file,%s\n",
1621                               fileNameOnServer);
1622#endif
1623                    }
1624                  else
1625                    {
1626                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
1627                             == cgiFormSuccess)
1628                        {
1629                          if (got > 0)
1630                            write (targetFile, buffer, got);
1631                        }
1632                    }
1633                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
1634                  cgiFormFileClose (file);
1635                  close (targetFile);
1636#ifdef DEBUG
1637                  fprintf (stderr, "File \"%s\" has been uploaded",
1638                           fileNameOnServer);
1639#endif
1640                }
1641            }
1642        }
1643      tmpReqI = tmpReqI->next;
1644    }
1645
1646  ensureDecodedBase64 (request_inputs);
1647  return 1;
1648}
1649
1650
1651/**
1652 * Verify if a parameter value is valid.
1653 *
1654 * @param request the request map
1655 * @param res the error map potentially generated
1656 * @param toCheck the parameter to use
1657 * @param avalues the acceptable values (or null if testing only for presence)
1658 * @param mandatory verify the presence of the parameter if mandatory > 0
1659 */
1660void checkValidValue(map* request,map** res,const char* toCheck,const char** avalues,int mandatory){
1661  map* lres=*res;
1662  map* r_inputs = getMap (request,toCheck);
1663  if (r_inputs == NULL){
1664    if(mandatory>0){
1665      char *replace=_("Mandatory parameter <%s> was not specified");
1666      char *message=(char*)malloc((strlen(replace)+strlen(toCheck)+1)*sizeof(char));
1667      sprintf(message,replace,toCheck);
1668      if(lres==NULL){
1669        lres=createMap("code","MissingParameterValue");
1670        addToMap(lres,"text",message);
1671        addToMap(lres,"locator",toCheck);       
1672      }else{
1673        int length=1;
1674        map* len=getMap(lres,"length");
1675        if(len!=NULL){
1676          length=atoi(len->value);
1677        }
1678        setMapArray(lres,"text",length,message);
1679        setMapArray(lres,"locator",length,toCheck);
1680        setMapArray(lres,"code",length,"MissingParameter");
1681      }
1682      free(message);
1683    }
1684  }else{
1685    if(avalues==NULL)
1686      return;
1687    int nb=0;
1688    int hasValidValue=-1;
1689    if(strncasecmp(toCheck,"Accept",6)==0){
1690      char *tmp=zStrdup(r_inputs->value);
1691      char *pToken,*saveptr;
1692      pToken=strtok_r(tmp,",",&saveptr);
1693      while(pToken!=NULL){
1694        while(avalues[nb]!=NULL){
1695          if(strcasecmp(avalues[nb],pToken)==0){
1696            hasValidValue=1;
1697            break;
1698          }
1699          nb++;
1700        }
1701        pToken=strtok_r(NULL,",",&saveptr);
1702      }
1703      free(tmp);
1704    }else{
1705      while(avalues[nb]!=NULL){
1706        if(strcasecmp(avalues[nb],r_inputs->value)==0){
1707          hasValidValue=1;
1708          break;
1709        }
1710        nb++;
1711      }
1712    }
1713    if(hasValidValue<0){
1714      char *replace=_("The value <%s> was not recognized, %s %s the only acceptable value.");
1715      nb=0;
1716      char *vvalues=NULL;
1717      char* num=_("is");
1718      while(avalues[nb]!=NULL){
1719        char *tvalues;
1720        if(vvalues==NULL){
1721          vvalues=(char*)malloc((strlen(avalues[nb])+3)*sizeof(char));
1722          sprintf(vvalues,"%s",avalues[nb]);
1723        }
1724        else{
1725          tvalues=zStrdup(vvalues);
1726          vvalues=(char*)realloc(vvalues,(strlen(tvalues)+strlen(avalues[nb])+3)*sizeof(char));
1727          sprintf(vvalues,"%s, %s",tvalues,avalues[nb]);
1728          free(tvalues);
1729          num=_("are");
1730        }
1731        nb++;
1732      }
1733      char *message=(char*)malloc((strlen(replace)+strlen(num)+strlen(vvalues)+strlen(toCheck)+1)*sizeof(char));
1734      sprintf(message,replace,toCheck,vvalues,num);
1735      const char *code="VersionNegotiationFailed";
1736      code="InvalidParameterValue";
1737      const char *locator=toCheck;
1738      if( strncasecmp(toCheck,"version",7)==0 ||
1739          strncasecmp(toCheck,"AcceptVersions",14)==0 )
1740        code="VersionNegotiationFailed";
1741      if( strncasecmp(toCheck,"request",7)==0){
1742        code="OperationNotSupported";
1743        locator=r_inputs->value;
1744      }
1745      if(lres==NULL){
1746        lres=createMap("code","InvalidParameterValue");
1747        addToMap(lres,"text",message);
1748        addToMap(lres,"locator",locator);       
1749      }else{
1750        int length=1;
1751        map* len=getMap(lres,"length");
1752        if(len!=NULL){
1753          length=atoi(len->value);
1754        }
1755        setMapArray(lres,"text",length,message);
1756        setMapArray(lres,"locator",length,locator);
1757        setMapArray(lres,"code",length,"InvalidParameterValue");
1758      }
1759    }
1760  }
1761  if(lres!=NULL){
1762    *res=lres;
1763  }
1764}
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