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

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

Initialize header curl_slist becore calling curl_slist_append.

  • Property svn:keywords set to Id
File size: 41.2 KB
RevLine 
[621]1/*
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2015 GeoLabs SARL
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "request_parser.h"
26#include "service_internal.h"
27
28/**
29 * Apply XPath Expression on XML document.
30 *
31 * @param doc the XML Document
32 * @param search the XPath expression
33 * @return xmlXPathObjectPtr containing the resulting nodes set
34 */
35xmlXPathObjectPtr
36extractFromDoc (xmlDocPtr doc, const char *search)
37{
38  xmlXPathContextPtr xpathCtx;
39  xmlXPathObjectPtr xpathObj;
40  xpathCtx = xmlXPathNewContext (doc);
41  xpathObj = xmlXPathEvalExpression (BAD_CAST search, xpathCtx);
42  xmlXPathFreeContext (xpathCtx);
43  return xpathObj;
44}
45
46/**
47 * Create (or append to) an array valued maps value = "["",""]"
48 *
49 * @param m the conf maps containing the main.cfg settings
50 * @param mo the map to update
51 * @param mi the map to append
52 * @param elem the elements containing default definitions
53 * @return 0 on success, -1 on failure
54 */
55int appendMapsToMaps (maps * m, maps * mo, maps * mi, elements * elem){
56  maps *tmpMaps = getMaps (mo, mi->name);
57  map *tmap = getMapType (tmpMaps->content);
58  elements *el = getElements (elem, mi->name);
59  int hasEl = 1;
60  if (el == NULL)
61    hasEl = -1;
62  if (tmap == NULL)
63    {
64      if (hasEl > 0)
65        tmap = getMapType (el->defaults->content);
66    }
67 
68  map *testMap = NULL;
69  if (hasEl > 0)
70    {
71      testMap = getMap (el->content, "maxOccurs");
72    }
73  else
74    {
75      testMap = createMap ("maxOccurs", "unbounded");
76    }
77   
78  if (testMap != NULL)
79    {
80      if (strncasecmp (testMap->value, "unbounded", 9) != 0
81          && atoi (testMap->value) > 1)
82        {
83          addMapsArrayToMaps (&mo, mi, tmap->name);
84          map* nb=getMapFromMaps(mo,mi->name,"length");
85          if (nb!=NULL && atoi(nb->value)>atoi(testMap->value))
86            {
87              char emsg[1024];
88              sprintf (emsg,
89                       _("The maximum allowed occurrences for <%s> (%i) was exceeded."),
90                       mi->name, atoi (testMap->value));
91              errorException (m, emsg, "InternalError", NULL);
92              return -1;
93            }
94        }
95      else
96        {
97          if (strncasecmp (testMap->value, "unbounded", 9) == 0)
98            {
99              if (hasEl < 0)
100                {
101                  freeMap (&testMap);
102                  free (testMap);
103                }
104              if (addMapsArrayToMaps (&mo, mi, tmap->name) < 0)
105                {
106                  char emsg[1024];
107                  map *tmpMap = getMap (mi->content, "length");
108                  sprintf (emsg,
109                           _
110                           ("ZOO-Kernel was unable to load your data for %s position %s."),
111                           mi->name, tmpMap->value);
112                  errorException (m, emsg, "InternalError", NULL);
113                  return -1;
114                }
115            }
116          else
117            {
118              char emsg[1024];
119              sprintf (emsg,
120                       _
121                       ("The maximum allowed occurrences for <%s> is one."),
122                       mi->name);
123              errorException (m, emsg, "InternalError", NULL);
124              return -1;
125            }
126        }
127    }
128  return 0;
129}
130
131/**
132 * Parse inputs provided as KVP and store them in a maps.
133 *
134 * @param main_conf the conf maps containing the main.cfg settings
135 * @param s the service
136 * @param request_inputs the map storing KVP raw value
137 * @param request_output the maps to store the KVP pairs
138 * @param hInternet the HINTERNET queue to add potential requests
139 * @return 0 on success, -1 on failure
140 */
141int kvpParseInputs(maps** main_conf,service* s,map *request_inputs,maps** request_output,HINTERNET* hInternet){
142  // Parsing inputs provided as KVP
143  maps *tmpmaps = *request_output;
144  map* r_inputs = getMap (request_inputs, "DataInputs");
145  char* cursor_input;
146  if (r_inputs != NULL){
147    //snprintf (cursor_input, 40960, "%s", r_inputs->value);
[625]148    if(strstr(r_inputs->value,"=")==NULL)
149      cursor_input = url_decode (r_inputs->value);
150    else
151      cursor_input = zStrdup (r_inputs->value);
[621]152    int j = 0;
153
154    // Put each DataInputs into the inputs_as_text array
155    char *pToken;
156    pToken = strtok (cursor_input, ";");
157    char **inputs_as_text = (char **) malloc (100 * sizeof (char *));
158    if (inputs_as_text == NULL)
159      {
[626]160        free(cursor_input);
[621]161        return errorException (*main_conf, _("Unable to allocate memory."),
162                               "InternalError", NULL);
163      }
164    int i = 0;
165    while (pToken != NULL)
166      {
167        inputs_as_text[i] =
168          (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
169        if (inputs_as_text[i] == NULL)
170          {
[626]171            free(cursor_input);
[621]172            return errorException (*main_conf, _("Unable to allocate memory."),
173                                   "InternalError", NULL);
174          }
175        snprintf (inputs_as_text[i], strlen (pToken) + 1, "%s", pToken);
176        pToken = strtok (NULL, ";");
177        i++;
178      }
179       
180    for (j = 0; j < i; j++)
181      {
182        char *tmp = zStrdup (inputs_as_text[j]);
183        free (inputs_as_text[j]);
184        char *tmpc;
185        tmpc = strtok (tmp, "@");
186        while (tmpc != NULL)
187          {
188            char *tmpv = strstr (tmpc, "=");
189            char tmpn[256];
190            memset (tmpn, 0, 256);
191            if (tmpv != NULL)
192              {
193                strncpy (tmpn, tmpc,
194                         (strlen (tmpc) - strlen (tmpv)) * sizeof (char));
195                tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
196              }
197            else
198              {
199                strncpy (tmpn, tmpc, strlen (tmpc) * sizeof (char));
200                tmpn[strlen (tmpc)] = 0;
201              }
202            if (tmpmaps == NULL)
203              {
204                tmpmaps = (maps *) malloc (MAPS_SIZE);
205                if (tmpmaps == NULL)
206                  {
[626]207                    free(cursor_input);
[621]208                    return errorException (*main_conf,
209                                           _("Unable to allocate memory."),
210                                           "InternalError", NULL);
211                  }
212                tmpmaps->name = zStrdup (tmpn);
213                if (tmpv != NULL)
214                  {
215                    char *tmpvf = url_decode (tmpv + 1);
216                    tmpmaps->content = createMap ("value", tmpvf);
217                    free (tmpvf);
218                  }
219                else
220                  tmpmaps->content = createMap ("value", "Reference");
221                tmpmaps->next = NULL;
222              }
223            tmpc = strtok (NULL, "@");
224            while (tmpc != NULL)
225              {
226                char *tmpv1 = strstr (tmpc, "=");
227                char tmpn1[1024];
228                memset (tmpn1, 0, 1024);
229                if (tmpv1 != NULL)
230                  {
231                    strncpy (tmpn1, tmpc, strlen (tmpc) - strlen (tmpv1));
232                    tmpn1[strlen (tmpc) - strlen (tmpv1)] = 0;
233                    addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
234                  }
235                else
236                  {
237                    strncpy (tmpn1, tmpc, strlen (tmpc));
238                    tmpn1[strlen (tmpc)] = 0;
239                    map *lmap = getLastMap (tmpmaps->content);
240                    char *tmpValue =
241                      (char *) malloc ((strlen (tmpv) + strlen (tmpc) + 1) *
242                                       sizeof (char));
243                    sprintf (tmpValue, "%s@%s", tmpv + 1, tmpc);
244                    free (lmap->value);
245                    lmap->value = zStrdup (tmpValue);
246                    free (tmpValue);
247                    tmpc = strtok (NULL, "@");
248                    continue;
249                  }
250                if (strcmp (tmpn1, "xlink:href") != 0)
251                  addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
252                else if (tmpv1 != NULL)
253                  {
254                    char *tmpx2 = url_decode (tmpv1 + 1);
255                    if (strncasecmp (tmpx2, "http://", 7) != 0 &&
256                        strncasecmp (tmpx2, "ftp://", 6) != 0 &&
257                        strncasecmp (tmpx2, "https://", 8) != 0)
258                      {
259                        char emsg[1024];
260                        sprintf (emsg,
261                                 _
262                                 ("Unable to find a valid protocol to download the remote file %s"),
263                                 tmpv1 + 1);
[626]264                        free(cursor_input);
[621]265                        return errorException (*main_conf, emsg, "InternalError", NULL);
266                      }
267                    addToMap (tmpmaps->content, tmpn1, tmpx2);
268                    {
269                      if (loadRemoteFile
270                          (&*main_conf, &tmpmaps->content, hInternet, tmpx2) < 0)
271                        {
[626]272                          free(cursor_input);
[621]273                          return errorException (*main_conf, "Unable to fetch any ressource", "InternalError", NULL);
[626]274                        }
[621]275                      }
276                    free (tmpx2);
[627]277                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]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                          {
[626]298                            free(cursor_input);
[621]299                            return errorException (*main_conf, "Unable to append maps", "InternalError", NULL);
300                          }
301                      }
302                  }
303                else
[623]304                  addMapsToMaps (request_output, tmpmaps);
[621]305              }
306            freeMaps (&tmpmaps);
307            free (tmpmaps);
308            tmpmaps = NULL;
309            free (tmp);
310          }
311      }
312    free (inputs_as_text);
[626]313    free(cursor_input);
[621]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        {
[626]349          free(cursor_output);
[621]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            {
[626]360              free(cursor_output);
[621]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                        {
[626]385                          free(cursor_output);
[621]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
[623]419            addMapsToMaps (request_output, tmp_output);
[621]420          freeMaps (&tmp_output);
421          free (tmp_output);
422          tmp_output = NULL;
423          free (tmp);
424        }
425      free (outputs_as_text);
[626]426      free(cursor_output);
[621]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){
[622]443  int k = 0;
444  int l = 0;
445  for (k=0; k < nodes->nodeNr; k++)
[621]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                    };
[622]528                  for (l = 0; l < 5; l++)
[621]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
[623]546                                      (main_conf, &tmpmaps->content, hInternet,
[621]547                                       (char *) val) != 0)
548                                    {
549                                      return errorException (*main_conf,
550                                                             _("Unable to add a request in the queue."),
551                                                             "InternalError",
552                                                             NULL);
553                                    }
[628]554                                  addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]555                                }
[627]556                              addToMap (tmpmaps->content, "Reference", (char*) val);
[621]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                            }
[629]614                          hInternet->ihandle[hInternet->nb].header = NULL;
[621]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                                        {
[623]666                                          addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
[621]667                                          InternetOpenUrl (hInternet,
668                                                           btmp->value,
669                                                           tmp,
670                                                           xmlStrlen(btmps),
671                                                           INTERNET_FLAG_NO_CACHE_WRITE,
672                                                           0);
[627]673                                          addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]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                                  {
[623]734                                    addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
[621]735
736                                    res =
737                                      InternetOpenUrl (hInternet,
738                                                       btmp->value,
739                                                       tmp,
740                                                       strlen(tmp),
741                                                       INTERNET_FLAG_NO_CACHE_WRITE,
742                                                       0);
[627]743                                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
[621]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");
[622]769                          for (l = 0; l < 2; l++)
[621]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" };
[622]795                            for (l = 0; l < 3; l++)
[621]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                          map *tmpv = getMap (tmpmaps->content, "value");
905                          char *res = NULL;
906                          char *curs = tmpv->value;
[622]907                          int i = 0;
908                          for (i = 0; i <= strlen (tmpv->value) / 64;
[621]909                               i++)
910                            {
911                              if (res == NULL)
912                                res =
913                                  (char *) malloc (67 * sizeof (char));
914                              else
915                                res =
916                                  (char *) realloc (res,
917                                                    (((i + 1) * 65) +
918                                                     i) * sizeof (char));
919                              int csize = i * 65;
920                              strncpy (res + csize, curs, 64);
921                              if (i == xmlStrlen (tmp) / 64)
922                                strcat (res, "\n\0");
923                              else
924                                {
925                                  strncpy (res + (((i + 1) * 64) + i),
926                                           "\n\0", 2);
927                                  curs += 64;
928                                }
929                            }
930                          free (tmpv->value);
931                          tmpv->value = zStrdup (res);
932                          free (res);
933                          xmlFree (tmp);
934                        }
935                      cur4 = cur4->next;
936                    }
937                }
938              cur2 = cur2->next;
939            }
940
941          {
942            maps *testPresence =
943              getMaps (*request_output, tmpmaps->name);
944            if (testPresence != NULL)
945              {
946                elements *elem = getElements (s->inputs, tmpmaps->name);
947                if (elem != NULL)
948                  {
949                    if (appendMapsToMaps
950                        (*main_conf, *request_output, tmpmaps, elem) < 0)
951                      {
952                        return errorException (*main_conf,
953                                               _("Unable to append maps to maps."),
954                                               "InternalError",
955                                               NULL);
956                      }
957                  }
958              }
959            else
[623]960              addMapsToMaps (request_output, tmpmaps);
[621]961          }
962          freeMaps (&tmpmaps);
963          free (tmpmaps);
964          tmpmaps = NULL;
965        }
966    }
967  return 1;
968}
969
970/**
971 * Parse outputs from XML nodes and store them in a maps.
972 *
973 * @param main_conf the conf maps containing the main.cfg settings
974 * @param request_inputs the map storing KVP raw value
975 * @param request_output the maps to store the KVP pairs
976 * @param doc the xmlDocPtr containing the original request
977 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
978 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
979 * @return 0 on success, -1 on failure
980 */
981int xmlParseOutputs(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodePtr cur,bool raw){
[622]982  int l=0;
[621]983  if( raw == true)
984    {
985      addToMap (*request_inputs, "RawDataOutput", "");
986      if (cur->type == XML_ELEMENT_NODE)
987        {
988
989          maps *tmpmaps = (maps *) malloc (MAPS_SIZE);
990          if (tmpmaps == NULL)
991            {
992              return errorException (*main_conf, _("Unable to allocate memory."),
993                                     "InternalError", NULL);
994            }
995          tmpmaps->name = zStrdup ("unknownIdentifier");
996          tmpmaps->content = NULL;
997          tmpmaps->next = NULL;
998
999          // Get every attribute from a RawDataOutput node
1000          // mimeType, encoding, schema, uom
1001          const char *outs[4] =
1002            { "mimeType", "encoding", "schema", "uom" };
[622]1003          for (l = 0; l < 4; l++)
[621]1004            {
1005              xmlChar *val = xmlGetProp (cur, BAD_CAST outs[l]);
1006              if (val != NULL)
1007                {
1008                  if (strlen ((char *) val) > 0)
1009                    {
1010                      if (tmpmaps->content != NULL)
1011                        addToMap (tmpmaps->content, outs[l],
1012                                  (char *) val);
1013                      else
1014                        tmpmaps->content =
1015                          createMap (outs[l], (char *) val);
1016                    }
1017                  xmlFree (val);
1018                }
1019            }
1020          xmlNodePtr cur2 = cur->children;
1021          while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1022            cur2 = cur2->next;
1023          while (cur2 != NULL)
1024            {
1025              if (xmlStrncasecmp
1026                  (cur2->name, BAD_CAST "Identifier",
1027                   xmlStrlen (cur2->name)) == 0)
1028                {
1029                  xmlChar *val =
1030                    xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
1031                  free (tmpmaps->name);
1032                  tmpmaps->name = zStrdup ((char *) val);
1033                  xmlFree (val);
1034                }
1035              cur2 = cur2->next;
1036              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1037                cur2 = cur2->next;
1038            }
1039          if (*request_output == NULL)
1040            *request_output = dupMaps (&tmpmaps);
1041          else
[623]1042            addMapsToMaps (request_output, tmpmaps);
[621]1043          if (tmpmaps != NULL)
1044            {
1045              freeMaps (&tmpmaps);
1046              free (tmpmaps);
1047              tmpmaps = NULL;
1048            }
1049        }
1050    }
1051  else
1052    {
1053      addToMap (*request_inputs, "ResponseDocument", "");
1054
1055      if (cur->type == XML_ELEMENT_NODE) {
1056        // Get every attribute: storeExecuteResponse, lineage, status
1057        const char *ress[3] =
1058          { "storeExecuteResponse", "lineage", "status" };
1059        xmlChar *val;
[622]1060        for (l = 0; l < 3; l++)
[621]1061          {
1062            val = xmlGetProp (cur, BAD_CAST ress[l]);
1063            if (val != NULL && strlen ((char *) val) > 0)
1064              {
1065                addToMap (*request_inputs, ress[l], (char *) val);
1066              }
1067            xmlFree (val);
1068          }
1069                       
1070        xmlNodePtr cur1 = cur->children;               
1071        while (cur1 != NULL) // iterate over Output nodes
1072          {
1073            if (cur1->type != XML_ELEMENT_NODE || 
1074                xmlStrncasecmp(cur1->name, BAD_CAST "Output", 
1075                               xmlStrlen (cur1->name)) != 0) {
1076              cur1 = cur1->next;
1077              continue;
1078            }
1079                               
1080            maps *tmpmaps = (maps *) malloc (MAPS_SIZE); // one per Output node
1081            if (tmpmaps == NULL) {
1082              return errorException (*main_conf,
1083                                     _
1084                                     ("Unable to allocate memory."),
1085                                     "InternalError", NULL);
1086            }
1087            tmpmaps->name = zStrdup ("unknownIdentifier");
1088            tmpmaps->content = NULL;
1089            tmpmaps->next = NULL;
1090                               
1091            xmlNodePtr elems = cur1->children;
1092                               
1093            while (elems != NULL) {
1094
1095              // Identifier
1096              if (xmlStrncasecmp
1097                  (elems->name, BAD_CAST "Identifier",
1098                   xmlStrlen (elems->name)) == 0)
1099                {
1100                  xmlChar *val =
1101                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1102               
1103                  free(tmpmaps->name);
1104                  tmpmaps->name = zStrdup ((char *) val);
1105                  if (tmpmaps->content == NULL) {
1106                    tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
1107                  }
1108                  else {
1109                    addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
1110                  }
1111
1112                  map* tt = getMap (*request_inputs, "ResponseDocument");
1113                  if (strlen(tt->value) == 0) {
1114                    addToMap (*request_inputs, "ResponseDocument",
1115                              (char *) val);
1116                  }
1117                  else {
1118                    char* tmp = (char*) malloc((strlen(tt->value) + 1 
1119                                                + strlen((char*) val) + 1) * sizeof(char));
1120                    sprintf (tmp, "%s;%s", tt->value, (char *) val);
1121                    free(tt->value);
1122                    tt->value = tmp;
1123                  }
1124                  xmlFree (val);
1125                }
1126             
1127              // Title, Abstract
1128              else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
1129                                      xmlStrlen (elems->name)) == 0
1130                       || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
1131                                         xmlStrlen (elems->name)) == 0)
1132                {
1133                  xmlChar *val =
1134                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1135                                                       
1136                  if (tmpmaps->content == NULL) {
1137                    tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
1138                  }
1139                  else {
1140                    addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
1141                  }
1142                  xmlFree (val);
1143                }
1144              elems = elems->next;
1145            }
1146                               
1147            // Get every attribute from an Output node:
1148            // mimeType, encoding, schema, uom, asReference
1149            const char *outs[5] =
1150              { "mimeType", "encoding", "schema", "uom", "asReference" };
1151                                         
[622]1152            for (l = 0; l < 5; l++) {
[621]1153              xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                               
1154              if (val != NULL && xmlStrlen(val) > 0) {
1155                if (tmpmaps->content != NULL) {
1156                  addToMap (tmpmaps->content, outs[l], (char *) val);
1157                }                         
1158                else {
1159                  tmpmaps->content = createMap (outs[l], (char *) val);
1160                }       
1161              }
1162              xmlFree (val);
1163            }
1164                               
1165            if (*request_output == NULL) {
1166              *request_output = tmpmaps;
1167            }   
1168            else if (getMaps(*request_output, tmpmaps->name) != NULL) {
1169              return errorException (*main_conf,
1170                                     _
1171                                     ("Duplicate <Output> elements in WPS Execute request"),
1172                                     "InternalError", NULL);
1173            }
1174            else {
1175              maps* mptr = *request_output;
1176              while (mptr->next != NULL) {
1177                mptr = mptr->next;
1178              }
1179              mptr->next = tmpmaps;     
1180            }                                   
1181            cur1 = cur1->next;
1182          }                     
1183      }
1184    }
1185  return 1;
1186}
1187
1188/**
1189 * Parse XML request and store informations in maps.
1190 *
1191 * @param main_conf the conf maps containing the main.cfg settings
1192 * @param post the string containing the XML request
1193 * @param request_inputs the map storing KVP raw value
1194 * @param s the service
1195 * @param inputs the maps to store the KVP pairs
1196 * @param outputs the maps to store the KVP pairs
1197 * @param hInternet the HINTERNET queue to add potential requests
1198 * @return 0 on success, -1 on failure
1199 */
1200int xmlParseRequest(maps** main_conf,const char* post,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1201  xmlInitParser ();
1202  xmlDocPtr doc = xmlParseMemory (post, cgiContentLength);
1203
1204  /**
1205   * Extract Input nodes from the XML Request.
1206   */
1207  xmlXPathObjectPtr tmpsptr =
1208    extractFromDoc (doc, "/*/*/*[local-name()='Input']");
1209  xmlNodeSet *tmps = tmpsptr->nodesetval;
1210  if(xmlParseInputs(main_conf,s,inputs,doc,tmps,hInternet)<0){
1211    xmlXPathFreeObject (tmpsptr);
1212    xmlFreeDoc (doc);
1213    xmlCleanupParser ();
1214    return -1;
1215  }
1216  xmlXPathFreeObject (tmpsptr);
1217
1218  // Extract ResponseDocument / RawDataOutput from the XML Request
1219  tmpsptr =
1220    extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
1221  bool asRaw = false;
1222  tmps = tmpsptr->nodesetval;
1223  if (tmps->nodeNr == 0)
1224    {
1225      xmlXPathFreeObject (tmpsptr);
1226      tmpsptr =
1227        extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
1228      tmps = tmpsptr->nodesetval;
1229      asRaw = true;
1230    }
1231  if(tmps->nodeNr != 0){
1232    if(xmlParseOutputs(main_conf,request_inputs,outputs,doc,tmps->nodeTab[0],asRaw)<0){
1233      xmlXPathFreeObject (tmpsptr);
1234      xmlFreeDoc (doc);
1235      xmlCleanupParser ();
1236      return -1;
1237    }
1238  }
1239  xmlXPathFreeObject (tmpsptr);
1240  xmlFreeDoc (doc);
1241  xmlCleanupParser ();
1242  return 1;
1243}
1244
1245/**
1246 * Parse request and store informations in maps.
1247 *
1248 * @param main_conf the conf maps containing the main.cfg settings
1249 * @param post the string containing the XML request
1250 * @param request_inputs the map storing KVP raw value
1251 * @param s the service
1252 * @param inputs the maps to store the KVP pairs
1253 * @param outputs the maps to store the KVP pairs
1254 * @param hInternet the HINTERNET queue to add potential requests
1255 * @return 0 on success, -1 on failure
1256 * @see kvpParseOutputs,kvpParseInputs,xmlParseRequest
1257 */
1258int parseRequest(maps** main_conf,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1259  map *postRequest = NULL;
1260  postRequest = getMap (*request_inputs, "xrequest");
1261  if (postRequest == NULLMAP)
1262    {
1263      if(kvpParseOutputs(main_conf,*request_inputs,outputs)<0){
1264        return -1;
1265      }
1266      if(kvpParseInputs(main_conf,s,*request_inputs,inputs,hInternet)<0){
1267        return -1;
1268      }
1269    }
1270  else
1271    {
1272      //Parse XML request
1273      if(xmlParseRequest(main_conf,postRequest->value,request_inputs,s,inputs,outputs,hInternet)<0){
1274        return -1;
1275      }
1276    }
1277  return 1;
1278}
1279
1280/**
1281 * Ensure that each requested arguments are present in the request
1282 * DataInputs and ResponseDocument / RawDataOutput. Potentially run
1283 * http requests from the queue in parallel.
1284 * For optional inputs add default values defined in the ZCFG file.
1285 *
1286 * @param main_conf
1287 * @param s
1288 * @param original_request
1289 * @param request_inputs
1290 * @param request_outputs
1291 * @param hInternet
1292 *
1293 * @see runHttpRequests
1294 */
1295int validateRequest(maps** main_conf,service* s,map* original_request, maps** request_inputs,maps** request_outputs,HINTERNET* hInternet){
1296
1297  runHttpRequests (main_conf, request_inputs, hInternet);
1298  InternetCloseHandle (hInternet);
1299
1300  map* errI=NULL;
1301  char *dfv = addDefaultValues (request_inputs, s->inputs, *main_conf, 0,&errI);
1302
1303  maps *ptr = *request_inputs;
1304  while (ptr != NULL)
1305    {
1306      map *tmp0 = getMap (ptr->content, "size");
1307      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
1308      if (tmp1 != NULL && tmp0 != NULL)
1309        {
1310          float i = atof (tmp0->value) / 1048576.0;
1311          if (i >= atoi (tmp1->value))
1312            {
1313              char tmps[1024];
1314              map *tmpe = createMap ("code", "FileSizeExceeded");
1315              snprintf (tmps, 1024,
1316                        _
1317                        ("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)."),
1318                        ptr->name, tmp1->value, i);
1319              addToMap (tmpe, "locator", ptr->name);
1320              addToMap (tmpe, "text", tmps);
1321              printExceptionReportResponse (*main_conf, tmpe);
1322              freeMap (&tmpe);
1323              free (tmpe);
1324              return -1;
1325            }
1326        }
1327      ptr = ptr->next;
1328    }
1329
1330  map* errO=NULL;
1331  char *dfv1 =
1332    addDefaultValues (request_outputs, s->outputs, *main_conf, 1,&errO);
1333  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
1334    {
1335      char tmps[1024];
1336      map *tmpe = NULL;
1337      if (strcmp (dfv, "") != 0)
1338        {
1339          tmpe = createMap ("code", "MissingParameterValue");
1340          int nb=0;
1341          int length=1;
1342          map* len=getMap(errI,"length");
1343          if(len!=NULL)
1344            length=atoi(len->value);
1345          for(nb=0;nb<length;nb++){
1346            map* errp=getMapArray(errI,"value",nb);
1347            snprintf (tmps, 1024,
1348                      _
1349                      ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
1350                      errp->value);
1351            setMapArray (tmpe, "locator", nb , errp->value);
1352            setMapArray (tmpe, "text", nb , tmps);
1353            setMapArray (tmpe, "code", nb , "MissingParameterValue");
1354          }
1355        }
1356      if (strcmp (dfv1, "") != 0)
1357        {
1358          int ilength=0;
1359          if(tmpe==NULL)
1360            tmpe = createMap ("code", "InvalidParameterValue");
1361          else{
1362            map* len=getMap(tmpe,"length");
1363            if(len!=NULL)
1364              ilength=atoi(len->value);
1365          }
1366          int nb=0;
1367          int length=1;
1368          map* len=getMap(errO,"length");
1369          if(len!=NULL)
1370            length=atoi(len->value);
1371          for(nb=0;nb<length;nb++){
1372            map* errp=getMapArray(errO,"value",nb);
1373            snprintf (tmps, 1024,
1374                      _
1375                      ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
1376                      errp->value,
1377                      ((getMap(original_request,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
1378            setMapArray (tmpe, "locator", nb+ilength , errp->value);
1379            setMapArray (tmpe, "text", nb+ilength , tmps);
1380            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
1381          }
1382        }
1383      printExceptionReportResponse (*main_conf, tmpe);
1384      if(errI!=NULL){
1385        freeMap(&errI);
1386        free(errI);
1387      }
1388      if(errO!=NULL){
1389        freeMap(&errO);
1390        free(errO);
1391      }
1392      freeMap (&tmpe);
1393      free (tmpe);
1394      return -1;
1395    }
1396  maps *tmpReqI = *request_inputs;
1397  while (tmpReqI != NULL)
1398    {
1399      char name[1024];
1400      if (getMap (tmpReqI->content, "isFile") != NULL)
1401        {
1402          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
1403              cgiFormSuccess)
1404            {
1405              int BufferLen = 1024;
1406              cgiFilePtr file;
1407              int targetFile;
1408              char storageNameOnServer[2048];
1409              char fileNameOnServer[64];
1410              char contentType[1024];
1411              char buffer[1024];
1412              char *tmpStr = NULL;
1413              int size;
1414              int got, t;
1415              map *path = getMapFromMaps (*main_conf, "main", "tmpPath");
1416              cgiFormFileSize (tmpReqI->name, &size);
1417              cgiFormFileContentType (tmpReqI->name, contentType,
1418                                      sizeof (contentType));
1419              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
1420                {
1421                  t = -1;
1422                  while (1)
1423                    {
1424                      tmpStr = strstr (name + t + 1, "\\");
1425                      if (NULL == tmpStr)
1426                        tmpStr = strstr (name + t + 1, "/");
1427                      if (NULL != tmpStr)
1428                        t = (int) (tmpStr - name);
1429                      else
1430                        break;
1431                    }
1432                  strcpy (fileNameOnServer, name + t + 1);
1433
1434                  sprintf (storageNameOnServer, "%s/%s", path->value,
1435                           fileNameOnServer);
1436#ifdef DEBUG
1437                  fprintf (stderr, "Name on server %s\n",
1438                           storageNameOnServer);
1439                  fprintf (stderr, "fileNameOnServer: %s\n",
1440                           fileNameOnServer);
1441#endif
1442                  targetFile =
1443                    open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
1444                          S_IRWXU | S_IRGRP | S_IROTH);
1445                  if (targetFile < 0)
1446                    {
1447#ifdef DEBUG
1448                      fprintf (stderr, "could not create the new file,%s\n",
1449                               fileNameOnServer);
1450#endif
1451                    }
1452                  else
1453                    {
1454                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
1455                             == cgiFormSuccess)
1456                        {
1457                          if (got > 0)
1458                            write (targetFile, buffer, got);
1459                        }
1460                    }
1461                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
1462                  cgiFormFileClose (file);
1463                  close (targetFile);
1464#ifdef DEBUG
1465                  fprintf (stderr, "File \"%s\" has been uploaded",
1466                           fileNameOnServer);
1467#endif
1468                }
1469            }
1470        }
1471      tmpReqI = tmpReqI->next;
1472    }
1473
1474  ensureDecodedBase64 (request_inputs);
1475  return 1;
1476}
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