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

Last change on this file since 938 was 938, checked in by djay, 5 years ago

Find cdata block when not the first child

  • Property svn:keywords set to Id
File size: 56.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#include "server_internal.h"
28#include "response_print.h"
29#include "caching.h"
30#include "cgic.h"
31
32/**
33 * Apply XPath Expression on XML document.
34 *
35 * @param doc the XML Document
36 * @param search the XPath expression
37 * @return xmlXPathObjectPtr containing the resulting nodes set
38 */
39xmlXPathObjectPtr
40extractFromDoc (xmlDocPtr doc, const char *search)
41{
42  xmlXPathContextPtr xpathCtx;
43  xmlXPathObjectPtr xpathObj;
44  xpathCtx = xmlXPathNewContext (doc);
45  xpathObj = xmlXPathEvalExpression (BAD_CAST search, xpathCtx);
46  xmlXPathFreeContext (xpathCtx);
47  return xpathObj;
48}
49
50/**
51 * Create (or append to) an array valued maps value = "["",""]"
52 *
53 * @param m the conf maps containing the main.cfg settings
54 * @param mo the map to update
55 * @param mi the map to append
56 * @param elem the elements containing default definitions
57 * @return 0 on success, -1 on failure
58 */
59int appendMapsToMaps (maps * m, maps * mo, maps * mi, elements * elem){
60  maps *tmpMaps = getMaps (mo, mi->name);
61  map *tmap = getMapType (tmpMaps->content);
62  elements *el = getElements (elem, mi->name);
63  elements *cursor = elem;
64  while(cursor!=NULL && el==NULL){
65    if(cursor->child!=NULL)
66      el = getElements (cursor->child, mi->name);
67    cursor=cursor->next;
68  }
69  int hasEl = 1;
70  if (el == NULL)
71    hasEl = -1;
72
73  if (tmap == NULL)
74    {
75      if (hasEl > 0)
76        tmap = getMapType (el->defaults->content);
77    }
78 
79  map *testMap = NULL;
80  if (hasEl > 0)
81    {
82      testMap = getMap (el->content, "maxOccurs");
83    }
84  else
85    {
86      testMap = createMap ("maxOccurs", "unbounded");
87    }
88   
89  if (testMap != NULL)
90    {
91      if (strncasecmp (testMap->value, "unbounded", 9) != 0
92          && atoi (testMap->value) > 1)
93        {
94          addMapsArrayToMaps (&mo, mi, tmap->name);
95          map* nb=getMapFromMaps(mo,mi->name,"length");
96          if (nb!=NULL && atoi(nb->value)>atoi(testMap->value))
97            {
98              char emsg[1024];
99              sprintf (emsg,
100                       _("The maximum allowed occurrences for <%s> (%i) was exceeded."),
101                       mi->name, atoi (testMap->value));
102              errorException (m, emsg, "InternalError", NULL);
103              return -1;
104            }
105        }
106      else
107        {
108          if (strncasecmp (testMap->value, "unbounded", 9) == 0)
109            {
110              if (hasEl < 0)
111                {
112                  freeMap (&testMap);
113                  free (testMap);
114                }
115              if (addMapsArrayToMaps (&mo, mi, tmap->name) < 0)
116                {
117                  char emsg[1024];
118                  map *tmpMap = getMap (mi->content, "length");
119                  sprintf (emsg,
120                           _
121                           ("ZOO-Kernel was unable to load your data for %s position %s."),
122                           mi->name, tmpMap->value);
123                  errorException (m, emsg, "InternalError", NULL);
124                  return -1;
125                }
126            }
127          else
128            {
129              char emsg[1024];
130              sprintf (emsg,
131                       _
132                       ("The maximum allowed occurrences for <%s> is one."),
133                       mi->name);
134              errorException (m, emsg, "InternalError", NULL);
135              return -1;
136            }
137        }
138    }
139  return 0;
140}
141
142/**
143 * Make sure that each value encoded in base64 in a maps is decoded.
144 *
145 * @param in the maps containing the values
146 * @see readBase64
147 */
148void ensureDecodedBase64(maps **in){
149  maps* cursor=*in;
150  while(cursor!=NULL){
151    map *tmp=getMap(cursor->content,"encoding");
152    if(tmp!=NULL && strncasecmp(tmp->value,"base64",6)==0){
153      tmp=getMap(cursor->content,"value");
154      readBase64(&tmp);
155      addToMap(cursor->content,"base64_value",tmp->value);
156      int size=0;
157      char *s=zStrdup(tmp->value);
158      free(tmp->value);
159      tmp->value=base64d(s,strlen(s),&size);
160      free(s);
161      char sizes[1024];
162      sprintf(sizes,"%d",size);
163      addToMap(cursor->content,"size",sizes);
164    }
165    map* length=getMap(cursor->content,"length");
166    if(length!=NULL){
167      int len=atoi(length->value);
168      for(int i=1;i<len;i++){
169        tmp=getMapArray(cursor->content,"encoding",i);
170        if(tmp!=NULL && strncasecmp(tmp->value,"base64",6)==0){
171          char key[17];
172          sprintf(key,"base64_value_%d",i);
173          tmp=getMapArray(cursor->content,"value",i);
174          readBase64(&tmp);
175          addToMap(cursor->content,key,tmp->value);
176          int size=0;
177          char *s=zStrdup(tmp->value);
178          free(tmp->value);
179          tmp->value=base64d(s,strlen(s),&size);
180          free(s);
181          char sizes[1024];
182          sprintf(sizes,"%d",size);
183          sprintf(key,"size_%d",i);
184          addToMap(cursor->content,key,sizes);
185        }
186      }
187    }
188    if(cursor->child!=NULL)
189      ensureDecodedBase64(&cursor->child);
190    cursor=cursor->next;
191  }
192}
193
194/**
195 * Parse inputs provided as KVP and store them in a maps.
196 *
197 * @param main_conf the conf maps containing the main.cfg settings
198 * @param s the service
199 * @param request_inputs the map storing KVP raw value
200 * @param request_output the maps to store the KVP pairs
201 * @param hInternet the HINTERNET queue to add potential requests
202 * @return 0 on success, -1 on failure
203 */
204int kvpParseInputs(maps** main_conf,service* s,map *request_inputs,maps** request_output,HINTERNET* hInternet){
205  // Parsing inputs provided as KVP
206  maps *tmpmaps = *request_output;
207  map* r_inputs = getMap (request_inputs, "DataInputs");
208  char* cursor_input;
209  if (r_inputs != NULL){
210    //snprintf (cursor_input, 40960, "%s", r_inputs->value);
211    if(strstr(r_inputs->value,"=")==NULL)
212      cursor_input = url_decode (r_inputs->value);
213    else
214      cursor_input = zStrdup (r_inputs->value);
215    int j = 0;
216
217    // Put each DataInputs into the inputs_as_text array
218    char *pToken;
219    pToken = strtok (cursor_input, ";");
220    char **inputs_as_text = (char **) malloc (100 * sizeof (char *));
221    if (inputs_as_text == NULL)
222      {
223        free(cursor_input);
224        return errorException (*main_conf, _("Unable to allocate memory"),
225                               "InternalError", NULL);
226      }
227    int i = 0;
228    while (pToken != NULL)
229      {
230        inputs_as_text[i] =
231          (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
232        if (inputs_as_text[i] == NULL)
233          {
234            free(cursor_input);
235            return errorException (*main_conf, _("Unable to allocate memory"),
236                                   "InternalError", NULL);
237          }
238        snprintf (inputs_as_text[i], strlen (pToken) + 1, "%s", pToken);
239        pToken = strtok (NULL, ";");
240        i++;
241      }
242       
243    for (j = 0; j < i; j++)
244      {
245        char *tmp = zStrdup (inputs_as_text[j]);
246        free (inputs_as_text[j]);
247        char *tmpc;
248        tmpc = strtok (tmp, "@");
249        while (tmpc != NULL)
250          {
251            char *tmpv = strstr (tmpc, "=");
252            char tmpn[256];
253            memset (tmpn, 0, 256);
254            if (tmpv != NULL)
255              {
256                strncpy (tmpn, tmpc,
257                         (strlen (tmpc) - strlen (tmpv)) * sizeof (char));
258                tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
259              }
260            else
261              {
262                strncpy (tmpn, tmpc, strlen (tmpc) * sizeof (char));
263                tmpn[strlen (tmpc)] = 0;
264              }
265            if (tmpmaps == NULL)
266              {
267                tmpmaps = createMaps(tmpn);
268                if (tmpmaps == NULL)
269                  {
270                    free(cursor_input);
271                    return errorException (*main_conf,
272                                           _("Unable to allocate memory"),
273                                           "InternalError", NULL);
274                  }
275                if (tmpv != NULL)
276                  {
277                    char *tmpvf = url_decode (tmpv + 1);
278                    tmpmaps->content = createMap ("value", tmpvf);
279                    free (tmpvf);
280                  }
281                else
282                  tmpmaps->content = createMap ("value", "Reference");
283                tmpmaps->next = NULL;
284              }
285            tmpc = strtok (NULL, "@");
286            while (tmpc != NULL)
287              {
288                char *tmpv1 = strstr (tmpc, "=");
289                char tmpn1[1024];
290                memset (tmpn1, 0, 1024);
291                if (tmpv1 != NULL)
292                  {
293                    strncpy (tmpn1, tmpc, strlen (tmpc) - strlen (tmpv1));
294                    tmpn1[strlen (tmpc) - strlen (tmpv1)] = 0;
295                    addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
296                  }
297                else
298                  {
299                    strncpy (tmpn1, tmpc, strlen (tmpc));
300                    tmpn1[strlen (tmpc)] = 0;
301                    map *lmap = getLastMap (tmpmaps->content);
302                    char *tmpValue =
303                      (char *) malloc ((strlen (tmpv) + strlen (tmpc) + 1) *
304                                       sizeof (char));
305                    sprintf (tmpValue, "%s@%s", tmpv + 1, tmpc);
306                    free (lmap->value);
307                    lmap->value = zStrdup (tmpValue);
308                    free (tmpValue);
309                    tmpc = strtok (NULL, "@");
310                    continue;
311                  }
312                if (strcmp (tmpn1, "xlink:href") != 0)
313                  addToMap (tmpmaps->content, tmpn1, tmpv1 + 1);
314                else if (tmpv1 != NULL)
315                  {
316                    char *tmpx2 = url_decode (tmpv1 + 1);
317                    if (strncasecmp (tmpx2, "http://", 7) != 0 &&
318                        strncasecmp (tmpx2, "ftp://", 6) != 0 &&
319                        strncasecmp (tmpx2, "https://", 8) != 0)
320                      {
321                        char emsg[1024];
322                        sprintf (emsg,
323                                 _
324                                 ("Unable to find a valid protocol to download the remote file %s"),
325                                 tmpv1 + 1);
326                        free(cursor_input);
327                        return errorException (*main_conf, emsg, "InternalError", NULL);
328                      }
329                    addToMap (tmpmaps->content, tmpn1, tmpx2);
330                    {
331                      if (loadRemoteFile
332                          (&*main_conf, &tmpmaps->content, hInternet, tmpx2) < 0)
333                        {
334                          free(cursor_input);
335                          return errorException (*main_conf, "Unable to fetch any resource", "InternalError", NULL);
336                        }
337                      }
338                    free (tmpx2);
339                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
340                    addToMap (tmpmaps->content, "Reference", tmpv1 + 1);
341                  }
342                tmpc = strtok (NULL, "@");
343              }
344            if (*request_output == NULL)
345              *request_output = dupMaps (&tmpmaps);
346            else
347              {
348                maps *testPresence =
349                  getMaps (*request_output, tmpmaps->name);
350                if (testPresence != NULL)
351                  {
352                    elements *elem =
353                      getElements (s->inputs, tmpmaps->name);
354                    if (elem != NULL)
355                      {
356                        if (appendMapsToMaps
357                            (*main_conf, *request_output, tmpmaps,
358                             elem) < 0)
359                          {
360                            free(cursor_input);
361                            return errorException (*main_conf, "Unable to append maps", "InternalError", NULL);
362                          }
363                      }
364                  }
365                else
366                  addMapsToMaps (request_output, tmpmaps);
367              }
368            freeMaps (&tmpmaps);
369            free (tmpmaps);
370            tmpmaps = NULL;
371            free (tmp);
372          }
373      }
374    free (inputs_as_text);
375    free(cursor_input);
376  }
377  return 1;
378}
379
380/**
381 * Parse outputs provided as KVP and store them in a maps.
382 *
383 * @param main_conf the conf maps containing the main.cfg settings
384 * @param request_inputs the map storing KVP raw value
385 * @param request_output the maps to store the KVP pairs
386 * @return 0 on success, -1 on failure
387 */
388int kvpParseOutputs(maps** main_conf,map *request_inputs,maps** request_output){
389  /**
390   * Parsing outputs provided as KVP
391   */
392  map *r_inputs = NULL;
393  r_inputs = getMap (request_inputs, "ResponseDocument");
394  if (r_inputs == NULL)
395    r_inputs = getMap (request_inputs, "RawDataOutput");
396
397  if (r_inputs != NULL)
398    {
399      char *cursor_output = zStrdup (r_inputs->value);
400      int j = 0;
401
402      /**
403       * Put each Output into the outputs_as_text array
404       */
405      char *pToken;
406      maps *tmp_output = NULL;
407      pToken = strtok (cursor_output, ";");
408      char **outputs_as_text = (char **) malloc (128 * sizeof (char *));
409      if (outputs_as_text == NULL)
410        {
411          free(cursor_output);
412          return errorException (*main_conf, _("Unable to allocate memory"),
413                                 "InternalError", NULL);
414        }
415      int i = 0;
416      while (pToken != NULL)
417        {
418          outputs_as_text[i] =
419            (char *) malloc ((strlen (pToken) + 1) * sizeof (char));
420          if (outputs_as_text[i] == NULL)
421            {
422              free(cursor_output);
423              return errorException (*main_conf, _("Unable to allocate memory"),
424                                     "InternalError", NULL);
425            }
426          snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s",
427                    pToken);
428          pToken = strtok (NULL, ";");
429          i++;
430        }
431      for (j = 0; j < i; j++)
432        {
433          char *tmp = zStrdup (outputs_as_text[j]);
434          free (outputs_as_text[j]);
435          char *tmpc;
436          tmpc = strtok (tmp, "@");
437          int k = 0;
438          while (tmpc != NULL)
439            {
440              if (k == 0)
441                {
442                  if (tmp_output == NULL)
443                    {
444                      tmp_output = createMaps(tmpc);
445                      if (tmp_output == NULL)
446                        {
447                          free(cursor_output);
448                          return errorException (*main_conf,
449                                                 _
450                                                 ("Unable to allocate memory"),
451                                                 "InternalError", NULL);
452                        }
453                    }
454                }
455              else
456                {
457                  char *tmpv = strstr (tmpc, "=");
458                  char tmpn[256];
459                  memset (tmpn, 0, 256);
460                  strncpy (tmpn, tmpc,
461                           (strlen (tmpc) -
462                            strlen (tmpv)) * sizeof (char));
463                  tmpn[strlen (tmpc) - strlen (tmpv)] = 0;
464                  if (tmp_output->content == NULL)
465                    {
466                      tmp_output->content = createMap (tmpn, tmpv + 1);
467                      tmp_output->content->next = NULL;
468                    }
469                  else
470                    addToMap (tmp_output->content, tmpn, tmpv + 1);
471                }
472              k++;
473              tmpc = strtok (NULL, "@");
474            }
475          if (*request_output == NULL)
476            *request_output = dupMaps (&tmp_output);
477          else
478            addMapsToMaps (request_output, tmp_output);
479          freeMaps (&tmp_output);
480          free (tmp_output);
481          tmp_output = NULL;
482          free (tmp);
483        }
484      free (outputs_as_text);
485      free(cursor_output);
486    }
487  return 1;
488}
489
490/**
491 * Create a "missingIdentifier" maps in case it is NULL.
492 *
493 * @param main_conf the conf maps containing the main.cfg settings
494 * @param mymaps the maps to update
495 * @return 0 on success, 4 on failure
496 */
497int defineMissingIdentifier(maps** main_conf,maps** mymaps){
498  if (*mymaps == NULL){
499    *mymaps = createMaps("missingIndetifier");
500    if (*mymaps == NULL){
501      return errorException (*main_conf,
502                             _("Unable to allocate memory"),
503                             "InternalError", NULL);
504    }
505  }
506  return 0;
507}
508
509/**
510 * Parse inputs from XML nodes and store them in a maps.
511 *
512 * @param main_conf the conf maps containing the main.cfg settings
513 * @param s the service
514 * @param request_output the maps to store the KVP pairs
515 * @param doc the xmlDocPtr containing the original request
516 * @param nodes the input nodes array
517 * @param hInternet the HINTERNET queue to add potential requests
518 * @return 0 on success, -1 on failure
519 */
520int xmlParseInputs(maps** main_conf,service* s,maps** request_output,xmlDocPtr doc,xmlNodeSet* nodes,HINTERNET* hInternet){
521  int k = 0;
522  int l = 0;
523  map* version=getMapFromMaps(*main_conf,"main","rversion");
524  map* memory=getMapFromMaps(*main_conf,"main","memory");
525  int vid=getVersionId(version->value);
526  for (k=0; k < nodes->nodeNr; k++)
527    {
528      maps *tmpmaps = NULL;
529      xmlNodePtr cur = nodes->nodeTab[k];
530
531      if (nodes->nodeTab[k]->type == XML_ELEMENT_NODE)
532        {
533          // A specific Input node.
534          if(vid==1){
535            xmlChar *val = xmlGetProp (cur, BAD_CAST "id");
536            tmpmaps = createMaps((char *) val);
537            xmlFree(val);
538          }
539
540          xmlNodePtr cur2 = cur->children;
541          while (cur2 != NULL)
542            {
543              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
544                cur2 = cur2->next;
545              if (cur2 == NULL)
546                break;
547              // Indentifier
548              if (xmlStrncasecmp
549                  (cur2->name, BAD_CAST "Identifier",
550                   xmlStrlen (cur2->name)) == 0)
551                {
552                  xmlChar *val =
553                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
554                  if (tmpmaps == NULL && val!=NULL)
555                    {
556                      tmpmaps = createMaps((char*)val);
557                      if (tmpmaps == NULL)
558                        {
559                          return errorException (*main_conf,
560                                                 _
561                                                 ("Unable to allocate memory"),
562                                                 "InternalError", NULL);
563                        }
564                      xmlFree (val);
565                    }
566                }
567              // Title, Asbtract
568              if (xmlStrncasecmp
569                  (cur2->name, BAD_CAST "Title",
570                   xmlStrlen (cur2->name)) == 0
571                  || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract",
572                                     xmlStrlen (cur2->name)) == 0)
573                {
574                  xmlChar *val =
575                    xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1);
576                  defineMissingIdentifier(main_conf,&tmpmaps);
577                  if(val!=NULL){
578                    if (tmpmaps->content != NULL)
579                      addToMap (tmpmaps->content,
580                                (char *) cur2->name, (char *) val);
581                    else
582                      tmpmaps->content =
583                        createMap ((char *) cur2->name, (char *) val);
584                    xmlFree (val);
585                  }
586                }
587              // InputDataFormChoice (Reference or Data ?) / 2.0.0 DataInputType / Input
588              if (xmlStrcasecmp (cur2->name, BAD_CAST "Input") == 0)
589                {
590                  char *xpathExpr=(char*)malloc(61+strlen(tmpmaps->name));
591                  sprintf(xpathExpr,"/*/*[local-name()='Input' and @id='%s']/*[local-name()='Input']",tmpmaps->name);
592                  xmlXPathObjectPtr tmpsptr = extractFromDoc (doc, xpathExpr);
593                  xmlNodeSet *tmps = tmpsptr->nodesetval;
594                  if(tmps!=NULL){
595                    maps* request_output1=NULL;
596                    if(xmlParseInputs(main_conf,s,&request_output1,doc,tmps,hInternet)<0)
597                      return -1;
598                    if(tmpmaps->child==NULL)
599                      tmpmaps->child=dupMaps(&request_output1);
600                    else
601                      addMapsToMaps(&tmpmaps->child,request_output1);
602                    freeMaps(&request_output1);
603                    free(request_output1);
604                  }
605                  while(cur2->next!=NULL)
606                    cur2=cur2->next;
607                }
608              else if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0)
609                {
610                  defineMissingIdentifier(main_conf,&tmpmaps);
611                  // Get every attribute from a Reference node
612                  // mimeType, encoding, schema, href, method
613                  // Header and Body gesture should be added here
614                  const char *refs[5] =
615                    { "mimeType", "encoding", "schema", "method",
616                      "href"
617                    };
618                  for (l = 0; l < 5; l++)
619                    {
620                      xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]);
621                      if (val != NULL && xmlStrlen (val) > 0)
622                        {
623                          if (tmpmaps->content != NULL)
624                            addToMap (tmpmaps->content, refs[l],
625                                      (char *) val);
626                          else
627                            tmpmaps->content =
628                              createMap (refs[l], (char *) val);
629                          map *ltmp = getMap (tmpmaps->content, "method");
630                          if (l == 4 )
631                            {
632                              if ((ltmp==NULL || strncmp (ltmp->value, "POST",4) != 0))
633                                {
634                                  if (loadRemoteFile
635                                      (main_conf, &tmpmaps->content, hInternet,
636                                       (char *) val) != 0)
637                                    {
638                                      return errorException (*main_conf,
639                                                             _("Unable to add a request in the queue."),
640                                                             "InternalError",
641                                                             NULL);
642                                    }
643                                  addIntToMap (tmpmaps->content, "Order", hInternet->nb);
644                                }
645                              addToMap (tmpmaps->content, "Reference", (char*) val);
646                            }
647                        }
648                      xmlFree (val);
649                    }
650                  // Parse Header and Body from Reference
651                  xmlNodePtr cur3 = cur2->children;
652                  while (cur3 != NULL)
653                    {
654                      while (cur3 != NULL
655                             && cur3->type != XML_ELEMENT_NODE)
656                        cur3 = cur3->next;
657                      if (cur3 == NULL)
658                        break;
659                      if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") ==
660                          0)
661                        {
662                          const char *ha[2];
663                          ha[0] = "key";
664                          ha[1] = "value";
665                          int hai;
666                          char *has=NULL;
667                          char *key;
668                          for (hai = 0; hai < 2; hai++)
669                            {
670                              xmlChar *val =
671                                xmlGetProp (cur3, BAD_CAST ha[hai]);
672#ifdef POST_DEBUG
673                              fprintf (stderr, "%s = %s\n", ha[hai],
674                                       (char *) val);
675#endif
676                              if (hai == 0)
677                                {
678                                  key = zStrdup ((char *) val);
679                                }
680                              else
681                                {
682                                  has =
683                                    (char *)
684                                    malloc ((4 + xmlStrlen (val) +
685                                             strlen (key)) *
686                                            sizeof (char));
687                                  if (has == NULL)
688                                    {
689                                      return errorException (*main_conf,
690                                                             _
691                                                             ("Unable to allocate memory"),
692                                                             "InternalError",
693                                                             NULL);
694                                    }
695                                  snprintf (has,
696                                            (3 + xmlStrlen (val) +
697                                             strlen (key)), "%s: %s", key,
698                                            (char *) val);
699                                  free (key);
700                                }
701                              xmlFree (val);
702                            }
703                          if (has != NULL){
704                            hInternet->ihandle[hInternet->nb].header = NULL;
705                            hInternet->ihandle[hInternet->nb].header =
706                              curl_slist_append (hInternet->ihandle
707                                                 [hInternet->nb].header,
708                                                 has);
709                            free (has);
710                          }
711                        }
712                      else
713                        {
714#ifdef POST_DEBUG
715                          fprintf (stderr,
716                                   "Try to fetch the body part of the request ...\n");
717#endif
718                          if (xmlStrcasecmp (cur3->name, BAD_CAST "Body")
719                              == 0)
720                            {
721#ifdef POST_DEBUG
722                              fprintf (stderr, "Body part found (%s) !!!\n",
723                                       (char *) cur3->content);
724#endif
725                              char *tmp = NULL;
726                              xmlNodePtr cur4 = cur3->children;
727                              while (cur4 != NULL)
728                                {
729                                  while (cur4 && cur4 != NULL && cur4->type && cur4->type != XML_ELEMENT_NODE){
730                                    if(cur4->next)
731                                      cur4 = cur4->next;
732                                    else
733                                      cur4 = NULL;
734                                  }
735                                  if(cur4 != NULL) {
736                                    xmlDocPtr bdoc =
737                                      xmlNewDoc (BAD_CAST "1.0");
738                                    bdoc->encoding =
739                                      xmlCharStrdup ("UTF-8");
740                                    xmlDocSetRootElement (bdoc, cur4);
741                                    xmlChar *btmps;
742                                    int bsize;
743                                    // TODO : check for encoding defined in the input
744                                    xmlDocDumpFormatMemoryEnc(bdoc, &btmps, &bsize, "UTF-8", 0);
745                                    if (btmps != NULL){
746                                      tmp = (char *) malloc ((bsize + 1) * sizeof (char));
747
748                                      sprintf (tmp, "%s", (char*) btmps);
749
750                                      //xmlFreeDoc (bdoc);
751                                         
752                                      map *btmp =
753                                        getMap (tmpmaps->content, "Reference");
754                                      addToMap (tmpmaps->content, "Body", tmp);
755                                      if (btmp != NULL)
756                                        {
757                                          addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
758                                          InternetOpenUrl (hInternet,
759                                                           btmp->value,
760                                                           tmp,
761                                                           xmlStrlen(btmps),
762                                                           INTERNET_FLAG_NO_CACHE_WRITE,
763                                                           0,
764                                                           *main_conf);
765                                          addIntToMap (tmpmaps->content, "Order", hInternet->nb);
766                                        }
767                                      xmlFree (btmps);
768                                      free (tmp);
769                                      break;
770                                    }
771                                  }
772                                  cur4 = cur4->next;
773                                }
774                            }
775                          else
776                            if (xmlStrcasecmp
777                                (cur3->name,
778                                 BAD_CAST "BodyReference") == 0)
779                              {
780                                xmlChar *val =
781                                  xmlGetProp (cur3, BAD_CAST "href");
782                                HINTERNET bInternet, res1, res;
783                                maps *tmpConf=createMaps("main");
784                                tmpConf->content=createMap("memory","load");
785                                bInternet = InternetOpen (
786#ifndef WIN32
787                                                          (LPCTSTR)
788#endif
789                                                          "ZooWPSClient\0",
790                                                          INTERNET_OPEN_TYPE_PRECONFIG,
791                                                          NULL, NULL, 0);
792#ifndef WIN32
793                                if (!CHECK_INET_HANDLE (bInternet))
794                                  fprintf (stderr,
795                                           "WARNING : bInternet handle failed to initialize");
796#endif
797                                bInternet.waitingRequests[0] =
798                                  zStrdup ((char *) val);
799                                res1 =
800                                  InternetOpenUrl (&bInternet,
801                                                   bInternet.waitingRequests
802                                                   [0], NULL, 0,
803                                                   INTERNET_FLAG_NO_CACHE_WRITE,
804                                                   0,
805                                                   tmpConf);
806                                processDownloads (&bInternet);
807                                freeMaps(&tmpConf);
808                                free(tmpConf);
809                                char *tmp =
810                                  (char *)
811                                  malloc ((bInternet.ihandle[0].nDataLen +
812                                           1) * sizeof (char));
813                                if (tmp == NULL)
814                                  {
815                                    return errorException (*main_conf,
816                                                           _
817                                                           ("Unable to allocate memory"),
818                                                           "InternalError",
819                                                           NULL);
820                                  }
821                                size_t bRead;
822                                InternetReadFile (bInternet.ihandle[0],
823                                                  (LPVOID) tmp,
824                                                  bInternet.
825                                                  ihandle[0].nDataLen,
826                                                  &bRead);
827                                tmp[bInternet.ihandle[0].nDataLen] = 0;
828                                InternetCloseHandle(&bInternet);
829                                addToMap (tmpmaps->content, "Body", tmp);
830                                map *btmp =
831                                  getMap (tmpmaps->content, "href");
832                                if (btmp != NULL)
833                                  {
834                                    addRequestToQueue(main_conf,hInternet,(char *) btmp->value,false);
835
836                                    res =
837                                      InternetOpenUrl (hInternet,
838                                                       btmp->value,
839                                                       tmp,
840                                                       strlen(tmp),
841                                                       INTERNET_FLAG_NO_CACHE_WRITE,
842                                                       0,
843                                                       *main_conf);
844                                    addIntToMap (tmpmaps->content, "Order", hInternet->nb);
845                                  }
846                                free (tmp);
847                                xmlFree (val);
848                              }
849                        }
850                      cur3 = cur3->next;
851                    }
852                }
853              else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0)
854                {
855                  defineMissingIdentifier(main_conf,&tmpmaps);
856                  xmlNodePtr cur4 = cur2->children;
857                  if(vid==1){
858                    // Get every dataEncodingAttributes from a Data node:
859                    // mimeType, encoding, schema
860                    const char *coms[3] =
861                      { "mimeType", "encoding", "schema" };
862                    for (l = 0; l < 3; l++){
863                      xmlChar *val =
864                          xmlGetProp (cur4, BAD_CAST coms[l]);
865                        if (val != NULL && strlen ((char *) val) > 0){
866                          if (tmpmaps->content != NULL)
867                            addToMap (tmpmaps->content,coms[l],(char *) val);
868                          else
869                            tmpmaps->content =
870                              createMap (coms[l],(char *) val);
871                        }
872                        xmlFree (val);
873                    }
874                    while (cur4 != NULL){
875                      while(cur4 != NULL && 
876                            cur4->type != XML_CDATA_SECTION_NODE &&
877                            cur4->type != XML_TEXT_NODE &&
878                            cur4->type != XML_ELEMENT_NODE)
879                        cur4=cur4->next;
880                      if(cur4!=NULL){
881                        if (cur4->type == XML_ELEMENT_NODE)
882                          {
883                            xmlChar *mv;
884                            int buffersize;
885                            xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
886                            xmlDocSetRootElement (doc1, cur4);
887                            xmlDocDumpFormatMemoryEnc (doc1, &mv,
888                                                       &buffersize,
889                                                       "utf-8", 0);
890                            if (tmpmaps->content != NULL)
891                              addToMap (tmpmaps->content, "value",
892                                        (char *) mv);
893                            else
894                              tmpmaps->content =
895                                createMap ("value", (char *) mv);
896                            free(mv);
897                          }
898                        else{
899                          if (tmpmaps->content != NULL)
900                            addToMap (tmpmaps->content, "value",
901                                      (char *) cur4->content);
902                          else
903                            tmpmaps->content =
904                              createMap ("value", (char *) cur4->content);
905                        }
906                        cur4=cur4->next;
907                      }
908                    }
909                  }
910
911                  while (cur4 != NULL)
912                    {
913                      while (cur4 != NULL
914                             && cur4->type != XML_ELEMENT_NODE)
915                        cur4 = cur4->next;
916                      if (cur4 == NULL)
917                        break;
918                      if (xmlStrcasecmp
919                          (cur4->name, BAD_CAST "LiteralData") == 0)
920                        {
921                          // Get every attribute from a LiteralData node
922                          // dataType , uom
923                          char *list[2];
924                          list[0] = zStrdup ("dataType");
925                          list[1] = zStrdup ("uom");
926                          for (l = 0; l < 2; l++)
927                            {
928                              xmlChar *val =
929                                xmlGetProp (cur4, BAD_CAST list[l]);
930                              if (val != NULL
931                                  && strlen ((char *) val) > 0)
932                                {
933                                  if (tmpmaps->content != NULL)
934                                    addToMap (tmpmaps->content, list[l],
935                                              (char *) val);
936                                  else
937                                    tmpmaps->content =
938                                      createMap (list[l], (char *) val);
939                                  xmlFree (val);
940                                }
941                              else{
942                                if(l==0){
943                                  if (tmpmaps->content != NULL)
944                                    addToMap (tmpmaps->content, list[l],
945                                              "string");
946                                  else
947                                    tmpmaps->content =
948                                      createMap (list[l],"string");
949                                }
950                              }
951                              free (list[l]);
952                            }
953                        }
954                      else
955                        if (xmlStrcasecmp
956                            (cur4->name, BAD_CAST "ComplexData") == 0)
957                          {
958                            // Get every attribute from a Reference node
959                            // mimeType, encoding, schema
960                            const char *coms[3] =
961                              { "mimeType", "encoding", "schema" };
962                            for (l = 0; l < 3; l++)
963                              {
964                                xmlChar *val =
965                                  xmlGetProp (cur4, BAD_CAST coms[l]);
966                                if (val != NULL
967                                    && strlen ((char *) val) > 0)
968                                  {
969                                    if (tmpmaps->content != NULL)
970                                      addToMap (tmpmaps->content, coms[l],
971                                                (char *) val);
972                                    else
973                                      tmpmaps->content =
974                                        createMap (coms[l], (char *) val);
975                                    xmlFree (val);
976                                  }
977                              }
978                          }
979
980                      map *test = getMap (tmpmaps->content, "encoding");
981                      if (test == NULL)
982                        {
983                          if (tmpmaps->content != NULL)
984                            addToMap (tmpmaps->content, "encoding",
985                                      "utf-8");
986                          else
987                            tmpmaps->content =
988                              createMap ("encoding", "utf-8");
989                          test = getMap (tmpmaps->content, "encoding");
990                        }
991
992                      if (getMap(tmpmaps->content,"dataType")==NULL && test!=NULL && strcasecmp (test->value, "base64") != 0)
993                        {
994                          xmlChar *mv = NULL;
995                          /*if(cur4!=NULL && cur4->xmlChildrenNode!=NULL)
996                            xmlChar *mv = xmlNodeListGetString (doc,
997                                                                cur4->xmlChildrenNode,
998                                                                1);*/
999                          map *ltmp =
1000                            getMap (tmpmaps->content, "mimeType");
1001                          if (/*mv == NULL
1002                              ||*/
1003                              (xmlStrcasecmp
1004                               (cur4->name, BAD_CAST "ComplexData") == 0
1005                               && (ltmp == NULL
1006                                   || strncasecmp (ltmp->value,
1007                                                   "text/xml", 8) == 0)))
1008                            {
1009                              xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
1010                              int buffersize;
1011                              xmlNodePtr cur5 = cur4->children;
1012                              while (cur5 != NULL
1013                                     && cur5->type != XML_ELEMENT_NODE
1014                                     && cur5->type != XML_CDATA_SECTION_NODE)
1015                                cur5 = cur5->next;
1016                              if (cur5 != NULL
1017                                  && cur5->type != XML_CDATA_SECTION_NODE)
1018                                {
1019                                  xmlDocSetRootElement (doc1, cur5);
1020                                  xmlDocDumpFormatMemoryEnc (doc1, &mv,
1021                                                             &buffersize,
1022                                                             "utf-8", 0);
1023                                  xmlFreeDoc (doc1);
1024                                }
1025                              else
1026                                {
1027                                  if (cur5 != NULL
1028                                      && cur5->type == XML_CDATA_SECTION_NODE){
1029                                    xmlDocPtr doc2 = xmlReadMemory((const char*)cur5->content,xmlStrlen(cur5->content),
1030                                                                   "input_content.xml", NULL, XML_PARSE_RECOVER);
1031                                    xmlDocDumpFormatMemoryEnc (doc2, &mv,
1032                                                               &buffersize,
1033                                                               "utf-8", 0);
1034                                    xmlFreeDoc (doc2);
1035                                  }
1036                                }
1037                              addIntToMap (tmpmaps->content, "size",
1038                                           buffersize);
1039                            }else{
1040
1041                            if(xmlStrcasecmp
1042                               (cur4->name, BAD_CAST "BoundingBoxData") == 0){
1043                              xmlDocPtr doc1 = xmlNewDoc(BAD_CAST "1.0");
1044                              int buffersize;
1045                              xmlDocSetRootElement(doc1,cur4);
1046                              xmlDocDumpFormatMemoryEnc(doc1,&mv,
1047                                                        &buffersize,
1048                                                        "utf-8",0);
1049                              addIntToMap (tmpmaps->content, "size",
1050                                           buffersize);
1051                              xmlParseBoundingBox(main_conf,&tmpmaps->content,doc1);
1052                            }else{
1053                              xmlNodePtr cur5 = cur4->children;
1054                              while (cur5 != NULL
1055                                     && cur5->type != XML_ELEMENT_NODE
1056                                     && cur5->type != XML_TEXT_NODE
1057                                     && cur5->type != XML_CDATA_SECTION_NODE)
1058                                cur5 = cur5->next;
1059                              if (cur5 != NULL
1060                                  && cur5->type != XML_CDATA_SECTION_NODE
1061                                  && cur5->type != XML_TEXT_NODE)
1062                                {
1063                                  xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0");
1064                                  int buffersize;
1065                                  xmlDocSetRootElement (doc1, cur5);
1066                                  xmlDocDumpFormatMemoryEnc (doc1, &mv,
1067                                                             &buffersize,
1068                                                             "utf-8", 0);
1069                                  addIntToMap (tmpmaps->content, "size",
1070                                               buffersize);
1071                                }
1072                              else if (cur5 != NULL)/*
1073                                     && cur5->type == XML_CDATA_SECTION_NODE)*/{
1074                                while(cur5!=NULL && cur5->type != XML_CDATA_SECTION_NODE)
1075                                  cur5=cur5->next;
1076                                xmlFree(mv);
1077                                if(cur5!=NULL && cur5->content!=NULL){
1078                                  mv=xmlStrdup(cur5->content);
1079                                }
1080                              }
1081                            }
1082                          }
1083                          if (mv != NULL)
1084                            {
1085                              addToMap (tmpmaps->content, "value",
1086                                        (char*) mv);
1087                              xmlFree (mv);
1088                            }
1089                        }
1090                      else
1091                        {
1092                          xmlNodePtr cur5 = cur4->children;
1093                          while (cur5 != NULL
1094                                 && cur5->type != XML_CDATA_SECTION_NODE)
1095                            cur5 = cur5->next;
1096                          if (cur5 != NULL
1097                              && cur5->type == XML_CDATA_SECTION_NODE)
1098                            {
1099                              addToMap (tmpmaps->content,
1100                                        "value",
1101                                        (char *) cur5->content);
1102                            }
1103                          else{
1104                            if(cur4->xmlChildrenNode!=NULL){
1105                              xmlChar *tmp = xmlNodeListGetRawString (doc,
1106                                                                      cur4->xmlChildrenNode,
1107                                                                      0);
1108                              addToMap (tmpmaps->content, "value",
1109                                        (char *) tmp);
1110                              xmlFree (tmp);
1111                            }
1112                          }
1113                        }
1114
1115                      cur4 = cur4->next;
1116                    }
1117                }
1118              cur2 = cur2->next;
1119              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE){
1120                cur2 = cur2->next;
1121              }
1122            }
1123          if(memory!=NULL && strncasecmp(memory->value,"load",4)!=0)
1124            if(getMap(tmpmaps->content,"to_load")==NULL){
1125              addToMap(tmpmaps->content,"to_load","false");
1126            }
1127          {
1128            map* test=getMap(tmpmaps->content,"value");
1129            if(test==NULL && tmpmaps->child==NULL)
1130              addToMap(tmpmaps->content,"value","");
1131            maps *testPresence = getMaps (*request_output, tmpmaps->name);
1132            maps *cursor=*request_output;
1133            while(testPresence == NULL && cursor!=NULL){
1134              if(cursor->child!=NULL){
1135                testPresence = getMaps (cursor->child, tmpmaps->name);
1136              }
1137              cursor=cursor->next;
1138            }
1139            if (testPresence != NULL)
1140              {
1141                elements *elem = getElements (s->inputs, tmpmaps->name);
1142                elements *cursor=s->inputs;
1143                while(elem == NULL && cursor!=NULL){
1144                  if(cursor->child!=NULL){
1145                    elem = getElements (cursor->child, tmpmaps->name);
1146                  }
1147                  cursor=cursor->next;
1148                }
1149                if (elem != NULL)
1150                  {
1151                    if (appendMapsToMaps
1152                        (*main_conf, testPresence, tmpmaps, elem) < 0)
1153                      {
1154                        return errorException (*main_conf,
1155                                               _("Unable to append maps to maps."),
1156                                               "InternalError",
1157                                               NULL);
1158                      }
1159                  }
1160              }
1161            else{
1162              addMapsToMaps (request_output, tmpmaps);
1163            }
1164          }
1165          freeMaps (&tmpmaps);
1166          free (tmpmaps);
1167          tmpmaps = NULL;
1168        }
1169    }
1170  return 1;
1171}
1172
1173/**
1174 * Parse a BoundingBoxData node
1175 *
1176 * http://schemas.opengis.net/ows/1.1.0/owsCommon.xsd: BoundingBoxType
1177 *
1178 * A map to store boundingbox information will contain:
1179 *  - LowerCorner : double double (minimum within this bounding box)
1180 *  - UpperCorner : double double (maximum within this bounding box)
1181 *  - crs : URI (Reference to definition of the CRS)
1182 *  - dimensions : int
1183 *
1184 * @param main_conf the conf maps containing the main.cfg settings
1185 * @param request_inputs the map storing KVP raw value
1186 * @param doc the xmlDocPtr containing the BoudingoxData node
1187 * @return a map containing all the bounding box keys
1188 */
1189int xmlParseBoundingBox(maps** main_conf,map** current_input,xmlDocPtr doc){
1190  xmlNode *root_element = xmlDocGetRootElement(doc);
1191  for(xmlAttrPtr attr = root_element->properties; NULL != attr; attr = attr->next){
1192    xmlChar *val = xmlGetProp (root_element, BAD_CAST attr->name);
1193    addToMap(*current_input,(char*)attr->name,(char*)val);
1194    xmlFree(val);
1195    xmlNodePtr cur = root_element->children;
1196    while(cur!=NULL && cur->type != XML_ELEMENT_NODE)
1197      cur=cur->next;
1198    while(cur!=NULL && cur->type==XML_ELEMENT_NODE){
1199      xmlChar *val =
1200        xmlNodeListGetString (doc, cur->xmlChildrenNode, 1);
1201      addToMap(*current_input,(char*)cur->name,(char*)val);
1202      cur=cur->next;
1203      xmlFree(val);
1204      while(cur!=NULL && cur->type != XML_ELEMENT_NODE)
1205        cur=cur->next;
1206    }
1207  }
1208  return 0;
1209}
1210
1211/**
1212 * Parse outputs from XML nodes and store them in a maps (WPS version 2.0.0).
1213 *
1214 * @param main_conf the conf maps containing the main.cfg settings
1215 * @param request_inputs the map storing KVP raw value
1216 * @param request_output the maps to store the KVP pairs
1217 * @param doc the xmlDocPtr containing the original request
1218 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
1219 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
1220 * @return 0 on success, -1 on failure
1221 */
1222int xmlParseOutputs2(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodeSet* nodes){
1223  int k = 0;
1224  int l = 0;
1225  for (k=0; k < nodes->nodeNr; k++){
1226    maps *tmpmaps = NULL;
1227    xmlNodePtr cur = nodes->nodeTab[k];
1228    if (cur->type == XML_ELEMENT_NODE){
1229      maps *tmpmaps = NULL;
1230      xmlChar *val = xmlGetProp (cur, BAD_CAST "id");
1231      if(val!=NULL)
1232        tmpmaps = createMaps((char *)val);
1233      else
1234        tmpmaps = createMaps("unknownIdentifier");
1235      const char ress[4][13] =
1236        { "mimeType", "encoding", "schema", "transmission" };
1237      xmlFree (val);
1238      for (l = 0; l < 4; l++){
1239        val = xmlGetProp (cur, BAD_CAST ress[l]);
1240        if (val != NULL && strlen ((char *) val) > 0)
1241          {
1242            if (tmpmaps->content != NULL)
1243              addToMap (tmpmaps->content, ress[l],
1244                        (char *) val);
1245            else
1246              tmpmaps->content =
1247                createMap (ress[l], (char *) val);
1248            if(l==3 && strncasecmp((char*)val,"reference",xmlStrlen(val))==0)
1249              addToMap (tmpmaps->content,"asReference","true");
1250          }
1251        xmlFree (val);
1252      }
1253      if(cur->children!=NULL){
1254        xmlNodePtr ccur = cur->children;
1255        while (ccur != NULL){
1256          if(ccur->type == XML_ELEMENT_NODE){
1257            char *xpathExpr=(char*)malloc(66+strlen(tmpmaps->name));
1258            sprintf(xpathExpr,"/*/*[local-name()='Output' and @id='%s']/*[local-name()='Output']",tmpmaps->name);           
1259            xmlXPathObjectPtr tmpsptr = extractFromDoc (doc, xpathExpr);
1260            xmlNodeSet* cnodes = tmpsptr->nodesetval;
1261            xmlParseOutputs2(main_conf,request_inputs,&tmpmaps->child,doc,cnodes);
1262            xmlXPathFreeObject (tmpsptr);
1263            free(xpathExpr);
1264            break;
1265          }
1266          ccur = ccur->next;
1267        }
1268      }
1269      if (*request_output == NULL){
1270        *request_output = dupMaps(&tmpmaps);
1271      }
1272      else{
1273        addMapsToMaps(request_output,tmpmaps);
1274      }
1275      freeMaps(&tmpmaps);
1276      free(tmpmaps);
1277    }
1278  }
1279  return 0;
1280}
1281
1282/**
1283 * Parse outputs from XML nodes and store them in a maps.
1284 *
1285 * @param main_conf the conf maps containing the main.cfg settings
1286 * @param request_inputs the map storing KVP raw value
1287 * @param request_output the maps to store the KVP pairs
1288 * @param doc the xmlDocPtr containing the original request
1289 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
1290 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
1291 * @return 0 on success, -1 on failure
1292 */
1293int xmlParseOutputs(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodePtr cur,bool raw){
1294  int l=0;
1295  if( raw == true)
1296    {
1297      addToMap (*request_inputs, "RawDataOutput", "");
1298      if (cur->type == XML_ELEMENT_NODE)
1299        {
1300
1301          maps *tmpmaps = createMaps("unknownIdentifier");
1302          if (tmpmaps == NULL)
1303            {
1304              return errorException (*main_conf, _("Unable to allocate memory"),
1305                                     "InternalError", NULL);
1306            }
1307
1308          // Get every attribute from a RawDataOutput node
1309          // mimeType, encoding, schema, uom
1310          const char *outs[4] =
1311            { "mimeType", "encoding", "schema", "uom" };
1312          for (l = 0; l < 4; l++)
1313            {
1314              xmlChar *val = xmlGetProp (cur, BAD_CAST outs[l]);
1315              if (val != NULL)
1316                {
1317                  if (strlen ((char *) val) > 0)
1318                    {
1319                      if (tmpmaps->content != NULL)
1320                        addToMap (tmpmaps->content, outs[l],
1321                                  (char *) val);
1322                      else
1323                        tmpmaps->content =
1324                          createMap (outs[l], (char *) val);
1325                    }
1326                  xmlFree (val);
1327                }
1328            }
1329          xmlNodePtr cur2 = cur->children;
1330          while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1331            cur2 = cur2->next;
1332          while (cur2 != NULL)
1333            {
1334              if (xmlStrncasecmp
1335                  (cur2->name, BAD_CAST "Identifier",
1336                   xmlStrlen (cur2->name)) == 0)
1337                {
1338                  xmlChar *val =
1339                    xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
1340                  free (tmpmaps->name);
1341                  tmpmaps->name = zStrdup ((char *) val);
1342                  xmlFree (val);
1343                }
1344              cur2 = cur2->next;
1345              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1346                cur2 = cur2->next;
1347            }
1348          if (*request_output == NULL)
1349            *request_output = dupMaps (&tmpmaps);
1350          else
1351            addMapsToMaps (request_output, tmpmaps);
1352          if (tmpmaps != NULL)
1353            {
1354              freeMaps (&tmpmaps);
1355              free (tmpmaps);
1356              tmpmaps = NULL;
1357            }
1358        }
1359    }
1360  else
1361    {
1362      addToMap (*request_inputs, "ResponseDocument", "");
1363
1364      if (cur->type == XML_ELEMENT_NODE) {
1365        // Get every attribute: storeExecuteResponse, lineage, status
1366        const char *ress[3] =
1367          { "storeExecuteResponse", "lineage", "status" };
1368        xmlChar *val;
1369        for (l = 0; l < 3; l++)
1370          {
1371            val = xmlGetProp (cur, BAD_CAST ress[l]);
1372            if (val != NULL && strlen ((char *) val) > 0)
1373              {
1374                addToMap (*request_inputs, ress[l], (char *) val);
1375              }
1376            xmlFree (val);
1377          }
1378                       
1379        xmlNodePtr cur1 = cur->children;               
1380        while (cur1 != NULL) // iterate over Output nodes
1381          {
1382            if (cur1->type != XML_ELEMENT_NODE || 
1383                xmlStrncasecmp(cur1->name, BAD_CAST "Output", 
1384                               xmlStrlen (cur1->name)) != 0) {
1385              cur1 = cur1->next;
1386              continue;
1387            }
1388                               
1389            maps *tmpmaps = createMaps("unknownIdentifier"); // one per Output node
1390            if (tmpmaps == NULL) {
1391              return errorException (*main_conf,
1392                                     _
1393                                     ("Unable to allocate memory"),
1394                                     "InternalError", NULL);
1395            }
1396                               
1397            xmlNodePtr elems = cur1->children;
1398                               
1399            while (elems != NULL) {
1400
1401              // Identifier
1402              if (xmlStrncasecmp
1403                  (elems->name, BAD_CAST "Identifier",
1404                   xmlStrlen (elems->name)) == 0)
1405                {
1406                  xmlChar *val =
1407                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1408               
1409                  free(tmpmaps->name);
1410                  tmpmaps->name = zStrdup ((char *) val);
1411                  if (tmpmaps->content == NULL) {
1412                    tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
1413                  }
1414                  else {
1415                    addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
1416                  }
1417
1418                  map* tt = getMap (*request_inputs, "ResponseDocument");
1419                  if (strlen(tt->value) == 0) {
1420                    addToMap (*request_inputs, "ResponseDocument",
1421                              (char *) val);
1422                  }
1423                  else {
1424                    char* tmp = (char*) malloc((strlen(tt->value) + 1 
1425                                                + strlen((char*) val) + 1) * sizeof(char));
1426                    sprintf (tmp, "%s;%s", tt->value, (char *) val);
1427                    free(tt->value);
1428                    tt->value = tmp;
1429                  }
1430                  xmlFree (val);
1431                }
1432             
1433              // Title, Abstract
1434              else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
1435                                      xmlStrlen (elems->name)) == 0
1436                       || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
1437                                         xmlStrlen (elems->name)) == 0)
1438                {
1439                  xmlChar *val =
1440                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1441                                                       
1442                  if (tmpmaps->content == NULL) {
1443                    tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
1444                  }
1445                  else {
1446                    addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
1447                  }
1448                  xmlFree (val);
1449                }
1450              elems = elems->next;
1451            }
1452                               
1453            // Get every attribute from an Output node:
1454            // mimeType, encoding, schema, uom, asReference
1455            const char *outs[5] =
1456              { "mimeType", "encoding", "schema", "uom", "asReference" };
1457                                         
1458            for (l = 0; l < 5; l++) {
1459              xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                               
1460              if (val != NULL && xmlStrlen(val) > 0) {
1461                if (tmpmaps->content != NULL) {
1462                  addToMap (tmpmaps->content, outs[l], (char *) val);
1463                }                         
1464                else {
1465                  tmpmaps->content = createMap (outs[l], (char *) val);
1466                }       
1467              }
1468              xmlFree (val);
1469            }
1470                               
1471            if (*request_output == NULL) {
1472              *request_output = tmpmaps;
1473            }   
1474            else if (getMaps(*request_output, tmpmaps->name) != NULL) {
1475              return errorException (*main_conf,
1476                                     _
1477                                     ("Duplicate <Output> elements in WPS Execute request"),
1478                                     "InternalError", NULL);
1479            }
1480            else {
1481              maps* mptr = *request_output;
1482              while (mptr->next != NULL) {
1483                mptr = mptr->next;
1484              }
1485              mptr->next = tmpmaps;     
1486            }                                   
1487            cur1 = cur1->next;
1488          }                     
1489      }
1490    }
1491  return 1;
1492}
1493
1494/**
1495 * Parse XML request and store information in maps.
1496 *
1497 * @param main_conf the conf maps containing the main.cfg settings
1498 * @param post the string containing the XML request
1499 * @param request_inputs the map storing KVP raw value
1500 * @param s the service
1501 * @param inputs the maps to store the KVP pairs
1502 * @param outputs the maps to store the KVP pairs
1503 * @param hInternet the HINTERNET queue to add potential requests
1504 * @return 0 on success, -1 on failure
1505 */
1506int xmlParseRequest(maps** main_conf,const char* post,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1507
1508  map* version=getMapFromMaps(*main_conf,"main","rversion");
1509  int vid=getVersionId(version->value);
1510
1511  xmlInitParser ();
1512  xmlDocPtr doc = xmlReadMemory (post, cgiContentLength, "input_request.xml", NULL, XML_PARSE_RECOVER);
1513
1514  /**
1515   * Extract Input nodes from the XML Request.
1516   */
1517  xmlXPathObjectPtr tmpsptr =
1518    extractFromDoc (doc, (vid==0?"/*/*/*[local-name()='Input']":"/*/*[local-name()='Input']"));
1519  xmlNodeSet *tmps = tmpsptr->nodesetval;
1520  if(tmps==NULL || xmlParseInputs(main_conf,s,inputs,doc,tmps,hInternet)<0){
1521    xmlXPathFreeObject (tmpsptr);
1522    xmlFreeDoc (doc);
1523    xmlCleanupParser ();
1524    return -1;
1525  }
1526  xmlXPathFreeObject (tmpsptr);
1527
1528  if(vid==1){
1529    tmpsptr =
1530      extractFromDoc (doc, "/*[local-name()='Execute']");
1531    bool asRaw = false;
1532    tmps = tmpsptr->nodesetval;
1533    if(tmps->nodeNr > 0){
1534      int k = 0;
1535      for (k=0; k < tmps->nodeNr; k++){
1536        maps *tmpmaps = NULL;
1537        xmlNodePtr cur = tmps->nodeTab[k];
1538        if (cur->type == XML_ELEMENT_NODE){
1539          xmlChar *val = xmlGetProp (cur, BAD_CAST "mode");
1540          if(val!=NULL)
1541            addToMap(*request_inputs,"mode",(char*)val);
1542          else
1543            addToMap(*request_inputs,"mode","auto");
1544          xmlFree(val);
1545          val = xmlGetProp (cur, BAD_CAST "response");
1546          if(val!=NULL){
1547            addToMap(*request_inputs,"response",(char*)val);
1548            if(strncasecmp((char*)val,"raw",xmlStrlen(val))==0)
1549              addToMap(*request_inputs,"RawDataOutput","");
1550            else
1551              addToMap(*request_inputs,"ResponseDocument","");
1552          }
1553          else{
1554            addToMap(*request_inputs,"response","document");
1555            addToMap(*request_inputs,"ResponseDocument","");
1556          }
1557          xmlFree(val);
1558        }
1559      }
1560    }
1561    xmlXPathFreeObject (tmpsptr);
1562    tmpsptr =
1563      extractFromDoc (doc, "/*/*[local-name()='Output']");
1564    tmps = tmpsptr->nodesetval;
1565    if(tmps->nodeNr > 0){
1566      if(xmlParseOutputs2(main_conf,request_inputs,outputs,doc,tmps)<0){
1567        xmlXPathFreeObject (tmpsptr);
1568        xmlFreeDoc (doc);
1569        xmlCleanupParser ();
1570        return -1;
1571      }
1572    }
1573  }
1574  else{
1575    // Extract ResponseDocument / RawDataOutput from the XML Request
1576    tmpsptr =
1577      extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
1578    bool asRaw = false;
1579    tmps = tmpsptr->nodesetval;
1580    if (tmps->nodeNr == 0)
1581      {
1582        xmlXPathFreeObject (tmpsptr);
1583        tmpsptr =
1584          extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
1585        tmps = tmpsptr->nodesetval;
1586        asRaw = true;
1587      }
1588    if(tmps->nodeNr != 0){
1589      if(xmlParseOutputs(main_conf,request_inputs,outputs,doc,tmps->nodeTab[0],asRaw)<0){
1590        xmlXPathFreeObject (tmpsptr);
1591        xmlFreeDoc (doc);
1592        xmlCleanupParser ();
1593        return -1;
1594      }
1595    }
1596  }
1597  xmlXPathFreeObject (tmpsptr);
1598  xmlFreeDoc (doc);
1599  xmlCleanupParser ();
1600  return 1;
1601}
1602
1603/**
1604 * Parse request and store information in maps.
1605 *
1606 * @param main_conf the conf maps containing the main.cfg settings
1607 * @param post the string containing the XML request
1608 * @param request_inputs the map storing KVP raw value
1609 * @param s the service
1610 * @param inputs the maps to store the KVP pairs
1611 * @param outputs the maps to store the KVP pairs
1612 * @param hInternet the HINTERNET queue to add potential requests
1613 * @return 0 on success, -1 on failure
1614 * @see kvpParseOutputs,kvpParseInputs,xmlParseRequest
1615 */
1616int parseRequest(maps** main_conf,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1617  map *postRequest = NULL;
1618  postRequest = getMap (*request_inputs, "xrequest");
1619  if (postRequest == NULLMAP)
1620    {
1621      if(kvpParseOutputs(main_conf,*request_inputs,outputs)<0){
1622        return -1;
1623      }
1624      if(kvpParseInputs(main_conf,s,*request_inputs,inputs,hInternet)<0){
1625        return -1;
1626      }
1627    }
1628  else
1629    {
1630      //Parse XML request
1631      if(xmlParseRequest(main_conf,postRequest->value,request_inputs,s,inputs,outputs,hInternet)<0){
1632        return -1;
1633      }
1634    }
1635  return 1;
1636}
1637
1638/**
1639 * Ensure that each requested arguments are present in the request
1640 * DataInputs and ResponseDocument / RawDataOutput. Potentially run
1641 * http requests from the queue in parallel.
1642 * For optional inputs add default values defined in the ZCFG file.
1643 *
1644 * @param main_conf
1645 * @param s
1646 * @param original_request
1647 * @param request_inputs
1648 * @param request_outputs
1649 * @param hInternet
1650 *
1651 * @see runHttpRequests
1652 */
1653int validateRequest(maps** main_conf,service* s,map* original_request, maps** request_inputs,maps** request_outputs,HINTERNET* hInternet){
1654
1655  map* errI0=NULL;
1656  runHttpRequests (main_conf, request_inputs, hInternet,&errI0);
1657  if(errI0!=NULL){
1658    printExceptionReportResponse (*main_conf, errI0);
1659    InternetCloseHandle (hInternet);
1660    return -1;
1661  }
1662  InternetCloseHandle (hInternet);
1663
1664
1665  map* errI=NULL;
1666  char *dfv = addDefaultValues (request_inputs, s->inputs, *main_conf, 0,&errI);
1667
1668  maps *ptr = *request_inputs;
1669  while (ptr != NULL)
1670    {
1671      map *tmp0 = getMap (ptr->content, "size");
1672      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
1673      if (tmp1 != NULL && tmp0 != NULL)
1674        {
1675          float i = atof (tmp0->value) / 1048576.0;
1676          if (i >= atoi (tmp1->value))
1677            {
1678              char tmps[1024];
1679              map *tmpe = createMap ("code", "FileSizeExceeded");
1680              snprintf (tmps, 1024,
1681                        _
1682                        ("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)."),
1683                        ptr->name, tmp1->value, i);
1684              addToMap (tmpe, "locator", ptr->name);
1685              addToMap (tmpe, "text", tmps);
1686              printExceptionReportResponse (*main_conf, tmpe);
1687              freeMap (&tmpe);
1688              free (tmpe);
1689              return -1;
1690            }
1691        }
1692      ptr = ptr->next;
1693    }
1694
1695  map* errO=NULL;
1696  char *dfv1 =
1697    addDefaultValues (request_outputs, s->outputs, *main_conf, 1,&errO);
1698
1699  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
1700    {
1701      char tmps[1024];
1702      map *tmpe = NULL;
1703      if (strcmp (dfv, "") != 0)
1704        {
1705          tmpe = createMap ("code", "MissingParameterValue");
1706          int nb=0;
1707          int length=1;
1708          map* len=getMap(errI,"length");
1709          if(len!=NULL)
1710            length=atoi(len->value);
1711          for(nb=0;nb<length;nb++){
1712            map* errp=getMapArray(errI,"value",nb);
1713            snprintf (tmps, 1024,
1714                      _
1715                      ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
1716                      errp->value);
1717            setMapArray (tmpe, "locator", nb , errp->value);
1718            setMapArray (tmpe, "text", nb , tmps);
1719            setMapArray (tmpe, "code", nb , "MissingParameterValue");
1720          }
1721        }
1722      if (strcmp (dfv1, "") != 0)
1723        {
1724          int ilength=0;
1725          if(tmpe==NULL)
1726            tmpe = createMap ("code", "InvalidParameterValue");
1727          else{
1728            map* len=getMap(tmpe,"length");
1729            if(len!=NULL)
1730              ilength=atoi(len->value);
1731          }
1732          int nb=0;
1733          int length=1;
1734          map* len=getMap(errO,"length");
1735          if(len!=NULL)
1736            length=atoi(len->value);
1737          for(nb=0;nb<length;nb++){
1738            map* errp=getMapArray(errO,"value",nb);
1739            snprintf (tmps, 1024,
1740                      _
1741                      ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
1742                      errp->value,
1743                      ((getMap(original_request,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
1744            setMapArray (tmpe, "locator", nb+ilength , errp->value);
1745            setMapArray (tmpe, "text", nb+ilength , tmps);
1746            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
1747          }
1748        }
1749      printExceptionReportResponse (*main_conf, tmpe);
1750      if(errI!=NULL){
1751        freeMap(&errI);
1752        free(errI);
1753      }
1754      if(errO!=NULL){
1755        freeMap(&errO);
1756        free(errO);
1757      }
1758      freeMap (&tmpe);
1759      free (tmpe);
1760      return -1;
1761    }
1762  maps *tmpReqI = *request_inputs;
1763  while (tmpReqI != NULL)
1764    {
1765      char name[1024];
1766      if (getMap (tmpReqI->content, "isFile") != NULL)
1767        {
1768          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
1769              cgiFormSuccess)
1770            {
1771              int BufferLen = 1024;
1772              cgiFilePtr file;
1773              int targetFile;
1774              char *storageNameOnServer;
1775              char *fileNameOnServer;
1776              char contentType[1024];
1777              char buffer[1024];
1778              char *tmpStr = NULL;
1779              int size;
1780              int got, t;
1781              map *path = getMapFromMaps (*main_conf, "main", "tmpPath");
1782              cgiFormFileSize (tmpReqI->name, &size);
1783              cgiFormFileContentType (tmpReqI->name, contentType,
1784                                      sizeof (contentType));
1785              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
1786                {
1787                  t = -1;
1788                  while (1)
1789                    {
1790                      tmpStr = strstr (name + t + 1, "\\");
1791                      if (NULL == tmpStr)
1792                        tmpStr = strstr (name + t + 1, "/");
1793                      if (NULL != tmpStr)
1794                        t = (int) (tmpStr - name);
1795                      else
1796                        break;
1797                    }
1798                  fileNameOnServer=(char*)malloc((strlen(name) - t - 1 )*sizeof(char));
1799                  strcpy (fileNameOnServer, name + t + 1);
1800
1801                  storageNameOnServer=(char*)malloc((strlen(path->value) + strlen(fileNameOnServer) + 2)*sizeof(char));
1802                  sprintf (storageNameOnServer, "%s/%s", path->value,
1803                           fileNameOnServer);
1804#ifdef DEBUG
1805                  fprintf (stderr, "Name on server %s\n",
1806                           storageNameOnServer);
1807                  fprintf (stderr, "fileNameOnServer: %s\n",
1808                           fileNameOnServer);
1809#endif
1810                  targetFile =
1811                    zOpen (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
1812                          S_IRWXU | S_IRGRP | S_IROTH);
1813                  if (targetFile < 0)
1814                    {
1815#ifdef DEBUG
1816                      fprintf (stderr, "could not create the new file,%s\n",
1817                               fileNameOnServer);
1818#endif
1819                    }
1820                  else
1821                    {
1822                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
1823                             == cgiFormSuccess)
1824                        {
1825                          if (got > 0)
1826                            write (targetFile, buffer, got);
1827                        }
1828                    }
1829                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
1830                  cgiFormFileClose (file);
1831                  zClose (targetFile);
1832                  free(fileNameOnServer);
1833                  free(storageNameOnServer);
1834#ifdef DEBUG
1835                  fprintf (stderr, "File \"%s\" has been uploaded",
1836                           fileNameOnServer);
1837#endif
1838                }
1839            }
1840        }
1841      tmpReqI = tmpReqI->next;
1842    }
1843
1844  ensureDecodedBase64 (request_inputs);
1845  return 1;
1846}
1847
1848
1849/**
1850 * Verify if a parameter value is valid.
1851 *
1852 * @param request the request map
1853 * @param res the error map potentially generated
1854 * @param toCheck the parameter to use
1855 * @param avalues the acceptable values (or null if testing only for presence)
1856 * @param mandatory verify the presence of the parameter if mandatory > 0
1857 */
1858void checkValidValue(map* request,map** res,const char* toCheck,const char** avalues,int mandatory){
1859  map* lres=*res;
1860  map* r_inputs = getMap (request,toCheck);
1861  if (r_inputs == NULL){
1862    if(mandatory>0){
1863      char *replace=_("Mandatory parameter <%s> was not specified");
1864      char *message=(char*)malloc((strlen(replace)+strlen(toCheck)+1)*sizeof(char));
1865      sprintf(message,replace,toCheck);
1866      if(lres==NULL){
1867        lres=createMap("code","MissingParameterValue");
1868        addToMap(lres,"text",message);
1869        addToMap(lres,"locator",toCheck);       
1870      }else{
1871        int length=1;
1872        map* len=getMap(lres,"length");
1873        if(len!=NULL){
1874          length=atoi(len->value);
1875        }
1876        setMapArray(lres,"text",length,message);
1877        setMapArray(lres,"locator",length,toCheck);
1878        setMapArray(lres,"code",length,"MissingParameterValue");
1879      }
1880      free(message);
1881    }
1882  }else{
1883    if(avalues==NULL)
1884      return;
1885    int nb=0;
1886    int hasValidValue=-1;
1887    if(strncasecmp(toCheck,"Accept",6)==0){
1888      char *tmp=zStrdup(r_inputs->value);
1889      char *pToken,*saveptr;
1890      pToken=strtok_r(tmp,",",&saveptr);
1891      while(pToken!=NULL){
1892        while(avalues[nb]!=NULL){
1893          if(strcasecmp(avalues[nb],pToken)==0){
1894            hasValidValue=1;
1895            break;
1896          }
1897          nb++;
1898        }
1899        pToken=strtok_r(NULL,",",&saveptr);
1900      }
1901      free(tmp);
1902    }else{
1903      while(avalues[nb]!=NULL){
1904        if(strcasecmp(avalues[nb],r_inputs->value)==0){
1905          hasValidValue=1;
1906          break;
1907        }
1908        nb++;
1909      }
1910    }
1911    if(hasValidValue<0){
1912      char *replace=_("The value <%s> was not recognized, %s %s the only acceptable value.");
1913      nb=0;
1914      char *vvalues=NULL;
1915      char* num=_("is");
1916      while(avalues[nb]!=NULL){
1917        char *tvalues;
1918        if(vvalues==NULL){
1919          vvalues=(char*)malloc((strlen(avalues[nb])+3)*sizeof(char));
1920          sprintf(vvalues,"%s",avalues[nb]);
1921        }
1922        else{
1923          tvalues=zStrdup(vvalues);
1924          vvalues=(char*)realloc(vvalues,(strlen(tvalues)+strlen(avalues[nb])+3)*sizeof(char));
1925          sprintf(vvalues,"%s, %s",tvalues,avalues[nb]);
1926          free(tvalues);
1927          num=_("are");
1928        }
1929        nb++;
1930      }
1931      char *message=(char*)malloc((strlen(replace)+strlen(num)+strlen(vvalues)+strlen(toCheck)+1)*sizeof(char));
1932      sprintf(message,replace,toCheck,vvalues,num);
1933      const char *code="VersionNegotiationFailed";
1934      code="InvalidParameterValue";
1935      const char *locator=toCheck;
1936      if( strncasecmp(toCheck,"version",7)==0 ||
1937          strncasecmp(toCheck,"AcceptVersions",14)==0 )
1938        code="VersionNegotiationFailed";
1939      if( strncasecmp(toCheck,"request",7)==0){
1940        code="OperationNotSupported";
1941        locator=r_inputs->value;
1942      }
1943      if(lres==NULL){
1944        lres=createMap("code",code);
1945        addToMap(lres,"text",message);
1946        addToMap(lres,"locator",locator);       
1947      }else{
1948        int length=1;
1949        map* len=getMap(lres,"length");
1950        if(len!=NULL){
1951          length=atoi(len->value);
1952        }
1953        setMapArray(lres,"text",length,message);
1954        setMapArray(lres,"locator",length,locator);
1955        setMapArray(lres,"code",length,code);
1956      }
1957    }
1958  }
1959  if(lres!=NULL){
1960    *res=lres;
1961  }
1962}
1963
1964/**
1965 * Parse cookie contained in request headers.
1966 *
1967 * @param conf the conf maps containinfg the main.cfg
1968 * @param cookie the
1969 */
1970void parseCookie(maps** conf,const char* cookie){
1971  char* tcook=zStrdup(cookie);
1972  char *token, *saveptr;
1973  token = strtok_r (tcook, "; ", &saveptr);
1974  maps* res=createMaps("cookies");
1975  while (token != NULL){
1976    char *token1, *saveptr1, *name;
1977    int i=0;
1978    token1 = strtok_r (token, "=", &saveptr1);
1979    while (token1 != NULL){
1980      if(i==0){
1981        name=zStrdup(token1);
1982        i++;
1983      }
1984      else{
1985        if(res->content==NULL)
1986          res->content=createMap(name,token1);
1987        else
1988          addToMap(res->content,name,token1);
1989        free(name);
1990        name=NULL;
1991        i=0;
1992      }
1993      token1 = strtok_r (NULL, "=", &saveptr1);
1994    }
1995    if(name!=NULL)
1996      free(name);
1997    token = strtok_r (NULL, "; ", &saveptr);
1998  }
1999  addMapsToMaps(conf,res);
2000  freeMaps(&res);
2001  free(res);
2002  free(tcook);
2003}
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