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

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

Fix const decelaration to build on windows, pass full children content for text/* ComplexData?.

  • Property svn:keywords set to Id
File size: 57.1 KB
Line 
1/*
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2015 GeoLabs SARL
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "request_parser.h"
26#include "service_internal.h"
27#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                                if(ltmp!= NULL && strstr(ltmp->value,"text/")!=NULL){
1074                                  xmlChar *tmp = xmlNodeListGetRawString (doc,
1075                                                                          cur4->xmlChildrenNode,
1076                                                                          0);
1077                                  addToMap (tmpmaps->content, "value",
1078                                            (char *) tmp);
1079                                  xmlFree (tmp);
1080                                }else{
1081                                  while(cur5!=NULL && cur5->type != XML_CDATA_SECTION_NODE)
1082                                    cur5=cur5->next;
1083                                  xmlFree(mv);
1084                                  if(cur5!=NULL && cur5->content!=NULL){
1085                                    mv=xmlStrdup(cur5->content);
1086                                  }
1087                                }
1088                              }
1089                            }
1090                          }
1091                          if (mv != NULL)
1092                            {
1093                              addToMap (tmpmaps->content, "value",
1094                                        (char*) mv);
1095                              xmlFree (mv);
1096                            }
1097                        }
1098                      else
1099                        {
1100                          xmlNodePtr cur5 = cur4->children;
1101                          while (cur5 != NULL
1102                                 && cur5->type != XML_CDATA_SECTION_NODE)
1103                            cur5 = cur5->next;
1104                          if (cur5 != NULL
1105                              && cur5->type == XML_CDATA_SECTION_NODE)
1106                            {
1107                              addToMap (tmpmaps->content,
1108                                        "value",
1109                                        (char *) cur5->content);
1110                            }
1111                          else{
1112                            if(cur4->xmlChildrenNode!=NULL){
1113                              xmlChar *tmp = xmlNodeListGetRawString (doc,
1114                                                                      cur4->xmlChildrenNode,
1115                                                                      0);
1116                              addToMap (tmpmaps->content, "value",
1117                                        (char *) tmp);
1118                              xmlFree (tmp);
1119                            }
1120                          }
1121                        }
1122
1123                      cur4 = cur4->next;
1124                    }
1125                }
1126              cur2 = cur2->next;
1127              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE){
1128                cur2 = cur2->next;
1129              }
1130            }
1131          if(memory!=NULL && strncasecmp(memory->value,"load",4)!=0)
1132            if(getMap(tmpmaps->content,"to_load")==NULL){
1133              addToMap(tmpmaps->content,"to_load","false");
1134            }
1135          {
1136            map* test=getMap(tmpmaps->content,"value");
1137            if(test==NULL && tmpmaps->child==NULL)
1138              addToMap(tmpmaps->content,"value","");
1139            maps *testPresence = getMaps (*request_output, tmpmaps->name);
1140            maps *cursor=*request_output;
1141            while(testPresence == NULL && cursor!=NULL){
1142              if(cursor->child!=NULL){
1143                testPresence = getMaps (cursor->child, tmpmaps->name);
1144              }
1145              cursor=cursor->next;
1146            }
1147            if (testPresence != NULL)
1148              {
1149                elements *elem = getElements (s->inputs, tmpmaps->name);
1150                elements *cursor=s->inputs;
1151                while(elem == NULL && cursor!=NULL){
1152                  if(cursor->child!=NULL){
1153                    elem = getElements (cursor->child, tmpmaps->name);
1154                  }
1155                  cursor=cursor->next;
1156                }
1157                if (elem != NULL)
1158                  {
1159                    if (appendMapsToMaps
1160                        (*main_conf, testPresence, tmpmaps, elem) < 0)
1161                      {
1162                        return errorException (*main_conf,
1163                                               _("Unable to append maps to maps."),
1164                                               "InternalError",
1165                                               NULL);
1166                      }
1167                  }
1168              }
1169            else{
1170              addMapsToMaps (request_output, tmpmaps);
1171            }
1172          }
1173          freeMaps (&tmpmaps);
1174          free (tmpmaps);
1175          tmpmaps = NULL;
1176        }
1177    }
1178  return 1;
1179}
1180
1181/**
1182 * Parse a BoundingBoxData node
1183 *
1184 * http://schemas.opengis.net/ows/1.1.0/owsCommon.xsd: BoundingBoxType
1185 *
1186 * A map to store boundingbox information will contain:
1187 *  - LowerCorner : double double (minimum within this bounding box)
1188 *  - UpperCorner : double double (maximum within this bounding box)
1189 *  - crs : URI (Reference to definition of the CRS)
1190 *  - dimensions : int
1191 *
1192 * @param main_conf the conf maps containing the main.cfg settings
1193 * @param request_inputs the map storing KVP raw value
1194 * @param doc the xmlDocPtr containing the BoudingoxData node
1195 * @return a map containing all the bounding box keys
1196 */
1197int xmlParseBoundingBox(maps** main_conf,map** current_input,xmlDocPtr doc){
1198  xmlNode *root_element = xmlDocGetRootElement(doc);
1199  for(xmlAttrPtr attr = root_element->properties; NULL != attr; attr = attr->next){
1200    xmlChar *val = xmlGetProp (root_element, BAD_CAST attr->name);
1201    addToMap(*current_input,(char*)attr->name,(char*)val);
1202    xmlFree(val);
1203    xmlNodePtr cur = root_element->children;
1204    while(cur!=NULL && cur->type != XML_ELEMENT_NODE)
1205      cur=cur->next;
1206    while(cur!=NULL && cur->type==XML_ELEMENT_NODE){
1207      xmlChar *val =
1208        xmlNodeListGetString (doc, cur->xmlChildrenNode, 1);
1209      addToMap(*current_input,(char*)cur->name,(char*)val);
1210      cur=cur->next;
1211      xmlFree(val);
1212      while(cur!=NULL && cur->type != XML_ELEMENT_NODE)
1213        cur=cur->next;
1214    }
1215  }
1216  return 0;
1217}
1218
1219/**
1220 * Parse outputs from XML nodes and store them in a maps (WPS version 2.0.0).
1221 *
1222 * @param main_conf the conf maps containing the main.cfg settings
1223 * @param request_inputs the map storing KVP raw value
1224 * @param request_output the maps to store the KVP pairs
1225 * @param doc the xmlDocPtr containing the original request
1226 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
1227 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
1228 * @return 0 on success, -1 on failure
1229 */
1230int xmlParseOutputs2(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodeSet* nodes){
1231  int k = 0;
1232  int l = 0;
1233  for (k=0; k < nodes->nodeNr; k++){
1234    maps *tmpmaps = NULL;
1235    xmlNodePtr cur = nodes->nodeTab[k];
1236    if (cur->type == XML_ELEMENT_NODE){
1237      maps *tmpmaps = NULL;
1238      xmlChar *val = xmlGetProp (cur, BAD_CAST "id");
1239      if(val!=NULL)
1240        tmpmaps = createMaps((char *)val);
1241      else
1242        tmpmaps = createMaps("unknownIdentifier");
1243      const char ress[4][13] =
1244        { "mimeType", "encoding", "schema", "transmission" };
1245      xmlFree (val);
1246      for (l = 0; l < 4; l++){
1247        val = xmlGetProp (cur, BAD_CAST ress[l]);
1248        if (val != NULL && strlen ((char *) val) > 0)
1249          {
1250            if (tmpmaps->content != NULL)
1251              addToMap (tmpmaps->content, ress[l],
1252                        (char *) val);
1253            else
1254              tmpmaps->content =
1255                createMap (ress[l], (char *) val);
1256            if(l==3 && strncasecmp((char*)val,"reference",xmlStrlen(val))==0)
1257              addToMap (tmpmaps->content,"asReference","true");
1258          }
1259        xmlFree (val);
1260      }
1261      if(cur->children!=NULL){
1262        xmlNodePtr ccur = cur->children;
1263        while (ccur != NULL){
1264          if(ccur->type == XML_ELEMENT_NODE){
1265            char *xpathExpr=(char*)malloc(66+strlen(tmpmaps->name));
1266            sprintf(xpathExpr,"/*/*[local-name()='Output' and @id='%s']/*[local-name()='Output']",tmpmaps->name);           
1267            xmlXPathObjectPtr tmpsptr = extractFromDoc (doc, xpathExpr);
1268            xmlNodeSet* cnodes = tmpsptr->nodesetval;
1269            xmlParseOutputs2(main_conf,request_inputs,&tmpmaps->child,doc,cnodes);
1270            xmlXPathFreeObject (tmpsptr);
1271            free(xpathExpr);
1272            break;
1273          }
1274          ccur = ccur->next;
1275        }
1276      }
1277      if (*request_output == NULL){
1278        *request_output = dupMaps(&tmpmaps);
1279      }
1280      else{
1281        addMapsToMaps(request_output,tmpmaps);
1282      }
1283      freeMaps(&tmpmaps);
1284      free(tmpmaps);
1285    }
1286  }
1287  return 0;
1288}
1289
1290/**
1291 * Parse outputs from XML nodes and store them in a maps.
1292 *
1293 * @param main_conf the conf maps containing the main.cfg settings
1294 * @param request_inputs the map storing KVP raw value
1295 * @param request_output the maps to store the KVP pairs
1296 * @param doc the xmlDocPtr containing the original request
1297 * @param cur the xmlNodePtr corresponding to the ResponseDocument or RawDataOutput XML node
1298 * @param raw true if the node is RawDataOutput, false in case of ResponseDocument
1299 * @return 0 on success, -1 on failure
1300 */
1301int xmlParseOutputs(maps** main_conf,map** request_inputs,maps** request_output,xmlDocPtr doc,xmlNodePtr cur,bool raw){
1302  int l=0;
1303  if( raw == true)
1304    {
1305      addToMap (*request_inputs, "RawDataOutput", "");
1306      if (cur->type == XML_ELEMENT_NODE)
1307        {
1308
1309          maps *tmpmaps = createMaps("unknownIdentifier");
1310          if (tmpmaps == NULL)
1311            {
1312              return errorException (*main_conf, _("Unable to allocate memory"),
1313                                     "InternalError", NULL);
1314            }
1315
1316          // Get every attribute from a RawDataOutput node
1317          // mimeType, encoding, schema, uom
1318          const char *outs[4] =
1319            { "mimeType", "encoding", "schema", "uom" };
1320          for (l = 0; l < 4; l++)
1321            {
1322              xmlChar *val = xmlGetProp (cur, BAD_CAST outs[l]);
1323              if (val != NULL)
1324                {
1325                  if (strlen ((char *) val) > 0)
1326                    {
1327                      if (tmpmaps->content != NULL)
1328                        addToMap (tmpmaps->content, outs[l],
1329                                  (char *) val);
1330                      else
1331                        tmpmaps->content =
1332                          createMap (outs[l], (char *) val);
1333                    }
1334                  xmlFree (val);
1335                }
1336            }
1337          xmlNodePtr cur2 = cur->children;
1338          while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1339            cur2 = cur2->next;
1340          while (cur2 != NULL)
1341            {
1342              if (xmlStrncasecmp
1343                  (cur2->name, BAD_CAST "Identifier",
1344                   xmlStrlen (cur2->name)) == 0)
1345                {
1346                  xmlChar *val =
1347                    xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1);
1348                  free (tmpmaps->name);
1349                  tmpmaps->name = zStrdup ((char *) val);
1350                  xmlFree (val);
1351                }
1352              cur2 = cur2->next;
1353              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE)
1354                cur2 = cur2->next;
1355            }
1356          if (*request_output == NULL)
1357            *request_output = dupMaps (&tmpmaps);
1358          else
1359            addMapsToMaps (request_output, tmpmaps);
1360          if (tmpmaps != NULL)
1361            {
1362              freeMaps (&tmpmaps);
1363              free (tmpmaps);
1364              tmpmaps = NULL;
1365            }
1366        }
1367    }
1368  else
1369    {
1370      addToMap (*request_inputs, "ResponseDocument", "");
1371
1372      if (cur->type == XML_ELEMENT_NODE) {
1373        // Get every attribute: storeExecuteResponse, lineage, status
1374        const char *ress[3] =
1375          { "storeExecuteResponse", "lineage", "status" };
1376        xmlChar *val;
1377        for (l = 0; l < 3; l++)
1378          {
1379            val = xmlGetProp (cur, BAD_CAST ress[l]);
1380            if (val != NULL && strlen ((char *) val) > 0)
1381              {
1382                addToMap (*request_inputs, ress[l], (char *) val);
1383              }
1384            xmlFree (val);
1385          }
1386                       
1387        xmlNodePtr cur1 = cur->children;               
1388        while (cur1 != NULL) // iterate over Output nodes
1389          {
1390            if (cur1->type != XML_ELEMENT_NODE || 
1391                xmlStrncasecmp(cur1->name, BAD_CAST "Output", 
1392                               xmlStrlen (cur1->name)) != 0) {
1393              cur1 = cur1->next;
1394              continue;
1395            }
1396                               
1397            maps *tmpmaps = createMaps("unknownIdentifier"); // one per Output node
1398            if (tmpmaps == NULL) {
1399              return errorException (*main_conf,
1400                                     _
1401                                     ("Unable to allocate memory"),
1402                                     "InternalError", NULL);
1403            }
1404                               
1405            xmlNodePtr elems = cur1->children;
1406                               
1407            while (elems != NULL) {
1408
1409              // Identifier
1410              if (xmlStrncasecmp
1411                  (elems->name, BAD_CAST "Identifier",
1412                   xmlStrlen (elems->name)) == 0)
1413                {
1414                  xmlChar *val =
1415                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1416               
1417                  free(tmpmaps->name);
1418                  tmpmaps->name = zStrdup ((char *) val);
1419                  if (tmpmaps->content == NULL) {
1420                    tmpmaps->content = createMap("Identifier", zStrdup ((char *) val));
1421                  }
1422                  else {
1423                    addToMap(tmpmaps->content, "Identifier", zStrdup ((char *) val));
1424                  }
1425
1426                  map* tt = getMap (*request_inputs, "ResponseDocument");
1427                  if (strlen(tt->value) == 0) {
1428                    addToMap (*request_inputs, "ResponseDocument",
1429                              (char *) val);
1430                  }
1431                  else {
1432                    char* tmp = (char*) malloc((strlen(tt->value) + 1 
1433                                                + strlen((char*) val) + 1) * sizeof(char));
1434                    sprintf (tmp, "%s;%s", tt->value, (char *) val);
1435                    free(tt->value);
1436                    tt->value = tmp;
1437                  }
1438                  xmlFree (val);
1439                }
1440             
1441              // Title, Abstract
1442              else if (xmlStrncasecmp(elems->name, BAD_CAST "Title",
1443                                      xmlStrlen (elems->name)) == 0
1444                       || xmlStrncasecmp(elems->name, BAD_CAST "Abstract",
1445                                         xmlStrlen (elems->name)) == 0)
1446                {
1447                  xmlChar *val =
1448                    xmlNodeListGetString (doc, elems->xmlChildrenNode, 1);
1449                                                       
1450                  if (tmpmaps->content == NULL) {
1451                    tmpmaps->content = createMap((char*) elems->name, zStrdup ((char *) val));
1452                  }
1453                  else {
1454                    addToMap(tmpmaps->content, (char*) elems->name, zStrdup ((char *) val));
1455                  }
1456                  xmlFree (val);
1457                }
1458              elems = elems->next;
1459            }
1460                               
1461            // Get every attribute from an Output node:
1462            // mimeType, encoding, schema, uom, asReference
1463            const char *outs[5] =
1464              { "mimeType", "encoding", "schema", "uom", "asReference" };
1465                                         
1466            for (l = 0; l < 5; l++) {
1467              xmlChar *val = xmlGetProp (cur1, BAD_CAST outs[l]);                               
1468              if (val != NULL && xmlStrlen(val) > 0) {
1469                if (tmpmaps->content != NULL) {
1470                  addToMap (tmpmaps->content, outs[l], (char *) val);
1471                }                         
1472                else {
1473                  tmpmaps->content = createMap (outs[l], (char *) val);
1474                }       
1475              }
1476              xmlFree (val);
1477            }
1478                               
1479            if (*request_output == NULL) {
1480              *request_output = tmpmaps;
1481            }   
1482            else if (getMaps(*request_output, tmpmaps->name) != NULL) {
1483              return errorException (*main_conf,
1484                                     _
1485                                     ("Duplicate <Output> elements in WPS Execute request"),
1486                                     "InternalError", NULL);
1487            }
1488            else {
1489              maps* mptr = *request_output;
1490              while (mptr->next != NULL) {
1491                mptr = mptr->next;
1492              }
1493              mptr->next = tmpmaps;     
1494            }                                   
1495            cur1 = cur1->next;
1496          }                     
1497      }
1498    }
1499  return 1;
1500}
1501
1502/**
1503 * Parse XML request and store information in maps.
1504 *
1505 * @param main_conf the conf maps containing the main.cfg settings
1506 * @param post the string containing the XML request
1507 * @param request_inputs the map storing KVP raw value
1508 * @param s the service
1509 * @param inputs the maps to store the KVP pairs
1510 * @param outputs the maps to store the KVP pairs
1511 * @param hInternet the HINTERNET queue to add potential requests
1512 * @return 0 on success, -1 on failure
1513 */
1514int xmlParseRequest(maps** main_conf,const char* post,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1515
1516  map* version=getMapFromMaps(*main_conf,"main","rversion");
1517  int vid=getVersionId(version->value);
1518
1519  xmlInitParser ();
1520  xmlDocPtr doc = xmlReadMemory (post, cgiContentLength, "input_request.xml", NULL, XML_PARSE_RECOVER);
1521
1522  /**
1523   * Extract Input nodes from the XML Request.
1524   */
1525  xmlXPathObjectPtr tmpsptr =
1526    extractFromDoc (doc, (vid==0?"/*/*/*[local-name()='Input']":"/*/*[local-name()='Input']"));
1527  xmlNodeSet *tmps = tmpsptr->nodesetval;
1528  if(tmps==NULL || xmlParseInputs(main_conf,s,inputs,doc,tmps,hInternet)<0){
1529    xmlXPathFreeObject (tmpsptr);
1530    xmlFreeDoc (doc);
1531    xmlCleanupParser ();
1532    return -1;
1533  }
1534  xmlXPathFreeObject (tmpsptr);
1535
1536  if(vid==1){
1537    tmpsptr =
1538      extractFromDoc (doc, "/*[local-name()='Execute']");
1539    bool asRaw = false;
1540    tmps = tmpsptr->nodesetval;
1541    if(tmps->nodeNr > 0){
1542      int k = 0;
1543      for (k=0; k < tmps->nodeNr; k++){
1544        maps *tmpmaps = NULL;
1545        xmlNodePtr cur = tmps->nodeTab[k];
1546        if (cur->type == XML_ELEMENT_NODE){
1547          xmlChar *val = xmlGetProp (cur, BAD_CAST "mode");
1548          if(val!=NULL)
1549            addToMap(*request_inputs,"mode",(char*)val);
1550          else
1551            addToMap(*request_inputs,"mode","auto");
1552          xmlFree(val);
1553          val = xmlGetProp (cur, BAD_CAST "response");
1554          if(val!=NULL){
1555            addToMap(*request_inputs,"response",(char*)val);
1556            if(strncasecmp((char*)val,"raw",xmlStrlen(val))==0)
1557              addToMap(*request_inputs,"RawDataOutput","");
1558            else
1559              addToMap(*request_inputs,"ResponseDocument","");
1560          }
1561          else{
1562            addToMap(*request_inputs,"response","document");
1563            addToMap(*request_inputs,"ResponseDocument","");
1564          }
1565          xmlFree(val);
1566        }
1567      }
1568    }
1569    xmlXPathFreeObject (tmpsptr);
1570    tmpsptr =
1571      extractFromDoc (doc, "/*/*[local-name()='Output']");
1572    tmps = tmpsptr->nodesetval;
1573    if(tmps->nodeNr > 0){
1574      if(xmlParseOutputs2(main_conf,request_inputs,outputs,doc,tmps)<0){
1575        xmlXPathFreeObject (tmpsptr);
1576        xmlFreeDoc (doc);
1577        xmlCleanupParser ();
1578        return -1;
1579      }
1580    }
1581  }
1582  else{
1583    // Extract ResponseDocument / RawDataOutput from the XML Request
1584    tmpsptr =
1585      extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']");
1586    bool asRaw = false;
1587    tmps = tmpsptr->nodesetval;
1588    if (tmps->nodeNr == 0)
1589      {
1590        xmlXPathFreeObject (tmpsptr);
1591        tmpsptr =
1592          extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']");
1593        tmps = tmpsptr->nodesetval;
1594        asRaw = true;
1595      }
1596    if(tmps->nodeNr != 0){
1597      if(xmlParseOutputs(main_conf,request_inputs,outputs,doc,tmps->nodeTab[0],asRaw)<0){
1598        xmlXPathFreeObject (tmpsptr);
1599        xmlFreeDoc (doc);
1600        xmlCleanupParser ();
1601        return -1;
1602      }
1603    }
1604  }
1605  xmlXPathFreeObject (tmpsptr);
1606  xmlFreeDoc (doc);
1607  xmlCleanupParser ();
1608  return 1;
1609}
1610
1611/**
1612 * Parse request and store information in maps.
1613 *
1614 * @param main_conf the conf maps containing the main.cfg settings
1615 * @param post the string containing the XML request
1616 * @param request_inputs the map storing KVP raw value
1617 * @param s the service
1618 * @param inputs the maps to store the KVP pairs
1619 * @param outputs the maps to store the KVP pairs
1620 * @param hInternet the HINTERNET queue to add potential requests
1621 * @return 0 on success, -1 on failure
1622 * @see kvpParseOutputs,kvpParseInputs,xmlParseRequest
1623 */
1624int parseRequest(maps** main_conf,map** request_inputs,service* s,maps** inputs,maps** outputs,HINTERNET* hInternet){
1625  map *postRequest = NULL;
1626  postRequest = getMap (*request_inputs, "xrequest");
1627  if (postRequest == NULLMAP)
1628    {
1629      if(kvpParseOutputs(main_conf,*request_inputs,outputs)<0){
1630        return -1;
1631      }
1632      if(kvpParseInputs(main_conf,s,*request_inputs,inputs,hInternet)<0){
1633        return -1;
1634      }
1635    }
1636  else
1637    {
1638      //Parse XML request
1639      if(xmlParseRequest(main_conf,postRequest->value,request_inputs,s,inputs,outputs,hInternet)<0){
1640        return -1;
1641      }
1642    }
1643  return 1;
1644}
1645
1646/**
1647 * Ensure that each requested arguments are present in the request
1648 * DataInputs and ResponseDocument / RawDataOutput. Potentially run
1649 * http requests from the queue in parallel.
1650 * For optional inputs add default values defined in the ZCFG file.
1651 *
1652 * @param main_conf
1653 * @param s
1654 * @param original_request
1655 * @param request_inputs
1656 * @param request_outputs
1657 * @param hInternet
1658 *
1659 * @see runHttpRequests
1660 */
1661int validateRequest(maps** main_conf,service* s,map* original_request, maps** request_inputs,maps** request_outputs,HINTERNET* hInternet){
1662
1663  map* errI0=NULL;
1664  runHttpRequests (main_conf, request_inputs, hInternet,&errI0);
1665  if(errI0!=NULL){
1666    printExceptionReportResponse (*main_conf, errI0);
1667    InternetCloseHandle (hInternet);
1668    return -1;
1669  }
1670  InternetCloseHandle (hInternet);
1671
1672
1673  map* errI=NULL;
1674  char *dfv = addDefaultValues (request_inputs, s->inputs, *main_conf, 0,&errI);
1675
1676  maps *ptr = *request_inputs;
1677  while (ptr != NULL)
1678    {
1679      map *tmp0 = getMap (ptr->content, "size");
1680      map *tmp1 = getMap (ptr->content, "maximumMegabytes");
1681      if (tmp1 != NULL && tmp0 != NULL)
1682        {
1683          float i = atof (tmp0->value) / 1048576.0;
1684          if (i >= atoi (tmp1->value))
1685            {
1686              char tmps[1024];
1687              map *tmpe = createMap ("code", "FileSizeExceeded");
1688              snprintf (tmps, 1024,
1689                        _
1690                        ("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)."),
1691                        ptr->name, tmp1->value, i);
1692              addToMap (tmpe, "locator", ptr->name);
1693              addToMap (tmpe, "text", tmps);
1694              printExceptionReportResponse (*main_conf, tmpe);
1695              freeMap (&tmpe);
1696              free (tmpe);
1697              return -1;
1698            }
1699        }
1700      ptr = ptr->next;
1701    }
1702
1703  map* errO=NULL;
1704  char *dfv1 =
1705    addDefaultValues (request_outputs, s->outputs, *main_conf, 1,&errO);
1706
1707  if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0)
1708    {
1709      char tmps[1024];
1710      map *tmpe = NULL;
1711      if (strcmp (dfv, "") != 0)
1712        {
1713          tmpe = createMap ("code", "MissingParameterValue");
1714          int nb=0;
1715          int length=1;
1716          map* len=getMap(errI,"length");
1717          if(len!=NULL)
1718            length=atoi(len->value);
1719          for(nb=0;nb<length;nb++){
1720            map* errp=getMapArray(errI,"value",nb);
1721            snprintf (tmps, 1024,
1722                      _
1723                      ("The <%s> argument was not specified in DataInputs but is required according to the ZOO ServicesProvider configuration file."),
1724                      errp->value);
1725            setMapArray (tmpe, "locator", nb , errp->value);
1726            setMapArray (tmpe, "text", nb , tmps);
1727            setMapArray (tmpe, "code", nb , "MissingParameterValue");
1728          }
1729        }
1730      if (strcmp (dfv1, "") != 0)
1731        {
1732          int ilength=0;
1733          if(tmpe==NULL)
1734            tmpe = createMap ("code", "InvalidParameterValue");
1735          else{
1736            map* len=getMap(tmpe,"length");
1737            if(len!=NULL)
1738              ilength=atoi(len->value);
1739          }
1740          int nb=0;
1741          int length=1;
1742          map* len=getMap(errO,"length");
1743          if(len!=NULL)
1744            length=atoi(len->value);
1745          for(nb=0;nb<length;nb++){
1746            map* errp=getMapArray(errO,"value",nb);
1747            snprintf (tmps, 1024,
1748                      _
1749                      ("The <%s> argument specified as %s identifier was not recognized (not defined in the ZOO Configuration File)."),
1750                      errp->value,
1751                      ((getMap(original_request,"RawDataOutput")!=NULL)?"RawDataOutput":"ResponseDocument"));
1752            setMapArray (tmpe, "locator", nb+ilength , errp->value);
1753            setMapArray (tmpe, "text", nb+ilength , tmps);
1754            setMapArray (tmpe, "code", nb+ilength , "InvalidParameterValue");
1755          }
1756        }
1757      printExceptionReportResponse (*main_conf, tmpe);
1758      if(errI!=NULL){
1759        freeMap(&errI);
1760        free(errI);
1761      }
1762      if(errO!=NULL){
1763        freeMap(&errO);
1764        free(errO);
1765      }
1766      freeMap (&tmpe);
1767      free (tmpe);
1768      return -1;
1769    }
1770  maps *tmpReqI = *request_inputs;
1771  while (tmpReqI != NULL)
1772    {
1773      char name[1024];
1774      if (getMap (tmpReqI->content, "isFile") != NULL)
1775        {
1776          if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) ==
1777              cgiFormSuccess)
1778            {
1779              int BufferLen = 1024;
1780              cgiFilePtr file;
1781              int targetFile;
1782              char *storageNameOnServer;
1783              char *fileNameOnServer;
1784              char contentType[1024];
1785              char buffer[1024];
1786              char *tmpStr = NULL;
1787              int size;
1788              int got, t;
1789              map *path = getMapFromMaps (*main_conf, "main", "tmpPath");
1790              cgiFormFileSize (tmpReqI->name, &size);
1791              cgiFormFileContentType (tmpReqI->name, contentType,
1792                                      sizeof (contentType));
1793              if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess)
1794                {
1795                  t = -1;
1796                  while (1)
1797                    {
1798                      tmpStr = strstr (name + t + 1, "\\");
1799                      if (NULL == tmpStr)
1800                        tmpStr = strstr (name + t + 1, "/");
1801                      if (NULL != tmpStr)
1802                        t = (int) (tmpStr - name);
1803                      else
1804                        break;
1805                    }
1806                  fileNameOnServer=(char*)malloc((strlen(name) - t - 1 )*sizeof(char));
1807                  strcpy (fileNameOnServer, name + t + 1);
1808
1809                  storageNameOnServer=(char*)malloc((strlen(path->value) + strlen(fileNameOnServer) + 2)*sizeof(char));
1810                  sprintf (storageNameOnServer, "%s/%s", path->value,
1811                           fileNameOnServer);
1812#ifdef DEBUG
1813                  fprintf (stderr, "Name on server %s\n",
1814                           storageNameOnServer);
1815                  fprintf (stderr, "fileNameOnServer: %s\n",
1816                           fileNameOnServer);
1817#endif
1818                  targetFile =
1819                    zOpen (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC,
1820                          S_IRWXU | S_IRGRP | S_IROTH);
1821                  if (targetFile < 0)
1822                    {
1823#ifdef DEBUG
1824                      fprintf (stderr, "could not create the new file,%s\n",
1825                               fileNameOnServer);
1826#endif
1827                    }
1828                  else
1829                    {
1830                      while (cgiFormFileRead (file, buffer, BufferLen, &got)
1831                             == cgiFormSuccess)
1832                        {
1833                          if (got > 0)
1834                            write (targetFile, buffer, got);
1835                        }
1836                    }
1837                  addToMap (tmpReqI->content, "lref", storageNameOnServer);
1838                  cgiFormFileClose (file);
1839                  zClose (targetFile);
1840                  free(fileNameOnServer);
1841                  free(storageNameOnServer);
1842#ifdef DEBUG
1843                  fprintf (stderr, "File \"%s\" has been uploaded",
1844                           fileNameOnServer);
1845#endif
1846                }
1847            }
1848        }
1849      tmpReqI = tmpReqI->next;
1850    }
1851
1852  ensureDecodedBase64 (request_inputs);
1853  return 1;
1854}
1855
1856
1857/**
1858 * Verify if a parameter value is valid.
1859 *
1860 * @param request the request map
1861 * @param res the error map potentially generated
1862 * @param toCheck the parameter to use
1863 * @param avalues the acceptable values (or null if testing only for presence)
1864 * @param mandatory verify the presence of the parameter if mandatory > 0
1865 */
1866void checkValidValue(map* request,map** res,const char* toCheck,const char** avalues,int mandatory){
1867  map* lres=*res;
1868  map* r_inputs = getMap (request,toCheck);
1869  if (r_inputs == NULL){
1870    if(mandatory>0){
1871      const char *replace=_("Mandatory parameter <%s> was not specified");
1872      char *message=(char*)malloc((strlen(replace)+strlen(toCheck)+1)*sizeof(char));
1873      sprintf(message,replace,toCheck);
1874      if(lres==NULL){
1875        lres=createMap("code","MissingParameterValue");
1876        addToMap(lres,"text",message);
1877        addToMap(lres,"locator",toCheck);       
1878      }else{
1879        int length=1;
1880        map* len=getMap(lres,"length");
1881        if(len!=NULL){
1882          length=atoi(len->value);
1883        }
1884        setMapArray(lres,"text",length,message);
1885        setMapArray(lres,"locator",length,toCheck);
1886        setMapArray(lres,"code",length,"MissingParameterValue");
1887      }
1888      free(message);
1889    }
1890  }else{
1891    if(avalues==NULL)
1892      return;
1893    int nb=0;
1894    int hasValidValue=-1;
1895    if(strncasecmp(toCheck,"Accept",6)==0){
1896      char *tmp=zStrdup(r_inputs->value);
1897      char *pToken,*saveptr;
1898      pToken=strtok_r(tmp,",",&saveptr);
1899      while(pToken!=NULL){
1900        while(avalues[nb]!=NULL){
1901          if(strcasecmp(avalues[nb],pToken)==0){
1902            hasValidValue=1;
1903            break;
1904          }
1905          nb++;
1906        }
1907        pToken=strtok_r(NULL,",",&saveptr);
1908      }
1909      free(tmp);
1910    }else{
1911      while(avalues[nb]!=NULL){
1912        if(strcasecmp(avalues[nb],r_inputs->value)==0){
1913          hasValidValue=1;
1914          break;
1915        }
1916        nb++;
1917      }
1918    }
1919    if(hasValidValue<0){
1920      const char *replace=_("The value <%s> was not recognized, %s %s the only acceptable value.");
1921      nb=0;
1922      char *vvalues=NULL;
1923      char* num=_("is");
1924      while(avalues[nb]!=NULL){
1925        char *tvalues;
1926        if(vvalues==NULL){
1927          vvalues=(char*)malloc((strlen(avalues[nb])+3)*sizeof(char));
1928          sprintf(vvalues,"%s",avalues[nb]);
1929        }
1930        else{
1931          tvalues=zStrdup(vvalues);
1932          vvalues=(char*)realloc(vvalues,(strlen(tvalues)+strlen(avalues[nb])+3)*sizeof(char));
1933          sprintf(vvalues,"%s, %s",tvalues,avalues[nb]);
1934          free(tvalues);
1935          num=_("are");
1936        }
1937        nb++;
1938      }
1939      char *message=(char*)malloc((strlen(replace)+strlen(num)+strlen(vvalues)+strlen(toCheck)+1)*sizeof(char));
1940      sprintf(message,replace,toCheck,vvalues,num);
1941      const char *code="VersionNegotiationFailed";
1942      code="InvalidParameterValue";
1943      const char *locator=toCheck;
1944      if( strncasecmp(toCheck,"version",7)==0 ||
1945          strncasecmp(toCheck,"AcceptVersions",14)==0 )
1946        code="VersionNegotiationFailed";
1947      if( strncasecmp(toCheck,"request",7)==0){
1948        code="OperationNotSupported";
1949        locator=r_inputs->value;
1950      }
1951      if(lres==NULL){
1952        lres=createMap("code",code);
1953        addToMap(lres,"text",message);
1954        addToMap(lres,"locator",locator);       
1955      }else{
1956        int length=1;
1957        map* len=getMap(lres,"length");
1958        if(len!=NULL){
1959          length=atoi(len->value);
1960        }
1961        setMapArray(lres,"text",length,message);
1962        setMapArray(lres,"locator",length,locator);
1963        setMapArray(lres,"code",length,code);
1964      }
1965    }
1966  }
1967  if(lres!=NULL){
1968    *res=lres;
1969  }
1970}
1971
1972/**
1973 * Parse cookie contained in request headers.
1974 *
1975 * @param conf the conf maps containinfg the main.cfg
1976 * @param cookie the
1977 */
1978void parseCookie(maps** conf,const char* cookie){
1979  char* tcook=zStrdup(cookie);
1980  char *token, *saveptr;
1981  token = strtok_r (tcook, "; ", &saveptr);
1982  maps* res=createMaps("cookies");
1983  while (token != NULL){
1984    char *token1, *saveptr1, *name;
1985    int i=0;
1986    token1 = strtok_r (token, "=", &saveptr1);
1987    while (token1 != NULL){
1988      if(i==0){
1989        name=zStrdup(token1);
1990        i++;
1991      }
1992      else{
1993        if(res->content==NULL)
1994          res->content=createMap(name,token1);
1995        else
1996          addToMap(res->content,name,token1);
1997        free(name);
1998        name=NULL;
1999        i=0;
2000      }
2001      token1 = strtok_r (NULL, "=", &saveptr1);
2002    }
2003    if(name!=NULL)
2004      free(name);
2005    token = strtok_r (NULL, "; ", &saveptr);
2006  }
2007  addMapsToMaps(conf,res);
2008  freeMaps(&res);
2009  free(res);
2010  free(tcook);
2011}
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