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

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

Fix for downloading content for simple GET requests, taking order into account.

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