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

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

First version including zoo_service shared library

  • Property svn:keywords set to Id
File size: 45.3 KB
RevLine 
[621]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"
[640]27#include "server_internal.h"
28#include "response_print.h"
29#include "caching.h"
[621]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/**
[640]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/**
[621]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);
[625]201    if(strstr(r_inputs->value,"=")==NULL)
202      cursor_input = url_decode (r_inputs->value);
203    else
204      cursor_input = zStrdup (r_inputs->value);
[621]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      {
[626]213        free(cursor_input);
[621]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          {
[626]224            free(cursor_input);
[621]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                  {
[626]260                    free(cursor_input);
[621]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);
[626]317                        free(cursor_input);
[621]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                        {
[626]325                          free(cursor_input);
[621]326                          return errorException (*main_conf, "Unable to fetch any ressource", "InternalError", NULL);
[626]327                        }
[621]328                      }
329                    free (tmpx2);
[627]330                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]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                          {
[626]351                            free(cursor_input);
[621]352                            return errorException (*main_conf, "Unable to append maps", "InternalError", NULL);
353                          }
354                      }
355                  }
356                else
[623]357                  addMapsToMaps (request_output, tmpmaps);
[621]358              }
359            freeMaps (&tmpmaps);
360            free (tmpmaps);
361            tmpmaps = NULL;
362            free (tmp);
363          }
364      }
365    free (inputs_as_text);
[626]366    free(cursor_input);
[621]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        {
[626]402          free(cursor_output);
[621]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            {
[626]413              free(cursor_output);
[621]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                        {
[626]438                          free(cursor_output);
[621]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
[623]472            addMapsToMaps (request_output, tmp_output);
[621]473          freeMaps (&tmp_output);
474          free (tmp_output);
475          tmp_output = NULL;
476          free (tmp);
477        }
478      free (outputs_as_text);
[626]479      free(cursor_output);
[621]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){
[622]496  int k = 0;
497  int l = 0;
498  for (k=0; k < nodes->nodeNr; k++)
[621]499    {
500      maps *tmpmaps = NULL;
501      xmlNodePtr cur = nodes->nodeTab[k];
502
503      if (nodes->nodeTab[k]->type == XML_ELEMENT_NODE)
504        {
505          // A specific Input node.
506          xmlNodePtr cur2 = cur->children;
507          while (cur2 != NULL)
508            {
509              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
510                cur2 = cur2->next;
511              if (cur2 == NULL)
512                break;
513              // Indentifier
514              if (xmlStrncasecmp
515                  (cur2->name, BAD_CAST "Identifier",
516                   xmlStrlen (cur2->name)) == 0)
517                {
518                  xmlChar *val =
519                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
520                  if (tmpmaps == NULL)
521                    {
522                      tmpmaps = (maps *) malloc (MAPS_SIZE);
523                      if (tmpmaps == NULL)
524                        {
525                          return errorException (*main_conf,
526                                                 _
527                                                 ("Unable to allocate memory."),
528                                                 "InternalError", NULL);
529                        }
530                      tmpmaps->name = zStrdup ((char *) val);
531                      tmpmaps->content = NULL;
532                      tmpmaps->next = NULL;
533                    }
534                  xmlFree (val);
535                }
536              // Title, Asbtract
537              if (xmlStrncasecmp
538                  (cur2->name, BAD_CAST "Title",
539                   xmlStrlen (cur2->name)) == 0
540                  || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract",
541                                     xmlStrlen (cur2->name)) == 0)
542                {
543                  xmlChar *val =
544                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
545                  if (tmpmaps == NULL)
546                    {
547                      tmpmaps = (maps *) malloc (MAPS_SIZE);
548                      if (tmpmaps == NULL)
549                        {
550                          return errorException (*main_conf,
551                                                 _
552                                                 ("Unable to allocate memory."),
553                                                 "InternalError", NULL);
554                        }
555                      tmpmaps->name = zStrdup ("missingIndetifier");
556                      tmpmaps->content =
557                        createMap ((char *) cur2->name, (char *) val);
558                      tmpmaps->next = NULL;
559                    }
560                  else
561                    {
562                      if (tmpmaps->content != NULL)
563                        addToMap (tmpmaps->content,
564                                  (char *) cur2->name, (char *) val);
565                      else
566                        tmpmaps->content =
567                          createMap ((char *) cur2->name, (char *) val);
568                    }
569                  xmlFree (val);
570                }
571              // InputDataFormChoice (Reference or Data ?)
572              if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0)
573                {
574                  // Get every attribute from a Reference node
575                  // mimeType, encoding, schema, href, method
576                  // Header and Body gesture should be added here
577                  const char *refs[5] =
578                    { "mimeType", "encoding", "schema", "method",
579                      "href"
580                    };
[622]581                  for (l = 0; l < 5; l++)
[621]582                    {
583                      xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]);
584                      if (val != NULL && xmlStrlen (val) > 0)
585                        {
586                          if (tmpmaps->content != NULL)
587                            addToMap (tmpmaps->content, refs[l],
588                                      (char *) val);
589                          else
590                            tmpmaps->content =
591                              createMap (refs[l], (char *) val);
592
593                          map *ltmp = getMap (tmpmaps->content, "method");
594                          if (l == 4 )
595                            {
596                              if ((ltmp==NULL || strncmp (ltmp->value, "POST",4) != 0))
597                                {
598                                  if (loadRemoteFile
[623]599                                      (main_conf, &tmpmaps->content, hInternet,
[621]600                                       (char *) val) != 0)
601                                    {
602                                      return errorException (*main_conf,
603                                                             _("Unable to add a request in the queue."),
604                                                             "InternalError",
605                                                             NULL);
606                                    }
[628]607                                  addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]608                                }
[627]609                              addToMap (tmpmaps->content, "Reference", (char*) val);
[621]610                            }
611                        }
612                      xmlFree (val);
613                    }
614                  // Parse Header and Body from Reference
615                  xmlNodePtr cur3 = cur2->children;
616                  while (cur3 != NULL)
617                    {
618                      while (cur3 != NULL
619                             && cur3->type != XML_ELEMENT_NODE)
620                        cur3 = cur3->next;
621                      if (cur3 == NULL)
622                        break;
623                      if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
624                          0)
625                        {
626                          const char *ha[2];
627                          ha[0] = "key";
628                          ha[1] = "value";
629                          int hai;
630                          char *has;
631                          char *key;
632                          for (hai = 0; hai < 2; hai++)
633                            {
634                              xmlChar *val =
635                                xmlGetProp (cur3, BAD_CAST ha[hai]);
636#ifdef POST_DEBUG
637                              fprintf (stderr, "%s = %s\n", ha[hai],
638                                       (char *) val);
639#endif
640                              if (hai == 0)
641                                {
642                                  key = zStrdup ((char *) val);
643                                }
644                              else
645                                {
646                                  has =
647                                    (char *)
648                                    malloc ((4 + xmlStrlen (val) +
649                                             strlen (key)) *
650                                            sizeof (char));
651                                  if (has == NULL)
652                                    {
653                                      return errorException (*main_conf,
654                                                             _
655                                                             ("Unable to allocate memory."),
656                                                             "InternalError",
657                                                             NULL);
658                                    }
659                                  snprintf (has,
660                                            (3 + xmlStrlen (val) +
661                                             strlen (key)), "%s: %s", key,
662                                            (char *) val);
663                                  free (key);
664                                }
665                              xmlFree (val);
666                            }
[629]667                          hInternet->ihandle[hInternet->nb].header = NULL;
[621]668                          hInternet->ihandle[hInternet->nb].header =
669                            curl_slist_append (hInternet->ihandle
670                                               [hInternet->nb].header,
671                                               has);
672                          if (has != NULL)
673                            free (has);
674                        }
675                      else
676                        {
677#ifdef POST_DEBUG
678                          fprintf (stderr,
679                                   "Try to fetch the body part of the request ...\n");
680#endif
681                          if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
682                              == 0)
683                            {
684#ifdef POST_DEBUG
685                              fprintf (stderr, "Body part found (%s) !!!\n",
686                                       (char *) cur3->content);
687#endif
688                              char *tmp = NULL;
689                              xmlNodePtr cur4 = cur3->children;
690                              while (cur4 != NULL)
691                                {
692                                  while (cur4 && cur4 != NULL && cur4->type && cur4->type != XML_ELEMENT_NODE){
693                                    if(cur4->next)
694                                      cur4 = cur4->next;
695                                    else
696                                      cur4 = NULL;
697                                  }
698                                  if(cur4 != NULL) {
699                                    xmlDocPtr bdoc =
700                                      xmlNewDoc (BAD_CAST "1.0");
701                                    bdoc->encoding =
702                                      xmlCharStrdup ("UTF-8");
703                                    xmlDocSetRootElement (bdoc, cur4);
704                                    xmlChar *btmps;
705                                    int bsize;
706                                    // TODO : check for encoding defined in the input
707                                    xmlDocDumpFormatMemoryEnc(bdoc, &btmps, &bsize, "UTF-8", 0);
708                                    if (btmps != NULL){
709                                      tmp = (char *) malloc ((bsize + 1) * sizeof (char));
710
711                                      sprintf (tmp, "%s", (char*) btmps);
712                                         
713                                      xmlFreeDoc (bdoc);
714                                         
715                                      map *btmp =
716                                        getMap (tmpmaps->content, "Reference");
717                                      if (btmp != NULL)
718                                        {
[623]719                                          addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
[621]720                                          InternetOpenUrl (hInternet,
721                                                           btmp->value,
722                                                           tmp,
723                                                           xmlStrlen(btmps),
724                                                           INTERNET_FLAG_NO_CACHE_WRITE,
725                                                           0);
[627]726                                          addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]727                                        }
728                                      xmlFree (btmps);
729                                      free (tmp);
730                                      break;
731                                    }
732                                  }
733                                  cur4 = cur4->next;
734                                }
735                            }
736                          else
737                            if (xmlStrcasecmp
738                                (cur3->name,
739                                 BAD_CAST "BodyReference") == 0)
740                              {
741                                xmlChar *val =
742                                  xmlGetProp (cur3, BAD_CAST "href");
743                                HINTERNET bInternet, res1, res;
744                                bInternet = InternetOpen (
745#ifndef WIN32
746                                                          (LPCTSTR)
747#endif
748                                                          "ZooWPSClient\0",
749                                                          INTERNET_OPEN_TYPE_PRECONFIG,
750                                                          NULL, NULL, 0);
751                                if (!CHECK_INET_HANDLE (bInternet))
752                                  fprintf (stderr,
753                                           "WARNING : bInternet handle failed to initialize");
754                                bInternet.waitingRequests[0] =
755                                  strdup ((char *) val);
756                                res1 =
757                                  InternetOpenUrl (&bInternet,
758                                                   bInternet.waitingRequests
759                                                   [0], NULL, 0,
760                                                   INTERNET_FLAG_NO_CACHE_WRITE,
761                                                   0);
762                                processDownloads (&bInternet);
763                                char *tmp =
764                                  (char *)
765                                  malloc ((bInternet.ihandle[0].nDataLen +
766                                           1) * sizeof (char));
767                                if (tmp == NULL)
768                                  {
769                                    return errorException (*main_conf,
770                                                           _
771                                                           ("Unable to allocate memory."),
772                                                           "InternalError",
773                                                           NULL);
774                                  }
775                                size_t bRead;
776                                InternetReadFile (bInternet.ihandle[0],
777                                                  (LPVOID) tmp,
778                                                  bInternet.
779                                                  ihandle[0].nDataLen,
780                                                  &bRead);
781                                tmp[bInternet.ihandle[0].nDataLen] = 0;
782                                InternetCloseHandle (&bInternet);
783                                map *btmp =
784                                  getMap (tmpmaps->content, "href");
785                                if (btmp != NULL)
786                                  {
[623]787                                    addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
[621]788
789                                    res =
790                                      InternetOpenUrl (hInternet,
791                                                       btmp->value,
792                                                       tmp,
793                                                       strlen(tmp),
794                                                       INTERNET_FLAG_NO_CACHE_WRITE,
795                                                       0);
[627]796                                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]797                                  }
798                                free (tmp);
799                              }
800                        }
801                      cur3 = cur3->next;
802                    }
803                }
804              else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
805                {
806                  xmlNodePtr cur4 = cur2->children;
807                  while (cur4 != NULL)
808                    {
809                      while (cur4 != NULL
810                             && cur4->type != XML_ELEMENT_NODE)
811                        cur4 = cur4->next;
812                      if (cur4 == NULL)
813                        break;
814                      if (xmlStrcasecmp
815                          (cur4->name, BAD_CAST "LiteralData") == 0)
816                        {
817                          // Get every attribute from a LiteralData node
818                          // dataType , uom
819                          char *list[2];
820                          list[0] = zStrdup ("dataType");
821                          list[1] = zStrdup ("uom");
[622]822                          for (l = 0; l < 2; l++)
[621]823                            {
824                              xmlChar *val =
825                                xmlGetProp (cur4, BAD_CAST list[l]);
826                              if (val != NULL
827                                  && strlen ((char *) val) > 0)
828                                {
829                                  if (tmpmaps->content != NULL)
830                                    addToMap (tmpmaps->content, list[l],
831                                              (char *) val);
832                                  else
833                                    tmpmaps->content =
834                                      createMap (list[l], (char *) val);
835                                }
836                              xmlFree (val);
837                              free (list[l]);
838                            }
839                        }
840                      else
841                        if (xmlStrcasecmp
842                            (cur4->name, BAD_CAST "ComplexData") == 0)
843                          {
844                            // Get every attribute from a Reference node
845                            // mimeType, encoding, schema
846                            const char *coms[3] =
847                              { "mimeType", "encoding", "schema" };
[622]848                            for (l = 0; l < 3; l++)
[621]849                              {
850                                xmlChar *val =
851                                  xmlGetProp (cur4, BAD_CAST coms[l]);
852                                if (val != NULL
853                                    && strlen ((char *) val) > 0)
854                                  {
855                                    if (tmpmaps->content != NULL)
856                                      addToMap (tmpmaps->content, coms[l],
857                                                (char *) val);
858                                    else
859                                      tmpmaps->content =
860                                        createMap (coms[l], (char *) val);
861                                  }
862                                xmlFree (val);
863                              }
864                          }
865
866                      map *test = getMap (tmpmaps->content, "encoding");
867
868                      if (test == NULL)
869                        { 
870                          if (tmpmaps->content != NULL)
871                            addToMap (tmpmaps->content, "encoding",
872                                      "utf-8");
873                          else
874                            tmpmaps->content =
875                              createMap ("encoding", "utf-8");
876                          test = getMap (tmpmaps->content, "encoding");
877                        }
878
879                      if (strcasecmp (test->value, "base64") != 0)
880                        { 
881                          xmlChar *mv = xmlNodeListGetString (doc,
882                                                              cur4->xmlChildrenNode,
883                                                              1);
884                          map *ltmp =
885                            getMap (tmpmaps->content, "mimeType");
886                          if (mv == NULL
887                              ||
888                              (xmlStrcasecmp
889                               (cur4->name, BAD_CAST "ComplexData") == 0
890                               && (ltmp == NULL
891                                   || strncasecmp (ltmp->value,
892                                                   "text/xml", 8) == 0)))
893                            {
894                              xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
895                              int buffersize;
896                              xmlNodePtr cur5 = cur4->children;
897                              while (cur5 != NULL
898                                     && cur5->type != XML_ELEMENT_NODE
899                                     && cur5->type !=
900                                     XML_CDATA_SECTION_NODE)
901                                cur5 = cur5->next;
902                              if (cur5 != NULL
903                                  && cur5->type != XML_CDATA_SECTION_NODE)
904                                {
905                                  xmlDocSetRootElement (doc1, cur5);
906                                  xmlDocDumpFormatMemoryEnc (doc1, &mv,
907                                                             &buffersize,
908                                                             "utf-8", 1);
909                                  char size[1024];
910                                  sprintf (size, "%d", buffersize);
911                                  addToMap (tmpmaps->content, "size",
912                                            size);
913                                  xmlFreeDoc (doc1);
914                                }
915                              else
916                                {
917                                  if (cur5 != NULL
918                                      && cur5->type == XML_CDATA_SECTION_NODE){
919                                    xmlDocPtr doc2 = xmlParseMemory((const char*)cur5->content,xmlStrlen(cur5->content));
920                                    xmlDocSetRootElement (doc1,xmlDocGetRootElement(doc2));
921                                    xmlDocDumpFormatMemoryEnc (doc1, &mv,
922                                                               &buffersize,
923                                                               "utf-8", 1);
924                                    char size[1024];
925                                    sprintf (size, "%d", buffersize);
926                                    addToMap (tmpmaps->content, "size",
927                                              size);
928                                    xmlFreeDoc (doc2);
929                                    xmlFreeDoc (doc1);
930                                  }
931                                }
932                            }else{
933                            xmlNodePtr cur5 = cur4->children;
934                            while (cur5 != NULL
935                                   && cur5->type != XML_CDATA_SECTION_NODE)
936                              cur5 = cur5->next;
937                            if (cur5 != NULL
938                                && cur5->type == XML_CDATA_SECTION_NODE){
939                              xmlFree(mv);
940                              mv=xmlStrdup(cur5->content);
941                            }
942                          }
943                          if (mv != NULL)
944                            {
945                              addToMap (tmpmaps->content, "value",
946                                        (char *) mv);
947                              xmlFree (mv);
948                            }
949                        }
950                      else
951                        {
952                          xmlChar *tmp = xmlNodeListGetRawString (doc,
953                                                                  cur4->xmlChildrenNode,
954                                                                  0);
955                          addToMap (tmpmaps->content, "value",
956                                    (char *) tmp);
957                          xmlFree (tmp);
958                        }
959                      cur4 = cur4->next;
960                    }
961                }
962              cur2 = cur2->next;
[631]963              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE){
964                cur2 = cur2->next;
965              }
[621]966            }
967          {
968            maps *testPresence =
969              getMaps (*request_output, tmpmaps->name);
970            if (testPresence != NULL)
971              {
972                elements *elem = getElements (s->inputs, tmpmaps->name);
973                if (elem != NULL)
974                  {
975                    if (appendMapsToMaps
976                        (*main_conf, *request_output, tmpmaps, elem) < 0)
977                      {
978                        return errorException (*main_conf,
979                                               _("Unable to append maps to maps."),
980                                               "InternalError",
981                                               NULL);
982                      }
983                  }
984              }
985            else
[623]986              addMapsToMaps (request_output, tmpmaps);
[621]987          }
988          freeMaps (&tmpmaps);
989          free (tmpmaps);
990          tmpmaps = NULL;
991        }
992    }
993  return 1;
994}
995
996/**
997 * Parse outputs from XML nodes and store them in a maps.
998 *
999 * @param main_conf the conf maps containing the main.cfg settings
1000 * @param request_inputs the map storing KVP raw value
1001 * @param request_output the maps to store the KVP pairs
1002 * @param doc the xmlDocPtr containing the original request
1003 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
1004 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
1005 * @return 0 on success, -1 on failure
1006 */
1007int xmlParseOutputs(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodePtr cur,bool raw){
[622]1008  int l=0;
[621]1009  if( raw == true)
1010    {
1011      addToMap (*request_inputs, "RawDataOutput", "");
1012      if (cur->type == XML_ELEMENT_NODE)
1013        {
1014
1015          maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
1016          if (tmpmaps == NULL)
1017            {
1018              return errorException (*main_conf, _("Unable to allocate memory."),
1019                                     "InternalError", NULL);
1020            }
1021          tmpmaps->name = zStrdup ("unknownIdentifier");
1022          tmpmaps->content = NULL;
1023          tmpmaps->next = NULL;
1024
1025          // Get every attribute from a RawDataOutput node
1026          // mimeType, encoding, schema, uom
1027          const char *outs[4] =
1028            { "mimeType", "encoding", "schema", "uom" };
[622]1029          for (l = 0; l < 4; l++)
[621]1030            {
1031              xmlChar *val = xmlGetProp (cur, BAD_CAST outs[l]);
1032              if (val != NULL)
1033                {
1034                  if (strlen ((char *) val) > 0)
1035                    {
1036                      if (tmpmaps->content != NULL)
1037                        addToMap (tmpmaps->content, outs[l],
1038                                  (char *) val);
1039                      else
1040                        tmpmaps->content =
1041                          createMap (outs[l], (char *) val);
1042                    }
1043                  xmlFree (val);
1044                }
1045            }
1046          xmlNodePtr cur2 = cur->children;
1047          while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1048            cur2 = cur2->next;
1049          while (cur2 != NULL)
1050            {
1051              if (xmlStrncasecmp
1052                  (cur2->name, BAD_CAST "Identifier",
1053                   xmlStrlen (cur2->name)) == 0)
1054                {
1055                  xmlChar *val =
1056                    xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
1057                  free (tmpmaps->name);
1058                  tmpmaps->name = zStrdup ((char *) val);
1059                  xmlFree (val);
1060                }
1061              cur2 = cur2->next;
1062              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1063                cur2 = cur2->next;
1064            }
1065          if (*request_output == NULL)
1066            *request_output = dupMaps (&tmpmaps);
1067          else
[623]1068            addMapsToMaps (request_output, tmpmaps);
[621]1069          if (tmpmaps != NULL)
1070            {
1071              freeMaps (&tmpmaps);
1072              free (tmpmaps);
1073              tmpmaps = NULL;
1074            }
1075        }
1076    }
1077  else
1078    {
1079      addToMap (*request_inputs, "ResponseDocument", "");
1080
1081      if (cur->type == XML_ELEMENT_NODE) {
1082        // Get every attribute: storeExecuteResponse, lineage, status
1083        const char *ress[3] =
1084          { "storeExecuteResponse", "lineage", "status" };
1085        xmlChar *val;
[622]1086        for (l = 0; l < 3; l++)
[621]1087          {
1088            val = xmlGetProp (cur, BAD_CAST ress[l]);
1089            if (val != NULL && strlen ((char *) val) > 0)
1090              {
1091                addToMap (*request_inputs, ress[l], (char *) val);
1092              }
1093            xmlFree (val);
1094          }
1095                       
1096        xmlNodePtr cur1 = cur->children;               
1097        while (cur1 != NULL) // iterate over Output nodes
1098          {
1099            if (cur1->type != XML_ELEMENT_NODE || 
1100                xmlStrncasecmp(cur1->name, BAD_CAST "Output", 
1101                               xmlStrlen (cur1->name)) != 0) {
1102              cur1 = cur1->next;
1103              continue;
1104            }
1105                               
1106            maps *tmpmaps = (maps *) malloc (MAPS_SIZE); // one per Output node
1107            if (tmpmaps == NULL) {
1108              return errorException (*main_conf,
1109                                     _
1110                                     ("Unable to allocate memory."),
1111                                     "InternalError", NULL);
1112            }
1113            tmpmaps->name = zStrdup ("unknownIdentifier");
1114            tmpmaps->content = NULL;
1115            tmpmaps->next = NULL;
1116                               
1117            xmlNodePtr elems = cur1->children;
1118                               
1119            while (elems != NULL) {
1120
1121              // Identifier
1122              if (xmlStrncasecmp
1123                  (elems->name, BAD_CAST "Identifier",
1124                   xmlStrlen (elems->name)) == 0)
1125                {
1126                  xmlChar *val =
1127                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1128               
1129                  free(tmpmaps->name);
1130                  tmpmaps->name = zStrdup ((char *) val);
1131                  if (tmpmaps->content == NULL) {
1132                    tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
1133                  }
1134                  else {
1135                    addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
1136                  }
1137
1138                  map* tt = getMap (*request_inputs, "ResponseDocument");
1139                  if (strlen(tt->value) == 0) {
1140                    addToMap (*request_inputs, "ResponseDocument",
1141                              (char *) val);
1142                  }
1143                  else {
1144                    char* tmp = (char*) malloc((strlen(tt->value) + 1 
1145                                                + strlen((char*) val) + 1) * sizeof(char));
1146                    sprintf (tmp, "%s;%s", tt->value, (char *) val);
1147                    free(tt->value);
1148                    tt->value = tmp;
1149                  }
1150                  xmlFree (val);
1151                }
1152             
1153              // Title, Abstract
1154              else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
1155                                      xmlStrlen (elems->name)) == 0
1156                       || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
1157                                         xmlStrlen (elems->name)) == 0)
1158                {
1159                  xmlChar *val =
1160                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1161                                                       
1162                  if (tmpmaps->content == NULL) {
1163                    tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
1164                  }
1165                  else {
1166                    addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
1167                  }
1168                  xmlFree (val);
1169                }
1170              elems = elems->next;
1171            }
1172                               
1173            // Get every attribute from an Output node:
1174            // mimeType, encoding, schema, uom, asReference
1175            const char *outs[5] =
1176              { "mimeType", "encoding", "schema", "uom", "asReference" };
1177                                         
[622]1178            for (l = 0; l < 5; l++) {
[621]1179              xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                               
1180              if (val != NULL && xmlStrlen(val) > 0) {
1181                if (tmpmaps->content != NULL) {
1182                  addToMap (tmpmaps->content, outs[l], (char *) val);
1183                }                         
1184                else {
1185                  tmpmaps->content = createMap (outs[l], (char *) val);
1186                }       
1187              }
1188              xmlFree (val);
1189            }
1190                               
1191            if (*request_output == NULL) {
1192              *request_output = tmpmaps;
1193            }   
1194            else if (getMaps(*request_output, tmpmaps->name) != NULL) {
1195              return errorException (*main_conf,
1196                                     _
1197                                     ("Duplicate <Output> elements in WPS Execute request"),
1198                                     "InternalError", NULL);
1199            }
1200            else {
1201              maps* mptr = *request_output;
1202              while (mptr->next != NULL) {
1203                mptr = mptr->next;
1204              }
1205              mptr->next = tmpmaps;     
1206            }                                   
1207            cur1 = cur1->next;
1208          }                     
1209      }
1210    }
1211  return 1;
1212}
1213
1214/**
1215 * Parse XML request and store informations in maps.
1216 *
1217 * @param main_conf the conf maps containing the main.cfg settings
1218 * @param post the string containing the XML request
1219 * @param request_inputs the map storing KVP raw value
1220 * @param s the service
1221 * @param inputs the maps to store the KVP pairs
1222 * @param outputs the maps to store the KVP pairs
1223 * @param hInternet the HINTERNET queue to add potential requests
1224 * @return 0 on success, -1 on failure
1225 */
1226int xmlParseRequest(maps** main_conf,const char* post,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1227  xmlInitParser ();
1228  xmlDocPtr doc = xmlParseMemory (post, cgiContentLength);
1229
1230  /**
1231   * Extract Input nodes from the XML Request.
1232   */
1233  xmlXPathObjectPtr tmpsptr =
1234    extractFromDoc (doc, "/*/*/*[local-name()='Input']");
1235  xmlNodeSet *tmps = tmpsptr->nodesetval;
1236  if(xmlParseInputs(main_conf,s,inputs,doc,tmps,hInternet)<0){
1237    xmlXPathFreeObject (tmpsptr);
1238    xmlFreeDoc (doc);
1239    xmlCleanupParser ();
1240    return -1;
1241  }
1242  xmlXPathFreeObject (tmpsptr);
1243
1244  // Extract ResponseDocument / RawDataOutput from the XML Request
1245  tmpsptr =
1246    extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
1247  bool asRaw = false;
1248  tmps = tmpsptr->nodesetval;
1249  if (tmps->nodeNr == 0)
1250    {
1251      xmlXPathFreeObject (tmpsptr);
1252      tmpsptr =
1253        extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
1254      tmps = tmpsptr->nodesetval;
1255      asRaw = true;
1256    }
1257  if(tmps->nodeNr != 0){
1258    if(xmlParseOutputs(main_conf,request_inputs,outputs,doc,tmps->nodeTab[0],asRaw)<0){
1259      xmlXPathFreeObject (tmpsptr);
1260      xmlFreeDoc (doc);
1261      xmlCleanupParser ();
1262      return -1;
1263    }
1264  }
1265  xmlXPathFreeObject (tmpsptr);
1266  xmlFreeDoc (doc);
1267  xmlCleanupParser ();
1268  return 1;
1269}
1270
1271/**
1272 * Parse request and store informations in maps.
1273 *
1274 * @param main_conf the conf maps containing the main.cfg settings
1275 * @param post the string containing the XML request
1276 * @param request_inputs the map storing KVP raw value
1277 * @param s the service
1278 * @param inputs the maps to store the KVP pairs
1279 * @param outputs the maps to store the KVP pairs
1280 * @param hInternet the HINTERNET queue to add potential requests
1281 * @return 0 on success, -1 on failure
1282 * @see kvpParseOutputs,kvpParseInputs,xmlParseRequest
1283 */
1284int parseRequest(maps** main_conf,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1285  map *postRequest = NULL;
1286  postRequest = getMap (*request_inputs, "xrequest");
1287  if (postRequest == NULLMAP)
1288    {
1289      if(kvpParseOutputs(main_conf,*request_inputs,outputs)<0){
1290        return -1;
1291      }
1292      if(kvpParseInputs(main_conf,s,*request_inputs,inputs,hInternet)<0){
1293        return -1;
1294      }
1295    }
1296  else
1297    {
1298      //Parse XML request
1299      if(xmlParseRequest(main_conf,postRequest->value,request_inputs,s,inputs,outputs,hInternet)<0){
1300        return -1;
1301      }
1302    }
1303  return 1;
1304}
1305
1306/**
1307 * Ensure that each requested arguments are present in the request
1308 * DataInputs and ResponseDocument / RawDataOutput. Potentially run
1309 * http requests from the queue in parallel.
1310 * For optional inputs add default values defined in the ZCFG file.
1311 *
1312 * @param main_conf
1313 * @param s
1314 * @param original_request
1315 * @param request_inputs
1316 * @param request_outputs
1317 * @param hInternet
1318 *
1319 * @see runHttpRequests
1320 */
1321int validateRequest(maps** main_conf,service* s,map* original_request, maps** request_inputs,maps** request_outputs,HINTERNET* hInternet){
1322
1323  runHttpRequests (main_conf, request_inputs, hInternet);
1324  InternetCloseHandle (hInternet);
1325
1326  map* errI=NULL;
1327  char *dfv = addDefaultValues (request_inputs, s->inputs, *main_conf, 0,&errI);
1328
1329  maps *ptr = *request_inputs;
1330  while (ptr != NULL)
1331    {
1332      map *tmp0 = getMap (ptr->content, "size");
1333      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
1334      if (tmp1 != NULL && tmp0 != NULL)
1335        {
1336          float i = atof (tmp0->value) / 1048576.0;
1337          if (i >= atoi (tmp1->value))
1338            {
1339              char tmps[1024];
1340              map *tmpe = createMap ("code", "FileSizeExceeded");
1341              snprintf (tmps, 1024,
1342                        _
1343                        ("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)."),
1344                        ptr->name, tmp1->value, i);
1345              addToMap (tmpe, "locator", ptr->name);
1346              addToMap (tmpe, "text", tmps);
1347              printExceptionReportResponse (*main_conf, tmpe);
1348              freeMap (&tmpe);
1349              free (tmpe);
1350              return -1;
1351            }
1352        }
1353      ptr = ptr->next;
1354    }
1355
1356  map* errO=NULL;
1357  char *dfv1 =
1358    addDefaultValues (request_outputs, s->outputs, *main_conf, 1,&errO);
1359  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
1360    {
1361      char tmps[1024];
1362      map *tmpe = NULL;
1363      if (strcmp (dfv, "") != 0)
1364        {
1365          tmpe = createMap ("code", "MissingParameterValue");
1366          int nb=0;
1367          int length=1;
1368          map* len=getMap(errI,"length");
1369          if(len!=NULL)
1370            length=atoi(len->value);
1371          for(nb=0;nb<length;nb++){
1372            map* errp=getMapArray(errI,"value",nb);
1373            snprintf (tmps, 1024,
1374                      _
1375                      ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
1376                      errp->value);
1377            setMapArray (tmpe, "locator", nb , errp->value);
1378            setMapArray (tmpe, "text", nb , tmps);
1379            setMapArray (tmpe, "code", nb , "MissingParameterValue");
1380          }
1381        }
1382      if (strcmp (dfv1, "") != 0)
1383        {
1384          int ilength=0;
1385          if(tmpe==NULL)
1386            tmpe = createMap ("code", "InvalidParameterValue");
1387          else{
1388            map* len=getMap(tmpe,"length");
1389            if(len!=NULL)
1390              ilength=atoi(len->value);
1391          }
1392          int nb=0;
1393          int length=1;
1394          map* len=getMap(errO,"length");
1395          if(len!=NULL)
1396            length=atoi(len->value);
1397          for(nb=0;nb<length;nb++){
1398            map* errp=getMapArray(errO,"value",nb);
1399            snprintf (tmps, 1024,
1400                      _
1401                      ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
1402                      errp->value,
1403                      ((getMap(original_request,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
1404            setMapArray (tmpe, "locator", nb+ilength , errp->value);
1405            setMapArray (tmpe, "text", nb+ilength , tmps);
1406            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
1407          }
1408        }
1409      printExceptionReportResponse (*main_conf, tmpe);
1410      if(errI!=NULL){
1411        freeMap(&errI);
1412        free(errI);
1413      }
1414      if(errO!=NULL){
1415        freeMap(&errO);
1416        free(errO);
1417      }
1418      freeMap (&tmpe);
1419      free (tmpe);
1420      return -1;
1421    }
1422  maps *tmpReqI = *request_inputs;
1423  while (tmpReqI != NULL)
1424    {
1425      char name[1024];
1426      if (getMap (tmpReqI->content, "isFile") != NULL)
1427        {
1428          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
1429              cgiFormSuccess)
1430            {
1431              int BufferLen = 1024;
1432              cgiFilePtr file;
1433              int targetFile;
1434              char storageNameOnServer[2048];
1435              char fileNameOnServer[64];
1436              char contentType[1024];
1437              char buffer[1024];
1438              char *tmpStr = NULL;
1439              int size;
1440              int got, t;
1441              map *path = getMapFromMaps (*main_conf, "main", "tmpPath");
1442              cgiFormFileSize (tmpReqI->name, &size);
1443              cgiFormFileContentType (tmpReqI->name, contentType,
1444                                      sizeof (contentType));
1445              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
1446                {
1447                  t = -1;
1448                  while (1)
1449                    {
1450                      tmpStr = strstr (name + t + 1, "\\");
1451                      if (NULL == tmpStr)
1452                        tmpStr = strstr (name + t + 1, "/");
1453                      if (NULL != tmpStr)
1454                        t = (int) (tmpStr - name);
1455                      else
1456                        break;
1457                    }
1458                  strcpy (fileNameOnServer, name + t + 1);
1459
1460                  sprintf (storageNameOnServer, "%s/%s", path->value,
1461                           fileNameOnServer);
1462#ifdef DEBUG
1463                  fprintf (stderr, "Name on server %s\n",
1464                           storageNameOnServer);
1465                  fprintf (stderr, "fileNameOnServer: %s\n",
1466                           fileNameOnServer);
1467#endif
1468                  targetFile =
1469                    open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
1470                          S_IRWXU | S_IRGRP | S_IROTH);
1471                  if (targetFile < 0)
1472                    {
1473#ifdef DEBUG
1474                      fprintf (stderr, "could not create the new file,%s\n",
1475                               fileNameOnServer);
1476#endif
1477                    }
1478                  else
1479                    {
1480                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
1481                             == cgiFormSuccess)
1482                        {
1483                          if (got > 0)
1484                            write (targetFile, buffer, got);
1485                        }
1486                    }
1487                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
1488                  cgiFormFileClose (file);
1489                  close (targetFile);
1490#ifdef DEBUG
1491                  fprintf (stderr, "File \"%s\" has been uploaded",
1492                           fileNameOnServer);
1493#endif
1494                }
1495            }
1496        }
1497      tmpReqI = tmpReqI->next;
1498    }
1499
1500  ensureDecodedBase64 (request_inputs);
1501  return 1;
1502}
[640]1503
1504
1505/**
1506 * Verify if a parameter value is valid.
1507 *
1508 * @param request the request map
1509 * @param res the error map potentially generated
1510 * @param toCheck the parameter to use
1511 * @param avalues the acceptable values (or null if testing only for presence)
1512 * @param mandatory verify the presence of the parameter if mandatory > 0
1513 */
1514void checkValidValue(map* request,map** res,const char* toCheck,const char** avalues,int mandatory){
1515  map* lres=*res;
1516  map* r_inputs = getMap (request,toCheck);
1517  if (r_inputs == NULL){
1518    if(mandatory>0){
1519      char *replace=_("Mandatory parameter <%s> was not specified");
1520      char *message=(char*)malloc((strlen(replace)+strlen(toCheck)+1)*sizeof(char));
1521      sprintf(message,replace,toCheck);
1522      if(lres==NULL){
1523        lres=createMap("code","MissingParameterValue");
1524        addToMap(lres,"text",message);
1525        addToMap(lres,"locator",toCheck);       
1526      }else{
1527        int length=1;
1528        map* len=getMap(lres,"length");
1529        if(len!=NULL){
1530          length=atoi(len->value);
1531        }
1532        setMapArray(lres,"text",length,message);
1533        setMapArray(lres,"locator",length,toCheck);
1534        setMapArray(lres,"code",length,"MissingParameter");
1535      }
1536      free(message);
1537    }
1538  }else{
1539    if(avalues==NULL)
1540      return;
1541    int nb=0;
1542    int hasValidValue=-1;
1543    if(strncasecmp(toCheck,"Accept",6)==0){
1544      char *tmp=zStrdup(r_inputs->value);
1545      char *pToken,*saveptr;
1546      pToken=strtok_r(tmp,",",&saveptr);
1547      while(pToken!=NULL){
1548        while(avalues[nb]!=NULL){
1549          if(strcasecmp(avalues[nb],pToken)==0){
1550            hasValidValue=1;
1551            break;
1552          }
1553          nb++;
1554        }
1555        pToken=strtok_r(NULL,",",&saveptr);
1556      }
1557      free(tmp);
1558    }else{
1559      while(avalues[nb]!=NULL){
1560        if(strcasecmp(avalues[nb],r_inputs->value)==0){
1561          hasValidValue=1;
1562          break;
1563        }
1564        nb++;
1565      }
1566    }
1567    if(hasValidValue<0){
1568      char *replace=_("The value <%s> was not recognized, %s %s the only acceptable value.");
1569      nb=0;
1570      char *vvalues=NULL;
1571      char* num=_("is");
1572      while(avalues[nb]!=NULL){
1573        char *tvalues;
1574        if(vvalues==NULL){
1575          vvalues=(char*)malloc((strlen(avalues[nb])+3)*sizeof(char));
1576          sprintf(vvalues,"%s",avalues[nb]);
1577        }
1578        else{
1579          tvalues=zStrdup(vvalues);
1580          vvalues=(char*)realloc(vvalues,(strlen(tvalues)+strlen(avalues[nb])+3)*sizeof(char));
1581          sprintf(vvalues,"%s, %s",tvalues,avalues[nb]);
1582          free(tvalues);
1583          num=_("are");
1584        }
1585        nb++;
1586      }
1587      char *message=(char*)malloc((strlen(replace)+strlen(num)+strlen(vvalues)+strlen(toCheck)+1)*sizeof(char));
1588      sprintf(message,replace,toCheck,vvalues,num);
1589      const char *code="VersionNegotiationFailed";
1590      code="InvalidParameterValue";
1591      const char *locator=toCheck;
1592      if( strncasecmp(toCheck,"version",7)==0 ||
1593          strncasecmp(toCheck,"AcceptVersions",14)==0 )
1594        code="VersionNegotiationFailed";
1595      if( strncasecmp(toCheck,"request",7)==0){
1596        code="OperationNotSupported";
1597        locator=r_inputs->value;
1598      }
1599      if(lres==NULL){
1600        lres=createMap("code","InvalidParameterValue");
1601        addToMap(lres,"text",message);
1602        addToMap(lres,"locator",locator);       
1603      }else{
1604        int length=1;
1605        map* len=getMap(lres,"length");
1606        if(len!=NULL){
1607          length=atoi(len->value);
1608        }
1609        setMapArray(lres,"text",length,message);
1610        setMapArray(lres,"locator",length,locator);
1611        setMapArray(lres,"code",length,"InvalidParameterValue");
1612      }
1613    }
1614  }
1615  if(lres!=NULL){
1616    *res=lres;
1617  }
1618}
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