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

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

Add readBase64 function, avoid calling it prior to fork . Add dumpMapsValuesToFiles function used to simplify OTB support.

  • Property svn:keywords set to Id
File size: 40.6 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                                  addIntToMap (tmpmaps->content, "Order", hInternet->nb);
555                                }
556                              addToMap (tmpmaps->content, "Reference", (char*) val);
557                            }
558                        }
559                      xmlFree (val);
560                    }
561                  // Parse Header and Body from Reference
562                  xmlNodePtr cur3 = cur2->children;
563                  while (cur3 != NULL)
564                    {
565                      while (cur3 != NULL
566                             && cur3->type != XML_ELEMENT_NODE)
567                        cur3 = cur3->next;
568                      if (cur3 == NULL)
569                        break;
570                      if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
571                          0)
572                        {
573                          const char *ha[2];
574                          ha[0] = "key";
575                          ha[1] = "value";
576                          int hai;
577                          char *has;
578                          char *key;
579                          for (hai = 0; hai < 2; hai++)
580                            {
581                              xmlChar *val =
582                                xmlGetProp (cur3, BAD_CAST ha[hai]);
583#ifdef POST_DEBUG
584                              fprintf (stderr, "%s = %s\n", ha[hai],
585                                       (char *) val);
586#endif
587                              if (hai == 0)
588                                {
589                                  key = zStrdup ((char *) val);
590                                }
591                              else
592                                {
593                                  has =
594                                    (char *)
595                                    malloc ((4 + xmlStrlen (val) +
596                                             strlen (key)) *
597                                            sizeof (char));
598                                  if (has == NULL)
599                                    {
600                                      return errorException (*main_conf,
601                                                             _
602                                                             ("Unable to allocate memory."),
603                                                             "InternalError",
604                                                             NULL);
605                                    }
606                                  snprintf (has,
607                                            (3 + xmlStrlen (val) +
608                                             strlen (key)), "%s: %s", key,
609                                            (char *) val);
610                                  free (key);
611                                }
612                              xmlFree (val);
613                            }
614                          hInternet->ihandle[hInternet->nb].header = NULL;
615                          hInternet->ihandle[hInternet->nb].header =
616                            curl_slist_append (hInternet->ihandle
617                                               [hInternet->nb].header,
618                                               has);
619                          if (has != NULL)
620                            free (has);
621                        }
622                      else
623                        {
624#ifdef POST_DEBUG
625                          fprintf (stderr,
626                                   "Try to fetch the body part of the request ...\n");
627#endif
628                          if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
629                              == 0)
630                            {
631#ifdef POST_DEBUG
632                              fprintf (stderr, "Body part found (%s) !!!\n",
633                                       (char *) cur3->content);
634#endif
635                              char *tmp = NULL;
636                              xmlNodePtr cur4 = cur3->children;
637                              while (cur4 != NULL)
638                                {
639                                  while (cur4 && cur4 != NULL && cur4->type && cur4->type != XML_ELEMENT_NODE){
640                                    if(cur4->next)
641                                      cur4 = cur4->next;
642                                    else
643                                      cur4 = NULL;
644                                  }
645                                  if(cur4 != NULL) {
646                                    xmlDocPtr bdoc =
647                                      xmlNewDoc (BAD_CAST "1.0");
648                                    bdoc->encoding =
649                                      xmlCharStrdup ("UTF-8");
650                                    xmlDocSetRootElement (bdoc, cur4);
651                                    xmlChar *btmps;
652                                    int bsize;
653                                    // TODO : check for encoding defined in the input
654                                    xmlDocDumpFormatMemoryEnc(bdoc, &btmps, &bsize, "UTF-8", 0);
655                                    if (btmps != NULL){
656                                      tmp = (char *) malloc ((bsize + 1) * sizeof (char));
657
658                                      sprintf (tmp, "%s", (char*) btmps);
659                                         
660                                      xmlFreeDoc (bdoc);
661                                         
662                                      map *btmp =
663                                        getMap (tmpmaps->content, "Reference");
664                                      if (btmp != NULL)
665                                        {
666                                          addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
667                                          InternetOpenUrl (hInternet,
668                                                           btmp->value,
669                                                           tmp,
670                                                           xmlStrlen(btmps),
671                                                           INTERNET_FLAG_NO_CACHE_WRITE,
672                                                           0);
673                                          addIntToMap (tmpmaps->content, "Order", hInternet->nb);
674                                        }
675                                      xmlFree (btmps);
676                                      free (tmp);
677                                      break;
678                                    }
679                                  }
680                                  cur4 = cur4->next;
681                                }
682                            }
683                          else
684                            if (xmlStrcasecmp
685                                (cur3->name,
686                                 BAD_CAST "BodyReference") == 0)
687                              {
688                                xmlChar *val =
689                                  xmlGetProp (cur3, BAD_CAST "href");
690                                HINTERNET bInternet, res1, res;
691                                bInternet = InternetOpen (
692#ifndef WIN32
693                                                          (LPCTSTR)
694#endif
695                                                          "ZooWPSClient\0",
696                                                          INTERNET_OPEN_TYPE_PRECONFIG,
697                                                          NULL, NULL, 0);
698                                if (!CHECK_INET_HANDLE (bInternet))
699                                  fprintf (stderr,
700                                           "WARNING : bInternet handle failed to initialize");
701                                bInternet.waitingRequests[0] =
702                                  strdup ((char *) val);
703                                res1 =
704                                  InternetOpenUrl (&bInternet,
705                                                   bInternet.waitingRequests
706                                                   [0], NULL, 0,
707                                                   INTERNET_FLAG_NO_CACHE_WRITE,
708                                                   0);
709                                processDownloads (&bInternet);
710                                char *tmp =
711                                  (char *)
712                                  malloc ((bInternet.ihandle[0].nDataLen +
713                                           1) * sizeof (char));
714                                if (tmp == NULL)
715                                  {
716                                    return errorException (*main_conf,
717                                                           _
718                                                           ("Unable to allocate memory."),
719                                                           "InternalError",
720                                                           NULL);
721                                  }
722                                size_t bRead;
723                                InternetReadFile (bInternet.ihandle[0],
724                                                  (LPVOID) tmp,
725                                                  bInternet.
726                                                  ihandle[0].nDataLen,
727                                                  &bRead);
728                                tmp[bInternet.ihandle[0].nDataLen] = 0;
729                                InternetCloseHandle (&bInternet);
730                                map *btmp =
731                                  getMap (tmpmaps->content, "href");
732                                if (btmp != NULL)
733                                  {
734                                    addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
735
736                                    res =
737                                      InternetOpenUrl (hInternet,
738                                                       btmp->value,
739                                                       tmp,
740                                                       strlen(tmp),
741                                                       INTERNET_FLAG_NO_CACHE_WRITE,
742                                                       0);
743                                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
744                                  }
745                                free (tmp);
746                              }
747                        }
748                      cur3 = cur3->next;
749                    }
750                }
751              else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
752                {
753                  xmlNodePtr cur4 = cur2->children;
754                  while (cur4 != NULL)
755                    {
756                      while (cur4 != NULL
757                             && cur4->type != XML_ELEMENT_NODE)
758                        cur4 = cur4->next;
759                      if (cur4 == NULL)
760                        break;
761                      if (xmlStrcasecmp
762                          (cur4->name, BAD_CAST "LiteralData") == 0)
763                        {
764                          // Get every attribute from a LiteralData node
765                          // dataType , uom
766                          char *list[2];
767                          list[0] = zStrdup ("dataType");
768                          list[1] = zStrdup ("uom");
769                          for (l = 0; l < 2; l++)
770                            {
771                              xmlChar *val =
772                                xmlGetProp (cur4, BAD_CAST list[l]);
773                              if (val != NULL
774                                  && strlen ((char *) val) > 0)
775                                {
776                                  if (tmpmaps->content != NULL)
777                                    addToMap (tmpmaps->content, list[l],
778                                              (char *) val);
779                                  else
780                                    tmpmaps->content =
781                                      createMap (list[l], (char *) val);
782                                }
783                              xmlFree (val);
784                              free (list[l]);
785                            }
786                        }
787                      else
788                        if (xmlStrcasecmp
789                            (cur4->name, BAD_CAST "ComplexData") == 0)
790                          {
791                            // Get every attribute from a Reference node
792                            // mimeType, encoding, schema
793                            const char *coms[3] =
794                              { "mimeType", "encoding", "schema" };
795                            for (l = 0; l < 3; l++)
796                              {
797                                xmlChar *val =
798                                  xmlGetProp (cur4, BAD_CAST coms[l]);
799                                if (val != NULL
800                                    && strlen ((char *) val) > 0)
801                                  {
802                                    if (tmpmaps->content != NULL)
803                                      addToMap (tmpmaps->content, coms[l],
804                                                (char *) val);
805                                    else
806                                      tmpmaps->content =
807                                        createMap (coms[l], (char *) val);
808                                  }
809                                xmlFree (val);
810                              }
811                          }
812
813                      map *test = getMap (tmpmaps->content, "encoding");
814
815                      if (test == NULL)
816                        { 
817                          if (tmpmaps->content != NULL)
818                            addToMap (tmpmaps->content, "encoding",
819                                      "utf-8");
820                          else
821                            tmpmaps->content =
822                              createMap ("encoding", "utf-8");
823                          test = getMap (tmpmaps->content, "encoding");
824                        }
825
826                      if (strcasecmp (test->value, "base64") != 0)
827                        { 
828                          xmlChar *mv = xmlNodeListGetString (doc,
829                                                              cur4->xmlChildrenNode,
830                                                              1);
831                          map *ltmp =
832                            getMap (tmpmaps->content, "mimeType");
833                          if (mv == NULL
834                              ||
835                              (xmlStrcasecmp
836                               (cur4->name, BAD_CAST "ComplexData") == 0
837                               && (ltmp == NULL
838                                   || strncasecmp (ltmp->value,
839                                                   "text/xml", 8) == 0)))
840                            {
841                              xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
842                              int buffersize;
843                              xmlNodePtr cur5 = cur4->children;
844                              while (cur5 != NULL
845                                     && cur5->type != XML_ELEMENT_NODE
846                                     && cur5->type !=
847                                     XML_CDATA_SECTION_NODE)
848                                cur5 = cur5->next;
849                              if (cur5 != NULL
850                                  && cur5->type != XML_CDATA_SECTION_NODE)
851                                {
852                                  xmlDocSetRootElement (doc1, cur5);
853                                  xmlDocDumpFormatMemoryEnc (doc1, &mv,
854                                                             &buffersize,
855                                                             "utf-8", 1);
856                                  char size[1024];
857                                  sprintf (size, "%d", buffersize);
858                                  addToMap (tmpmaps->content, "size",
859                                            size);
860                                  xmlFreeDoc (doc1);
861                                }
862                              else
863                                {
864                                  if (cur5 != NULL
865                                      && cur5->type == XML_CDATA_SECTION_NODE){
866                                    xmlDocPtr doc2 = xmlParseMemory((const char*)cur5->content,xmlStrlen(cur5->content));
867                                    xmlDocSetRootElement (doc1,xmlDocGetRootElement(doc2));
868                                    xmlDocDumpFormatMemoryEnc (doc1, &mv,
869                                                               &buffersize,
870                                                               "utf-8", 1);
871                                    char size[1024];
872                                    sprintf (size, "%d", buffersize);
873                                    addToMap (tmpmaps->content, "size",
874                                              size);
875                                    xmlFreeDoc (doc2);
876                                    xmlFreeDoc (doc1);
877                                  }
878                                }
879                            }else{
880                            xmlNodePtr cur5 = cur4->children;
881                            while (cur5 != NULL
882                                   && cur5->type != XML_CDATA_SECTION_NODE)
883                              cur5 = cur5->next;
884                            if (cur5 != NULL
885                                && cur5->type == XML_CDATA_SECTION_NODE){
886                              xmlFree(mv);
887                              mv=xmlStrdup(cur5->content);
888                            }
889                          }
890                          if (mv != NULL)
891                            {
892                              addToMap (tmpmaps->content, "value",
893                                        (char *) mv);
894                              xmlFree (mv);
895                            }
896                        }
897                      else
898                        {
899                          xmlChar *tmp = xmlNodeListGetRawString (doc,
900                                                                  cur4->xmlChildrenNode,
901                                                                  0);
902                          addToMap (tmpmaps->content, "value",
903                                    (char *) tmp);
904                          xmlFree (tmp);
905                        }
906                      cur4 = cur4->next;
907                    }
908                }
909              cur2 = cur2->next;
910              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE){
911                cur2 = cur2->next;
912              }
913            }
914          {
915            maps *testPresence =
916              getMaps (*request_output, tmpmaps->name);
917            if (testPresence != NULL)
918              {
919                elements *elem = getElements (s->inputs, tmpmaps->name);
920                if (elem != NULL)
921                  {
922                    if (appendMapsToMaps
923                        (*main_conf, *request_output, tmpmaps, elem) < 0)
924                      {
925                        return errorException (*main_conf,
926                                               _("Unable to append maps to maps."),
927                                               "InternalError",
928                                               NULL);
929                      }
930                  }
931              }
932            else
933              addMapsToMaps (request_output, tmpmaps);
934          }
935          freeMaps (&tmpmaps);
936          free (tmpmaps);
937          tmpmaps = NULL;
938        }
939    }
940  return 1;
941}
942
943/**
944 * Parse outputs from XML nodes and store them in a maps.
945 *
946 * @param main_conf the conf maps containing the main.cfg settings
947 * @param request_inputs the map storing KVP raw value
948 * @param request_output the maps to store the KVP pairs
949 * @param doc the xmlDocPtr containing the original request
950 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
951 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
952 * @return 0 on success, -1 on failure
953 */
954int xmlParseOutputs(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodePtr cur,bool raw){
955  int l=0;
956  if( raw == true)
957    {
958      addToMap (*request_inputs, "RawDataOutput", "");
959      if (cur->type == XML_ELEMENT_NODE)
960        {
961
962          maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
963          if (tmpmaps == NULL)
964            {
965              return errorException (*main_conf, _("Unable to allocate memory."),
966                                     "InternalError", NULL);
967            }
968          tmpmaps->name = zStrdup ("unknownIdentifier");
969          tmpmaps->content = NULL;
970          tmpmaps->next = NULL;
971
972          // Get every attribute from a RawDataOutput node
973          // mimeType, encoding, schema, uom
974          const char *outs[4] =
975            { "mimeType", "encoding", "schema", "uom" };
976          for (l = 0; l < 4; l++)
977            {
978              xmlChar *val = xmlGetProp (cur, BAD_CAST outs[l]);
979              if (val != NULL)
980                {
981                  if (strlen ((char *) val) > 0)
982                    {
983                      if (tmpmaps->content != NULL)
984                        addToMap (tmpmaps->content, outs[l],
985                                  (char *) val);
986                      else
987                        tmpmaps->content =
988                          createMap (outs[l], (char *) val);
989                    }
990                  xmlFree (val);
991                }
992            }
993          xmlNodePtr cur2 = cur->children;
994          while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
995            cur2 = cur2->next;
996          while (cur2 != NULL)
997            {
998              if (xmlStrncasecmp
999                  (cur2->name, BAD_CAST "Identifier",
1000                   xmlStrlen (cur2->name)) == 0)
1001                {
1002                  xmlChar *val =
1003                    xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
1004                  free (tmpmaps->name);
1005                  tmpmaps->name = zStrdup ((char *) val);
1006                  xmlFree (val);
1007                }
1008              cur2 = cur2->next;
1009              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1010                cur2 = cur2->next;
1011            }
1012          if (*request_output == NULL)
1013            *request_output = dupMaps (&tmpmaps);
1014          else
1015            addMapsToMaps (request_output, tmpmaps);
1016          if (tmpmaps != NULL)
1017            {
1018              freeMaps (&tmpmaps);
1019              free (tmpmaps);
1020              tmpmaps = NULL;
1021            }
1022        }
1023    }
1024  else
1025    {
1026      addToMap (*request_inputs, "ResponseDocument", "");
1027
1028      if (cur->type == XML_ELEMENT_NODE) {
1029        // Get every attribute: storeExecuteResponse, lineage, status
1030        const char *ress[3] =
1031          { "storeExecuteResponse", "lineage", "status" };
1032        xmlChar *val;
1033        for (l = 0; l < 3; l++)
1034          {
1035            val = xmlGetProp (cur, BAD_CAST ress[l]);
1036            if (val != NULL && strlen ((char *) val) > 0)
1037              {
1038                addToMap (*request_inputs, ress[l], (char *) val);
1039              }
1040            xmlFree (val);
1041          }
1042                       
1043        xmlNodePtr cur1 = cur->children;               
1044        while (cur1 != NULL) // iterate over Output nodes
1045          {
1046            if (cur1->type != XML_ELEMENT_NODE || 
1047                xmlStrncasecmp(cur1->name, BAD_CAST "Output", 
1048                               xmlStrlen (cur1->name)) != 0) {
1049              cur1 = cur1->next;
1050              continue;
1051            }
1052                               
1053            maps *tmpmaps = (maps *) malloc (MAPS_SIZE); // one per Output node
1054            if (tmpmaps == NULL) {
1055              return errorException (*main_conf,
1056                                     _
1057                                     ("Unable to allocate memory."),
1058                                     "InternalError", NULL);
1059            }
1060            tmpmaps->name = zStrdup ("unknownIdentifier");
1061            tmpmaps->content = NULL;
1062            tmpmaps->next = NULL;
1063                               
1064            xmlNodePtr elems = cur1->children;
1065                               
1066            while (elems != NULL) {
1067
1068              // Identifier
1069              if (xmlStrncasecmp
1070                  (elems->name, BAD_CAST "Identifier",
1071                   xmlStrlen (elems->name)) == 0)
1072                {
1073                  xmlChar *val =
1074                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1075               
1076                  free(tmpmaps->name);
1077                  tmpmaps->name = zStrdup ((char *) val);
1078                  if (tmpmaps->content == NULL) {
1079                    tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
1080                  }
1081                  else {
1082                    addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
1083                  }
1084
1085                  map* tt = getMap (*request_inputs, "ResponseDocument");
1086                  if (strlen(tt->value) == 0) {
1087                    addToMap (*request_inputs, "ResponseDocument",
1088                              (char *) val);
1089                  }
1090                  else {
1091                    char* tmp = (char*) malloc((strlen(tt->value) + 1 
1092                                                + strlen((char*) val) + 1) * sizeof(char));
1093                    sprintf (tmp, "%s;%s", tt->value, (char *) val);
1094                    free(tt->value);
1095                    tt->value = tmp;
1096                  }
1097                  xmlFree (val);
1098                }
1099             
1100              // Title, Abstract
1101              else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
1102                                      xmlStrlen (elems->name)) == 0
1103                       || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
1104                                         xmlStrlen (elems->name)) == 0)
1105                {
1106                  xmlChar *val =
1107                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1108                                                       
1109                  if (tmpmaps->content == NULL) {
1110                    tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
1111                  }
1112                  else {
1113                    addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
1114                  }
1115                  xmlFree (val);
1116                }
1117              elems = elems->next;
1118            }
1119                               
1120            // Get every attribute from an Output node:
1121            // mimeType, encoding, schema, uom, asReference
1122            const char *outs[5] =
1123              { "mimeType", "encoding", "schema", "uom", "asReference" };
1124                                         
1125            for (l = 0; l < 5; l++) {
1126              xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                               
1127              if (val != NULL && xmlStrlen(val) > 0) {
1128                if (tmpmaps->content != NULL) {
1129                  addToMap (tmpmaps->content, outs[l], (char *) val);
1130                }                         
1131                else {
1132                  tmpmaps->content = createMap (outs[l], (char *) val);
1133                }       
1134              }
1135              xmlFree (val);
1136            }
1137                               
1138            if (*request_output == NULL) {
1139              *request_output = tmpmaps;
1140            }   
1141            else if (getMaps(*request_output, tmpmaps->name) != NULL) {
1142              return errorException (*main_conf,
1143                                     _
1144                                     ("Duplicate <Output> elements in WPS Execute request"),
1145                                     "InternalError", NULL);
1146            }
1147            else {
1148              maps* mptr = *request_output;
1149              while (mptr->next != NULL) {
1150                mptr = mptr->next;
1151              }
1152              mptr->next = tmpmaps;     
1153            }                                   
1154            cur1 = cur1->next;
1155          }                     
1156      }
1157    }
1158  return 1;
1159}
1160
1161/**
1162 * Parse XML request and store informations in maps.
1163 *
1164 * @param main_conf the conf maps containing the main.cfg settings
1165 * @param post the string containing the XML request
1166 * @param request_inputs the map storing KVP raw value
1167 * @param s the service
1168 * @param inputs the maps to store the KVP pairs
1169 * @param outputs the maps to store the KVP pairs
1170 * @param hInternet the HINTERNET queue to add potential requests
1171 * @return 0 on success, -1 on failure
1172 */
1173int xmlParseRequest(maps** main_conf,const char* post,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1174  xmlInitParser ();
1175  xmlDocPtr doc = xmlParseMemory (post, cgiContentLength);
1176
1177  /**
1178   * Extract Input nodes from the XML Request.
1179   */
1180  xmlXPathObjectPtr tmpsptr =
1181    extractFromDoc (doc, "/*/*/*[local-name()='Input']");
1182  xmlNodeSet *tmps = tmpsptr->nodesetval;
1183  if(xmlParseInputs(main_conf,s,inputs,doc,tmps,hInternet)<0){
1184    xmlXPathFreeObject (tmpsptr);
1185    xmlFreeDoc (doc);
1186    xmlCleanupParser ();
1187    return -1;
1188  }
1189  xmlXPathFreeObject (tmpsptr);
1190
1191  // Extract ResponseDocument / RawDataOutput from the XML Request
1192  tmpsptr =
1193    extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
1194  bool asRaw = false;
1195  tmps = tmpsptr->nodesetval;
1196  if (tmps->nodeNr == 0)
1197    {
1198      xmlXPathFreeObject (tmpsptr);
1199      tmpsptr =
1200        extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
1201      tmps = tmpsptr->nodesetval;
1202      asRaw = true;
1203    }
1204  if(tmps->nodeNr != 0){
1205    if(xmlParseOutputs(main_conf,request_inputs,outputs,doc,tmps->nodeTab[0],asRaw)<0){
1206      xmlXPathFreeObject (tmpsptr);
1207      xmlFreeDoc (doc);
1208      xmlCleanupParser ();
1209      return -1;
1210    }
1211  }
1212  xmlXPathFreeObject (tmpsptr);
1213  xmlFreeDoc (doc);
1214  xmlCleanupParser ();
1215  return 1;
1216}
1217
1218/**
1219 * Parse request and store informations in maps.
1220 *
1221 * @param main_conf the conf maps containing the main.cfg settings
1222 * @param post the string containing the XML request
1223 * @param request_inputs the map storing KVP raw value
1224 * @param s the service
1225 * @param inputs the maps to store the KVP pairs
1226 * @param outputs the maps to store the KVP pairs
1227 * @param hInternet the HINTERNET queue to add potential requests
1228 * @return 0 on success, -1 on failure
1229 * @see kvpParseOutputs,kvpParseInputs,xmlParseRequest
1230 */
1231int parseRequest(maps** main_conf,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1232  map *postRequest = NULL;
1233  postRequest = getMap (*request_inputs, "xrequest");
1234  if (postRequest == NULLMAP)
1235    {
1236      if(kvpParseOutputs(main_conf,*request_inputs,outputs)<0){
1237        return -1;
1238      }
1239      if(kvpParseInputs(main_conf,s,*request_inputs,inputs,hInternet)<0){
1240        return -1;
1241      }
1242    }
1243  else
1244    {
1245      //Parse XML request
1246      if(xmlParseRequest(main_conf,postRequest->value,request_inputs,s,inputs,outputs,hInternet)<0){
1247        return -1;
1248      }
1249    }
1250  return 1;
1251}
1252
1253/**
1254 * Ensure that each requested arguments are present in the request
1255 * DataInputs and ResponseDocument / RawDataOutput. Potentially run
1256 * http requests from the queue in parallel.
1257 * For optional inputs add default values defined in the ZCFG file.
1258 *
1259 * @param main_conf
1260 * @param s
1261 * @param original_request
1262 * @param request_inputs
1263 * @param request_outputs
1264 * @param hInternet
1265 *
1266 * @see runHttpRequests
1267 */
1268int validateRequest(maps** main_conf,service* s,map* original_request, maps** request_inputs,maps** request_outputs,HINTERNET* hInternet){
1269
1270  runHttpRequests (main_conf, request_inputs, hInternet);
1271  InternetCloseHandle (hInternet);
1272
1273  map* errI=NULL;
1274  char *dfv = addDefaultValues (request_inputs, s->inputs, *main_conf, 0,&errI);
1275
1276  maps *ptr = *request_inputs;
1277  while (ptr != NULL)
1278    {
1279      map *tmp0 = getMap (ptr->content, "size");
1280      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
1281      if (tmp1 != NULL && tmp0 != NULL)
1282        {
1283          float i = atof (tmp0->value) / 1048576.0;
1284          if (i >= atoi (tmp1->value))
1285            {
1286              char tmps[1024];
1287              map *tmpe = createMap ("code", "FileSizeExceeded");
1288              snprintf (tmps, 1024,
1289                        _
1290                        ("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)."),
1291                        ptr->name, tmp1->value, i);
1292              addToMap (tmpe, "locator", ptr->name);
1293              addToMap (tmpe, "text", tmps);
1294              printExceptionReportResponse (*main_conf, tmpe);
1295              freeMap (&tmpe);
1296              free (tmpe);
1297              return -1;
1298            }
1299        }
1300      ptr = ptr->next;
1301    }
1302
1303  map* errO=NULL;
1304  char *dfv1 =
1305    addDefaultValues (request_outputs, s->outputs, *main_conf, 1,&errO);
1306  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
1307    {
1308      char tmps[1024];
1309      map *tmpe = NULL;
1310      if (strcmp (dfv, "") != 0)
1311        {
1312          tmpe = createMap ("code", "MissingParameterValue");
1313          int nb=0;
1314          int length=1;
1315          map* len=getMap(errI,"length");
1316          if(len!=NULL)
1317            length=atoi(len->value);
1318          for(nb=0;nb<length;nb++){
1319            map* errp=getMapArray(errI,"value",nb);
1320            snprintf (tmps, 1024,
1321                      _
1322                      ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
1323                      errp->value);
1324            setMapArray (tmpe, "locator", nb , errp->value);
1325            setMapArray (tmpe, "text", nb , tmps);
1326            setMapArray (tmpe, "code", nb , "MissingParameterValue");
1327          }
1328        }
1329      if (strcmp (dfv1, "") != 0)
1330        {
1331          int ilength=0;
1332          if(tmpe==NULL)
1333            tmpe = createMap ("code", "InvalidParameterValue");
1334          else{
1335            map* len=getMap(tmpe,"length");
1336            if(len!=NULL)
1337              ilength=atoi(len->value);
1338          }
1339          int nb=0;
1340          int length=1;
1341          map* len=getMap(errO,"length");
1342          if(len!=NULL)
1343            length=atoi(len->value);
1344          for(nb=0;nb<length;nb++){
1345            map* errp=getMapArray(errO,"value",nb);
1346            snprintf (tmps, 1024,
1347                      _
1348                      ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
1349                      errp->value,
1350                      ((getMap(original_request,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
1351            setMapArray (tmpe, "locator", nb+ilength , errp->value);
1352            setMapArray (tmpe, "text", nb+ilength , tmps);
1353            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
1354          }
1355        }
1356      printExceptionReportResponse (*main_conf, tmpe);
1357      if(errI!=NULL){
1358        freeMap(&errI);
1359        free(errI);
1360      }
1361      if(errO!=NULL){
1362        freeMap(&errO);
1363        free(errO);
1364      }
1365      freeMap (&tmpe);
1366      free (tmpe);
1367      return -1;
1368    }
1369  maps *tmpReqI = *request_inputs;
1370  while (tmpReqI != NULL)
1371    {
1372      char name[1024];
1373      if (getMap (tmpReqI->content, "isFile") != NULL)
1374        {
1375          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
1376              cgiFormSuccess)
1377            {
1378              int BufferLen = 1024;
1379              cgiFilePtr file;
1380              int targetFile;
1381              char storageNameOnServer[2048];
1382              char fileNameOnServer[64];
1383              char contentType[1024];
1384              char buffer[1024];
1385              char *tmpStr = NULL;
1386              int size;
1387              int got, t;
1388              map *path = getMapFromMaps (*main_conf, "main", "tmpPath");
1389              cgiFormFileSize (tmpReqI->name, &size);
1390              cgiFormFileContentType (tmpReqI->name, contentType,
1391                                      sizeof (contentType));
1392              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
1393                {
1394                  t = -1;
1395                  while (1)
1396                    {
1397                      tmpStr = strstr (name + t + 1, "\\");
1398                      if (NULL == tmpStr)
1399                        tmpStr = strstr (name + t + 1, "/");
1400                      if (NULL != tmpStr)
1401                        t = (int) (tmpStr - name);
1402                      else
1403                        break;
1404                    }
1405                  strcpy (fileNameOnServer, name + t + 1);
1406
1407                  sprintf (storageNameOnServer, "%s/%s", path->value,
1408                           fileNameOnServer);
1409#ifdef DEBUG
1410                  fprintf (stderr, "Name on server %s\n",
1411                           storageNameOnServer);
1412                  fprintf (stderr, "fileNameOnServer: %s\n",
1413                           fileNameOnServer);
1414#endif
1415                  targetFile =
1416                    open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
1417                          S_IRWXU | S_IRGRP | S_IROTH);
1418                  if (targetFile < 0)
1419                    {
1420#ifdef DEBUG
1421                      fprintf (stderr, "could not create the new file,%s\n",
1422                               fileNameOnServer);
1423#endif
1424                    }
1425                  else
1426                    {
1427                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
1428                             == cgiFormSuccess)
1429                        {
1430                          if (got > 0)
1431                            write (targetFile, buffer, got);
1432                        }
1433                    }
1434                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
1435                  cgiFormFileClose (file);
1436                  close (targetFile);
1437#ifdef DEBUG
1438                  fprintf (stderr, "File \"%s\" has been uploaded",
1439                           fileNameOnServer);
1440#endif
1441                }
1442            }
1443        }
1444      tmpReqI = tmpReqI->next;
1445    }
1446
1447  ensureDecodedBase64 (request_inputs);
1448  return 1;
1449}
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