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

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

Remove memory leak in parser_request.

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