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