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

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

Avoid issue with declaration in loop.

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