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