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

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

Major update. Creation of a basic parsing api. Call validateRequest after fork if any.

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

Search

Context Navigation

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