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

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

Fix issue for downloaded inputs which support multiple values.

  • Property svn:keywords set to Id
File size: 41.1 KB
Line 
1/*
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2015 GeoLabs SARL
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "request_parser.h"
26#include "service_internal.h"
27
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);
148    if(strstr(r_inputs->value,"=")==NULL)
149      cursor_input = url_decode (r_inputs->value);
150    else
151      cursor_input = zStrdup (r_inputs->value);
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      {
160        free(cursor_input);
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          {
171            free(cursor_input);
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                  {
207                    free(cursor_input);
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);
264                        free(cursor_input);
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                        {
272                          free(cursor_input);
273                          return errorException (*main_conf, "Unable to fetch any ressource", "InternalError", NULL);
274                        }
275                      }
276                    free (tmpx2);
277                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
278                    addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
279                  }
280                tmpc = strtok (NULL, "@");
281              }
282            if (*request_output == NULL)
283              *request_output = dupMaps (&tmpmaps);
284            else
285              {
286                maps *testPresence =
287                  getMaps (*request_output, tmpmaps->name);
288                if (testPresence != NULL)
289                  {
290                    elements *elem =
291                      getElements (s->inputs, tmpmaps->name);
292                    if (elem != NULL)
293                      {
294                        if (appendMapsToMaps
295                            (*main_conf, *request_output, tmpmaps,
296                             elem) < 0)
297                          {
298                            free(cursor_input);
299                            return errorException (*main_conf, "Unable to append maps", "InternalError", NULL);
300                          }
301                      }
302                  }
303                else
304                  addMapsToMaps (request_output, tmpmaps);
305              }
306            freeMaps (&tmpmaps);
307            free (tmpmaps);
308            tmpmaps = NULL;
309            free (tmp);
310          }
311      }
312    free (inputs_as_text);
313    free(cursor_input);
314  }
315  return 1;
316}
317
318/**
319 * Parse outputs provided as KVP and store them in a maps.
320 *
321 * @param main_conf the conf maps containing the main.cfg settings
322 * @param request_inputs the map storing KVP raw value
323 * @param request_output the maps to store the KVP pairs
324 * @return 0 on success, -1 on failure
325 */
326int kvpParseOutputs(maps** main_conf,map *request_inputs,maps** request_output){
327  /**
328   * Parsing outputs provided as KVP
329   */
330  map *r_inputs = NULL;
331  r_inputs = getMap (request_inputs, "ResponseDocument");
332  if (r_inputs == NULL)
333    r_inputs = getMap (request_inputs, "RawDataOutput");
334
335  if (r_inputs != NULL)
336    {
337      char *cursor_output = zStrdup (r_inputs->value);
338      int j = 0;
339
340      /**
341       * Put each Output into the outputs_as_text array
342       */
343      char *pToken;
344      maps *tmp_output = NULL;
345      pToken = strtok (cursor_output, ";");
346      char **outputs_as_text = (char **) malloc (128 * sizeof (char *));
347      if (outputs_as_text == NULL)
348        {
349          free(cursor_output);
350          return errorException (*main_conf, _("Unable to allocate memory"),
351                                 "InternalError", NULL);
352        }
353      int i = 0;
354      while (pToken != NULL)
355        {
356          outputs_as_text[i] =
357            (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
358          if (outputs_as_text[i] == NULL)
359            {
360              free(cursor_output);
361              return errorException (*main_conf, _("Unable to allocate memory"),
362                                     "InternalError", NULL);
363            }
364          snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s",
365                    pToken);
366          pToken = strtok (NULL, ";");
367          i++;
368        }
369      for (j = 0; j < i; j++)
370        {
371          char *tmp = zStrdup (outputs_as_text[j]);
372          free (outputs_as_text[j]);
373          char *tmpc;
374          tmpc = strtok (tmp, "@");
375          int k = 0;
376          while (tmpc != NULL)
377            {
378              if (k == 0)
379                {
380                  if (tmp_output == NULL)
381                    {
382                      tmp_output = (maps *) malloc (MAPS_SIZE);
383                      if (tmp_output == NULL)
384                        {
385                          free(cursor_output);
386                          return errorException (*main_conf,
387                                                 _
388                                                 ("Unable to allocate memory."),
389                                                 "InternalError", NULL);
390                        }
391                      tmp_output->name = zStrdup (tmpc);
392                      tmp_output->content = NULL;
393                      tmp_output->next = NULL;
394                    }
395                }
396              else
397                {
398                  char *tmpv = strstr (tmpc, "=");
399                  char tmpn[256];
400                  memset (tmpn, 0, 256);
401                  strncpy (tmpn, tmpc,
402                           (strlen (tmpc) -
403                            strlen (tmpv)) * sizeof (char));
404                  tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
405                  if (tmp_output->content == NULL)
406                    {
407                      tmp_output->content = createMap (tmpn, tmpv + 1);
408                      tmp_output->content->next = NULL;
409                    }
410                  else
411                    addToMap (tmp_output->content, tmpn, tmpv + 1);
412                }
413              k++;
414              tmpc = strtok (NULL, "@");
415            }
416          if (*request_output == NULL)
417            *request_output = dupMaps (&tmp_output);
418          else
419            addMapsToMaps (request_output, tmp_output);
420          freeMaps (&tmp_output);
421          free (tmp_output);
422          tmp_output = NULL;
423          free (tmp);
424        }
425      free (outputs_as_text);
426      free(cursor_output);
427    }
428  return 1;
429}
430
431/**
432 * Parse inputs from XML nodes and store them in a maps.
433 *
434 * @param main_conf the conf maps containing the main.cfg settings
435 * @param s the service
436 * @param request_output the maps to store the KVP pairs
437 * @param doc the xmlDocPtr containing the original request
438 * @param nodes the input nodes array
439 * @param hInternet the HINTERNET queue to add potential requests
440 * @return 0 on success, -1 on failure
441 */
442int xmlParseInputs(maps** main_conf,service* s,maps** request_output,xmlDocPtr doc,xmlNodeSet* nodes,HINTERNET* hInternet){
443  int k = 0;
444  int l = 0;
445  for (k=0; k < nodes->nodeNr; k++)
446    {
447      maps *tmpmaps = NULL;
448      xmlNodePtr cur = nodes->nodeTab[k];
449
450      if (nodes->nodeTab[k]->type == XML_ELEMENT_NODE)
451        {
452          // A specific Input node.
453          xmlNodePtr cur2 = cur->children;
454          while (cur2 != NULL)
455            {
456              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
457                cur2 = cur2->next;
458              if (cur2 == NULL)
459                break;
460              // Indentifier
461              if (xmlStrncasecmp
462                  (cur2->name, BAD_CAST "Identifier",
463                   xmlStrlen (cur2->name)) == 0)
464                {
465                  xmlChar *val =
466                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
467                  if (tmpmaps == NULL)
468                    {
469                      tmpmaps = (maps *) malloc (MAPS_SIZE);
470                      if (tmpmaps == NULL)
471                        {
472                          return errorException (*main_conf,
473                                                 _
474                                                 ("Unable to allocate memory."),
475                                                 "InternalError", NULL);
476                        }
477                      tmpmaps->name = zStrdup ((char *) val);
478                      tmpmaps->content = NULL;
479                      tmpmaps->next = NULL;
480                    }
481                  xmlFree (val);
482                }
483              // Title, Asbtract
484              if (xmlStrncasecmp
485                  (cur2->name, BAD_CAST "Title",
486                   xmlStrlen (cur2->name)) == 0
487                  || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract",
488                                     xmlStrlen (cur2->name)) == 0)
489                {
490                  xmlChar *val =
491                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
492                  if (tmpmaps == NULL)
493                    {
494                      tmpmaps = (maps *) malloc (MAPS_SIZE);
495                      if (tmpmaps == NULL)
496                        {
497                          return errorException (*main_conf,
498                                                 _
499                                                 ("Unable to allocate memory."),
500                                                 "InternalError", NULL);
501                        }
502                      tmpmaps->name = zStrdup ("missingIndetifier");
503                      tmpmaps->content =
504                        createMap ((char *) cur2->name, (char *) val);
505                      tmpmaps->next = NULL;
506                    }
507                  else
508                    {
509                      if (tmpmaps->content != NULL)
510                        addToMap (tmpmaps->content,
511                                  (char *) cur2->name, (char *) val);
512                      else
513                        tmpmaps->content =
514                          createMap ((char *) cur2->name, (char *) val);
515                    }
516                  xmlFree (val);
517                }
518              // InputDataFormChoice (Reference or Data ?)
519              if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0)
520                {
521                  // Get every attribute from a Reference node
522                  // mimeType, encoding, schema, href, method
523                  // Header and Body gesture should be added here
524                  const char *refs[5] =
525                    { "mimeType", "encoding", "schema", "method",
526                      "href"
527                    };
528                  for (l = 0; l < 5; l++)
529                    {
530                      xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]);
531                      if (val != NULL && xmlStrlen (val) > 0)
532                        {
533                          if (tmpmaps->content != NULL)
534                            addToMap (tmpmaps->content, refs[l],
535                                      (char *) val);
536                          else
537                            tmpmaps->content =
538                              createMap (refs[l], (char *) val);
539
540                          map *ltmp = getMap (tmpmaps->content, "method");
541                          if (l == 4 )
542                            {
543                              if ((ltmp==NULL || strncmp (ltmp->value, "POST",4) != 0))
544                                {
545                                  if (loadRemoteFile
546                                      (main_conf, &tmpmaps->content, hInternet,
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                              addToMap (tmpmaps->content, "Reference", (char*) val);
556                            }
557                        }
558                      xmlFree (val);
559                    }
560                  // Parse Header and Body from Reference
561                  xmlNodePtr cur3 = cur2->children;
562                  while (cur3 != NULL)
563                    {
564                      while (cur3 != NULL
565                             && cur3->type != XML_ELEMENT_NODE)
566                        cur3 = cur3->next;
567                      if (cur3 == NULL)
568                        break;
569                      if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
570                          0)
571                        {
572                          const char *ha[2];
573                          ha[0] = "key";
574                          ha[1] = "value";
575                          int hai;
576                          char *has;
577                          char *key;
578                          for (hai = 0; hai < 2; hai++)
579                            {
580                              xmlChar *val =
581                                xmlGetProp (cur3, BAD_CAST ha[hai]);
582#ifdef POST_DEBUG
583                              fprintf (stderr, "%s = %s\n", ha[hai],
584                                       (char *) val);
585#endif
586                              if (hai == 0)
587                                {
588                                  key = zStrdup ((char *) val);
589                                }
590                              else
591                                {
592                                  has =
593                                    (char *)
594                                    malloc ((4 + xmlStrlen (val) +
595                                             strlen (key)) *
596                                            sizeof (char));
597                                  if (has == NULL)
598                                    {
599                                      return errorException (*main_conf,
600                                                             _
601                                                             ("Unable to allocate memory."),
602                                                             "InternalError",
603                                                             NULL);
604                                    }
605                                  snprintf (has,
606                                            (3 + xmlStrlen (val) +
607                                             strlen (key)), "%s: %s", key,
608                                            (char *) val);
609                                  free (key);
610                                }
611                              xmlFree (val);
612                            }
613                          hInternet->ihandle[hInternet->nb].header =
614                            curl_slist_append (hInternet->ihandle
615                                               [hInternet->nb].header,
616                                               has);
617                          if (has != NULL)
618                            free (has);
619                        }
620                      else
621                        {
622#ifdef POST_DEBUG
623                          fprintf (stderr,
624                                   "Try to fetch the body part of the request ...\n");
625#endif
626                          if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
627                              == 0)
628                            {
629#ifdef POST_DEBUG
630                              fprintf (stderr, "Body part found (%s) !!!\n",
631                                       (char *) cur3->content);
632#endif
633                              char *tmp = NULL;
634                              xmlNodePtr cur4 = cur3->children;
635                              while (cur4 != NULL)
636                                {
637                                  while (cur4 && cur4 != NULL && cur4->type && cur4->type != XML_ELEMENT_NODE){
638                                    if(cur4->next)
639                                      cur4 = cur4->next;
640                                    else
641                                      cur4 = NULL;
642                                  }
643                                  if(cur4 != NULL) {
644                                    xmlDocPtr bdoc =
645                                      xmlNewDoc (BAD_CAST "1.0");
646                                    bdoc->encoding =
647                                      xmlCharStrdup ("UTF-8");
648                                    xmlDocSetRootElement (bdoc, cur4);
649                                    xmlChar *btmps;
650                                    int bsize;
651                                    // TODO : check for encoding defined in the input
652                                    xmlDocDumpFormatMemoryEnc(bdoc, &btmps, &bsize, "UTF-8", 0);
653                                    if (btmps != NULL){
654                                      tmp = (char *) malloc ((bsize + 1) * sizeof (char));
655
656                                      sprintf (tmp, "%s", (char*) btmps);
657                                         
658                                      xmlFreeDoc (bdoc);
659                                         
660                                      map *btmp =
661                                        getMap (tmpmaps->content, "Reference");
662                                      if (btmp != NULL)
663                                        {
664                                          addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
665                                          InternetOpenUrl (hInternet,
666                                                           btmp->value,
667                                                           tmp,
668                                                           xmlStrlen(btmps),
669                                                           INTERNET_FLAG_NO_CACHE_WRITE,
670                                                           0);
671                                          addIntToMap (tmpmaps->content, "Order", hInternet->nb);
672                                        }
673                                      xmlFree (btmps);
674                                      free (tmp);
675                                      break;
676                                    }
677                                  }
678                                  cur4 = cur4->next;
679                                }
680                            }
681                          else
682                            if (xmlStrcasecmp
683                                (cur3->name,
684                                 BAD_CAST "BodyReference") == 0)
685                              {
686                                xmlChar *val =
687                                  xmlGetProp (cur3, BAD_CAST "href");
688                                HINTERNET bInternet, res1, res;
689                                bInternet = InternetOpen (
690#ifndef WIN32
691                                                          (LPCTSTR)
692#endif
693                                                          "ZooWPSClient\0",
694                                                          INTERNET_OPEN_TYPE_PRECONFIG,
695                                                          NULL, NULL, 0);
696                                if (!CHECK_INET_HANDLE (bInternet))
697                                  fprintf (stderr,
698                                           "WARNING : bInternet handle failed to initialize");
699                                bInternet.waitingRequests[0] =
700                                  strdup ((char *) val);
701                                res1 =
702                                  InternetOpenUrl (&bInternet,
703                                                   bInternet.waitingRequests
704                                                   [0], NULL, 0,
705                                                   INTERNET_FLAG_NO_CACHE_WRITE,
706                                                   0);
707                                processDownloads (&bInternet);
708                                char *tmp =
709                                  (char *)
710                                  malloc ((bInternet.ihandle[0].nDataLen +
711                                           1) * sizeof (char));
712                                if (tmp == NULL)
713                                  {
714                                    return errorException (*main_conf,
715                                                           _
716                                                           ("Unable to allocate memory."),
717                                                           "InternalError",
718                                                           NULL);
719                                  }
720                                size_t bRead;
721                                InternetReadFile (bInternet.ihandle[0],
722                                                  (LPVOID) tmp,
723                                                  bInternet.
724                                                  ihandle[0].nDataLen,
725                                                  &bRead);
726                                tmp[bInternet.ihandle[0].nDataLen] = 0;
727                                InternetCloseHandle (&bInternet);
728                                map *btmp =
729                                  getMap (tmpmaps->content, "href");
730                                if (btmp != NULL)
731                                  {
732                                    addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
733
734                                    res =
735                                      InternetOpenUrl (hInternet,
736                                                       btmp->value,
737                                                       tmp,
738                                                       strlen(tmp),
739                                                       INTERNET_FLAG_NO_CACHE_WRITE,
740                                                       0);
741                                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
742                                  }
743                                free (tmp);
744                              }
745                        }
746                      cur3 = cur3->next;
747                    }
748                }
749              else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
750                {
751                  xmlNodePtr cur4 = cur2->children;
752                  while (cur4 != NULL)
753                    {
754                      while (cur4 != NULL
755                             && cur4->type != XML_ELEMENT_NODE)
756                        cur4 = cur4->next;
757                      if (cur4 == NULL)
758                        break;
759                      if (xmlStrcasecmp
760                          (cur4->name, BAD_CAST "LiteralData") == 0)
761                        {
762                          // Get every attribute from a LiteralData node
763                          // dataType , uom
764                          char *list[2];
765                          list[0] = zStrdup ("dataType");
766                          list[1] = zStrdup ("uom");
767                          for (l = 0; l < 2; l++)
768                            {
769                              xmlChar *val =
770                                xmlGetProp (cur4, BAD_CAST list[l]);
771                              if (val != NULL
772                                  && strlen ((char *) val) > 0)
773                                {
774                                  if (tmpmaps->content != NULL)
775                                    addToMap (tmpmaps->content, list[l],
776                                              (char *) val);
777                                  else
778                                    tmpmaps->content =
779                                      createMap (list[l], (char *) val);
780                                }
781                              xmlFree (val);
782                              free (list[l]);
783                            }
784                        }
785                      else
786                        if (xmlStrcasecmp
787                            (cur4->name, BAD_CAST "ComplexData") == 0)
788                          {
789                            // Get every attribute from a Reference node
790                            // mimeType, encoding, schema
791                            const char *coms[3] =
792                              { "mimeType", "encoding", "schema" };
793                            for (l = 0; l < 3; l++)
794                              {
795                                xmlChar *val =
796                                  xmlGetProp (cur4, BAD_CAST coms[l]);
797                                if (val != NULL
798                                    && strlen ((char *) val) > 0)
799                                  {
800                                    if (tmpmaps->content != NULL)
801                                      addToMap (tmpmaps->content, coms[l],
802                                                (char *) val);
803                                    else
804                                      tmpmaps->content =
805                                        createMap (coms[l], (char *) val);
806                                  }
807                                xmlFree (val);
808                              }
809                          }
810
811                      map *test = getMap (tmpmaps->content, "encoding");
812
813                      if (test == NULL)
814                        { 
815                          if (tmpmaps->content != NULL)
816                            addToMap (tmpmaps->content, "encoding",
817                                      "utf-8");
818                          else
819                            tmpmaps->content =
820                              createMap ("encoding", "utf-8");
821                          test = getMap (tmpmaps->content, "encoding");
822                        }
823
824                      if (strcasecmp (test->value, "base64") != 0)
825                        { 
826                          xmlChar *mv = xmlNodeListGetString (doc,
827                                                              cur4->xmlChildrenNode,
828                                                              1);
829                          map *ltmp =
830                            getMap (tmpmaps->content, "mimeType");
831                          if (mv == NULL
832                              ||
833                              (xmlStrcasecmp
834                               (cur4->name, BAD_CAST "ComplexData") == 0
835                               && (ltmp == NULL
836                                   || strncasecmp (ltmp->value,
837                                                   "text/xml", 8) == 0)))
838                            {
839                              xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
840                              int buffersize;
841                              xmlNodePtr cur5 = cur4->children;
842                              while (cur5 != NULL
843                                     && cur5->type != XML_ELEMENT_NODE
844                                     && cur5->type !=
845                                     XML_CDATA_SECTION_NODE)
846                                cur5 = cur5->next;
847                              if (cur5 != NULL
848                                  && cur5->type != XML_CDATA_SECTION_NODE)
849                                {
850                                  xmlDocSetRootElement (doc1, cur5);
851                                  xmlDocDumpFormatMemoryEnc (doc1, &mv,
852                                                             &buffersize,
853                                                             "utf-8", 1);
854                                  char size[1024];
855                                  sprintf (size, "%d", buffersize);
856                                  addToMap (tmpmaps->content, "size",
857                                            size);
858                                  xmlFreeDoc (doc1);
859                                }
860                              else
861                                {
862                                  if (cur5 != NULL
863                                      && cur5->type == XML_CDATA_SECTION_NODE){
864                                    xmlDocPtr doc2 = xmlParseMemory((const char*)cur5->content,xmlStrlen(cur5->content));
865                                    xmlDocSetRootElement (doc1,xmlDocGetRootElement(doc2));
866                                    xmlDocDumpFormatMemoryEnc (doc1, &mv,
867                                                               &buffersize,
868                                                               "utf-8", 1);
869                                    char size[1024];
870                                    sprintf (size, "%d", buffersize);
871                                    addToMap (tmpmaps->content, "size",
872                                              size);
873                                    xmlFreeDoc (doc2);
874                                    xmlFreeDoc (doc1);
875                                  }
876                                }
877                            }else{
878                            xmlNodePtr cur5 = cur4->children;
879                            while (cur5 != NULL
880                                   && cur5->type != XML_CDATA_SECTION_NODE)
881                              cur5 = cur5->next;
882                            if (cur5 != NULL
883                                && cur5->type == XML_CDATA_SECTION_NODE){
884                              xmlFree(mv);
885                              mv=xmlStrdup(cur5->content);
886                            }
887                          }
888                          if (mv != NULL)
889                            {
890                              addToMap (tmpmaps->content, "value",
891                                        (char *) mv);
892                              xmlFree (mv);
893                            }
894                        }
895                      else
896                        {
897                          xmlChar *tmp = xmlNodeListGetRawString (doc,
898                                                                  cur4->xmlChildrenNode,
899                                                                  0);
900                          addToMap (tmpmaps->content, "value",
901                                    (char *) tmp);
902                          map *tmpv = getMap (tmpmaps->content, "value");
903                          char *res = NULL;
904                          char *curs = tmpv->value;
905                          int i = 0;
906                          for (i = 0; i <= strlen (tmpv->value) / 64;
907                               i++)
908                            {
909                              if (res == NULL)
910                                res =
911                                  (char *) malloc (67 * sizeof (char));
912                              else
913                                res =
914                                  (char *) realloc (res,
915                                                    (((i + 1) * 65) +
916                                                     i) * sizeof (char));
917                              int csize = i * 65;
918                              strncpy (res + csize, curs, 64);
919                              if (i == xmlStrlen (tmp) / 64)
920                                strcat (res, "\n\0");
921                              else
922                                {
923                                  strncpy (res + (((i + 1) * 64) + i),
924                                           "\n\0", 2);
925                                  curs += 64;
926                                }
927                            }
928                          free (tmpv->value);
929                          tmpv->value = zStrdup (res);
930                          free (res);
931                          xmlFree (tmp);
932                        }
933                      cur4 = cur4->next;
934                    }
935                }
936              cur2 = cur2->next;
937            }
938
939          {
940            maps *testPresence =
941              getMaps (*request_output, tmpmaps->name);
942            if (testPresence != NULL)
943              {
944                elements *elem = getElements (s->inputs, tmpmaps->name);
945                if (elem != NULL)
946                  {
947                    if (appendMapsToMaps
948                        (*main_conf, *request_output, tmpmaps, elem) < 0)
949                      {
950                        return errorException (*main_conf,
951                                               _("Unable to append maps to maps."),
952                                               "InternalError",
953                                               NULL);
954                      }
955                  }
956              }
957            else
958              addMapsToMaps (request_output, tmpmaps);
959          }
960          freeMaps (&tmpmaps);
961          free (tmpmaps);
962          tmpmaps = NULL;
963        }
964    }
965  return 1;
966}
967
968/**
969 * Parse outputs from XML nodes and store them in a maps.
970 *
971 * @param main_conf the conf maps containing the main.cfg settings
972 * @param request_inputs the map storing KVP raw value
973 * @param request_output the maps to store the KVP pairs
974 * @param doc the xmlDocPtr containing the original request
975 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
976 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
977 * @return 0 on success, -1 on failure
978 */
979int xmlParseOutputs(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodePtr cur,bool raw){
980  int l=0;
981  if( raw == true)
982    {
983      addToMap (*request_inputs, "RawDataOutput", "");
984      if (cur->type == XML_ELEMENT_NODE)
985        {
986
987          maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
988          if (tmpmaps == NULL)
989            {
990              return errorException (*main_conf, _("Unable to allocate memory."),
991                                     "InternalError", NULL);
992            }
993          tmpmaps->name = zStrdup ("unknownIdentifier");
994          tmpmaps->content = NULL;
995          tmpmaps->next = NULL;
996
997          // Get every attribute from a RawDataOutput node
998          // mimeType, encoding, schema, uom
999          const char *outs[4] =
1000            { "mimeType", "encoding", "schema", "uom" };
1001          for (l = 0; l < 4; l++)
1002            {
1003              xmlChar *val = xmlGetProp (cur, BAD_CAST outs[l]);
1004              if (val != NULL)
1005                {
1006                  if (strlen ((char *) val) > 0)
1007                    {
1008                      if (tmpmaps->content != NULL)
1009                        addToMap (tmpmaps->content, outs[l],
1010                                  (char *) val);
1011                      else
1012                        tmpmaps->content =
1013                          createMap (outs[l], (char *) val);
1014                    }
1015                  xmlFree (val);
1016                }
1017            }
1018          xmlNodePtr cur2 = cur->children;
1019          while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1020            cur2 = cur2->next;
1021          while (cur2 != NULL)
1022            {
1023              if (xmlStrncasecmp
1024                  (cur2->name, BAD_CAST "Identifier",
1025                   xmlStrlen (cur2->name)) == 0)
1026                {
1027                  xmlChar *val =
1028                    xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
1029                  free (tmpmaps->name);
1030                  tmpmaps->name = zStrdup ((char *) val);
1031                  xmlFree (val);
1032                }
1033              cur2 = cur2->next;
1034              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1035                cur2 = cur2->next;
1036            }
1037          if (*request_output == NULL)
1038            *request_output = dupMaps (&tmpmaps);
1039          else
1040            addMapsToMaps (request_output, tmpmaps);
1041          if (tmpmaps != NULL)
1042            {
1043              freeMaps (&tmpmaps);
1044              free (tmpmaps);
1045              tmpmaps = NULL;
1046            }
1047        }
1048    }
1049  else
1050    {
1051      addToMap (*request_inputs, "ResponseDocument", "");
1052
1053      if (cur->type == XML_ELEMENT_NODE) {
1054        // Get every attribute: storeExecuteResponse, lineage, status
1055        const char *ress[3] =
1056          { "storeExecuteResponse", "lineage", "status" };
1057        xmlChar *val;
1058        for (l = 0; l < 3; l++)
1059          {
1060            val = xmlGetProp (cur, BAD_CAST ress[l]);
1061            if (val != NULL && strlen ((char *) val) > 0)
1062              {
1063                addToMap (*request_inputs, ress[l], (char *) val);
1064              }
1065            xmlFree (val);
1066          }
1067                       
1068        xmlNodePtr cur1 = cur->children;               
1069        while (cur1 != NULL) // iterate over Output nodes
1070          {
1071            if (cur1->type != XML_ELEMENT_NODE || 
1072                xmlStrncasecmp(cur1->name, BAD_CAST "Output", 
1073                               xmlStrlen (cur1->name)) != 0) {
1074              cur1 = cur1->next;
1075              continue;
1076            }
1077                               
1078            maps *tmpmaps = (maps *) malloc (MAPS_SIZE); // one per Output node
1079            if (tmpmaps == NULL) {
1080              return errorException (*main_conf,
1081                                     _
1082                                     ("Unable to allocate memory."),
1083                                     "InternalError", NULL);
1084            }
1085            tmpmaps->name = zStrdup ("unknownIdentifier");
1086            tmpmaps->content = NULL;
1087            tmpmaps->next = NULL;
1088                               
1089            xmlNodePtr elems = cur1->children;
1090                               
1091            while (elems != NULL) {
1092
1093              // Identifier
1094              if (xmlStrncasecmp
1095                  (elems->name, BAD_CAST "Identifier",
1096                   xmlStrlen (elems->name)) == 0)
1097                {
1098                  xmlChar *val =
1099                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1100               
1101                  free(tmpmaps->name);
1102                  tmpmaps->name = zStrdup ((char *) val);
1103                  if (tmpmaps->content == NULL) {
1104                    tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
1105                  }
1106                  else {
1107                    addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
1108                  }
1109
1110                  map* tt = getMap (*request_inputs, "ResponseDocument");
1111                  if (strlen(tt->value) == 0) {
1112                    addToMap (*request_inputs, "ResponseDocument",
1113                              (char *) val);
1114                  }
1115                  else {
1116                    char* tmp = (char*) malloc((strlen(tt->value) + 1 
1117                                                + strlen((char*) val) + 1) * sizeof(char));
1118                    sprintf (tmp, "%s;%s", tt->value, (char *) val);
1119                    free(tt->value);
1120                    tt->value = tmp;
1121                  }
1122                  xmlFree (val);
1123                }
1124             
1125              // Title, Abstract
1126              else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
1127                                      xmlStrlen (elems->name)) == 0
1128                       || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
1129                                         xmlStrlen (elems->name)) == 0)
1130                {
1131                  xmlChar *val =
1132                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1133                                                       
1134                  if (tmpmaps->content == NULL) {
1135                    tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
1136                  }
1137                  else {
1138                    addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
1139                  }
1140                  xmlFree (val);
1141                }
1142              elems = elems->next;
1143            }
1144                               
1145            // Get every attribute from an Output node:
1146            // mimeType, encoding, schema, uom, asReference
1147            const char *outs[5] =
1148              { "mimeType", "encoding", "schema", "uom", "asReference" };
1149                                         
1150            for (l = 0; l < 5; l++) {
1151              xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                               
1152              if (val != NULL && xmlStrlen(val) > 0) {
1153                if (tmpmaps->content != NULL) {
1154                  addToMap (tmpmaps->content, outs[l], (char *) val);
1155                }                         
1156                else {
1157                  tmpmaps->content = createMap (outs[l], (char *) val);
1158                }       
1159              }
1160              xmlFree (val);
1161            }
1162                               
1163            if (*request_output == NULL) {
1164              *request_output = tmpmaps;
1165            }   
1166            else if (getMaps(*request_output, tmpmaps->name) != NULL) {
1167              return errorException (*main_conf,
1168                                     _
1169                                     ("Duplicate <Output> elements in WPS Execute request"),
1170                                     "InternalError", NULL);
1171            }
1172            else {
1173              maps* mptr = *request_output;
1174              while (mptr->next != NULL) {
1175                mptr = mptr->next;
1176              }
1177              mptr->next = tmpmaps;     
1178            }                                   
1179            cur1 = cur1->next;
1180          }                     
1181      }
1182    }
1183  return 1;
1184}
1185
1186/**
1187 * Parse XML request and store informations in maps.
1188 *
1189 * @param main_conf the conf maps containing the main.cfg settings
1190 * @param post the string containing the XML request
1191 * @param request_inputs the map storing KVP raw value
1192 * @param s the service
1193 * @param inputs the maps to store the KVP pairs
1194 * @param outputs the maps to store the KVP pairs
1195 * @param hInternet the HINTERNET queue to add potential requests
1196 * @return 0 on success, -1 on failure
1197 */
1198int xmlParseRequest(maps** main_conf,const char* post,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1199  xmlInitParser ();
1200  xmlDocPtr doc = xmlParseMemory (post, cgiContentLength);
1201
1202  /**
1203   * Extract Input nodes from the XML Request.
1204   */
1205  xmlXPathObjectPtr tmpsptr =
1206    extractFromDoc (doc, "/*/*/*[local-name()='Input']");
1207  xmlNodeSet *tmps = tmpsptr->nodesetval;
1208  if(xmlParseInputs(main_conf,s,inputs,doc,tmps,hInternet)<0){
1209    xmlXPathFreeObject (tmpsptr);
1210    xmlFreeDoc (doc);
1211    xmlCleanupParser ();
1212    return -1;
1213  }
1214  xmlXPathFreeObject (tmpsptr);
1215
1216  // Extract ResponseDocument / RawDataOutput from the XML Request
1217  tmpsptr =
1218    extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
1219  bool asRaw = false;
1220  tmps = tmpsptr->nodesetval;
1221  if (tmps->nodeNr == 0)
1222    {
1223      xmlXPathFreeObject (tmpsptr);
1224      tmpsptr =
1225        extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
1226      tmps = tmpsptr->nodesetval;
1227      asRaw = true;
1228    }
1229  if(tmps->nodeNr != 0){
1230    if(xmlParseOutputs(main_conf,request_inputs,outputs,doc,tmps->nodeTab[0],asRaw)<0){
1231      xmlXPathFreeObject (tmpsptr);
1232      xmlFreeDoc (doc);
1233      xmlCleanupParser ();
1234      return -1;
1235    }
1236  }
1237  xmlXPathFreeObject (tmpsptr);
1238  xmlFreeDoc (doc);
1239  xmlCleanupParser ();
1240  return 1;
1241}
1242
1243/**
1244 * Parse request and store informations in maps.
1245 *
1246 * @param main_conf the conf maps containing the main.cfg settings
1247 * @param post the string containing the XML request
1248 * @param request_inputs the map storing KVP raw value
1249 * @param s the service
1250 * @param inputs the maps to store the KVP pairs
1251 * @param outputs the maps to store the KVP pairs
1252 * @param hInternet the HINTERNET queue to add potential requests
1253 * @return 0 on success, -1 on failure
1254 * @see kvpParseOutputs,kvpParseInputs,xmlParseRequest
1255 */
1256int parseRequest(maps** main_conf,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1257  map *postRequest = NULL;
1258  postRequest = getMap (*request_inputs, "xrequest");
1259  if (postRequest == NULLMAP)
1260    {
1261      if(kvpParseOutputs(main_conf,*request_inputs,outputs)<0){
1262        return -1;
1263      }
1264      if(kvpParseInputs(main_conf,s,*request_inputs,inputs,hInternet)<0){
1265        return -1;
1266      }
1267    }
1268  else
1269    {
1270      //Parse XML request
1271      if(xmlParseRequest(main_conf,postRequest->value,request_inputs,s,inputs,outputs,hInternet)<0){
1272        return -1;
1273      }
1274    }
1275  return 1;
1276}
1277
1278/**
1279 * Ensure that each requested arguments are present in the request
1280 * DataInputs and ResponseDocument / RawDataOutput. Potentially run
1281 * http requests from the queue in parallel.
1282 * For optional inputs add default values defined in the ZCFG file.
1283 *
1284 * @param main_conf
1285 * @param s
1286 * @param original_request
1287 * @param request_inputs
1288 * @param request_outputs
1289 * @param hInternet
1290 *
1291 * @see runHttpRequests
1292 */
1293int validateRequest(maps** main_conf,service* s,map* original_request, maps** request_inputs,maps** request_outputs,HINTERNET* hInternet){
1294
1295  runHttpRequests (main_conf, request_inputs, hInternet);
1296  InternetCloseHandle (hInternet);
1297
1298  map* errI=NULL;
1299  char *dfv = addDefaultValues (request_inputs, s->inputs, *main_conf, 0,&errI);
1300
1301  maps *ptr = *request_inputs;
1302  while (ptr != NULL)
1303    {
1304      map *tmp0 = getMap (ptr->content, "size");
1305      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
1306      if (tmp1 != NULL && tmp0 != NULL)
1307        {
1308          float i = atof (tmp0->value) / 1048576.0;
1309          if (i >= atoi (tmp1->value))
1310            {
1311              char tmps[1024];
1312              map *tmpe = createMap ("code", "FileSizeExceeded");
1313              snprintf (tmps, 1024,
1314                        _
1315                        ("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)."),
1316                        ptr->name, tmp1->value, i);
1317              addToMap (tmpe, "locator", ptr->name);
1318              addToMap (tmpe, "text", tmps);
1319              printExceptionReportResponse (*main_conf, tmpe);
1320              freeMap (&tmpe);
1321              free (tmpe);
1322              return -1;
1323            }
1324        }
1325      ptr = ptr->next;
1326    }
1327
1328  map* errO=NULL;
1329  char *dfv1 =
1330    addDefaultValues (request_outputs, s->outputs, *main_conf, 1,&errO);
1331  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
1332    {
1333      char tmps[1024];
1334      map *tmpe = NULL;
1335      if (strcmp (dfv, "") != 0)
1336        {
1337          tmpe = createMap ("code", "MissingParameterValue");
1338          int nb=0;
1339          int length=1;
1340          map* len=getMap(errI,"length");
1341          if(len!=NULL)
1342            length=atoi(len->value);
1343          for(nb=0;nb<length;nb++){
1344            map* errp=getMapArray(errI,"value",nb);
1345            snprintf (tmps, 1024,
1346                      _
1347                      ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
1348                      errp->value);
1349            setMapArray (tmpe, "locator", nb , errp->value);
1350            setMapArray (tmpe, "text", nb , tmps);
1351            setMapArray (tmpe, "code", nb , "MissingParameterValue");
1352          }
1353        }
1354      if (strcmp (dfv1, "") != 0)
1355        {
1356          int ilength=0;
1357          if(tmpe==NULL)
1358            tmpe = createMap ("code", "InvalidParameterValue");
1359          else{
1360            map* len=getMap(tmpe,"length");
1361            if(len!=NULL)
1362              ilength=atoi(len->value);
1363          }
1364          int nb=0;
1365          int length=1;
1366          map* len=getMap(errO,"length");
1367          if(len!=NULL)
1368            length=atoi(len->value);
1369          for(nb=0;nb<length;nb++){
1370            map* errp=getMapArray(errO,"value",nb);
1371            snprintf (tmps, 1024,
1372                      _
1373                      ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
1374                      errp->value,
1375                      ((getMap(original_request,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
1376            setMapArray (tmpe, "locator", nb+ilength , errp->value);
1377            setMapArray (tmpe, "text", nb+ilength , tmps);
1378            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
1379          }
1380        }
1381      printExceptionReportResponse (*main_conf, tmpe);
1382      if(errI!=NULL){
1383        freeMap(&errI);
1384        free(errI);
1385      }
1386      if(errO!=NULL){
1387        freeMap(&errO);
1388        free(errO);
1389      }
1390      freeMap (&tmpe);
1391      free (tmpe);
1392      return -1;
1393    }
1394  maps *tmpReqI = *request_inputs;
1395  while (tmpReqI != NULL)
1396    {
1397      char name[1024];
1398      if (getMap (tmpReqI->content, "isFile") != NULL)
1399        {
1400          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
1401              cgiFormSuccess)
1402            {
1403              int BufferLen = 1024;
1404              cgiFilePtr file;
1405              int targetFile;
1406              char storageNameOnServer[2048];
1407              char fileNameOnServer[64];
1408              char contentType[1024];
1409              char buffer[1024];
1410              char *tmpStr = NULL;
1411              int size;
1412              int got, t;
1413              map *path = getMapFromMaps (*main_conf, "main", "tmpPath");
1414              cgiFormFileSize (tmpReqI->name, &size);
1415              cgiFormFileContentType (tmpReqI->name, contentType,
1416                                      sizeof (contentType));
1417              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
1418                {
1419                  t = -1;
1420                  while (1)
1421                    {
1422                      tmpStr = strstr (name + t + 1, "\\");
1423                      if (NULL == tmpStr)
1424                        tmpStr = strstr (name + t + 1, "/");
1425                      if (NULL != tmpStr)
1426                        t = (int) (tmpStr - name);
1427                      else
1428                        break;
1429                    }
1430                  strcpy (fileNameOnServer, name + t + 1);
1431
1432                  sprintf (storageNameOnServer, "%s/%s", path->value,
1433                           fileNameOnServer);
1434#ifdef DEBUG
1435                  fprintf (stderr, "Name on server %s\n",
1436                           storageNameOnServer);
1437                  fprintf (stderr, "fileNameOnServer: %s\n",
1438                           fileNameOnServer);
1439#endif
1440                  targetFile =
1441                    open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
1442                          S_IRWXU | S_IRGRP | S_IROTH);
1443                  if (targetFile < 0)
1444                    {
1445#ifdef DEBUG
1446                      fprintf (stderr, "could not create the new file,%s\n",
1447                               fileNameOnServer);
1448#endif
1449                    }
1450                  else
1451                    {
1452                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
1453                             == cgiFormSuccess)
1454                        {
1455                          if (got > 0)
1456                            write (targetFile, buffer, got);
1457                        }
1458                    }
1459                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
1460                  cgiFormFileClose (file);
1461                  close (targetFile);
1462#ifdef DEBUG
1463                  fprintf (stderr, "File \"%s\" has been uploaded",
1464                           fileNameOnServer);
1465#endif
1466                }
1467            }
1468        }
1469      tmpReqI = tmpReqI->next;
1470    }
1471
1472  ensureDecodedBase64 (request_inputs);
1473  return 1;
1474}
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