1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2008-2013 GeoLabs SARL. All rights reserved. |
---|
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 | |
---|
26 | |
---|
27 | extern "C" int yylex (); |
---|
28 | extern "C" int crlex (); |
---|
29 | |
---|
30 | #ifdef USE_OTB |
---|
31 | #include "service_internal_otb.h" |
---|
32 | #else |
---|
33 | #define length(x) (sizeof(x) / sizeof(x[0])) |
---|
34 | #endif |
---|
35 | |
---|
36 | #include "cgic.h" |
---|
37 | |
---|
38 | extern "C" |
---|
39 | { |
---|
40 | #include <libxml/tree.h> |
---|
41 | #include <libxml/xmlmemory.h> |
---|
42 | #include <libxml/parser.h> |
---|
43 | #include <libxml/xpath.h> |
---|
44 | #include <libxml/xpathInternals.h> |
---|
45 | } |
---|
46 | |
---|
47 | #include "ulinet.h" |
---|
48 | |
---|
49 | #include <libintl.h> |
---|
50 | #include <locale.h> |
---|
51 | #include <string.h> |
---|
52 | |
---|
53 | #include "service.h" |
---|
54 | |
---|
55 | #include "service_internal.h" |
---|
56 | |
---|
57 | #ifdef USE_PYTHON |
---|
58 | #include "service_internal_python.h" |
---|
59 | #endif |
---|
60 | |
---|
61 | #ifdef USE_JAVA |
---|
62 | #include "service_internal_java.h" |
---|
63 | #endif |
---|
64 | |
---|
65 | #ifdef USE_PHP |
---|
66 | #include "service_internal_php.h" |
---|
67 | #endif |
---|
68 | |
---|
69 | #ifdef USE_JS |
---|
70 | #include "service_internal_js.h" |
---|
71 | #endif |
---|
72 | |
---|
73 | #ifdef USE_RUBY |
---|
74 | #include "service_internal_ruby.h" |
---|
75 | #endif |
---|
76 | |
---|
77 | #ifdef USE_PERL |
---|
78 | #include "service_internal_perl.h" |
---|
79 | #endif |
---|
80 | |
---|
81 | #include <dirent.h> |
---|
82 | #include <signal.h> |
---|
83 | #include <unistd.h> |
---|
84 | #ifndef WIN32 |
---|
85 | #include <dlfcn.h> |
---|
86 | #include <libgen.h> |
---|
87 | #else |
---|
88 | #include <windows.h> |
---|
89 | #include <direct.h> |
---|
90 | #include <sys/types.h> |
---|
91 | #include <sys/stat.h> |
---|
92 | #include <unistd.h> |
---|
93 | #define pid_t int; |
---|
94 | #endif |
---|
95 | #include <fcntl.h> |
---|
96 | #include <time.h> |
---|
97 | #include <stdarg.h> |
---|
98 | |
---|
99 | #ifdef WIN32 |
---|
100 | extern "C" |
---|
101 | { |
---|
102 | __declspec (dllexport) char *strcasestr (char const *a, char const *b) |
---|
103 | #ifndef USE_MS |
---|
104 | { |
---|
105 | char *x = zStrdup (a); |
---|
106 | char *y = zStrdup (b); |
---|
107 | |
---|
108 | x = _strlwr (x); |
---|
109 | y = _strlwr (y); |
---|
110 | char *pos = strstr (x, y); |
---|
111 | char *ret = pos == NULL ? NULL : (char *) (a + (pos - x)); |
---|
112 | free (x); |
---|
113 | free (y); |
---|
114 | return ret; |
---|
115 | }; |
---|
116 | #else |
---|
117 | ; |
---|
118 | #endif |
---|
119 | } |
---|
120 | #endif |
---|
121 | |
---|
122 | #define _(String) dgettext ("zoo-kernel",String) |
---|
123 | #define __(String) dgettext ("zoo-service",String) |
---|
124 | |
---|
125 | extern int getServiceFromFile (maps *, const char *, service **); |
---|
126 | |
---|
127 | int |
---|
128 | readServiceFile (maps * conf, char *file, service ** service, char *name) |
---|
129 | { |
---|
130 | int t = getServiceFromFile (conf, file, service); |
---|
131 | #ifdef YAML |
---|
132 | if (t < 0) |
---|
133 | { |
---|
134 | t = getServiceFromYAML (conf, file, service, name); |
---|
135 | } |
---|
136 | #endif |
---|
137 | return t; |
---|
138 | } |
---|
139 | |
---|
140 | void |
---|
141 | translateChar (char *str, char toReplace, char toReplaceBy) |
---|
142 | { |
---|
143 | int i = 0, len = strlen (str); |
---|
144 | for (i = 0; i < len; i++) |
---|
145 | { |
---|
146 | if (str[i] == toReplace) |
---|
147 | str[i] = toReplaceBy; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Create (or append to) an array valued maps |
---|
153 | * value = "["",""]" |
---|
154 | */ |
---|
155 | int |
---|
156 | appendMapsToMaps (maps * m, maps * mo, maps * mi, elements * elem) |
---|
157 | { |
---|
158 | maps *tmpMaps = getMaps (mo, mi->name); |
---|
159 | map *tmap = getMapType (tmpMaps->content); |
---|
160 | elements *el = getElements (elem, mi->name); |
---|
161 | int hasEl = 1; |
---|
162 | if (el == NULL) |
---|
163 | hasEl = -1; |
---|
164 | if (tmap == NULL) |
---|
165 | { |
---|
166 | if (hasEl > 0) |
---|
167 | tmap = getMapType (el->defaults->content); |
---|
168 | } |
---|
169 | |
---|
170 | map *testMap = NULL; |
---|
171 | if (hasEl > 0) |
---|
172 | { |
---|
173 | testMap = getMap (el->content, "maxOccurs"); |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | testMap = createMap ("maxOccurs", "unbounded"); |
---|
178 | } |
---|
179 | |
---|
180 | if (testMap != NULL) |
---|
181 | { |
---|
182 | if (strncasecmp (testMap->value, "unbounded", 9) != 0 |
---|
183 | && atoi (testMap->value) > 1) |
---|
184 | { |
---|
185 | addMapsArrayToMaps (&mo, mi, tmap->name); |
---|
186 | map* nb=getMapFromMaps(mo,mi->name,"length"); |
---|
187 | if (nb!=NULL && atoi(nb->value)>atoi(testMap->value)) |
---|
188 | { |
---|
189 | char emsg[1024]; |
---|
190 | sprintf (emsg, |
---|
191 | _("You set maximum occurences for <%s> as %i but you tried to use it more than the limit you set. Please correct your ZCFG file or your request."), |
---|
192 | mi->name, atoi (testMap->value)); |
---|
193 | errorException (m, emsg, "InternalError", NULL); |
---|
194 | return -1; |
---|
195 | } |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | if (strncasecmp (testMap->value, "unbounded", 9) == 0) |
---|
200 | { |
---|
201 | if (hasEl < 0) |
---|
202 | { |
---|
203 | freeMap (&testMap); |
---|
204 | free (testMap); |
---|
205 | } |
---|
206 | if (addMapsArrayToMaps (&mo, mi, tmap->name) < 0) |
---|
207 | { |
---|
208 | char emsg[1024]; |
---|
209 | map *tmpMap = getMap (mi->content, "length"); |
---|
210 | sprintf (emsg, |
---|
211 | _ |
---|
212 | ("ZOO-Kernel was unable to load your data for %s position %s."), |
---|
213 | mi->name, tmpMap->value); |
---|
214 | errorException (m, emsg, "InternalError", NULL); |
---|
215 | return -1; |
---|
216 | } |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | char emsg[1024]; |
---|
221 | sprintf (emsg, |
---|
222 | _ |
---|
223 | ("You set maximum occurences for <%s> to one but you tried to use it more than once. Please correct your ZCFG file or your request."), |
---|
224 | mi->name); |
---|
225 | errorException (m, emsg, "InternalError", NULL); |
---|
226 | return -1; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | return 0; |
---|
231 | } |
---|
232 | |
---|
233 | int |
---|
234 | recursReaddirF (maps * m, xmlNodePtr n, char *conf_dir, char *prefix, |
---|
235 | int saved_stdout, int level, void (func) (maps *, xmlNodePtr, |
---|
236 | service *)) |
---|
237 | { |
---|
238 | struct dirent *dp; |
---|
239 | int scount = 0; |
---|
240 | |
---|
241 | if (conf_dir == NULL) |
---|
242 | return 1; |
---|
243 | DIR *dirp = opendir (conf_dir); |
---|
244 | if (dirp == NULL) |
---|
245 | { |
---|
246 | if (level > 0) |
---|
247 | return 1; |
---|
248 | else |
---|
249 | return -1; |
---|
250 | } |
---|
251 | char tmp1[25]; |
---|
252 | sprintf (tmp1, "sprefix_%d", level); |
---|
253 | char levels[17]; |
---|
254 | sprintf (levels, "%d", level); |
---|
255 | setMapInMaps (m, "lenv", "level", levels); |
---|
256 | while ((dp = readdir (dirp)) != NULL) |
---|
257 | if ((dp->d_type == DT_DIR || dp->d_type == DT_LNK) && dp->d_name[0] != '.' |
---|
258 | && strstr (dp->d_name, ".") == NULL) |
---|
259 | { |
---|
260 | |
---|
261 | char *tmp = |
---|
262 | (char *) malloc ((strlen (conf_dir) + strlen (dp->d_name) + 2) * |
---|
263 | sizeof (char)); |
---|
264 | sprintf (tmp, "%s/%s", conf_dir, dp->d_name); |
---|
265 | |
---|
266 | if (prefix != NULL) |
---|
267 | { |
---|
268 | prefix = NULL; |
---|
269 | } |
---|
270 | prefix = (char *) malloc ((strlen (dp->d_name) + 2) * sizeof (char)); |
---|
271 | sprintf (prefix, "%s.", dp->d_name); |
---|
272 | |
---|
273 | //map* tmpMap=getMapFromMaps(m,"lenv",tmp1); |
---|
274 | |
---|
275 | int res; |
---|
276 | if (prefix != NULL) |
---|
277 | { |
---|
278 | setMapInMaps (m, "lenv", tmp1, prefix); |
---|
279 | char levels1[17]; |
---|
280 | sprintf (levels1, "%d", level + 1); |
---|
281 | setMapInMaps (m, "lenv", "level", levels1); |
---|
282 | res = |
---|
283 | recursReaddirF (m, n, tmp, prefix, saved_stdout, level + 1, |
---|
284 | func); |
---|
285 | sprintf (levels1, "%d", level); |
---|
286 | setMapInMaps (m, "lenv", "level", levels1); |
---|
287 | free (prefix); |
---|
288 | prefix = NULL; |
---|
289 | } |
---|
290 | else |
---|
291 | res = -1; |
---|
292 | free (tmp); |
---|
293 | if (res < 0) |
---|
294 | { |
---|
295 | return res; |
---|
296 | } |
---|
297 | } |
---|
298 | else |
---|
299 | { |
---|
300 | if (dp->d_name[0] != '.' && strstr (dp->d_name, ".zcfg") != 0) |
---|
301 | { |
---|
302 | int t; |
---|
303 | char tmps1[1024]; |
---|
304 | memset (tmps1, 0, 1024); |
---|
305 | snprintf (tmps1, 1024, "%s/%s", conf_dir, dp->d_name); |
---|
306 | service *s1 = (service *) malloc (SERVICE_SIZE); |
---|
307 | if (s1 == NULL) |
---|
308 | { |
---|
309 | dup2 (saved_stdout, fileno (stdout)); |
---|
310 | errorException (m, _("Unable to allocate memory."), |
---|
311 | "InternalError", NULL); |
---|
312 | return -1; |
---|
313 | } |
---|
314 | #ifdef DEBUG |
---|
315 | fprintf (stderr, "#################\n%s\n#################\n", |
---|
316 | tmps1); |
---|
317 | #endif |
---|
318 | char *tmpsn = zStrdup (dp->d_name); |
---|
319 | tmpsn[strlen (tmpsn) - 5] = 0; |
---|
320 | t = readServiceFile (m, tmps1, &s1, tmpsn); |
---|
321 | free (tmpsn); |
---|
322 | if (t < 0) |
---|
323 | { |
---|
324 | map *tmp00 = getMapFromMaps (m, "lenv", "message"); |
---|
325 | char tmp01[1024]; |
---|
326 | if (tmp00 != NULL) |
---|
327 | sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"), |
---|
328 | dp->d_name, tmp00->value); |
---|
329 | else |
---|
330 | sprintf (tmp01, _("Unable to parse the ZCFG file: %s."), |
---|
331 | dp->d_name); |
---|
332 | dup2 (saved_stdout, fileno (stdout)); |
---|
333 | errorException (m, tmp01, "InternalError", NULL); |
---|
334 | return -1; |
---|
335 | } |
---|
336 | #ifdef DEBUG |
---|
337 | dumpService (s1); |
---|
338 | fflush (stdout); |
---|
339 | fflush (stderr); |
---|
340 | #endif |
---|
341 | func (m, n, s1); |
---|
342 | freeService (&s1); |
---|
343 | free (s1); |
---|
344 | scount++; |
---|
345 | } |
---|
346 | } |
---|
347 | (void) closedir (dirp); |
---|
348 | return 1; |
---|
349 | } |
---|
350 | |
---|
351 | xmlXPathObjectPtr |
---|
352 | extractFromDoc (xmlDocPtr doc, const char *search) |
---|
353 | { |
---|
354 | xmlXPathContextPtr xpathCtx; |
---|
355 | xmlXPathObjectPtr xpathObj; |
---|
356 | xpathCtx = xmlXPathNewContext (doc); |
---|
357 | xpathObj = xmlXPathEvalExpression (BAD_CAST search, xpathCtx); |
---|
358 | xmlXPathFreeContext (xpathCtx); |
---|
359 | return xpathObj; |
---|
360 | } |
---|
361 | |
---|
362 | void |
---|
363 | donothing (int sig) |
---|
364 | { |
---|
365 | #ifdef DEBUG |
---|
366 | fprintf (stderr, "Signal %d after the ZOO-Kernel returned result !\n", sig); |
---|
367 | #endif |
---|
368 | exit (0); |
---|
369 | } |
---|
370 | |
---|
371 | void |
---|
372 | sig_handler (int sig) |
---|
373 | { |
---|
374 | char tmp[100]; |
---|
375 | const char *ssig; |
---|
376 | switch (sig) |
---|
377 | { |
---|
378 | case SIGSEGV: |
---|
379 | ssig = "SIGSEGV"; |
---|
380 | break; |
---|
381 | case SIGTERM: |
---|
382 | ssig = "SIGTERM"; |
---|
383 | break; |
---|
384 | case SIGINT: |
---|
385 | ssig = "SIGINT"; |
---|
386 | break; |
---|
387 | case SIGILL: |
---|
388 | ssig = "SIGILL"; |
---|
389 | break; |
---|
390 | case SIGFPE: |
---|
391 | ssig = "SIGFPE"; |
---|
392 | break; |
---|
393 | case SIGABRT: |
---|
394 | ssig = "SIGABRT"; |
---|
395 | break; |
---|
396 | default: |
---|
397 | ssig = "UNKNOWN"; |
---|
398 | break; |
---|
399 | } |
---|
400 | sprintf (tmp, |
---|
401 | _ |
---|
402 | ("ZOO Kernel failed to process your request receiving signal %d = %s"), |
---|
403 | sig, ssig); |
---|
404 | errorException (NULL, tmp, "InternalError", NULL); |
---|
405 | #ifdef DEBUG |
---|
406 | fprintf (stderr, "Not this time!\n"); |
---|
407 | #endif |
---|
408 | exit (0); |
---|
409 | } |
---|
410 | |
---|
411 | void |
---|
412 | loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs, |
---|
413 | maps ** inputs, maps ** ioutputs, int *eres) |
---|
414 | { |
---|
415 | char tmps1[1024]; |
---|
416 | char ntmp[1024]; |
---|
417 | maps *m = *myMap; |
---|
418 | maps *request_output_real_format = *ioutputs; |
---|
419 | maps *request_input_real_format = *inputs; |
---|
420 | /** |
---|
421 | * Extract serviceType to know what kind of service should be loaded |
---|
422 | */ |
---|
423 | map *r_inputs = NULL; |
---|
424 | #ifndef WIN32 |
---|
425 | getcwd (ntmp, 1024); |
---|
426 | #else |
---|
427 | _getcwd (ntmp, 1024); |
---|
428 | #endif |
---|
429 | r_inputs = getMap (s1->content, "serviceType"); |
---|
430 | #ifdef DEBUG |
---|
431 | fprintf (stderr, "LOAD A %s SERVICE PROVIDER \n", r_inputs->value); |
---|
432 | fflush (stderr); |
---|
433 | #endif |
---|
434 | if (strlen (r_inputs->value) == 1 |
---|
435 | && strncasecmp (r_inputs->value, "C", 1) == 0) |
---|
436 | { |
---|
437 | r_inputs = getMap (request_inputs, "metapath"); |
---|
438 | if (r_inputs != NULL) |
---|
439 | sprintf (tmps1, "%s/%s", ntmp, r_inputs->value); |
---|
440 | else |
---|
441 | sprintf (tmps1, "%s/", ntmp); |
---|
442 | char *altPath = zStrdup (tmps1); |
---|
443 | r_inputs = getMap (s1->content, "ServiceProvider"); |
---|
444 | sprintf (tmps1, "%s/%s", altPath, r_inputs->value); |
---|
445 | free (altPath); |
---|
446 | #ifdef DEBUG |
---|
447 | fprintf (stderr, "Trying to load %s\n", tmps1); |
---|
448 | #endif |
---|
449 | #ifdef WIN32 |
---|
450 | HINSTANCE so = |
---|
451 | LoadLibraryEx (tmps1, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
---|
452 | #else |
---|
453 | void *so = dlopen (tmps1, RTLD_LAZY); |
---|
454 | #endif |
---|
455 | #ifdef WIN32 |
---|
456 | DWORD errstr; |
---|
457 | errstr = GetLastError (); |
---|
458 | #else |
---|
459 | char *errstr; |
---|
460 | errstr = dlerror (); |
---|
461 | #endif |
---|
462 | #ifdef DEBUG |
---|
463 | fprintf (stderr, "%s loaded (%d) \n", tmps1, errstr); |
---|
464 | #endif |
---|
465 | if (so != NULL) |
---|
466 | { |
---|
467 | #ifdef DEBUG |
---|
468 | fprintf (stderr, "Library loaded %s \n", errstr); |
---|
469 | fprintf (stderr, "Service Shared Object = %s\n", r_inputs->value); |
---|
470 | #endif |
---|
471 | r_inputs = getMap (s1->content, "serviceType"); |
---|
472 | #ifdef DEBUG |
---|
473 | dumpMap (r_inputs); |
---|
474 | fprintf (stderr, "%s\n", r_inputs->value); |
---|
475 | fflush (stderr); |
---|
476 | #endif |
---|
477 | if (strncasecmp (r_inputs->value, "C-FORTRAN", 9) == 0) |
---|
478 | { |
---|
479 | r_inputs = getMap (request_inputs, "Identifier"); |
---|
480 | char fname[1024]; |
---|
481 | sprintf (fname, "%s_", r_inputs->value); |
---|
482 | #ifdef DEBUG |
---|
483 | fprintf (stderr, "Try to load function %s\n", fname); |
---|
484 | #endif |
---|
485 | #ifdef WIN32 |
---|
486 | typedef int (CALLBACK * execute_t) (char ***, char ***, |
---|
487 | char ***); |
---|
488 | execute_t execute = (execute_t) GetProcAddress (so, fname); |
---|
489 | #else |
---|
490 | typedef int (*execute_t) (char ***, char ***, char ***); |
---|
491 | execute_t execute = (execute_t) dlsym (so, fname); |
---|
492 | #endif |
---|
493 | #ifdef DEBUG |
---|
494 | #ifdef WIN32 |
---|
495 | errstr = GetLastError (); |
---|
496 | #else |
---|
497 | errstr = dlerror (); |
---|
498 | #endif |
---|
499 | fprintf (stderr, "Function loaded %s\n", errstr); |
---|
500 | #endif |
---|
501 | |
---|
502 | char main_conf[10][30][1024]; |
---|
503 | char inputs[10][30][1024]; |
---|
504 | char outputs[10][30][1024]; |
---|
505 | for (int i = 0; i < 10; i++) |
---|
506 | { |
---|
507 | for (int j = 0; j < 30; j++) |
---|
508 | { |
---|
509 | memset (main_conf[i][j], 0, 1024); |
---|
510 | memset (inputs[i][j], 0, 1024); |
---|
511 | memset (outputs[i][j], 0, 1024); |
---|
512 | } |
---|
513 | } |
---|
514 | mapsToCharXXX (m, (char ***) main_conf); |
---|
515 | mapsToCharXXX (request_input_real_format, (char ***) inputs); |
---|
516 | mapsToCharXXX (request_output_real_format, (char ***) outputs); |
---|
517 | *eres = |
---|
518 | execute ((char ***) &main_conf[0], (char ***) &inputs[0], |
---|
519 | (char ***) &outputs[0]); |
---|
520 | #ifdef DEBUG |
---|
521 | fprintf (stderr, "Function run successfully \n"); |
---|
522 | #endif |
---|
523 | charxxxToMaps ((char ***) &outputs[0], |
---|
524 | &request_output_real_format); |
---|
525 | } |
---|
526 | else |
---|
527 | { |
---|
528 | #ifdef DEBUG |
---|
529 | #ifdef WIN32 |
---|
530 | errstr = GetLastError (); |
---|
531 | fprintf (stderr, "Function %s failed to load because of %d\n", |
---|
532 | r_inputs->value, errstr); |
---|
533 | #endif |
---|
534 | #endif |
---|
535 | r_inputs = getMapFromMaps (m, "lenv", "Identifier"); |
---|
536 | #ifdef DEBUG |
---|
537 | fprintf (stderr, "Try to load function %s\n", r_inputs->value); |
---|
538 | #endif |
---|
539 | typedef int (*execute_t) (maps **, maps **, maps **); |
---|
540 | #ifdef WIN32 |
---|
541 | execute_t execute = |
---|
542 | (execute_t) GetProcAddress (so, r_inputs->value); |
---|
543 | #else |
---|
544 | execute_t execute = (execute_t) dlsym (so, r_inputs->value); |
---|
545 | #endif |
---|
546 | |
---|
547 | if (execute == NULL) |
---|
548 | { |
---|
549 | #ifdef WIN32 |
---|
550 | errstr = GetLastError (); |
---|
551 | #else |
---|
552 | errstr = dlerror (); |
---|
553 | #endif |
---|
554 | char *tmpMsg = |
---|
555 | (char *) malloc (2048 + strlen (r_inputs->value)); |
---|
556 | sprintf (tmpMsg, |
---|
557 | _ |
---|
558 | ("Error occured while running the %s function: %s"), |
---|
559 | r_inputs->value, errstr); |
---|
560 | errorException (m, tmpMsg, "InternalError", NULL); |
---|
561 | free (tmpMsg); |
---|
562 | #ifdef DEBUG |
---|
563 | fprintf (stderr, "Function %s error %s\n", r_inputs->value, |
---|
564 | errstr); |
---|
565 | #endif |
---|
566 | *eres = -1; |
---|
567 | return; |
---|
568 | } |
---|
569 | |
---|
570 | #ifdef DEBUG |
---|
571 | #ifdef WIN32 |
---|
572 | errstr = GetLastError (); |
---|
573 | #else |
---|
574 | errstr = dlerror (); |
---|
575 | #endif |
---|
576 | fprintf (stderr, "Function loaded %s\n", errstr); |
---|
577 | #endif |
---|
578 | |
---|
579 | #ifdef DEBUG |
---|
580 | fprintf (stderr, "Now run the function \n"); |
---|
581 | fflush (stderr); |
---|
582 | #endif |
---|
583 | *eres = |
---|
584 | execute (&m, &request_input_real_format, |
---|
585 | &request_output_real_format); |
---|
586 | #ifdef DEBUG |
---|
587 | fprintf (stderr, "Function loaded and returned %d\n", eres); |
---|
588 | fflush (stderr); |
---|
589 | #endif |
---|
590 | } |
---|
591 | #ifdef WIN32 |
---|
592 | *ioutputs = dupMaps (&request_output_real_format); |
---|
593 | FreeLibrary (so); |
---|
594 | #else |
---|
595 | dlclose (so); |
---|
596 | #endif |
---|
597 | } |
---|
598 | else |
---|
599 | { |
---|
600 | /** |
---|
601 | * Unable to load the specified shared library |
---|
602 | */ |
---|
603 | char tmps[1024]; |
---|
604 | #ifdef WIN32 |
---|
605 | DWORD errstr = GetLastError (); |
---|
606 | #else |
---|
607 | char *errstr = dlerror (); |
---|
608 | #endif |
---|
609 | sprintf (tmps, _("C Library can't be loaded %s"), errstr); |
---|
610 | map *tmps1 = createMap ("text", tmps); |
---|
611 | printExceptionReportResponse (m, tmps1); |
---|
612 | *eres = -1; |
---|
613 | freeMap (&tmps1); |
---|
614 | free (tmps1); |
---|
615 | } |
---|
616 | } |
---|
617 | else |
---|
618 | |
---|
619 | #ifdef USE_OTB |
---|
620 | if (strncasecmp (r_inputs->value, "OTB", 6) == 0) |
---|
621 | { |
---|
622 | *eres = |
---|
623 | zoo_otb_support (&m, request_inputs, s1, |
---|
624 | &request_input_real_format, |
---|
625 | &request_output_real_format); |
---|
626 | } |
---|
627 | else |
---|
628 | #endif |
---|
629 | |
---|
630 | #ifdef USE_PYTHON |
---|
631 | if (strncasecmp (r_inputs->value, "PYTHON", 6) == 0) |
---|
632 | { |
---|
633 | *eres = |
---|
634 | zoo_python_support (&m, request_inputs, s1, |
---|
635 | &request_input_real_format, |
---|
636 | &request_output_real_format); |
---|
637 | } |
---|
638 | else |
---|
639 | #endif |
---|
640 | |
---|
641 | #ifdef USE_JAVA |
---|
642 | if (strncasecmp (r_inputs->value, "JAVA", 4) == 0) |
---|
643 | { |
---|
644 | *eres = |
---|
645 | zoo_java_support (&m, request_inputs, s1, &request_input_real_format, |
---|
646 | &request_output_real_format); |
---|
647 | } |
---|
648 | else |
---|
649 | #endif |
---|
650 | |
---|
651 | #ifdef USE_PHP |
---|
652 | if (strncasecmp (r_inputs->value, "PHP", 3) == 0) |
---|
653 | { |
---|
654 | *eres = |
---|
655 | zoo_php_support (&m, request_inputs, s1, &request_input_real_format, |
---|
656 | &request_output_real_format); |
---|
657 | } |
---|
658 | else |
---|
659 | #endif |
---|
660 | |
---|
661 | |
---|
662 | #ifdef USE_PERL |
---|
663 | if (strncasecmp (r_inputs->value, "PERL", 4) == 0) |
---|
664 | { |
---|
665 | *eres = |
---|
666 | zoo_perl_support (&m, request_inputs, s1, &request_input_real_format, |
---|
667 | &request_output_real_format); |
---|
668 | } |
---|
669 | else |
---|
670 | #endif |
---|
671 | |
---|
672 | #ifdef USE_JS |
---|
673 | if (strncasecmp (r_inputs->value, "JS", 2) == 0) |
---|
674 | { |
---|
675 | *eres = |
---|
676 | zoo_js_support (&m, request_inputs, s1, &request_input_real_format, |
---|
677 | &request_output_real_format); |
---|
678 | } |
---|
679 | else |
---|
680 | #endif |
---|
681 | |
---|
682 | #ifdef USE_RUBY |
---|
683 | if (strncasecmp (r_inputs->value, "Ruby", 4) == 0) |
---|
684 | { |
---|
685 | *eres = |
---|
686 | zoo_ruby_support (&m, request_inputs, s1, &request_input_real_format, |
---|
687 | &request_output_real_format); |
---|
688 | } |
---|
689 | else |
---|
690 | #endif |
---|
691 | |
---|
692 | { |
---|
693 | char tmpv[1024]; |
---|
694 | sprintf (tmpv, |
---|
695 | _ |
---|
696 | ("Programming Language (%s) set in ZCFG file is not currently supported by ZOO Kernel.\n"), |
---|
697 | r_inputs->value); |
---|
698 | map *tmps = createMap ("text", tmpv); |
---|
699 | printExceptionReportResponse (m, tmps); |
---|
700 | *eres = -1; |
---|
701 | } |
---|
702 | *myMap = m; |
---|
703 | *ioutputs = request_output_real_format; |
---|
704 | } |
---|
705 | |
---|
706 | |
---|
707 | #ifdef WIN32 |
---|
708 | /** |
---|
709 | * createProcess function: create a new process after setting some env variables |
---|
710 | */ |
---|
711 | void |
---|
712 | createProcess (maps * m, map * request_inputs, service * s1, char *opts, |
---|
713 | int cpid, maps * inputs, maps * outputs) |
---|
714 | { |
---|
715 | STARTUPINFO si; |
---|
716 | PROCESS_INFORMATION pi; |
---|
717 | ZeroMemory (&si, sizeof (si)); |
---|
718 | si.cb = sizeof (si); |
---|
719 | ZeroMemory (&pi, sizeof (pi)); |
---|
720 | char *tmp = (char *) malloc ((1024 + cgiContentLength) * sizeof (char)); |
---|
721 | char *tmpq = (char *) malloc ((1024 + cgiContentLength) * sizeof (char)); |
---|
722 | map *req = getMap (request_inputs, "request"); |
---|
723 | map *id = getMap (request_inputs, "identifier"); |
---|
724 | map *di = getMap (request_inputs, "DataInputs"); |
---|
725 | |
---|
726 | char *dataInputsKVP = getMapsAsKVP (inputs, cgiContentLength, 0); |
---|
727 | char *dataOutputsKVP = getMapsAsKVP (outputs, cgiContentLength, 1); |
---|
728 | #ifdef DEBUG |
---|
729 | fprintf (stderr, "DATAINPUTSKVP %s\n", dataInputsKVP); |
---|
730 | fprintf (stderr, "DATAOUTPUTSKVP %s\n", dataOutputsKVP); |
---|
731 | #endif |
---|
732 | map *sid = getMapFromMaps (m, "lenv", "sid"); |
---|
733 | map *r_inputs = getMapFromMaps (m, "main", "tmpPath"); |
---|
734 | map *r_inputs1 = getMap (request_inputs, "metapath"); |
---|
735 | int hasIn = -1; |
---|
736 | if (r_inputs1 == NULL) |
---|
737 | { |
---|
738 | r_inputs1 = createMap ("metapath", ""); |
---|
739 | hasIn = 1; |
---|
740 | } |
---|
741 | map *r_inputs2 = getMap (request_inputs, "ResponseDocument"); |
---|
742 | if (r_inputs2 == NULL) |
---|
743 | r_inputs2 = getMap (request_inputs, "RawDataOutput"); |
---|
744 | map *tmpPath = getMapFromMaps (m, "lenv", "cwd"); |
---|
745 | |
---|
746 | map *tmpReq = getMap (request_inputs, "xrequest"); |
---|
747 | if (r_inputs2 != NULL) |
---|
748 | { |
---|
749 | sprintf (tmp, |
---|
750 | "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s&cgiSid=%s\"", |
---|
751 | r_inputs1->value, req->value, id->value, dataInputsKVP, |
---|
752 | r_inputs2->name, dataOutputsKVP, sid->value); |
---|
753 | sprintf (tmpq, |
---|
754 | "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&%s=%s", |
---|
755 | r_inputs1->value, req->value, id->value, dataInputsKVP, |
---|
756 | r_inputs2->name, dataOutputsKVP); |
---|
757 | } |
---|
758 | else |
---|
759 | { |
---|
760 | sprintf (tmp, |
---|
761 | "\"metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s&cgiSid=%s\"", |
---|
762 | r_inputs1->value, req->value, id->value, dataInputsKVP, |
---|
763 | sid->value); |
---|
764 | sprintf (tmpq, |
---|
765 | "metapath=%s&request=%s&service=WPS&version=1.0.0&Identifier=%s&DataInputs=%s", |
---|
766 | r_inputs1->value, req->value, id->value, dataInputsKVP, |
---|
767 | sid->value); |
---|
768 | } |
---|
769 | |
---|
770 | if (hasIn > 0) |
---|
771 | { |
---|
772 | freeMap (&r_inputs1); |
---|
773 | free (r_inputs1); |
---|
774 | } |
---|
775 | char *tmp1 = zStrdup (tmp); |
---|
776 | sprintf (tmp, "\"zoo_loader.cgi\" %s \"%s\"", tmp1, sid->value); |
---|
777 | |
---|
778 | free (dataInputsKVP); |
---|
779 | free (dataOutputsKVP); |
---|
780 | #ifdef DEBUG |
---|
781 | fprintf (stderr, "REQUEST IS : %s \n", tmp); |
---|
782 | #endif |
---|
783 | SetEnvironmentVariable ("CGISID", TEXT (sid->value)); |
---|
784 | SetEnvironmentVariable ("QUERY_STRING", TEXT (tmpq)); |
---|
785 | char clen[1000]; |
---|
786 | sprintf (clen, "%d", strlen (tmpq)); |
---|
787 | SetEnvironmentVariable ("CONTENT_LENGTH", TEXT (clen)); |
---|
788 | |
---|
789 | if (!CreateProcess (NULL, // No module name (use command line) |
---|
790 | TEXT (tmp), // Command line |
---|
791 | NULL, // Process handle not inheritable |
---|
792 | NULL, // Thread handle not inheritable |
---|
793 | FALSE, // Set handle inheritance to FALSE |
---|
794 | CREATE_NO_WINDOW, // Apache won't wait until the end |
---|
795 | NULL, // Use parent's environment block |
---|
796 | NULL, // Use parent's starting directory |
---|
797 | &si, // Pointer to STARTUPINFO struct |
---|
798 | &pi) // Pointer to PROCESS_INFORMATION struct |
---|
799 | ) |
---|
800 | { |
---|
801 | #ifdef DEBUG |
---|
802 | fprintf (stderr, "CreateProcess failed (%d).\n", GetLastError ()); |
---|
803 | #endif |
---|
804 | return; |
---|
805 | } |
---|
806 | else |
---|
807 | { |
---|
808 | #ifdef DEBUG |
---|
809 | fprintf (stderr, "CreateProcess successfull (%d).\n\n\n\n", |
---|
810 | GetLastError ()); |
---|
811 | #endif |
---|
812 | } |
---|
813 | CloseHandle (pi.hProcess); |
---|
814 | CloseHandle (pi.hThread); |
---|
815 | #ifdef DEBUG |
---|
816 | fprintf (stderr, "CreateProcess finished !\n"); |
---|
817 | #endif |
---|
818 | } |
---|
819 | #endif |
---|
820 | |
---|
821 | int |
---|
822 | runRequest (map ** inputs) |
---|
823 | { |
---|
824 | |
---|
825 | #ifndef USE_GDB |
---|
826 | signal (SIGCHLD, SIG_IGN); |
---|
827 | signal (SIGSEGV, sig_handler); |
---|
828 | signal (SIGTERM, sig_handler); |
---|
829 | signal (SIGINT, sig_handler); |
---|
830 | signal (SIGILL, sig_handler); |
---|
831 | signal (SIGFPE, sig_handler); |
---|
832 | signal (SIGABRT, sig_handler); |
---|
833 | #endif |
---|
834 | |
---|
835 | map *r_inputs = NULL; |
---|
836 | map *request_inputs = *inputs; |
---|
837 | maps *m = NULL; |
---|
838 | char *REQUEST = NULL; |
---|
839 | /** |
---|
840 | * Parsing service specfic configuration file |
---|
841 | */ |
---|
842 | m = (maps *) malloc (MAPS_SIZE); |
---|
843 | if (m == NULL) |
---|
844 | { |
---|
845 | return errorException (m, _("Unable to allocate memory."), |
---|
846 | "InternalError", NULL); |
---|
847 | } |
---|
848 | char ntmp[1024]; |
---|
849 | #ifndef WIN32 |
---|
850 | getcwd (ntmp, 1024); |
---|
851 | #else |
---|
852 | _getcwd (ntmp, 1024); |
---|
853 | #endif |
---|
854 | r_inputs = getMapOrFill (&request_inputs, "metapath", ""); |
---|
855 | |
---|
856 | |
---|
857 | char conf_file[10240]; |
---|
858 | snprintf (conf_file, 10240, "%s/%s/main.cfg", ntmp, r_inputs->value); |
---|
859 | if (conf_read (conf_file, m) == 2) |
---|
860 | { |
---|
861 | errorException (NULL, _("Unable to load the main.cfg file."), |
---|
862 | "InternalError", NULL); |
---|
863 | free (m); |
---|
864 | return 1; |
---|
865 | } |
---|
866 | #ifdef DEBUG |
---|
867 | fprintf (stderr, "***** BEGIN MAPS\n"); |
---|
868 | dumpMaps (m); |
---|
869 | fprintf (stderr, "***** END MAPS\n"); |
---|
870 | #endif |
---|
871 | |
---|
872 | map *getPath = getMapFromMaps (m, "main", "gettextPath"); |
---|
873 | if (getPath != NULL) |
---|
874 | { |
---|
875 | bindtextdomain ("zoo-kernel", getPath->value); |
---|
876 | bindtextdomain ("zoo-services", getPath->value); |
---|
877 | } |
---|
878 | else |
---|
879 | { |
---|
880 | bindtextdomain ("zoo-kernel", "/usr/share/locale/"); |
---|
881 | bindtextdomain ("zoo-services", "/usr/share/locale/"); |
---|
882 | } |
---|
883 | |
---|
884 | |
---|
885 | /** |
---|
886 | * Manage our own error log file (usefull to separate standard apache debug |
---|
887 | * messages from the ZOO-Kernel ones but also for IIS users to avoid wrong |
---|
888 | * headers messages returned by the CGI due to wrong redirection of stderr) |
---|
889 | */ |
---|
890 | FILE *fstde = NULL; |
---|
891 | map *fstdem = getMapFromMaps (m, "main", "logPath"); |
---|
892 | if (fstdem != NULL) |
---|
893 | fstde = freopen (fstdem->value, "a+", stderr); |
---|
894 | |
---|
895 | r_inputs = getMap (request_inputs, "language"); |
---|
896 | if (r_inputs == NULL) |
---|
897 | r_inputs = getMapFromMaps (m, "main", "language"); |
---|
898 | if (r_inputs != NULL) |
---|
899 | { |
---|
900 | if (isValidLang (m, r_inputs->value) < 0) |
---|
901 | { |
---|
902 | char tmp[1024]; |
---|
903 | sprintf (tmp, |
---|
904 | _ |
---|
905 | ("The value %s is not supported for the <language> parameter"), |
---|
906 | r_inputs->value); |
---|
907 | errorException (m, tmp, "InvalidParameterValue", "language"); |
---|
908 | freeMaps (&m); |
---|
909 | free (m); |
---|
910 | free (REQUEST); |
---|
911 | return 1; |
---|
912 | |
---|
913 | } |
---|
914 | char *tmp = zStrdup (r_inputs->value); |
---|
915 | setMapInMaps (m, "main", "language", tmp); |
---|
916 | #ifdef DEB |
---|
917 | char tmp2[12]; |
---|
918 | sprintf (tmp2, "%s.utf-8", tmp); |
---|
919 | translateChar (tmp2, '-', '_'); |
---|
920 | setlocale (LC_ALL, tmp2); |
---|
921 | #else |
---|
922 | translateChar (tmp, '-', '_'); |
---|
923 | setlocale (LC_ALL, tmp); |
---|
924 | #endif |
---|
925 | #ifndef WIN32 |
---|
926 | setenv ("LC_ALL", tmp, 1); |
---|
927 | #else |
---|
928 | char tmp1[12]; |
---|
929 | sprintf (tmp1, "LC_ALL=%s", tmp); |
---|
930 | putenv (tmp1); |
---|
931 | #endif |
---|
932 | free (tmp); |
---|
933 | } |
---|
934 | else |
---|
935 | { |
---|
936 | setlocale (LC_ALL, "en_US"); |
---|
937 | #ifndef WIN32 |
---|
938 | setenv ("LC_ALL", "en_US", 1); |
---|
939 | #else |
---|
940 | char tmp1[12]; |
---|
941 | sprintf (tmp1, "LC_ALL=en_US"); |
---|
942 | putenv (tmp1); |
---|
943 | #endif |
---|
944 | setMapInMaps (m, "main", "language", "en-US"); |
---|
945 | } |
---|
946 | setlocale (LC_NUMERIC, "en_US"); |
---|
947 | bind_textdomain_codeset ("zoo-kernel", "UTF-8"); |
---|
948 | textdomain ("zoo-kernel"); |
---|
949 | bind_textdomain_codeset ("zoo-services", "UTF-8"); |
---|
950 | textdomain ("zoo-services"); |
---|
951 | |
---|
952 | map *lsoap = getMap (request_inputs, "soap"); |
---|
953 | if (lsoap != NULL && strcasecmp (lsoap->value, "true") == 0) |
---|
954 | setMapInMaps (m, "main", "isSoap", "true"); |
---|
955 | else |
---|
956 | setMapInMaps (m, "main", "isSoap", "false"); |
---|
957 | |
---|
958 | if (strlen (cgiServerName) > 0) |
---|
959 | { |
---|
960 | char tmpUrl[1024]; |
---|
961 | if (strncmp (cgiServerPort, "80", 2) == 0) |
---|
962 | { |
---|
963 | sprintf (tmpUrl, "http://%s%s", cgiServerName, cgiScriptName); |
---|
964 | } |
---|
965 | else |
---|
966 | { |
---|
967 | sprintf (tmpUrl, "http://%s:%s%s", cgiServerName, cgiServerPort, |
---|
968 | cgiScriptName); |
---|
969 | } |
---|
970 | #ifdef DEBUG |
---|
971 | fprintf (stderr, "*** %s ***\n", tmpUrl); |
---|
972 | #endif |
---|
973 | setMapInMaps (m, "main", "serverAddress", tmpUrl); |
---|
974 | } |
---|
975 | |
---|
976 | /** |
---|
977 | * Check for minimum inputs |
---|
978 | */ |
---|
979 | r_inputs = getMap (request_inputs, "Request"); |
---|
980 | if (request_inputs == NULL || r_inputs == NULL) |
---|
981 | { |
---|
982 | errorException (m, _("Parameter <request> was not specified"), |
---|
983 | "MissingParameterValue", "request"); |
---|
984 | if (count (request_inputs) == 1) |
---|
985 | { |
---|
986 | freeMap (&request_inputs); |
---|
987 | free (request_inputs); |
---|
988 | } |
---|
989 | freeMaps (&m); |
---|
990 | free (m); |
---|
991 | return 1; |
---|
992 | } |
---|
993 | else |
---|
994 | { |
---|
995 | REQUEST = zStrdup (r_inputs->value); |
---|
996 | if (strncasecmp (r_inputs->value, "GetCapabilities", 15) != 0 |
---|
997 | && strncasecmp (r_inputs->value, "DescribeProcess", 15) != 0 |
---|
998 | && strncasecmp (r_inputs->value, "Execute", 7) != 0) |
---|
999 | { |
---|
1000 | errorException (m, |
---|
1001 | _ |
---|
1002 | ("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), |
---|
1003 | "OperationNotSupported", r_inputs->value); |
---|
1004 | freeMaps (&m); |
---|
1005 | free (m); |
---|
1006 | free (REQUEST); |
---|
1007 | return 1; |
---|
1008 | } |
---|
1009 | } |
---|
1010 | r_inputs = NULL; |
---|
1011 | r_inputs = getMap (request_inputs, "Service"); |
---|
1012 | if (r_inputs == NULLMAP) |
---|
1013 | { |
---|
1014 | errorException (m, _("Parameter <service> was not specified"), |
---|
1015 | "MissingParameterValue", "service"); |
---|
1016 | freeMaps (&m); |
---|
1017 | free (m); |
---|
1018 | free (REQUEST); |
---|
1019 | return 1; |
---|
1020 | } |
---|
1021 | else |
---|
1022 | { |
---|
1023 | if (strcasecmp (r_inputs->value, "WPS") != 0) |
---|
1024 | { |
---|
1025 | errorException (m, |
---|
1026 | _ |
---|
1027 | ("Unenderstood <service> value, WPS is the only acceptable value."), |
---|
1028 | "InvalidParameterValue", "service"); |
---|
1029 | freeMaps (&m); |
---|
1030 | free (m); |
---|
1031 | free (REQUEST); |
---|
1032 | return 1; |
---|
1033 | } |
---|
1034 | } |
---|
1035 | if (strncasecmp (REQUEST, "GetCapabilities", 15) != 0) |
---|
1036 | { |
---|
1037 | r_inputs = getMap (request_inputs, "Version"); |
---|
1038 | if (r_inputs == NULL) |
---|
1039 | { |
---|
1040 | errorException (m, _("Parameter <version> was not specified"), |
---|
1041 | "MissingParameterValue", "version"); |
---|
1042 | freeMaps (&m); |
---|
1043 | free (m); |
---|
1044 | free (REQUEST); |
---|
1045 | return 1; |
---|
1046 | } |
---|
1047 | else |
---|
1048 | { |
---|
1049 | if (strcasecmp (r_inputs->value, "1.0.0") != 0) |
---|
1050 | { |
---|
1051 | errorException (m, |
---|
1052 | _ |
---|
1053 | ("Unenderstood <version> value, 1.0.0 is the only acceptable value."), |
---|
1054 | "InvalidParameterValue", "service"); |
---|
1055 | freeMaps (&m); |
---|
1056 | free (m); |
---|
1057 | free (REQUEST); |
---|
1058 | return 1; |
---|
1059 | } |
---|
1060 | } |
---|
1061 | } |
---|
1062 | else |
---|
1063 | { |
---|
1064 | r_inputs = getMap (request_inputs, "AcceptVersions"); |
---|
1065 | if (r_inputs != NULL) |
---|
1066 | { |
---|
1067 | if (strncmp (r_inputs->value, "1.0.0", 5) != 0) |
---|
1068 | { |
---|
1069 | errorException (m, |
---|
1070 | _ |
---|
1071 | ("Unenderstood <AcceptVersions> value, 1.0.0 is the only acceptable value."), |
---|
1072 | "VersionNegotiationFailed", NULL); |
---|
1073 | freeMaps (&m); |
---|
1074 | free (m); |
---|
1075 | free (REQUEST); |
---|
1076 | return 1; |
---|
1077 | } |
---|
1078 | } |
---|
1079 | } |
---|
1080 | |
---|
1081 | r_inputs = getMap (request_inputs, "serviceprovider"); |
---|
1082 | if (r_inputs == NULL) |
---|
1083 | { |
---|
1084 | addToMap (request_inputs, "serviceprovider", ""); |
---|
1085 | } |
---|
1086 | |
---|
1087 | maps *request_output_real_format = NULL; |
---|
1088 | map *tmpm = getMapFromMaps (m, "main", "serverAddress"); |
---|
1089 | if (tmpm != NULL) |
---|
1090 | SERVICE_URL = zStrdup (tmpm->value); |
---|
1091 | else |
---|
1092 | SERVICE_URL = zStrdup (DEFAULT_SERVICE_URL); |
---|
1093 | |
---|
1094 | service *s1; |
---|
1095 | int scount = 0; |
---|
1096 | #ifdef DEBUG |
---|
1097 | dumpMap (r_inputs); |
---|
1098 | #endif |
---|
1099 | char conf_dir[1024]; |
---|
1100 | int t; |
---|
1101 | char tmps1[1024]; |
---|
1102 | |
---|
1103 | r_inputs = NULL; |
---|
1104 | r_inputs = getMap (request_inputs, "metapath"); |
---|
1105 | if (r_inputs != NULL) |
---|
1106 | snprintf (conf_dir, 1024, "%s/%s", ntmp, r_inputs->value); |
---|
1107 | else |
---|
1108 | snprintf (conf_dir, 1024, "%s", ntmp); |
---|
1109 | |
---|
1110 | if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0) |
---|
1111 | { |
---|
1112 | #ifdef DEBUG |
---|
1113 | dumpMap (r_inputs); |
---|
1114 | #endif |
---|
1115 | xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0"); |
---|
1116 | r_inputs = NULL; |
---|
1117 | r_inputs = getMap (request_inputs, "ServiceProvider"); |
---|
1118 | xmlNodePtr n; |
---|
1119 | if (r_inputs != NULL) |
---|
1120 | n = printGetCapabilitiesHeader (doc, r_inputs->value, m); |
---|
1121 | else |
---|
1122 | n = printGetCapabilitiesHeader (doc, "", m); |
---|
1123 | /** |
---|
1124 | * Here we need to close stdout to ensure that not supported chars |
---|
1125 | * has been found in the zcfg and then printed on stdout |
---|
1126 | */ |
---|
1127 | int saved_stdout = dup (fileno (stdout)); |
---|
1128 | dup2 (fileno (stderr), fileno (stdout)); |
---|
1129 | if (int res = |
---|
1130 | recursReaddirF (m, n, conf_dir, NULL, saved_stdout, 0, |
---|
1131 | printGetCapabilitiesForProcess) < 0) |
---|
1132 | { |
---|
1133 | freeMaps (&m); |
---|
1134 | free (m); |
---|
1135 | free (REQUEST); |
---|
1136 | free (SERVICE_URL); |
---|
1137 | fflush (stdout); |
---|
1138 | return res; |
---|
1139 | } |
---|
1140 | dup2 (saved_stdout, fileno (stdout)); |
---|
1141 | printDocument (m, doc, getpid ()); |
---|
1142 | freeMaps (&m); |
---|
1143 | free (m); |
---|
1144 | free (REQUEST); |
---|
1145 | free (SERVICE_URL); |
---|
1146 | fflush (stdout); |
---|
1147 | return 0; |
---|
1148 | } |
---|
1149 | else |
---|
1150 | { |
---|
1151 | r_inputs = getMap (request_inputs, "Identifier"); |
---|
1152 | if (r_inputs == NULL |
---|
1153 | || strlen (r_inputs->name) == 0 || strlen (r_inputs->value) == 0) |
---|
1154 | { |
---|
1155 | errorException (m, _("Mandatory <identifier> was not specified"), |
---|
1156 | "MissingParameterValue", "identifier"); |
---|
1157 | freeMaps (&m); |
---|
1158 | free (m); |
---|
1159 | free (REQUEST); |
---|
1160 | free (SERVICE_URL); |
---|
1161 | return 0; |
---|
1162 | } |
---|
1163 | |
---|
1164 | struct dirent *dp; |
---|
1165 | DIR *dirp = opendir (conf_dir); |
---|
1166 | if (dirp == NULL) |
---|
1167 | { |
---|
1168 | errorException (m, _("The specified path path doesn't exist."), |
---|
1169 | "InvalidParameterValue", conf_dir); |
---|
1170 | freeMaps (&m); |
---|
1171 | free (m); |
---|
1172 | free (REQUEST); |
---|
1173 | free (SERVICE_URL); |
---|
1174 | return 0; |
---|
1175 | } |
---|
1176 | if (strncasecmp (REQUEST, "DescribeProcess", 15) == 0) |
---|
1177 | { |
---|
1178 | /** |
---|
1179 | * Loop over Identifier list |
---|
1180 | */ |
---|
1181 | xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0"); |
---|
1182 | r_inputs = NULL; |
---|
1183 | r_inputs = getMap (request_inputs, "ServiceProvider"); |
---|
1184 | |
---|
1185 | xmlNodePtr n; |
---|
1186 | if (r_inputs != NULL) |
---|
1187 | n = printDescribeProcessHeader (doc, r_inputs->value, m); |
---|
1188 | else |
---|
1189 | n = printDescribeProcessHeader (doc, "", m); |
---|
1190 | |
---|
1191 | r_inputs = getMap (request_inputs, "Identifier"); |
---|
1192 | |
---|
1193 | char *orig = zStrdup (r_inputs->value); |
---|
1194 | |
---|
1195 | int saved_stdout = dup (fileno (stdout)); |
---|
1196 | dup2 (fileno (stderr), fileno (stdout)); |
---|
1197 | if (strcasecmp ("all", orig) == 0) |
---|
1198 | { |
---|
1199 | if (int res = |
---|
1200 | recursReaddirF (m, n, conf_dir, NULL, saved_stdout, 0, |
---|
1201 | printDescribeProcessForProcess) < 0) |
---|
1202 | return res; |
---|
1203 | } |
---|
1204 | else |
---|
1205 | { |
---|
1206 | char *saveptr; |
---|
1207 | char *tmps = strtok_r (orig, ",", &saveptr); |
---|
1208 | |
---|
1209 | char buff[256]; |
---|
1210 | char buff1[1024]; |
---|
1211 | while (tmps != NULL) |
---|
1212 | { |
---|
1213 | int hasVal = -1; |
---|
1214 | char *corig = zStrdup (tmps); |
---|
1215 | if (strstr (corig, ".") != NULL) |
---|
1216 | { |
---|
1217 | |
---|
1218 | parseIdentifier (m, conf_dir, corig, buff1); |
---|
1219 | map *tmpMap = getMapFromMaps (m, "lenv", "metapath"); |
---|
1220 | if (tmpMap != NULL) |
---|
1221 | addToMap (request_inputs, "metapath", tmpMap->value); |
---|
1222 | map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier"); |
---|
1223 | |
---|
1224 | s1 = (service *) malloc (SERVICE_SIZE); |
---|
1225 | t = readServiceFile (m, buff1, &s1, tmpMapI->value); |
---|
1226 | if (t < 0) |
---|
1227 | { |
---|
1228 | map *tmp00 = getMapFromMaps (m, "lenv", "message"); |
---|
1229 | char tmp01[1024]; |
---|
1230 | if (tmp00 != NULL) |
---|
1231 | sprintf (tmp01, |
---|
1232 | _ |
---|
1233 | ("Unable to parse the ZCFG file for the following ZOO-Service: %s. Message: %s"), |
---|
1234 | tmps, tmp00->value); |
---|
1235 | else |
---|
1236 | sprintf (tmp01, |
---|
1237 | _ |
---|
1238 | ("Unable to parse the ZCFG file for the following ZOO-Service: %s."), |
---|
1239 | tmps); |
---|
1240 | dup2 (saved_stdout, fileno (stdout)); |
---|
1241 | errorException (m, tmp01, "InvalidParameterValue", |
---|
1242 | "identifier"); |
---|
1243 | freeMaps (&m); |
---|
1244 | free (m); |
---|
1245 | free (REQUEST); |
---|
1246 | free (corig); |
---|
1247 | free (orig); |
---|
1248 | free (SERVICE_URL); |
---|
1249 | free (s1); |
---|
1250 | closedir (dirp); |
---|
1251 | xmlFreeDoc (doc); |
---|
1252 | xmlCleanupParser (); |
---|
1253 | zooXmlCleanupNs (); |
---|
1254 | return 1; |
---|
1255 | } |
---|
1256 | #ifdef DEBUG |
---|
1257 | dumpService (s1); |
---|
1258 | #endif |
---|
1259 | printDescribeProcessForProcess (m, n, s1); |
---|
1260 | freeService (&s1); |
---|
1261 | free (s1); |
---|
1262 | s1 = NULL; |
---|
1263 | scount++; |
---|
1264 | hasVal = 1; |
---|
1265 | setMapInMaps (m, "lenv", "level", "0"); |
---|
1266 | } |
---|
1267 | else |
---|
1268 | { |
---|
1269 | memset (buff, 0, 256); |
---|
1270 | snprintf (buff, 256, "%s.zcfg", corig); |
---|
1271 | memset (buff1, 0, 1024); |
---|
1272 | #ifdef DEBUG |
---|
1273 | printf ("\n#######%s\n########\n", buff); |
---|
1274 | #endif |
---|
1275 | while ((dp = readdir (dirp)) != NULL) |
---|
1276 | { |
---|
1277 | if (strcasecmp (dp->d_name, buff) == 0) |
---|
1278 | { |
---|
1279 | memset (buff1, 0, 1024); |
---|
1280 | snprintf (buff1, 1024, "%s/%s", conf_dir, |
---|
1281 | dp->d_name); |
---|
1282 | s1 = (service *) malloc (SERVICE_SIZE); |
---|
1283 | if (s1 == NULL) |
---|
1284 | { |
---|
1285 | dup2 (saved_stdout, fileno (stdout)); |
---|
1286 | return errorException (m, |
---|
1287 | _ |
---|
1288 | ("Unable to allocate memory."), |
---|
1289 | "InternalError", |
---|
1290 | NULL); |
---|
1291 | } |
---|
1292 | #ifdef DEBUG |
---|
1293 | printf |
---|
1294 | ("#################\n(%s) %s\n#################\n", |
---|
1295 | r_inputs->value, buff1); |
---|
1296 | #endif |
---|
1297 | char *tmp0 = zStrdup (dp->d_name); |
---|
1298 | tmp0[strlen (tmp0) - 5] = 0; |
---|
1299 | t = readServiceFile (m, buff1, &s1, tmp0); |
---|
1300 | free (tmp0); |
---|
1301 | if (t < 0) |
---|
1302 | { |
---|
1303 | map *tmp00 = |
---|
1304 | getMapFromMaps (m, "lenv", "message"); |
---|
1305 | char tmp01[1024]; |
---|
1306 | if (tmp00 != NULL) |
---|
1307 | sprintf (tmp01, |
---|
1308 | _ |
---|
1309 | ("Unable to parse the ZCFG file: %s (%s)"), |
---|
1310 | dp->d_name, tmp00->value); |
---|
1311 | else |
---|
1312 | sprintf (tmp01, |
---|
1313 | _ |
---|
1314 | ("Unable to parse the ZCFG file: %s."), |
---|
1315 | dp->d_name); |
---|
1316 | dup2 (saved_stdout, fileno (stdout)); |
---|
1317 | errorException (m, tmp01, "InternalError", |
---|
1318 | NULL); |
---|
1319 | freeMaps (&m); |
---|
1320 | free (m); |
---|
1321 | free (orig); |
---|
1322 | free (REQUEST); |
---|
1323 | closedir (dirp); |
---|
1324 | xmlFreeDoc (doc); |
---|
1325 | xmlCleanupParser (); |
---|
1326 | zooXmlCleanupNs (); |
---|
1327 | return 1; |
---|
1328 | } |
---|
1329 | #ifdef DEBUG |
---|
1330 | dumpService (s1); |
---|
1331 | #endif |
---|
1332 | printDescribeProcessForProcess (m, n, s1); |
---|
1333 | freeService (&s1); |
---|
1334 | free (s1); |
---|
1335 | s1 = NULL; |
---|
1336 | scount++; |
---|
1337 | hasVal = 1; |
---|
1338 | } |
---|
1339 | } |
---|
1340 | } |
---|
1341 | if (hasVal < 0) |
---|
1342 | { |
---|
1343 | map *tmp00 = getMapFromMaps (m, "lenv", "message"); |
---|
1344 | char tmp01[1024]; |
---|
1345 | if (tmp00 != NULL) |
---|
1346 | sprintf (tmp01, |
---|
1347 | _("Unable to parse the ZCFG file: %s (%s)"), |
---|
1348 | buff, tmp00->value); |
---|
1349 | else |
---|
1350 | sprintf (tmp01, |
---|
1351 | _("Unable to parse the ZCFG file: %s."), |
---|
1352 | buff); |
---|
1353 | dup2 (saved_stdout, fileno (stdout)); |
---|
1354 | errorException (m, tmp01, "InvalidParameterValue", |
---|
1355 | "Identifier"); |
---|
1356 | freeMaps (&m); |
---|
1357 | free (m); |
---|
1358 | free (orig); |
---|
1359 | free (REQUEST); |
---|
1360 | closedir (dirp); |
---|
1361 | xmlFreeDoc (doc); |
---|
1362 | xmlCleanupParser (); |
---|
1363 | zooXmlCleanupNs (); |
---|
1364 | return 1; |
---|
1365 | } |
---|
1366 | rewinddir (dirp); |
---|
1367 | tmps = strtok_r (NULL, ",", &saveptr); |
---|
1368 | if (corig != NULL) |
---|
1369 | free (corig); |
---|
1370 | } |
---|
1371 | } |
---|
1372 | closedir (dirp); |
---|
1373 | fflush (stdout); |
---|
1374 | dup2 (saved_stdout, fileno (stdout)); |
---|
1375 | free (orig); |
---|
1376 | printDocument (m, doc, getpid ()); |
---|
1377 | freeMaps (&m); |
---|
1378 | free (m); |
---|
1379 | free (REQUEST); |
---|
1380 | free (SERVICE_URL); |
---|
1381 | fflush (stdout); |
---|
1382 | return 0; |
---|
1383 | } |
---|
1384 | else if (strncasecmp (REQUEST, "Execute", strlen (REQUEST)) != 0) |
---|
1385 | { |
---|
1386 | errorException (m, |
---|
1387 | _ |
---|
1388 | ("Unenderstood <request> value. Please check that it was set to GetCapabilities, DescribeProcess or Execute."), |
---|
1389 | "InvalidParameterValue", "request"); |
---|
1390 | #ifdef DEBUG |
---|
1391 | fprintf (stderr, "No request found %s", REQUEST); |
---|
1392 | #endif |
---|
1393 | closedir (dirp); |
---|
1394 | freeMaps (&m); |
---|
1395 | free (m); |
---|
1396 | free (REQUEST); |
---|
1397 | free (SERVICE_URL); |
---|
1398 | fflush (stdout); |
---|
1399 | return 0; |
---|
1400 | } |
---|
1401 | closedir (dirp); |
---|
1402 | } |
---|
1403 | |
---|
1404 | s1 = NULL; |
---|
1405 | s1 = (service *) malloc (SERVICE_SIZE); |
---|
1406 | if (s1 == NULL) |
---|
1407 | { |
---|
1408 | freeMaps (&m); |
---|
1409 | free (m); |
---|
1410 | free (REQUEST); |
---|
1411 | free (SERVICE_URL); |
---|
1412 | return errorException (m, _("Unable to allocate memory."), |
---|
1413 | "InternalError", NULL); |
---|
1414 | } |
---|
1415 | r_inputs = getMap (request_inputs, "MetaPath"); |
---|
1416 | if (r_inputs != NULL) |
---|
1417 | snprintf (tmps1, 1024, "%s/%s", ntmp, r_inputs->value); |
---|
1418 | else |
---|
1419 | snprintf (tmps1, 1024, "%s/", ntmp); |
---|
1420 | r_inputs = getMap (request_inputs, "Identifier"); |
---|
1421 | char *ttmp = zStrdup (tmps1); |
---|
1422 | snprintf (tmps1, 1024, "%s/%s.zcfg", ttmp, r_inputs->value); |
---|
1423 | free (ttmp); |
---|
1424 | #ifdef DEBUG |
---|
1425 | fprintf (stderr, "Trying to load %s\n", tmps1); |
---|
1426 | #endif |
---|
1427 | if (strstr (r_inputs->value, ".") != NULL) |
---|
1428 | { |
---|
1429 | char *identifier = zStrdup (r_inputs->value); |
---|
1430 | parseIdentifier (m, conf_dir, identifier, tmps1); |
---|
1431 | map *tmpMap = getMapFromMaps (m, "lenv", "metapath"); |
---|
1432 | if (tmpMap != NULL) |
---|
1433 | addToMap (request_inputs, "metapath", tmpMap->value); |
---|
1434 | free (identifier); |
---|
1435 | } |
---|
1436 | else |
---|
1437 | { |
---|
1438 | setMapInMaps (m, "lenv", "Identifier", r_inputs->value); |
---|
1439 | setMapInMaps (m, "lenv", "oIdentifier", r_inputs->value); |
---|
1440 | } |
---|
1441 | |
---|
1442 | r_inputs = getMapFromMaps (m, "lenv", "Identifier"); |
---|
1443 | int saved_stdout = dup (fileno (stdout)); |
---|
1444 | dup2 (fileno (stderr), fileno (stdout)); |
---|
1445 | t = readServiceFile (m, tmps1, &s1, r_inputs->value); |
---|
1446 | fflush (stdout); |
---|
1447 | dup2 (saved_stdout, fileno (stdout)); |
---|
1448 | if (t < 0) |
---|
1449 | { |
---|
1450 | char *tmpMsg = (char *) malloc (2048 + strlen (r_inputs->value)); |
---|
1451 | sprintf (tmpMsg, |
---|
1452 | _ |
---|
1453 | ("The value for <indetifier> seems to be wrong (%s). Please, ensure that the process exist using the GetCapabilities request."), |
---|
1454 | r_inputs->value); |
---|
1455 | errorException (m, tmpMsg, "InvalidParameterValue", "identifier"); |
---|
1456 | free (tmpMsg); |
---|
1457 | free (s1); |
---|
1458 | freeMaps (&m); |
---|
1459 | free (m); |
---|
1460 | free (REQUEST); |
---|
1461 | free (SERVICE_URL); |
---|
1462 | return 0; |
---|
1463 | } |
---|
1464 | close (saved_stdout); |
---|
1465 | |
---|
1466 | #ifdef DEBUG |
---|
1467 | dumpService (s1); |
---|
1468 | #endif |
---|
1469 | int j; |
---|
1470 | |
---|
1471 | |
---|
1472 | /** |
---|
1473 | * Create the input and output maps data structure |
---|
1474 | */ |
---|
1475 | int i = 0; |
---|
1476 | HINTERNET hInternet; |
---|
1477 | HINTERNET res; |
---|
1478 | hInternet = InternetOpen ( |
---|
1479 | #ifndef WIN32 |
---|
1480 | (LPCTSTR) |
---|
1481 | #endif |
---|
1482 | "ZooWPSClient\0", |
---|
1483 | INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); |
---|
1484 | |
---|
1485 | #ifndef WIN32 |
---|
1486 | if (!CHECK_INET_HANDLE (hInternet)) |
---|
1487 | fprintf (stderr, "WARNING : hInternet handle failed to initialize"); |
---|
1488 | #endif |
---|
1489 | maps *request_input_real_format = NULL; |
---|
1490 | maps *tmpmaps = request_input_real_format; |
---|
1491 | map *postRequest = NULL; |
---|
1492 | postRequest = getMap (request_inputs, "xrequest"); |
---|
1493 | if (postRequest == NULLMAP) |
---|
1494 | { |
---|
1495 | /** |
---|
1496 | * Parsing outputs provided as KVP |
---|
1497 | */ |
---|
1498 | r_inputs = NULL; |
---|
1499 | #ifdef DEBUG |
---|
1500 | fprintf (stderr, "OUTPUT Parsing ... \n"); |
---|
1501 | #endif |
---|
1502 | r_inputs = getMap (request_inputs, "ResponseDocument"); |
---|
1503 | if (r_inputs == NULL) |
---|
1504 | r_inputs = getMap (request_inputs, "RawDataOutput"); |
---|
1505 | |
---|
1506 | #ifdef DEBUG |
---|
1507 | fprintf (stderr, "OUTPUT Parsing ... \n"); |
---|
1508 | #endif |
---|
1509 | if (r_inputs != NULL) |
---|
1510 | { |
---|
1511 | #ifdef DEBUG |
---|
1512 | fprintf (stderr, "OUTPUT Parsing start now ... \n"); |
---|
1513 | #endif |
---|
1514 | char cursor_output[10240]; |
---|
1515 | char *cotmp = zStrdup (r_inputs->value); |
---|
1516 | snprintf (cursor_output, 10240, "%s", cotmp); |
---|
1517 | free (cotmp); |
---|
1518 | j = 0; |
---|
1519 | |
---|
1520 | /** |
---|
1521 | * Put each Output into the outputs_as_text array |
---|
1522 | */ |
---|
1523 | char *pToken; |
---|
1524 | maps *tmp_output = NULL; |
---|
1525 | #ifdef DEBUG |
---|
1526 | fprintf (stderr, "OUTPUT [%s]\n", cursor_output); |
---|
1527 | #endif |
---|
1528 | pToken = strtok (cursor_output, ";"); |
---|
1529 | char **outputs_as_text = (char **) malloc (128 * sizeof (char *)); |
---|
1530 | if (outputs_as_text == NULL) |
---|
1531 | { |
---|
1532 | return errorException (m, _("Unable to allocate memory"), |
---|
1533 | "InternalError", NULL); |
---|
1534 | } |
---|
1535 | i = 0; |
---|
1536 | while (pToken != NULL) |
---|
1537 | { |
---|
1538 | #ifdef DEBUG |
---|
1539 | fprintf (stderr, "***%s***\n", pToken); |
---|
1540 | fflush (stderr); |
---|
1541 | fprintf (stderr, "***%s***\n", pToken); |
---|
1542 | #endif |
---|
1543 | outputs_as_text[i] = |
---|
1544 | (char *) malloc ((strlen (pToken) + 1) * sizeof (char)); |
---|
1545 | if (outputs_as_text[i] == NULL) |
---|
1546 | { |
---|
1547 | return errorException (m, _("Unable to allocate memory"), |
---|
1548 | "InternalError", NULL); |
---|
1549 | } |
---|
1550 | snprintf (outputs_as_text[i], strlen (pToken) + 1, "%s", |
---|
1551 | pToken); |
---|
1552 | pToken = strtok (NULL, ";"); |
---|
1553 | i++; |
---|
1554 | } |
---|
1555 | for (j = 0; j < i; j++) |
---|
1556 | { |
---|
1557 | char *tmp = zStrdup (outputs_as_text[j]); |
---|
1558 | free (outputs_as_text[j]); |
---|
1559 | char *tmpc; |
---|
1560 | tmpc = strtok (tmp, "@"); |
---|
1561 | int k = 0; |
---|
1562 | while (tmpc != NULL) |
---|
1563 | { |
---|
1564 | if (k == 0) |
---|
1565 | { |
---|
1566 | if (tmp_output == NULL) |
---|
1567 | { |
---|
1568 | tmp_output = (maps *) malloc (MAPS_SIZE); |
---|
1569 | if (tmp_output == NULL) |
---|
1570 | { |
---|
1571 | return errorException (m, |
---|
1572 | _ |
---|
1573 | ("Unable to allocate memory."), |
---|
1574 | "InternalError", NULL); |
---|
1575 | } |
---|
1576 | tmp_output->name = zStrdup (tmpc); |
---|
1577 | tmp_output->content = NULL; |
---|
1578 | tmp_output->next = NULL; |
---|
1579 | } |
---|
1580 | } |
---|
1581 | else |
---|
1582 | { |
---|
1583 | char *tmpv = strstr (tmpc, "="); |
---|
1584 | char tmpn[256]; |
---|
1585 | memset (tmpn, 0, 256); |
---|
1586 | strncpy (tmpn, tmpc, |
---|
1587 | (strlen (tmpc) - |
---|
1588 | strlen (tmpv)) * sizeof (char)); |
---|
1589 | tmpn[strlen (tmpc) - strlen (tmpv)] = 0; |
---|
1590 | #ifdef DEBUG |
---|
1591 | fprintf (stderr, "OUTPUT DEF [%s]=[%s]\n", tmpn, |
---|
1592 | tmpv + 1); |
---|
1593 | #endif |
---|
1594 | if (tmp_output->content == NULL) |
---|
1595 | { |
---|
1596 | tmp_output->content = createMap (tmpn, tmpv + 1); |
---|
1597 | tmp_output->content->next = NULL; |
---|
1598 | } |
---|
1599 | else |
---|
1600 | addToMap (tmp_output->content, tmpn, tmpv + 1); |
---|
1601 | } |
---|
1602 | k++; |
---|
1603 | #ifdef DEBUG |
---|
1604 | fprintf (stderr, "***%s***\n", tmpc); |
---|
1605 | #endif |
---|
1606 | tmpc = strtok (NULL, "@"); |
---|
1607 | } |
---|
1608 | if (request_output_real_format == NULL) |
---|
1609 | request_output_real_format = dupMaps (&tmp_output); |
---|
1610 | else |
---|
1611 | addMapsToMaps (&request_output_real_format, tmp_output); |
---|
1612 | freeMaps (&tmp_output); |
---|
1613 | free (tmp_output); |
---|
1614 | tmp_output = NULL; |
---|
1615 | #ifdef DEBUG |
---|
1616 | dumpMaps (tmp_output); |
---|
1617 | fflush (stderr); |
---|
1618 | #endif |
---|
1619 | free (tmp); |
---|
1620 | } |
---|
1621 | free (outputs_as_text); |
---|
1622 | } |
---|
1623 | |
---|
1624 | |
---|
1625 | /** |
---|
1626 | * Parsing inputs provided as KVP |
---|
1627 | */ |
---|
1628 | r_inputs = getMap (request_inputs, "DataInputs"); |
---|
1629 | #ifdef DEBUG |
---|
1630 | fprintf (stderr, "DATA INPUTS [%s]\n", r_inputs->value); |
---|
1631 | #endif |
---|
1632 | char cursor_input[40960]; |
---|
1633 | if (r_inputs != NULL) |
---|
1634 | snprintf (cursor_input, 40960, "%s", r_inputs->value); |
---|
1635 | else |
---|
1636 | { |
---|
1637 | errorException (m, _("Parameter <DataInputs> was not specified"), |
---|
1638 | "MissingParameterValue", "DataInputs"); |
---|
1639 | freeMaps (&m); |
---|
1640 | free (m); |
---|
1641 | free (REQUEST); |
---|
1642 | free (SERVICE_URL); |
---|
1643 | InternetCloseHandle (&hInternet); |
---|
1644 | freeService (&s1); |
---|
1645 | free (s1); |
---|
1646 | return 0; |
---|
1647 | } |
---|
1648 | j = 0; |
---|
1649 | |
---|
1650 | /** |
---|
1651 | * Put each DataInputs into the inputs_as_text array |
---|
1652 | */ |
---|
1653 | char *tmp1 = zStrdup (cursor_input); |
---|
1654 | char *pToken; |
---|
1655 | pToken = strtok (cursor_input, ";"); |
---|
1656 | if (pToken != NULL && strncasecmp (pToken, tmp1, strlen (tmp1)) == 0) |
---|
1657 | { |
---|
1658 | char *tmp2 = url_decode (tmp1); |
---|
1659 | snprintf (cursor_input, (strlen (tmp2) + 1) * sizeof (char), "%s", |
---|
1660 | tmp2); |
---|
1661 | free (tmp2); |
---|
1662 | pToken = strtok (cursor_input, ";"); |
---|
1663 | } |
---|
1664 | free (tmp1); |
---|
1665 | |
---|
1666 | char **inputs_as_text = (char **) malloc (100 * sizeof (char *)); |
---|
1667 | if (inputs_as_text == NULL) |
---|
1668 | { |
---|
1669 | return errorException (m, _("Unable to allocate memory."), |
---|
1670 | "InternalError", NULL); |
---|
1671 | } |
---|
1672 | i = 0; |
---|
1673 | while (pToken != NULL) |
---|
1674 | { |
---|
1675 | #ifdef DEBUG |
---|
1676 | fprintf (stderr, "***%s***\n", pToken); |
---|
1677 | #endif |
---|
1678 | fflush (stderr); |
---|
1679 | #ifdef DEBUG |
---|
1680 | fprintf (stderr, "***%s***\n", pToken); |
---|
1681 | #endif |
---|
1682 | inputs_as_text[i] = |
---|
1683 | (char *) malloc ((strlen (pToken) + 1) * sizeof (char)); |
---|
1684 | snprintf (inputs_as_text[i], strlen (pToken) + 1, "%s", pToken); |
---|
1685 | if (inputs_as_text[i] == NULL) |
---|
1686 | { |
---|
1687 | return errorException (m, _("Unable to allocate memory."), |
---|
1688 | "InternalError", NULL); |
---|
1689 | } |
---|
1690 | pToken = strtok (NULL, ";"); |
---|
1691 | i++; |
---|
1692 | } |
---|
1693 | |
---|
1694 | for (j = 0; j < i; j++) |
---|
1695 | { |
---|
1696 | char *tmp = zStrdup (inputs_as_text[j]); |
---|
1697 | free (inputs_as_text[j]); |
---|
1698 | char *tmpc; |
---|
1699 | tmpc = strtok (tmp, "@"); |
---|
1700 | while (tmpc != NULL) |
---|
1701 | { |
---|
1702 | #ifdef DEBUG |
---|
1703 | fprintf (stderr, "***\n***%s***\n", tmpc); |
---|
1704 | #endif |
---|
1705 | char *tmpv = strstr (tmpc, "="); |
---|
1706 | char tmpn[256]; |
---|
1707 | memset (tmpn, 0, 256); |
---|
1708 | if (tmpv != NULL) |
---|
1709 | { |
---|
1710 | strncpy (tmpn, tmpc, |
---|
1711 | (strlen (tmpc) - strlen (tmpv)) * sizeof (char)); |
---|
1712 | tmpn[strlen (tmpc) - strlen (tmpv)] = 0; |
---|
1713 | } |
---|
1714 | else |
---|
1715 | { |
---|
1716 | strncpy (tmpn, tmpc, strlen (tmpc) * sizeof (char)); |
---|
1717 | tmpn[strlen (tmpc)] = 0; |
---|
1718 | } |
---|
1719 | #ifdef DEBUG |
---|
1720 | fprintf (stderr, "***\n*** %s = %s ***\n", tmpn, tmpv + 1); |
---|
1721 | #endif |
---|
1722 | if (tmpmaps == NULL) |
---|
1723 | { |
---|
1724 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
1725 | if (tmpmaps == NULL) |
---|
1726 | { |
---|
1727 | return errorException (m, |
---|
1728 | _("Unable to allocate memory."), |
---|
1729 | "InternalError", NULL); |
---|
1730 | } |
---|
1731 | tmpmaps->name = zStrdup (tmpn); |
---|
1732 | if (tmpv != NULL) |
---|
1733 | { |
---|
1734 | char *tmpvf = url_decode (tmpv + 1); |
---|
1735 | tmpmaps->content = createMap ("value", tmpvf); |
---|
1736 | free (tmpvf); |
---|
1737 | } |
---|
1738 | else |
---|
1739 | tmpmaps->content = createMap ("value", "Reference"); |
---|
1740 | tmpmaps->next = NULL; |
---|
1741 | } |
---|
1742 | tmpc = strtok (NULL, "@"); |
---|
1743 | while (tmpc != NULL) |
---|
1744 | { |
---|
1745 | #ifdef DEBUG |
---|
1746 | fprintf (stderr, "*** KVP NON URL-ENCODED \n***%s***\n", |
---|
1747 | tmpc); |
---|
1748 | #endif |
---|
1749 | char *tmpv1 = strstr (tmpc, "="); |
---|
1750 | #ifdef DEBUG |
---|
1751 | fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n", |
---|
1752 | tmpv1 + 1); |
---|
1753 | #endif |
---|
1754 | char tmpn1[1024]; |
---|
1755 | memset (tmpn1, 0, 1024); |
---|
1756 | if (tmpv1 != NULL) |
---|
1757 | { |
---|
1758 | strncpy (tmpn1, tmpc, strlen (tmpc) - strlen (tmpv1)); |
---|
1759 | tmpn1[strlen (tmpc) - strlen (tmpv1)] = 0; |
---|
1760 | addToMap (tmpmaps->content, tmpn1, tmpv1 + 1); |
---|
1761 | } |
---|
1762 | else |
---|
1763 | { |
---|
1764 | strncpy (tmpn1, tmpc, strlen (tmpc)); |
---|
1765 | tmpn1[strlen (tmpc)] = 0; |
---|
1766 | map *lmap = getLastMap (tmpmaps->content); |
---|
1767 | char *tmpValue = |
---|
1768 | (char *) malloc ((strlen (tmpv) + strlen (tmpc) + 1) * |
---|
1769 | sizeof (char)); |
---|
1770 | sprintf (tmpValue, "%s@%s", tmpv + 1, tmpc); |
---|
1771 | free (lmap->value); |
---|
1772 | lmap->value = zStrdup (tmpValue); |
---|
1773 | free (tmpValue); |
---|
1774 | tmpc = strtok (NULL, "@"); |
---|
1775 | continue; |
---|
1776 | } |
---|
1777 | #ifdef DEBUG |
---|
1778 | fprintf (stderr, "*** NAME NON URL-ENCODED \n***%s***\n", |
---|
1779 | tmpn1); |
---|
1780 | fprintf (stderr, "*** VALUE NON URL-ENCODED \n***%s***\n", |
---|
1781 | tmpv1 + 1); |
---|
1782 | #endif |
---|
1783 | if (strcmp (tmpn1, "xlink:href") != 0) |
---|
1784 | addToMap (tmpmaps->content, tmpn1, tmpv1 + 1); |
---|
1785 | else if (tmpv1 != NULL) |
---|
1786 | { |
---|
1787 | char *tmpx2 = url_decode (tmpv1 + 1); |
---|
1788 | if (strncasecmp (tmpx2, "http://", 7) != 0 && |
---|
1789 | strncasecmp (tmpx2, "ftp://", 6) != 0 && |
---|
1790 | strncasecmp (tmpx2, "https://", 8) != 0) |
---|
1791 | { |
---|
1792 | char emsg[1024]; |
---|
1793 | sprintf (emsg, |
---|
1794 | _ |
---|
1795 | ("Unable to find a valid protocol to download the remote file %s"), |
---|
1796 | tmpv1 + 1); |
---|
1797 | errorException (m, emsg, "InternalError", NULL); |
---|
1798 | freeMaps (&m); |
---|
1799 | free (m); |
---|
1800 | free (REQUEST); |
---|
1801 | free (SERVICE_URL); |
---|
1802 | InternetCloseHandle (&hInternet); |
---|
1803 | freeService (&s1); |
---|
1804 | free (s1); |
---|
1805 | return 0; |
---|
1806 | } |
---|
1807 | #ifdef DEBUG |
---|
1808 | fprintf (stderr, |
---|
1809 | "REQUIRE TO DOWNLOAD A FILE FROM A SERVER : url(%s)\n", |
---|
1810 | tmpv1 + 1); |
---|
1811 | #endif |
---|
1812 | addToMap (tmpmaps->content, tmpn1, tmpx2); |
---|
1813 | #ifndef WIN32 |
---|
1814 | if (CHECK_INET_HANDLE (hInternet)) |
---|
1815 | #endif |
---|
1816 | { |
---|
1817 | if (loadRemoteFile |
---|
1818 | (&m, &tmpmaps->content, &hInternet, tmpx2) < 0) |
---|
1819 | { |
---|
1820 | freeMaps (&m); |
---|
1821 | free (m); |
---|
1822 | free (REQUEST); |
---|
1823 | free (SERVICE_URL); |
---|
1824 | InternetCloseHandle (&hInternet); |
---|
1825 | freeService (&s1); |
---|
1826 | free (s1); |
---|
1827 | return 0; |
---|
1828 | } |
---|
1829 | } |
---|
1830 | free (tmpx2); |
---|
1831 | addToMap (tmpmaps->content, "Reference", tmpv1 + 1); |
---|
1832 | } |
---|
1833 | tmpc = strtok (NULL, "@"); |
---|
1834 | } |
---|
1835 | #ifdef DEBUG |
---|
1836 | dumpMaps (tmpmaps); |
---|
1837 | fflush (stderr); |
---|
1838 | #endif |
---|
1839 | if (request_input_real_format == NULL) |
---|
1840 | request_input_real_format = dupMaps (&tmpmaps); |
---|
1841 | else |
---|
1842 | { |
---|
1843 | maps *testPresence = |
---|
1844 | getMaps (request_input_real_format, tmpmaps->name); |
---|
1845 | if (testPresence != NULL) |
---|
1846 | { |
---|
1847 | elements *elem = |
---|
1848 | getElements (s1->inputs, tmpmaps->name); |
---|
1849 | if (elem != NULL) |
---|
1850 | { |
---|
1851 | if (appendMapsToMaps |
---|
1852 | (m, request_input_real_format, tmpmaps, |
---|
1853 | elem) < 0) |
---|
1854 | { |
---|
1855 | freeMaps (&m); |
---|
1856 | free (m); |
---|
1857 | free (REQUEST); |
---|
1858 | free (SERVICE_URL); |
---|
1859 | InternetCloseHandle (&hInternet); |
---|
1860 | freeService (&s1); |
---|
1861 | free (s1); |
---|
1862 | return 0; |
---|
1863 | } |
---|
1864 | } |
---|
1865 | } |
---|
1866 | else |
---|
1867 | addMapsToMaps (&request_input_real_format, tmpmaps); |
---|
1868 | } |
---|
1869 | freeMaps (&tmpmaps); |
---|
1870 | free (tmpmaps); |
---|
1871 | tmpmaps = NULL; |
---|
1872 | free (tmp); |
---|
1873 | } |
---|
1874 | } |
---|
1875 | free (inputs_as_text); |
---|
1876 | } |
---|
1877 | else |
---|
1878 | { |
---|
1879 | /** |
---|
1880 | * Parse XML request |
---|
1881 | */ |
---|
1882 | xmlInitParser (); |
---|
1883 | #ifdef DEBUG |
---|
1884 | fflush (stderr); |
---|
1885 | fprintf (stderr, "BEFORE %s\n", postRequest->value); |
---|
1886 | fflush (stderr); |
---|
1887 | #endif |
---|
1888 | xmlDocPtr doc = xmlParseMemory (postRequest->value, cgiContentLength); |
---|
1889 | #ifdef DEBUG |
---|
1890 | fprintf (stderr, "AFTER\n"); |
---|
1891 | fflush (stderr); |
---|
1892 | #endif |
---|
1893 | /** |
---|
1894 | * Parse every Input in DataInputs node. |
---|
1895 | */ |
---|
1896 | xmlXPathObjectPtr tmpsptr = |
---|
1897 | extractFromDoc (doc, "/*/*/*[local-name()='Input']"); |
---|
1898 | xmlNodeSet *tmps = tmpsptr->nodesetval; |
---|
1899 | #ifdef DEBUG |
---|
1900 | fprintf (stderr, "*****%d*****\n", tmps->nodeNr); |
---|
1901 | #endif |
---|
1902 | for (int k = 0; k < tmps->nodeNr; k++) |
---|
1903 | { |
---|
1904 | maps *tmpmaps = NULL; |
---|
1905 | xmlNodePtr cur = tmps->nodeTab[k]; |
---|
1906 | if (tmps->nodeTab[k]->type == XML_ELEMENT_NODE) |
---|
1907 | { |
---|
1908 | /** |
---|
1909 | * A specific Input node. |
---|
1910 | */ |
---|
1911 | #ifdef DEBUG |
---|
1912 | fprintf (stderr, "= element 0 node \"%s\"\n", cur->name); |
---|
1913 | #endif |
---|
1914 | xmlNodePtr cur2 = cur->children; |
---|
1915 | while (cur2 != NULL) |
---|
1916 | { |
---|
1917 | while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE) |
---|
1918 | cur2 = cur2->next; |
---|
1919 | if (cur2 == NULL) |
---|
1920 | break; |
---|
1921 | /** |
---|
1922 | * Indentifier |
---|
1923 | */ |
---|
1924 | if (xmlStrncasecmp |
---|
1925 | (cur2->name, BAD_CAST "Identifier", |
---|
1926 | xmlStrlen (cur2->name)) == 0) |
---|
1927 | { |
---|
1928 | xmlChar *val = |
---|
1929 | xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1); |
---|
1930 | if (tmpmaps == NULL) |
---|
1931 | { |
---|
1932 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
1933 | if (tmpmaps == NULL) |
---|
1934 | { |
---|
1935 | return errorException (m, |
---|
1936 | _ |
---|
1937 | ("Unable to allocate memory."), |
---|
1938 | "InternalError", NULL); |
---|
1939 | } |
---|
1940 | tmpmaps->name = zStrdup ((char *) val); |
---|
1941 | tmpmaps->content = NULL; |
---|
1942 | tmpmaps->next = NULL; |
---|
1943 | } |
---|
1944 | xmlFree (val); |
---|
1945 | } |
---|
1946 | /** |
---|
1947 | * Title, Asbtract |
---|
1948 | */ |
---|
1949 | if (xmlStrncasecmp |
---|
1950 | (cur2->name, BAD_CAST "Title", |
---|
1951 | xmlStrlen (cur2->name)) == 0 |
---|
1952 | || xmlStrncasecmp (cur2->name, BAD_CAST "Abstract", |
---|
1953 | xmlStrlen (cur2->name)) == 0) |
---|
1954 | { |
---|
1955 | xmlChar *val = |
---|
1956 | xmlNodeListGetString (doc, cur2->xmlChildrenNode, 1); |
---|
1957 | if (tmpmaps == NULL) |
---|
1958 | { |
---|
1959 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
1960 | if (tmpmaps == NULL) |
---|
1961 | { |
---|
1962 | return errorException (m, |
---|
1963 | _ |
---|
1964 | ("Unable to allocate memory."), |
---|
1965 | "InternalError", NULL); |
---|
1966 | } |
---|
1967 | tmpmaps->name = zStrdup ("missingIndetifier"); |
---|
1968 | tmpmaps->content = |
---|
1969 | createMap ((char *) cur2->name, (char *) val); |
---|
1970 | tmpmaps->next = NULL; |
---|
1971 | } |
---|
1972 | else |
---|
1973 | { |
---|
1974 | if (tmpmaps->content != NULL) |
---|
1975 | addToMap (tmpmaps->content, |
---|
1976 | (char *) cur2->name, (char *) val); |
---|
1977 | else |
---|
1978 | tmpmaps->content = |
---|
1979 | createMap ((char *) cur2->name, (char *) val); |
---|
1980 | } |
---|
1981 | #ifdef DEBUG |
---|
1982 | dumpMaps (tmpmaps); |
---|
1983 | #endif |
---|
1984 | xmlFree (val); |
---|
1985 | } |
---|
1986 | /** |
---|
1987 | * InputDataFormChoice (Reference or Data ?) |
---|
1988 | */ |
---|
1989 | if (xmlStrcasecmp (cur2->name, BAD_CAST "Reference") == 0) |
---|
1990 | { |
---|
1991 | /** |
---|
1992 | * Get every attribute from a Reference node |
---|
1993 | * mimeType, encoding, schema, href, method |
---|
1994 | * Header and Body gesture should be added here |
---|
1995 | */ |
---|
1996 | #ifdef DEBUG |
---|
1997 | fprintf (stderr, "REFERENCE\n"); |
---|
1998 | #endif |
---|
1999 | const char *refs[5] = |
---|
2000 | { "mimeType", "encoding", "schema", "method", |
---|
2001 | "href" |
---|
2002 | }; |
---|
2003 | for (int l = 0; l < 5; l++) |
---|
2004 | { |
---|
2005 | #ifdef DEBUG |
---|
2006 | fprintf (stderr, "*** %s ***", refs[l]); |
---|
2007 | #endif |
---|
2008 | xmlChar *val = xmlGetProp (cur2, BAD_CAST refs[l]); |
---|
2009 | if (val != NULL && xmlStrlen (val) > 0) |
---|
2010 | { |
---|
2011 | if (tmpmaps->content != NULL) |
---|
2012 | addToMap (tmpmaps->content, refs[l], |
---|
2013 | (char *) val); |
---|
2014 | else |
---|
2015 | tmpmaps->content = |
---|
2016 | createMap (refs[l], (char *) val); |
---|
2017 | map *ltmp = getMap (tmpmaps->content, "method"); |
---|
2018 | if (l == 4) |
---|
2019 | { |
---|
2020 | if (! |
---|
2021 | (ltmp != NULL |
---|
2022 | && strncmp (ltmp->value, "POST", |
---|
2023 | 4) == 0) |
---|
2024 | && CHECK_INET_HANDLE (hInternet)) |
---|
2025 | { |
---|
2026 | if (loadRemoteFile |
---|
2027 | (&m, &tmpmaps->content, &hInternet, |
---|
2028 | (char *) val) != 0) |
---|
2029 | { |
---|
2030 | freeMaps (&m); |
---|
2031 | free (m); |
---|
2032 | free (REQUEST); |
---|
2033 | free (SERVICE_URL); |
---|
2034 | InternetCloseHandle (&hInternet); |
---|
2035 | freeService (&s1); |
---|
2036 | free (s1); |
---|
2037 | return 0; |
---|
2038 | } |
---|
2039 | } |
---|
2040 | } |
---|
2041 | } |
---|
2042 | #ifdef DEBUG |
---|
2043 | fprintf (stderr, "%s\n", val); |
---|
2044 | #endif |
---|
2045 | xmlFree (val); |
---|
2046 | } |
---|
2047 | #ifdef POST_DEBUG |
---|
2048 | fprintf (stderr, |
---|
2049 | "Parse Header and Body from Reference \n"); |
---|
2050 | #endif |
---|
2051 | xmlNodePtr cur3 = cur2->children; |
---|
2052 | /* HINTERNET hInternetP; |
---|
2053 | hInternetP=InternetOpen( |
---|
2054 | #ifndef WIN32 |
---|
2055 | (LPCTSTR) |
---|
2056 | #endif |
---|
2057 | "ZooWPSClient\0", |
---|
2058 | INTERNET_OPEN_TYPE_PRECONFIG, |
---|
2059 | NULL,NULL, 0); */ |
---|
2060 | //hInternet.ihandle[hInternet.nb].header=NULL; |
---|
2061 | while (cur3 != NULL) |
---|
2062 | { |
---|
2063 | while (cur3 != NULL |
---|
2064 | && cur3->type != XML_ELEMENT_NODE) |
---|
2065 | cur3 = cur3->next; |
---|
2066 | if (cur3 == NULL) |
---|
2067 | break; |
---|
2068 | if (xmlStrcasecmp (cur3->name, BAD_CAST "Header") == |
---|
2069 | 0) |
---|
2070 | { |
---|
2071 | const char *ha[2]; |
---|
2072 | ha[0] = "key"; |
---|
2073 | ha[1] = "value"; |
---|
2074 | int hai; |
---|
2075 | char *has; |
---|
2076 | char *key; |
---|
2077 | for (hai = 0; hai < 2; hai++) |
---|
2078 | { |
---|
2079 | xmlChar *val = |
---|
2080 | xmlGetProp (cur3, BAD_CAST ha[hai]); |
---|
2081 | #ifdef POST_DEBUG |
---|
2082 | fprintf (stderr, "%s = %s\n", ha[hai], |
---|
2083 | (char *) val); |
---|
2084 | #endif |
---|
2085 | if (hai == 0) |
---|
2086 | { |
---|
2087 | key = zStrdup ((char *) val); |
---|
2088 | } |
---|
2089 | else |
---|
2090 | { |
---|
2091 | has = |
---|
2092 | (char *) |
---|
2093 | malloc ((4 + xmlStrlen (val) + |
---|
2094 | strlen (key)) * |
---|
2095 | sizeof (char)); |
---|
2096 | if (has == NULL) |
---|
2097 | { |
---|
2098 | return errorException (m, |
---|
2099 | _ |
---|
2100 | ("Unable to allocate memory."), |
---|
2101 | "InternalError", |
---|
2102 | NULL); |
---|
2103 | } |
---|
2104 | snprintf (has, |
---|
2105 | (3 + xmlStrlen (val) + |
---|
2106 | strlen (key)), "%s: %s", key, |
---|
2107 | (char *) val); |
---|
2108 | free (key); |
---|
2109 | #ifdef POST_DEBUG |
---|
2110 | fprintf (stderr, "%s\n", has); |
---|
2111 | #endif |
---|
2112 | } |
---|
2113 | xmlFree (val); |
---|
2114 | } |
---|
2115 | hInternet.ihandle[hInternet.nb].header = |
---|
2116 | curl_slist_append (hInternet.ihandle |
---|
2117 | [hInternet.nb].header, |
---|
2118 | has); |
---|
2119 | if (has != NULL) |
---|
2120 | free (has); |
---|
2121 | } |
---|
2122 | else |
---|
2123 | { |
---|
2124 | #ifdef POST_DEBUG |
---|
2125 | fprintf (stderr, |
---|
2126 | "Try to fetch the body part of the request ...\n"); |
---|
2127 | #endif |
---|
2128 | if (xmlStrcasecmp (cur3->name, BAD_CAST "Body") |
---|
2129 | == 0) |
---|
2130 | { |
---|
2131 | #ifdef POST_DEBUG |
---|
2132 | fprintf (stderr, "Body part found !!!\n", |
---|
2133 | (char *) cur3->content); |
---|
2134 | #endif |
---|
2135 | char *tmp = |
---|
2136 | (char *) malloc (cgiContentLength + |
---|
2137 | 1 * sizeof (char)); |
---|
2138 | memset (tmp, 0, cgiContentLength); |
---|
2139 | xmlNodePtr cur4 = cur3->children; |
---|
2140 | while (cur4 != NULL) |
---|
2141 | { |
---|
2142 | while (cur4->type != XML_ELEMENT_NODE) |
---|
2143 | cur4 = cur4->next; |
---|
2144 | xmlDocPtr bdoc = |
---|
2145 | xmlNewDoc (BAD_CAST "1.0"); |
---|
2146 | bdoc->encoding = |
---|
2147 | xmlCharStrdup ("UTF-8"); |
---|
2148 | xmlDocSetRootElement (bdoc, cur4); |
---|
2149 | xmlChar *btmps; |
---|
2150 | int bsize; |
---|
2151 | xmlDocDumpMemory (bdoc, &btmps, &bsize); |
---|
2152 | #ifdef POST_DEBUG |
---|
2153 | fprintf (stderr, |
---|
2154 | "Body part found !!! %s %s\n", |
---|
2155 | tmp, (char *) btmps); |
---|
2156 | #endif |
---|
2157 | if (btmps != NULL) |
---|
2158 | sprintf (tmp, "%s", (char *) btmps); |
---|
2159 | xmlFree (btmps); |
---|
2160 | cur4 = cur4->next; |
---|
2161 | xmlFreeDoc (bdoc); |
---|
2162 | } |
---|
2163 | map *btmp = |
---|
2164 | getMap (tmpmaps->content, "href"); |
---|
2165 | if (btmp != NULL) |
---|
2166 | { |
---|
2167 | #ifdef POST_DEBUG |
---|
2168 | fprintf (stderr, "%s %s\n", btmp->value, |
---|
2169 | tmp); |
---|
2170 | curl_easy_setopt (hInternet.handle, |
---|
2171 | CURLOPT_VERBOSE, 1); |
---|
2172 | #endif |
---|
2173 | hInternet. |
---|
2174 | waitingRequests[hInternet.nb] = |
---|
2175 | strdup (tmp); |
---|
2176 | InternetOpenUrl (&hInternet, |
---|
2177 | btmp->value, |
---|
2178 | hInternet.waitingRequests |
---|
2179 | [hInternet.nb], |
---|
2180 | strlen |
---|
2181 | (hInternet.waitingRequests |
---|
2182 | [hInternet.nb]), |
---|
2183 | INTERNET_FLAG_NO_CACHE_WRITE, |
---|
2184 | 0); |
---|
2185 | } |
---|
2186 | free (tmp); |
---|
2187 | } |
---|
2188 | else |
---|
2189 | if (xmlStrcasecmp |
---|
2190 | (cur3->name, |
---|
2191 | BAD_CAST "BodyReference") == 0) |
---|
2192 | { |
---|
2193 | xmlChar *val = |
---|
2194 | xmlGetProp (cur3, BAD_CAST "href"); |
---|
2195 | HINTERNET bInternet, res1; |
---|
2196 | bInternet = InternetOpen ( |
---|
2197 | #ifndef WIN32 |
---|
2198 | (LPCTSTR) |
---|
2199 | #endif |
---|
2200 | "ZooWPSClient\0", |
---|
2201 | INTERNET_OPEN_TYPE_PRECONFIG, |
---|
2202 | NULL, NULL, 0); |
---|
2203 | if (!CHECK_INET_HANDLE (hInternet)) |
---|
2204 | fprintf (stderr, |
---|
2205 | "WARNING : hInternet handle failed to initialize"); |
---|
2206 | #ifdef POST_DEBUG |
---|
2207 | curl_easy_setopt (bInternet.handle, |
---|
2208 | CURLOPT_VERBOSE, 1); |
---|
2209 | #endif |
---|
2210 | bInternet.waitingRequests[0] = |
---|
2211 | strdup ((char *) val); |
---|
2212 | res1 = |
---|
2213 | InternetOpenUrl (&bInternet, |
---|
2214 | bInternet.waitingRequests |
---|
2215 | [0], NULL, 0, |
---|
2216 | INTERNET_FLAG_NO_CACHE_WRITE, |
---|
2217 | 0); |
---|
2218 | processDownloads (&bInternet); |
---|
2219 | char *tmp = |
---|
2220 | (char *) |
---|
2221 | malloc ((bInternet.ihandle[0].nDataLen + |
---|
2222 | 1) * sizeof (char)); |
---|
2223 | if (tmp == NULL) |
---|
2224 | { |
---|
2225 | return errorException (m, |
---|
2226 | _ |
---|
2227 | ("Unable to allocate memory."), |
---|
2228 | "InternalError", |
---|
2229 | NULL); |
---|
2230 | } |
---|
2231 | size_t bRead; |
---|
2232 | InternetReadFile (bInternet.ihandle[0], |
---|
2233 | (LPVOID) tmp, |
---|
2234 | bInternet. |
---|
2235 | ihandle[0].nDataLen, |
---|
2236 | &bRead); |
---|
2237 | tmp[bInternet.ihandle[0].nDataLen] = 0; |
---|
2238 | InternetCloseHandle (&bInternet); |
---|
2239 | map *btmp = |
---|
2240 | getMap (tmpmaps->content, "href"); |
---|
2241 | if (btmp != NULL) |
---|
2242 | { |
---|
2243 | #ifdef POST_DEBUG |
---|
2244 | fprintf (stderr, "%s %s\n", btmp->value, |
---|
2245 | tmp); |
---|
2246 | #endif |
---|
2247 | hInternet. |
---|
2248 | waitingRequests[hInternet.nb] = |
---|
2249 | strdup (tmp); |
---|
2250 | res = |
---|
2251 | InternetOpenUrl (&hInternet, |
---|
2252 | btmp->value, |
---|
2253 | hInternet.waitingRequests |
---|
2254 | [hInternet.nb], |
---|
2255 | strlen |
---|
2256 | (hInternet.waitingRequests |
---|
2257 | [hInternet.nb]), |
---|
2258 | INTERNET_FLAG_NO_CACHE_WRITE, |
---|
2259 | 0); |
---|
2260 | } |
---|
2261 | free (tmp); |
---|
2262 | } |
---|
2263 | } |
---|
2264 | cur3 = cur3->next; |
---|
2265 | } |
---|
2266 | #ifdef POST_DEBUG |
---|
2267 | fprintf (stderr, |
---|
2268 | "Header and Body was parsed from Reference \n"); |
---|
2269 | #endif |
---|
2270 | #ifdef DEBUG |
---|
2271 | dumpMap (tmpmaps->content); |
---|
2272 | fprintf (stderr, "= element 2 node \"%s\" = (%s)\n", |
---|
2273 | cur2->name, cur2->content); |
---|
2274 | #endif |
---|
2275 | } |
---|
2276 | else if (xmlStrcasecmp (cur2->name, BAD_CAST "Data") == 0) |
---|
2277 | { |
---|
2278 | #ifdef DEBUG |
---|
2279 | fprintf (stderr, "DATA\n"); |
---|
2280 | #endif |
---|
2281 | xmlNodePtr cur4 = cur2->children; |
---|
2282 | while (cur4 != NULL) |
---|
2283 | { |
---|
2284 | while (cur4 != NULL |
---|
2285 | && cur4->type != XML_ELEMENT_NODE) |
---|
2286 | cur4 = cur4->next; |
---|
2287 | if (cur4 == NULL) |
---|
2288 | break; |
---|
2289 | if (xmlStrcasecmp |
---|
2290 | (cur4->name, BAD_CAST "LiteralData") == 0) |
---|
2291 | { |
---|
2292 | /** |
---|
2293 | * Get every attribute from a LiteralData node |
---|
2294 | * dataType , uom |
---|
2295 | */ |
---|
2296 | char *list[2]; |
---|
2297 | list[0] = zStrdup ("dataType"); |
---|
2298 | list[1] = zStrdup ("uom"); |
---|
2299 | for (int l = 0; l < 2; l++) |
---|
2300 | { |
---|
2301 | #ifdef DEBUG |
---|
2302 | fprintf (stderr, "*** LiteralData %s ***", |
---|
2303 | list[l]); |
---|
2304 | #endif |
---|
2305 | xmlChar *val = |
---|
2306 | xmlGetProp (cur4, BAD_CAST list[l]); |
---|
2307 | if (val != NULL |
---|
2308 | && strlen ((char *) val) > 0) |
---|
2309 | { |
---|
2310 | if (tmpmaps->content != NULL) |
---|
2311 | addToMap (tmpmaps->content, list[l], |
---|
2312 | (char *) val); |
---|
2313 | else |
---|
2314 | tmpmaps->content = |
---|
2315 | createMap (list[l], (char *) val); |
---|
2316 | #ifdef DEBUG |
---|
2317 | fprintf (stderr, "%s\n", val); |
---|
2318 | #endif |
---|
2319 | } |
---|
2320 | xmlFree (val); |
---|
2321 | free (list[l]); |
---|
2322 | } |
---|
2323 | } |
---|
2324 | else |
---|
2325 | if (xmlStrcasecmp |
---|
2326 | (cur4->name, BAD_CAST "ComplexData") == 0) |
---|
2327 | { |
---|
2328 | /** |
---|
2329 | * Get every attribute from a Reference node |
---|
2330 | * mimeType, encoding, schema |
---|
2331 | */ |
---|
2332 | const char *coms[3] = |
---|
2333 | { "mimeType", "encoding", "schema" }; |
---|
2334 | for (int l = 0; l < 3; l++) |
---|
2335 | { |
---|
2336 | #ifdef DEBUG |
---|
2337 | fprintf (stderr, "*** ComplexData %s ***\n", |
---|
2338 | coms[l]); |
---|
2339 | #endif |
---|
2340 | xmlChar *val = |
---|
2341 | xmlGetProp (cur4, BAD_CAST coms[l]); |
---|
2342 | if (val != NULL |
---|
2343 | && strlen ((char *) val) > 0) |
---|
2344 | { |
---|
2345 | if (tmpmaps->content != NULL) |
---|
2346 | addToMap (tmpmaps->content, coms[l], |
---|
2347 | (char *) val); |
---|
2348 | else |
---|
2349 | tmpmaps->content = |
---|
2350 | createMap (coms[l], (char *) val); |
---|
2351 | #ifdef DEBUG |
---|
2352 | fprintf (stderr, "%s\n", val); |
---|
2353 | #endif |
---|
2354 | } |
---|
2355 | xmlFree (val); |
---|
2356 | } |
---|
2357 | } |
---|
2358 | |
---|
2359 | map *test = getMap (tmpmaps->content, "encoding"); |
---|
2360 | if (test == NULL) |
---|
2361 | { |
---|
2362 | if (tmpmaps->content != NULL) |
---|
2363 | addToMap (tmpmaps->content, "encoding", |
---|
2364 | "utf-8"); |
---|
2365 | else |
---|
2366 | tmpmaps->content = |
---|
2367 | createMap ("encoding", "utf-8"); |
---|
2368 | test = getMap (tmpmaps->content, "encoding"); |
---|
2369 | } |
---|
2370 | |
---|
2371 | if (strcasecmp (test->value, "base64") != 0) |
---|
2372 | { |
---|
2373 | xmlChar *mv = xmlNodeListGetString (doc, |
---|
2374 | cur4-> |
---|
2375 | xmlChildrenNode, |
---|
2376 | 1); |
---|
2377 | map *ltmp = |
---|
2378 | getMap (tmpmaps->content, "mimeType"); |
---|
2379 | if (mv == NULL |
---|
2380 | || |
---|
2381 | (xmlStrcasecmp |
---|
2382 | (cur4->name, BAD_CAST "ComplexData") == 0 |
---|
2383 | && (ltmp == NULL |
---|
2384 | || strncasecmp (ltmp->value, |
---|
2385 | "text/xml", 8) == 0))) |
---|
2386 | { |
---|
2387 | xmlDocPtr doc1 = xmlNewDoc (BAD_CAST "1.0"); |
---|
2388 | int buffersize; |
---|
2389 | xmlNodePtr cur5 = cur4->children; |
---|
2390 | while (cur5 != NULL |
---|
2391 | && cur5->type != XML_ELEMENT_NODE |
---|
2392 | && cur5->type != |
---|
2393 | XML_CDATA_SECTION_NODE) |
---|
2394 | cur5 = cur5->next; |
---|
2395 | if (cur5 != NULL |
---|
2396 | && cur5->type != XML_CDATA_SECTION_NODE) |
---|
2397 | { |
---|
2398 | xmlDocSetRootElement (doc1, cur5); |
---|
2399 | xmlDocDumpFormatMemoryEnc (doc1, &mv, |
---|
2400 | &buffersize, |
---|
2401 | "utf-8", 1); |
---|
2402 | char size[1024]; |
---|
2403 | sprintf (size, "%d", buffersize); |
---|
2404 | addToMap (tmpmaps->content, "size", |
---|
2405 | size); |
---|
2406 | xmlFreeDoc (doc1); |
---|
2407 | } |
---|
2408 | } |
---|
2409 | if (mv != NULL) |
---|
2410 | { |
---|
2411 | addToMap (tmpmaps->content, "value", |
---|
2412 | (char *) mv); |
---|
2413 | xmlFree (mv); |
---|
2414 | } |
---|
2415 | } |
---|
2416 | else |
---|
2417 | { |
---|
2418 | xmlChar *tmp = xmlNodeListGetRawString (doc, |
---|
2419 | cur4->xmlChildrenNode, |
---|
2420 | 0); |
---|
2421 | addToMap (tmpmaps->content, "value", |
---|
2422 | (char *) tmp); |
---|
2423 | map *tmpv = getMap (tmpmaps->content, "value"); |
---|
2424 | char *res = NULL; |
---|
2425 | char *curs = tmpv->value; |
---|
2426 | for (int i = 0; i <= strlen (tmpv->value) / 64; |
---|
2427 | i++) |
---|
2428 | { |
---|
2429 | if (res == NULL) |
---|
2430 | res = |
---|
2431 | (char *) malloc (67 * sizeof (char)); |
---|
2432 | else |
---|
2433 | res = |
---|
2434 | (char *) realloc (res, |
---|
2435 | (((i + 1) * 65) + |
---|
2436 | i) * sizeof (char)); |
---|
2437 | int csize = i * 65; |
---|
2438 | strncpy (res + csize, curs, 64); |
---|
2439 | if (i == xmlStrlen (tmp) / 64) |
---|
2440 | strcat (res, "\n\0"); |
---|
2441 | else |
---|
2442 | { |
---|
2443 | strncpy (res + (((i + 1) * 64) + i), |
---|
2444 | "\n\0", 2); |
---|
2445 | curs += 64; |
---|
2446 | } |
---|
2447 | } |
---|
2448 | free (tmpv->value); |
---|
2449 | tmpv->value = zStrdup (res); |
---|
2450 | free (res); |
---|
2451 | xmlFree (tmp); |
---|
2452 | } |
---|
2453 | cur4 = cur4->next; |
---|
2454 | } |
---|
2455 | } |
---|
2456 | #ifdef DEBUG |
---|
2457 | fprintf (stderr, "cur2 next \n"); |
---|
2458 | fflush (stderr); |
---|
2459 | #endif |
---|
2460 | cur2 = cur2->next; |
---|
2461 | } |
---|
2462 | #ifdef DEBUG |
---|
2463 | fprintf (stderr, "ADD MAPS TO REQUEST MAPS !\n"); |
---|
2464 | fflush (stderr); |
---|
2465 | #endif |
---|
2466 | |
---|
2467 | { |
---|
2468 | maps *testPresence = |
---|
2469 | getMaps (request_input_real_format, tmpmaps->name); |
---|
2470 | if (testPresence != NULL) |
---|
2471 | { |
---|
2472 | elements *elem = getElements (s1->inputs, tmpmaps->name); |
---|
2473 | if (elem != NULL) |
---|
2474 | { |
---|
2475 | if (appendMapsToMaps |
---|
2476 | (m, request_input_real_format, tmpmaps, elem) < 0) |
---|
2477 | { |
---|
2478 | freeMaps (&m); |
---|
2479 | free (m); |
---|
2480 | free (REQUEST); |
---|
2481 | free (SERVICE_URL); |
---|
2482 | InternetCloseHandle (&hInternet); |
---|
2483 | freeService (&s1); |
---|
2484 | free (s1); |
---|
2485 | return 0; |
---|
2486 | } |
---|
2487 | } |
---|
2488 | } |
---|
2489 | else |
---|
2490 | addMapsToMaps (&request_input_real_format, tmpmaps); |
---|
2491 | } |
---|
2492 | |
---|
2493 | #ifdef DEBUG |
---|
2494 | fprintf (stderr, "******TMPMAPS*****\n"); |
---|
2495 | dumpMaps (tmpmaps); |
---|
2496 | fprintf (stderr, "******REQUESTMAPS*****\n"); |
---|
2497 | dumpMaps (request_input_real_format); |
---|
2498 | #endif |
---|
2499 | freeMaps (&tmpmaps); |
---|
2500 | free (tmpmaps); |
---|
2501 | tmpmaps = NULL; |
---|
2502 | } |
---|
2503 | #ifdef DEBUG |
---|
2504 | dumpMaps (tmpmaps); |
---|
2505 | #endif |
---|
2506 | } |
---|
2507 | #ifdef DEBUG |
---|
2508 | fprintf (stderr, "Search for response document node\n"); |
---|
2509 | #endif |
---|
2510 | xmlXPathFreeObject (tmpsptr); |
---|
2511 | |
---|
2512 | tmpsptr = |
---|
2513 | extractFromDoc (doc, "/*/*/*[local-name()='ResponseDocument']"); |
---|
2514 | bool asRaw = false; |
---|
2515 | tmps = tmpsptr->nodesetval; |
---|
2516 | if (tmps->nodeNr == 0) |
---|
2517 | { |
---|
2518 | xmlXPathFreeObject (tmpsptr); |
---|
2519 | tmpsptr = |
---|
2520 | extractFromDoc (doc, "/*/*/*[local-name()='RawDataOutput']"); |
---|
2521 | tmps = tmpsptr->nodesetval; |
---|
2522 | asRaw = true; |
---|
2523 | } |
---|
2524 | #ifdef DEBUG |
---|
2525 | fprintf (stderr, "*****%d*****\n", tmps->nodeNr); |
---|
2526 | #endif |
---|
2527 | if (asRaw == true) |
---|
2528 | { |
---|
2529 | addToMap (request_inputs, "RawDataOutput", ""); |
---|
2530 | xmlNodePtr cur0 = tmps->nodeTab[0]; |
---|
2531 | if (cur0->type == XML_ELEMENT_NODE) |
---|
2532 | { |
---|
2533 | |
---|
2534 | maps *tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
2535 | if (tmpmaps == NULL) |
---|
2536 | { |
---|
2537 | return errorException (m, _("Unable to allocate memory."), |
---|
2538 | "InternalError", NULL); |
---|
2539 | } |
---|
2540 | tmpmaps->name = zStrdup ("unknownIdentifier"); |
---|
2541 | tmpmaps->content = NULL; |
---|
2542 | tmpmaps->next = NULL; |
---|
2543 | |
---|
2544 | /** |
---|
2545 | * Get every attribute from a RawDataOutput node |
---|
2546 | * mimeType, encoding, schema, uom |
---|
2547 | */ |
---|
2548 | const char *outs[4] = |
---|
2549 | { "mimeType", "encoding", "schema", "uom" }; |
---|
2550 | for (int l = 0; l < 4; l++) |
---|
2551 | { |
---|
2552 | #ifdef DEBUG |
---|
2553 | fprintf (stderr, "*** %s ***\t", outs[l]); |
---|
2554 | #endif |
---|
2555 | xmlChar *val = xmlGetProp (cur0, BAD_CAST outs[l]); |
---|
2556 | if (val != NULL) |
---|
2557 | { |
---|
2558 | if (strlen ((char *) val) > 0) |
---|
2559 | { |
---|
2560 | if (tmpmaps->content != NULL) |
---|
2561 | addToMap (tmpmaps->content, outs[l], |
---|
2562 | (char *) val); |
---|
2563 | else |
---|
2564 | tmpmaps->content = |
---|
2565 | createMap (outs[l], (char *) val); |
---|
2566 | } |
---|
2567 | xmlFree (val); |
---|
2568 | } |
---|
2569 | } |
---|
2570 | xmlNodePtr cur2 = cur0->children; |
---|
2571 | while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE) |
---|
2572 | cur2 = cur2->next; |
---|
2573 | int cur1cnt = 0; |
---|
2574 | while (cur2 != NULL) |
---|
2575 | { |
---|
2576 | if (xmlStrncasecmp |
---|
2577 | (cur2->name, BAD_CAST "Identifier", |
---|
2578 | xmlStrlen (cur2->name)) == 0) |
---|
2579 | { |
---|
2580 | xmlChar *val = |
---|
2581 | xmlNodeListGetString (NULL, cur2->xmlChildrenNode, 1); |
---|
2582 | free (tmpmaps->name); |
---|
2583 | tmpmaps->name = zStrdup ((char *) val); |
---|
2584 | xmlFree (val); |
---|
2585 | } |
---|
2586 | cur2 = cur2->next; |
---|
2587 | while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE) |
---|
2588 | cur2 = cur2->next; |
---|
2589 | } |
---|
2590 | if (request_output_real_format == NULL) |
---|
2591 | request_output_real_format = dupMaps (&tmpmaps); |
---|
2592 | else |
---|
2593 | addMapsToMaps (&request_output_real_format, tmpmaps); |
---|
2594 | if (tmpmaps != NULL) |
---|
2595 | { |
---|
2596 | freeMaps (&tmpmaps); |
---|
2597 | free (tmpmaps); |
---|
2598 | tmpmaps = NULL; |
---|
2599 | } |
---|
2600 | } |
---|
2601 | } |
---|
2602 | else |
---|
2603 | for (int k = 0; k < tmps->nodeNr; k++) |
---|
2604 | { |
---|
2605 | //else |
---|
2606 | addToMap (request_inputs, "ResponseDocument", ""); |
---|
2607 | maps *tmpmaps = NULL; |
---|
2608 | xmlNodePtr cur = tmps->nodeTab[k]; |
---|
2609 | if (cur->type == XML_ELEMENT_NODE) |
---|
2610 | { |
---|
2611 | /** |
---|
2612 | * A specific responseDocument node. |
---|
2613 | */ |
---|
2614 | if (tmpmaps == NULL) |
---|
2615 | { |
---|
2616 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
2617 | if (tmpmaps == NULL) |
---|
2618 | { |
---|
2619 | return errorException (m, |
---|
2620 | _ |
---|
2621 | ("Unable to allocate memory."), |
---|
2622 | "InternalError", NULL); |
---|
2623 | } |
---|
2624 | tmpmaps->name = zStrdup ("unknownIdentifier"); |
---|
2625 | tmpmaps->content = NULL; |
---|
2626 | tmpmaps->next = NULL; |
---|
2627 | } |
---|
2628 | /** |
---|
2629 | * Get every attribute: storeExecuteResponse, lineage, status |
---|
2630 | */ |
---|
2631 | const char *ress[3] = |
---|
2632 | { "storeExecuteResponse", "lineage", "status" }; |
---|
2633 | xmlChar *val; |
---|
2634 | for (int l = 0; l < 3; l++) |
---|
2635 | { |
---|
2636 | #ifdef DEBUG |
---|
2637 | fprintf (stderr, "*** %s ***\t", ress[l]); |
---|
2638 | #endif |
---|
2639 | val = xmlGetProp (cur, BAD_CAST ress[l]); |
---|
2640 | if (val != NULL && strlen ((char *) val) > 0) |
---|
2641 | { |
---|
2642 | if (tmpmaps->content != NULL) |
---|
2643 | addToMap (tmpmaps->content, ress[l], (char *) val); |
---|
2644 | else |
---|
2645 | tmpmaps->content = |
---|
2646 | createMap (ress[l], (char *) val); |
---|
2647 | addToMap (request_inputs, ress[l], (char *) val); |
---|
2648 | } |
---|
2649 | #ifdef DEBUG |
---|
2650 | fprintf (stderr, "%s\n", val); |
---|
2651 | #endif |
---|
2652 | xmlFree (val); |
---|
2653 | } |
---|
2654 | xmlNodePtr cur1 = cur->children; |
---|
2655 | while (cur1 != NULL && cur1->type != XML_ELEMENT_NODE) |
---|
2656 | cur1 = cur1->next; |
---|
2657 | int cur1cnt = 0; |
---|
2658 | while (cur1) |
---|
2659 | { |
---|
2660 | /** |
---|
2661 | * Indentifier |
---|
2662 | */ |
---|
2663 | if (xmlStrncasecmp |
---|
2664 | (cur1->name, BAD_CAST "Identifier", |
---|
2665 | xmlStrlen (cur1->name)) == 0) |
---|
2666 | { |
---|
2667 | xmlChar *val = |
---|
2668 | xmlNodeListGetString (doc, cur1->xmlChildrenNode, |
---|
2669 | 1); |
---|
2670 | if (tmpmaps == NULL) |
---|
2671 | { |
---|
2672 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
2673 | if (tmpmaps == NULL) |
---|
2674 | { |
---|
2675 | return errorException (m, |
---|
2676 | _ |
---|
2677 | ("Unable to allocate memory."), |
---|
2678 | "InternalError", NULL); |
---|
2679 | } |
---|
2680 | tmpmaps->name = zStrdup ((char *) val); |
---|
2681 | tmpmaps->content = NULL; |
---|
2682 | tmpmaps->next = NULL; |
---|
2683 | } |
---|
2684 | else |
---|
2685 | { |
---|
2686 | free (tmpmaps->name); |
---|
2687 | tmpmaps->name = zStrdup ((char *) val); |
---|
2688 | } |
---|
2689 | if (asRaw == true) |
---|
2690 | addToMap (request_inputs, "RawDataOutput", |
---|
2691 | (char *) val); |
---|
2692 | else |
---|
2693 | { |
---|
2694 | if (cur1cnt == 0) |
---|
2695 | addToMap (request_inputs, "ResponseDocument", |
---|
2696 | (char *) val); |
---|
2697 | else |
---|
2698 | { |
---|
2699 | map *tt = |
---|
2700 | getMap (request_inputs, "ResponseDocument"); |
---|
2701 | char *tmp = zStrdup (tt->value); |
---|
2702 | free (tt->value); |
---|
2703 | tt->value = |
---|
2704 | (char *) |
---|
2705 | malloc ((strlen (tmp) + |
---|
2706 | strlen ((char *) val) + |
---|
2707 | 1) * sizeof (char)); |
---|
2708 | sprintf (tt->value, "%s;%s", tmp, |
---|
2709 | (char *) val); |
---|
2710 | free (tmp); |
---|
2711 | } |
---|
2712 | } |
---|
2713 | cur1cnt += 1; |
---|
2714 | xmlFree (val); |
---|
2715 | } |
---|
2716 | /** |
---|
2717 | * Title, Asbtract |
---|
2718 | */ |
---|
2719 | else |
---|
2720 | if (xmlStrncasecmp |
---|
2721 | (cur1->name, BAD_CAST "Title", |
---|
2722 | xmlStrlen (cur1->name)) == 0 |
---|
2723 | || xmlStrncasecmp (cur1->name, BAD_CAST "Abstract", |
---|
2724 | xmlStrlen (cur1->name)) == 0) |
---|
2725 | { |
---|
2726 | xmlChar *val = |
---|
2727 | xmlNodeListGetString (doc, cur1->xmlChildrenNode, |
---|
2728 | 1); |
---|
2729 | if (tmpmaps == NULL) |
---|
2730 | { |
---|
2731 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
2732 | if (tmpmaps == NULL) |
---|
2733 | { |
---|
2734 | return errorException (m, |
---|
2735 | _ |
---|
2736 | ("Unable to allocate memory."), |
---|
2737 | "InternalError", NULL); |
---|
2738 | } |
---|
2739 | tmpmaps->name = zStrdup ("missingIndetifier"); |
---|
2740 | tmpmaps->content = |
---|
2741 | createMap ((char *) cur1->name, (char *) val); |
---|
2742 | tmpmaps->next = NULL; |
---|
2743 | } |
---|
2744 | else |
---|
2745 | { |
---|
2746 | if (tmpmaps->content != NULL) |
---|
2747 | addToMap (tmpmaps->content, (char *) cur1->name, |
---|
2748 | (char *) val); |
---|
2749 | else |
---|
2750 | tmpmaps->content = |
---|
2751 | createMap ((char *) cur1->name, (char *) val); |
---|
2752 | } |
---|
2753 | xmlFree (val); |
---|
2754 | } |
---|
2755 | else |
---|
2756 | if (xmlStrncasecmp |
---|
2757 | (cur1->name, BAD_CAST "Output", |
---|
2758 | xmlStrlen (cur1->name)) == 0) |
---|
2759 | { |
---|
2760 | /** |
---|
2761 | * Get every attribute from a Output node |
---|
2762 | * mimeType, encoding, schema, uom, asReference |
---|
2763 | */ |
---|
2764 | const char *outs[5] = |
---|
2765 | { "mimeType", "encoding", "schema", "uom", |
---|
2766 | "asReference" |
---|
2767 | }; |
---|
2768 | for (int l = 0; l < 5; l++) |
---|
2769 | { |
---|
2770 | #ifdef DEBUG |
---|
2771 | fprintf (stderr, "*** %s ***\t", outs[l]); |
---|
2772 | #endif |
---|
2773 | xmlChar *val = |
---|
2774 | xmlGetProp (cur1, BAD_CAST outs[l]); |
---|
2775 | if (val != NULL && strlen ((char *) val) > 0) |
---|
2776 | { |
---|
2777 | if (tmpmaps->content != NULL) |
---|
2778 | addToMap (tmpmaps->content, outs[l], |
---|
2779 | (char *) val); |
---|
2780 | else |
---|
2781 | tmpmaps->content = |
---|
2782 | createMap (outs[l], (char *) val); |
---|
2783 | } |
---|
2784 | #ifdef DEBUG |
---|
2785 | fprintf (stderr, "%s\n", val); |
---|
2786 | #endif |
---|
2787 | xmlFree (val); |
---|
2788 | } |
---|
2789 | xmlNodePtr cur2 = cur1->children; |
---|
2790 | while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE) |
---|
2791 | cur2 = cur2->next; |
---|
2792 | while (cur2) |
---|
2793 | { |
---|
2794 | /** |
---|
2795 | * Indentifier |
---|
2796 | */ |
---|
2797 | if (xmlStrncasecmp |
---|
2798 | (cur2->name, BAD_CAST "Identifier", |
---|
2799 | xmlStrlen (cur2->name)) == 0) |
---|
2800 | { |
---|
2801 | xmlChar *val = xmlNodeListGetString (doc, |
---|
2802 | cur2-> |
---|
2803 | xmlChildrenNode, |
---|
2804 | 1); |
---|
2805 | if (tmpmaps == NULL) |
---|
2806 | { |
---|
2807 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
2808 | if (tmpmaps == NULL) |
---|
2809 | { |
---|
2810 | return errorException (m, |
---|
2811 | _ |
---|
2812 | ("Unable to allocate memory."), |
---|
2813 | "InternalError", |
---|
2814 | NULL); |
---|
2815 | } |
---|
2816 | tmpmaps->name = zStrdup ((char *) val); |
---|
2817 | tmpmaps->content = NULL; |
---|
2818 | tmpmaps->next = NULL; |
---|
2819 | } |
---|
2820 | else |
---|
2821 | { |
---|
2822 | if (tmpmaps->name != NULL) |
---|
2823 | free (tmpmaps->name); |
---|
2824 | tmpmaps->name = zStrdup ((char *) val);; |
---|
2825 | } |
---|
2826 | xmlFree (val); |
---|
2827 | } |
---|
2828 | /** |
---|
2829 | * Title, Asbtract |
---|
2830 | */ |
---|
2831 | else |
---|
2832 | if (xmlStrncasecmp |
---|
2833 | (cur2->name, BAD_CAST "Title", |
---|
2834 | xmlStrlen (cur2->name)) == 0 |
---|
2835 | || xmlStrncasecmp (cur2->name, |
---|
2836 | BAD_CAST "Abstract", |
---|
2837 | xmlStrlen (cur2->name)) |
---|
2838 | == 0) |
---|
2839 | { |
---|
2840 | xmlChar *val = xmlNodeListGetString (doc, |
---|
2841 | cur2-> |
---|
2842 | xmlChildrenNode, |
---|
2843 | 1); |
---|
2844 | if (tmpmaps == NULL) |
---|
2845 | { |
---|
2846 | tmpmaps = (maps *) malloc (MAPS_SIZE); |
---|
2847 | if (tmpmaps == NULL) |
---|
2848 | { |
---|
2849 | return errorException (m, |
---|
2850 | _ |
---|
2851 | ("Unable to allocate memory."), |
---|
2852 | "InternalError", |
---|
2853 | NULL); |
---|
2854 | } |
---|
2855 | tmpmaps->name = |
---|
2856 | zStrdup ("missingIndetifier"); |
---|
2857 | tmpmaps->content = |
---|
2858 | createMap ((char *) cur2->name, |
---|
2859 | (char *) val); |
---|
2860 | tmpmaps->next = NULL; |
---|
2861 | } |
---|
2862 | else |
---|
2863 | { |
---|
2864 | if (tmpmaps->content != NULL) |
---|
2865 | addToMap (tmpmaps->content, |
---|
2866 | (char *) cur2->name, |
---|
2867 | (char *) val); |
---|
2868 | else |
---|
2869 | tmpmaps->content = |
---|
2870 | createMap ((char *) cur2->name, |
---|
2871 | (char *) val); |
---|
2872 | } |
---|
2873 | xmlFree (val); |
---|
2874 | } |
---|
2875 | cur2 = cur2->next; |
---|
2876 | while (cur2 != NULL |
---|
2877 | && cur2->type != XML_ELEMENT_NODE) |
---|
2878 | cur2 = cur2->next; |
---|
2879 | } |
---|
2880 | } |
---|
2881 | cur1 = cur1->next; |
---|
2882 | while (cur1 != NULL && cur1->type != XML_ELEMENT_NODE) |
---|
2883 | cur1 = cur1->next; |
---|
2884 | } |
---|
2885 | } |
---|
2886 | if (request_output_real_format == NULL) |
---|
2887 | request_output_real_format = dupMaps (&tmpmaps); |
---|
2888 | else |
---|
2889 | addMapsToMaps (&request_output_real_format, tmpmaps); |
---|
2890 | if (tmpmaps != NULL) |
---|
2891 | { |
---|
2892 | freeMaps (&tmpmaps); |
---|
2893 | free (tmpmaps); |
---|
2894 | tmpmaps = NULL; |
---|
2895 | } |
---|
2896 | } |
---|
2897 | xmlXPathFreeObject (tmpsptr); |
---|
2898 | xmlFreeDoc (doc); |
---|
2899 | xmlCleanupParser (); |
---|
2900 | } |
---|
2901 | |
---|
2902 | runHttpRequests (&m, &request_input_real_format, &hInternet); |
---|
2903 | |
---|
2904 | // if(CHECK_INET_HANDLE(hInternet)) |
---|
2905 | InternetCloseHandle (&hInternet); |
---|
2906 | |
---|
2907 | #ifdef DEBUG |
---|
2908 | fprintf (stderr, "\n%d\n", __LINE__); |
---|
2909 | fflush (stderr); |
---|
2910 | dumpMaps (request_input_real_format); |
---|
2911 | dumpMaps (request_output_real_format); |
---|
2912 | dumpMap (request_inputs); |
---|
2913 | fprintf (stderr, "\n%d\n", __LINE__); |
---|
2914 | fflush (stderr); |
---|
2915 | #endif |
---|
2916 | |
---|
2917 | /** |
---|
2918 | * Ensure that each requested arguments are present in the request |
---|
2919 | * DataInputs and ResponseDocument / RawDataOutput |
---|
2920 | */ |
---|
2921 | char *dfv = addDefaultValues (&request_input_real_format, s1->inputs, m, 0); |
---|
2922 | maps *ptr = request_input_real_format; |
---|
2923 | while (ptr != NULL) |
---|
2924 | { |
---|
2925 | map *tmp0 = getMap (ptr->content, "size"); |
---|
2926 | map *tmp1 = getMap (ptr->content, "maximumMegabytes"); |
---|
2927 | if (tmp1 != NULL && tmp0 != NULL) |
---|
2928 | { |
---|
2929 | float i = atof (tmp0->value) / 1048576.0; |
---|
2930 | if (i >= atoi (tmp1->value)) |
---|
2931 | { |
---|
2932 | char tmps[1024]; |
---|
2933 | map *tmpe = createMap ("code", "FileSizeExceeded"); |
---|
2934 | snprintf (tmps, 1024, |
---|
2935 | _ |
---|
2936 | ("The <%s> parameter has a limited size (%sMB) defined in ZOO ServicesProvider configuration file but the reference you provided exceed this limitation (%fMB), please correct your query or the ZOO Configuration file."), |
---|
2937 | ptr->name, tmp1->value, i); |
---|
2938 | addToMap (tmpe, "locator", ptr->name); |
---|
2939 | addToMap (tmpe, "text", tmps); |
---|
2940 | printExceptionReportResponse (m, tmpe); |
---|
2941 | freeService (&s1); |
---|
2942 | free (s1); |
---|
2943 | freeMap (&tmpe); |
---|
2944 | free (tmpe); |
---|
2945 | freeMaps (&m); |
---|
2946 | free (m); |
---|
2947 | free (REQUEST); |
---|
2948 | free (SERVICE_URL); |
---|
2949 | freeMaps (&request_input_real_format); |
---|
2950 | free (request_input_real_format); |
---|
2951 | freeMaps (&request_output_real_format); |
---|
2952 | free (request_output_real_format); |
---|
2953 | freeMaps (&tmpmaps); |
---|
2954 | free (tmpmaps); |
---|
2955 | return 1; |
---|
2956 | } |
---|
2957 | } |
---|
2958 | ptr = ptr->next; |
---|
2959 | } |
---|
2960 | |
---|
2961 | char *dfv1 = |
---|
2962 | addDefaultValues (&request_output_real_format, s1->outputs, m, 1); |
---|
2963 | if (strcmp (dfv1, "") != 0 || strcmp (dfv, "") != 0) |
---|
2964 | { |
---|
2965 | char tmps[1024]; |
---|
2966 | map *tmpe = createMap ("code", "MissingParameterValue"); |
---|
2967 | if (strcmp (dfv, "") != 0) |
---|
2968 | { |
---|
2969 | snprintf (tmps, 1024, |
---|
2970 | _ |
---|
2971 | ("The <%s> argument was not specified in DataInputs but defined as requested in ZOO ServicesProvider configuration file, please correct your query or the ZOO Configuration file."), |
---|
2972 | dfv); |
---|
2973 | addToMap (tmpe, "locator", dfv); |
---|
2974 | } |
---|
2975 | else if (strcmp (dfv1, "") != 0) |
---|
2976 | { |
---|
2977 | snprintf (tmps, 1024, |
---|
2978 | _ |
---|
2979 | ("The <%s> argument was specified as Output identifier but not defined in the ZOO Configuration File. Please, correct your query or the ZOO Configuration File."), |
---|
2980 | dfv1); |
---|
2981 | addToMap (tmpe, "locator", dfv1); |
---|
2982 | } |
---|
2983 | addToMap (tmpe, "text", tmps); |
---|
2984 | printExceptionReportResponse (m, tmpe); |
---|
2985 | freeService (&s1); |
---|
2986 | free (s1); |
---|
2987 | freeMap (&tmpe); |
---|
2988 | free (tmpe); |
---|
2989 | freeMaps (&m); |
---|
2990 | free (m); |
---|
2991 | free (REQUEST); |
---|
2992 | free (SERVICE_URL); |
---|
2993 | freeMaps (&request_input_real_format); |
---|
2994 | free (request_input_real_format); |
---|
2995 | freeMaps (&request_output_real_format); |
---|
2996 | free (request_output_real_format); |
---|
2997 | freeMaps (&tmpmaps); |
---|
2998 | free (tmpmaps); |
---|
2999 | return 1; |
---|
3000 | } |
---|
3001 | maps *tmpReqI = request_input_real_format; |
---|
3002 | while (tmpReqI != NULL) |
---|
3003 | { |
---|
3004 | char name[1024]; |
---|
3005 | if (getMap (tmpReqI->content, "isFile") != NULL) |
---|
3006 | { |
---|
3007 | if (cgiFormFileName (tmpReqI->name, name, sizeof (name)) == |
---|
3008 | cgiFormSuccess) |
---|
3009 | { |
---|
3010 | int BufferLen = 1024; |
---|
3011 | cgiFilePtr file; |
---|
3012 | int targetFile; |
---|
3013 | char storageNameOnServer[2048]; |
---|
3014 | char fileNameOnServer[64]; |
---|
3015 | char contentType[1024]; |
---|
3016 | char buffer[1024]; |
---|
3017 | char *tmpStr = NULL; |
---|
3018 | int size; |
---|
3019 | int got, t; |
---|
3020 | map *path = getMapFromMaps (m, "main", "tmpPath"); |
---|
3021 | cgiFormFileSize (tmpReqI->name, &size); |
---|
3022 | cgiFormFileContentType (tmpReqI->name, contentType, |
---|
3023 | sizeof (contentType)); |
---|
3024 | if (cgiFormFileOpen (tmpReqI->name, &file) == cgiFormSuccess) |
---|
3025 | { |
---|
3026 | t = -1; |
---|
3027 | while (1) |
---|
3028 | { |
---|
3029 | tmpStr = strstr (name + t + 1, "\\"); |
---|
3030 | if (NULL == tmpStr) |
---|
3031 | tmpStr = strstr (name + t + 1, "/"); |
---|
3032 | if (NULL != tmpStr) |
---|
3033 | t = (int) (tmpStr - name); |
---|
3034 | else |
---|
3035 | break; |
---|
3036 | } |
---|
3037 | strcpy (fileNameOnServer, name + t + 1); |
---|
3038 | |
---|
3039 | sprintf (storageNameOnServer, "%s/%s", path->value, |
---|
3040 | fileNameOnServer); |
---|
3041 | #ifdef DEBUG |
---|
3042 | fprintf (stderr, "Name on server %s\n", |
---|
3043 | storageNameOnServer); |
---|
3044 | fprintf (stderr, "fileNameOnServer: %s\n", |
---|
3045 | fileNameOnServer); |
---|
3046 | #endif |
---|
3047 | targetFile = |
---|
3048 | open (storageNameOnServer, O_RDWR | O_CREAT | O_TRUNC, |
---|
3049 | S_IRWXU | S_IRGRP | S_IROTH); |
---|
3050 | if (targetFile < 0) |
---|
3051 | { |
---|
3052 | #ifdef DEBUG |
---|
3053 | fprintf (stderr, "could not create the new file,%s\n", |
---|
3054 | fileNameOnServer); |
---|
3055 | #endif |
---|
3056 | } |
---|
3057 | else |
---|
3058 | { |
---|
3059 | while (cgiFormFileRead (file, buffer, BufferLen, &got) |
---|
3060 | == cgiFormSuccess) |
---|
3061 | { |
---|
3062 | if (got > 0) |
---|
3063 | write (targetFile, buffer, got); |
---|
3064 | } |
---|
3065 | } |
---|
3066 | addToMap (tmpReqI->content, "lref", storageNameOnServer); |
---|
3067 | cgiFormFileClose (file); |
---|
3068 | close (targetFile); |
---|
3069 | #ifdef DEBUG |
---|
3070 | fprintf (stderr, "File \"%s\" has been uploaded", |
---|
3071 | fileNameOnServer); |
---|
3072 | #endif |
---|
3073 | } |
---|
3074 | } |
---|
3075 | } |
---|
3076 | tmpReqI = tmpReqI->next; |
---|
3077 | } |
---|
3078 | |
---|
3079 | ensureDecodedBase64 (&request_input_real_format); |
---|
3080 | |
---|
3081 | #ifdef DEBUG |
---|
3082 | fprintf (stderr, "REQUEST_INPUTS\n"); |
---|
3083 | dumpMaps (request_input_real_format); |
---|
3084 | fprintf (stderr, "REQUEST_OUTPUTS\n"); |
---|
3085 | dumpMaps (request_output_real_format); |
---|
3086 | #endif |
---|
3087 | |
---|
3088 | maps *curs = getMaps (m, "env"); |
---|
3089 | if (curs != NULL) |
---|
3090 | { |
---|
3091 | map *mapcs = curs->content; |
---|
3092 | while (mapcs != NULLMAP) |
---|
3093 | { |
---|
3094 | #ifndef WIN32 |
---|
3095 | setenv (mapcs->name, mapcs->value, 1); |
---|
3096 | #else |
---|
3097 | #ifdef DEBUG |
---|
3098 | fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name, |
---|
3099 | mapcs->value); |
---|
3100 | #endif |
---|
3101 | if (mapcs->value[strlen (mapcs->value) - 2] == '\r') |
---|
3102 | { |
---|
3103 | #ifdef DEBUG |
---|
3104 | fprintf (stderr, "[ZOO: Env var finish with \r]\n"); |
---|
3105 | #endif |
---|
3106 | mapcs->value[strlen (mapcs->value) - 1] = 0; |
---|
3107 | } |
---|
3108 | #ifdef DEBUG |
---|
3109 | if (SetEnvironmentVariable (mapcs->name, mapcs->value) == 0) |
---|
3110 | { |
---|
3111 | fflush (stderr); |
---|
3112 | fprintf (stderr, "setting variable... %s\n", "OK"); |
---|
3113 | } |
---|
3114 | else |
---|
3115 | { |
---|
3116 | fflush (stderr); |
---|
3117 | fprintf (stderr, "setting variable... %s\n", "OK"); |
---|
3118 | } |
---|
3119 | #else |
---|
3120 | |
---|
3121 | |
---|
3122 | SetEnvironmentVariable (mapcs->name, mapcs->value); |
---|
3123 | #endif |
---|
3124 | char *toto = |
---|
3125 | (char *) |
---|
3126 | malloc ((strlen (mapcs->name) + strlen (mapcs->value) + |
---|
3127 | 2) * sizeof (char)); |
---|
3128 | sprintf (toto, "%s=%s", mapcs->name, mapcs->value); |
---|
3129 | putenv (toto); |
---|
3130 | #ifdef DEBUG |
---|
3131 | fflush (stderr); |
---|
3132 | #endif |
---|
3133 | #endif |
---|
3134 | #ifdef DEBUG |
---|
3135 | fprintf (stderr, "[ZOO: setenv (%s=%s)]\n", mapcs->name, |
---|
3136 | mapcs->value); |
---|
3137 | fflush (stderr); |
---|
3138 | #endif |
---|
3139 | mapcs = mapcs->next; |
---|
3140 | } |
---|
3141 | } |
---|
3142 | |
---|
3143 | #ifdef DEBUG |
---|
3144 | dumpMap (request_inputs); |
---|
3145 | #endif |
---|
3146 | |
---|
3147 | /** |
---|
3148 | * Need to check if we need to fork to load a status enabled |
---|
3149 | */ |
---|
3150 | r_inputs = NULL; |
---|
3151 | map *store = getMap (request_inputs, "storeExecuteResponse"); |
---|
3152 | map *status = getMap (request_inputs, "status"); |
---|
3153 | /** |
---|
3154 | * 05-007r7 WPS 1.0.0 page 57 : |
---|
3155 | * 'If status="true" and storeExecuteResponse is "false" then the service |
---|
3156 | * shall raise an exception.' |
---|
3157 | */ |
---|
3158 | if (status != NULL && strcmp (status->value, "true") == 0 && |
---|
3159 | store != NULL && strcmp (store->value, "false") == 0) |
---|
3160 | { |
---|
3161 | errorException (m, |
---|
3162 | _ |
---|
3163 | ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."), |
---|
3164 | "InvalidParameterValue", "storeExecuteResponse"); |
---|
3165 | freeService (&s1); |
---|
3166 | free (s1); |
---|
3167 | freeMaps (&m); |
---|
3168 | free (m); |
---|
3169 | |
---|
3170 | freeMaps (&request_input_real_format); |
---|
3171 | free (request_input_real_format); |
---|
3172 | |
---|
3173 | freeMaps (&request_output_real_format); |
---|
3174 | free (request_output_real_format); |
---|
3175 | |
---|
3176 | free (REQUEST); |
---|
3177 | free (SERVICE_URL); |
---|
3178 | return 1; |
---|
3179 | } |
---|
3180 | r_inputs = getMap (request_inputs, "storeExecuteResponse"); |
---|
3181 | int eres = SERVICE_STARTED; |
---|
3182 | int cpid = getpid (); |
---|
3183 | |
---|
3184 | /** |
---|
3185 | * Initialize the specific [lenv] section which contains runtime variables: |
---|
3186 | * |
---|
3187 | * - usid : it is an unique identification number |
---|
3188 | * - sid : it is the process idenfitication number (OS) |
---|
3189 | * - status : value between 0 and 100 to express the completude of |
---|
3190 | * the operations of the running service |
---|
3191 | * - message : is a string where you can store error messages, in case |
---|
3192 | * service is failing, or o provide details on the ongoing operation. |
---|
3193 | * - cwd : is the current working directory |
---|
3194 | * - soap : is a boolean value, true if the request was contained in a SOAP |
---|
3195 | * Envelop |
---|
3196 | * - sessid : string storing the session identifier (only when cookie is |
---|
3197 | * used) |
---|
3198 | * - cgiSid : only defined on Window platforms (for being able to identify |
---|
3199 | * the created process) |
---|
3200 | * |
---|
3201 | */ |
---|
3202 | maps *_tmpMaps = (maps *) malloc (MAPS_SIZE); |
---|
3203 | _tmpMaps->name = zStrdup ("lenv"); |
---|
3204 | char tmpBuff[100]; |
---|
3205 | int lid = getShmLockId (NULL, 1); |
---|
3206 | lockShm (lid); |
---|
3207 | struct ztimeval tp; |
---|
3208 | if (zGettimeofday (&tp, NULL) == 0) |
---|
3209 | sprintf (tmpBuff, "%i", (cpid + ((int) tp.tv_sec + (int) tp.tv_usec))); |
---|
3210 | else |
---|
3211 | sprintf (tmpBuff, "%i", (cpid + (int) time (NULL))); |
---|
3212 | unlockShm (lid); |
---|
3213 | removeShmLock (NULL, 1); |
---|
3214 | _tmpMaps->content = createMap ("usid", tmpBuff); |
---|
3215 | _tmpMaps->next = NULL; |
---|
3216 | sprintf (tmpBuff, "%i", cpid); |
---|
3217 | addToMap (_tmpMaps->content, "sid", tmpBuff); |
---|
3218 | addToMap (_tmpMaps->content, "status", "0"); |
---|
3219 | addToMap (_tmpMaps->content, "cwd", ntmp); |
---|
3220 | addToMap (_tmpMaps->content, "message", _("No message provided")); |
---|
3221 | map *ltmp = getMap (request_inputs, "soap"); |
---|
3222 | if (ltmp != NULL) |
---|
3223 | addToMap (_tmpMaps->content, "soap", ltmp->value); |
---|
3224 | else |
---|
3225 | addToMap (_tmpMaps->content, "soap", "false"); |
---|
3226 | if (cgiCookie != NULL && strlen (cgiCookie) > 0) |
---|
3227 | { |
---|
3228 | int hasValidCookie = -1; |
---|
3229 | char *tcook = zStrdup (cgiCookie); |
---|
3230 | char *tmp = NULL; |
---|
3231 | map *testing = getMapFromMaps (m, "main", "cookiePrefix"); |
---|
3232 | if (testing == NULL) |
---|
3233 | { |
---|
3234 | tmp = zStrdup ("ID="); |
---|
3235 | } |
---|
3236 | else |
---|
3237 | { |
---|
3238 | tmp = |
---|
3239 | (char *) malloc ((strlen (testing->value) + 2) * sizeof (char)); |
---|
3240 | sprintf (tmp, "%s=", testing->value); |
---|
3241 | } |
---|
3242 | if (strstr (cgiCookie, ";") != NULL) |
---|
3243 | { |
---|
3244 | char *token, *saveptr; |
---|
3245 | token = strtok_r (cgiCookie, ";", &saveptr); |
---|
3246 | while (token != NULL) |
---|
3247 | { |
---|
3248 | if (strcasestr (token, tmp) != NULL) |
---|
3249 | { |
---|
3250 | if (tcook != NULL) |
---|
3251 | free (tcook); |
---|
3252 | tcook = zStrdup (token); |
---|
3253 | hasValidCookie = 1; |
---|
3254 | } |
---|
3255 | token = strtok_r (NULL, ";", &saveptr); |
---|
3256 | } |
---|
3257 | } |
---|
3258 | else |
---|
3259 | { |
---|
3260 | if (strstr (cgiCookie, "=") != NULL |
---|
3261 | && strcasestr (cgiCookie, tmp) != NULL) |
---|
3262 | { |
---|
3263 | tcook = zStrdup (cgiCookie); |
---|
3264 | hasValidCookie = 1; |
---|
3265 | } |
---|
3266 | if (tmp != NULL) |
---|
3267 | { |
---|
3268 | free (tmp); |
---|
3269 | } |
---|
3270 | } |
---|
3271 | if (hasValidCookie > 0) |
---|
3272 | { |
---|
3273 | addToMap (_tmpMaps->content, "sessid", strstr (tcook, "=") + 1); |
---|
3274 | char session_file_path[1024]; |
---|
3275 | map *tmpPath = getMapFromMaps (m, "main", "sessPath"); |
---|
3276 | if (tmpPath == NULL) |
---|
3277 | tmpPath = getMapFromMaps (m, "main", "tmpPath"); |
---|
3278 | char *tmp1 = strtok (tcook, ";"); |
---|
3279 | if (tmp1 != NULL) |
---|
3280 | sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value, |
---|
3281 | strstr (tmp1, "=") + 1); |
---|
3282 | else |
---|
3283 | sprintf (session_file_path, "%s/sess_%s.cfg", tmpPath->value, |
---|
3284 | strstr (cgiCookie, "=") + 1); |
---|
3285 | free (tcook); |
---|
3286 | maps *tmpSess = (maps *) malloc (MAPS_SIZE); |
---|
3287 | struct stat file_status; |
---|
3288 | int istat = stat (session_file_path, &file_status); |
---|
3289 | if (istat == 0 && file_status.st_size > 0) |
---|
3290 | { |
---|
3291 | conf_read (session_file_path, tmpSess); |
---|
3292 | addMapsToMaps (&m, tmpSess); |
---|
3293 | freeMaps (&tmpSess); |
---|
3294 | free (tmpSess); |
---|
3295 | } |
---|
3296 | } |
---|
3297 | } |
---|
3298 | addMapsToMaps (&m, _tmpMaps); |
---|
3299 | freeMaps (&_tmpMaps); |
---|
3300 | free (_tmpMaps); |
---|
3301 | |
---|
3302 | #ifdef DEBUG |
---|
3303 | dumpMap (request_inputs); |
---|
3304 | #endif |
---|
3305 | #ifdef WIN32 |
---|
3306 | char *cgiSidL = NULL; |
---|
3307 | if (getenv ("CGISID") != NULL) |
---|
3308 | addToMap (request_inputs, "cgiSid", getenv ("CGISID")); |
---|
3309 | map *test1 = getMap (request_inputs, "cgiSid"); |
---|
3310 | if (test1 != NULL) |
---|
3311 | { |
---|
3312 | cgiSid = test1->value; |
---|
3313 | addToMap (request_inputs, "storeExecuteResponse", "true"); |
---|
3314 | addToMap (request_inputs, "status", "true"); |
---|
3315 | setMapInMaps (m, "lenv", "sid", test1->value); |
---|
3316 | status = getMap (request_inputs, "status"); |
---|
3317 | } |
---|
3318 | #endif |
---|
3319 | char *fbkp, *fbkp1; |
---|
3320 | FILE *f0, *f1; |
---|
3321 | if (status != NULL) |
---|
3322 | if (strcasecmp (status->value, "false") == 0) |
---|
3323 | status = NULLMAP; |
---|
3324 | if (status == NULLMAP) |
---|
3325 | { |
---|
3326 | loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format, |
---|
3327 | &request_output_real_format, &eres); |
---|
3328 | } |
---|
3329 | else |
---|
3330 | { |
---|
3331 | int pid; |
---|
3332 | #ifdef DEBUG |
---|
3333 | fprintf (stderr, "\nPID : %d\n", cpid); |
---|
3334 | #endif |
---|
3335 | |
---|
3336 | #ifndef WIN32 |
---|
3337 | pid = fork (); |
---|
3338 | #else |
---|
3339 | if (cgiSid == NULL) |
---|
3340 | { |
---|
3341 | createProcess (m, request_inputs, s1, NULL, cpid, |
---|
3342 | request_input_real_format, |
---|
3343 | request_output_real_format); |
---|
3344 | pid = cpid; |
---|
3345 | } |
---|
3346 | else |
---|
3347 | { |
---|
3348 | pid = 0; |
---|
3349 | cpid = atoi (cgiSid); |
---|
3350 | } |
---|
3351 | #endif |
---|
3352 | if (pid > 0) |
---|
3353 | { |
---|
3354 | /** |
---|
3355 | * dady : |
---|
3356 | * set status to SERVICE_ACCEPTED |
---|
3357 | */ |
---|
3358 | #ifdef DEBUG |
---|
3359 | fprintf (stderr, "father pid continue (origin %d) %d ...\n", cpid, |
---|
3360 | getpid ()); |
---|
3361 | #endif |
---|
3362 | eres = SERVICE_ACCEPTED; |
---|
3363 | } |
---|
3364 | else if (pid == 0) |
---|
3365 | { |
---|
3366 | /** |
---|
3367 | * son : have to close the stdout, stdin and stderr to let the parent |
---|
3368 | * process answer to http client. |
---|
3369 | */ |
---|
3370 | #ifndef WIN32 |
---|
3371 | zSleep (1); |
---|
3372 | #endif |
---|
3373 | r_inputs = getMapFromMaps (m, "lenv", "usid"); |
---|
3374 | int cpid = atoi (r_inputs->value); |
---|
3375 | r_inputs = getMapFromMaps (m, "main", "tmpPath"); |
---|
3376 | map *r_inputs1 = getMap (s1->content, "ServiceProvider"); |
---|
3377 | fbkp = |
---|
3378 | (char *) |
---|
3379 | malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) + |
---|
3380 | 1024) * sizeof (char)); |
---|
3381 | sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value, r_inputs1->value, |
---|
3382 | cpid); |
---|
3383 | char *flog = |
---|
3384 | (char *) |
---|
3385 | malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) + |
---|
3386 | 1024) * sizeof (char)); |
---|
3387 | sprintf (flog, "%s/%s_%d_error.log", r_inputs->value, |
---|
3388 | r_inputs1->value, cpid); |
---|
3389 | #ifdef DEBUG |
---|
3390 | fprintf (stderr, "RUN IN BACKGROUND MODE \n"); |
---|
3391 | fprintf (stderr, "son pid continue (origin %d) %d ...\n", cpid, |
---|
3392 | getpid ()); |
---|
3393 | fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value); |
---|
3394 | #endif |
---|
3395 | freopen (flog, "w+", stderr); |
---|
3396 | semid lid = getShmLockId (m, 1); |
---|
3397 | fflush (stderr); |
---|
3398 | if (lid < 0) |
---|
3399 | { |
---|
3400 | fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__); |
---|
3401 | fflush (stderr); |
---|
3402 | return -1; |
---|
3403 | } |
---|
3404 | else |
---|
3405 | { |
---|
3406 | if (lockShm (lid) < 0) |
---|
3407 | { |
---|
3408 | fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__); |
---|
3409 | fflush (stderr); |
---|
3410 | return -1; |
---|
3411 | } |
---|
3412 | fflush (stderr); |
---|
3413 | } |
---|
3414 | f0 = freopen (fbkp, "w+", stdout); |
---|
3415 | rewind (stdout); |
---|
3416 | #ifndef WIN32 |
---|
3417 | fclose (stdin); |
---|
3418 | #endif |
---|
3419 | free (flog); |
---|
3420 | /** |
---|
3421 | * set status to SERVICE_STARTED and flush stdout to ensure full |
---|
3422 | * content was outputed (the file used to store the ResponseDocument). |
---|
3423 | * The rewind stdout to restart writing from the bgining of the file, |
---|
3424 | * this way the data will be updated at the end of the process run. |
---|
3425 | */ |
---|
3426 | printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value, |
---|
3427 | SERVICE_STARTED, request_input_real_format, |
---|
3428 | request_output_real_format); |
---|
3429 | fflush (stdout); |
---|
3430 | unlockShm (lid); |
---|
3431 | fflush (stderr); |
---|
3432 | fbkp1 = |
---|
3433 | (char *) |
---|
3434 | malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) + |
---|
3435 | 1024) * sizeof (char)); |
---|
3436 | sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value, |
---|
3437 | r_inputs1->value, cpid); |
---|
3438 | f1 = freopen (fbkp1, "w+", stdout); |
---|
3439 | loadServiceAndRun (&m, s1, request_inputs, |
---|
3440 | &request_input_real_format, |
---|
3441 | &request_output_real_format, &eres); |
---|
3442 | } |
---|
3443 | else |
---|
3444 | { |
---|
3445 | /** |
---|
3446 | * error server don't accept the process need to output a valid |
---|
3447 | * error response here !!! |
---|
3448 | */ |
---|
3449 | eres = -1; |
---|
3450 | errorException (m, _("Unable to run the child process properly"), |
---|
3451 | "InternalError", NULL); |
---|
3452 | } |
---|
3453 | } |
---|
3454 | |
---|
3455 | #ifdef DEBUG |
---|
3456 | dumpMaps (request_output_real_format); |
---|
3457 | #endif |
---|
3458 | if (eres != -1) |
---|
3459 | outputResponse (s1, request_input_real_format, |
---|
3460 | request_output_real_format, request_inputs, |
---|
3461 | cpid, m, eres); |
---|
3462 | fflush (stdout); |
---|
3463 | /** |
---|
3464 | * Ensure that if error occurs when freeing memory, no signal will return |
---|
3465 | * an ExceptionReport document as the result was already returned to the |
---|
3466 | * client. |
---|
3467 | */ |
---|
3468 | #ifndef USE_GDB |
---|
3469 | signal (SIGSEGV, donothing); |
---|
3470 | signal (SIGTERM, donothing); |
---|
3471 | signal (SIGINT, donothing); |
---|
3472 | signal (SIGILL, donothing); |
---|
3473 | signal (SIGFPE, donothing); |
---|
3474 | signal (SIGABRT, donothing); |
---|
3475 | #endif |
---|
3476 | if (((int) getpid ()) != cpid || cgiSid != NULL) |
---|
3477 | { |
---|
3478 | fclose (stdout); |
---|
3479 | fclose (stderr); |
---|
3480 | /** |
---|
3481 | * Dump back the final file fbkp1 to fbkp |
---|
3482 | */ |
---|
3483 | fclose (f0); |
---|
3484 | fclose (f1); |
---|
3485 | FILE *f2 = fopen (fbkp1, "rb"); |
---|
3486 | semid lid = getShmLockId (m, 1); |
---|
3487 | if (lid < 0) |
---|
3488 | return -1; |
---|
3489 | lockShm (lid); |
---|
3490 | FILE *f3 = fopen (fbkp, "wb+"); |
---|
3491 | free (fbkp); |
---|
3492 | fseek (f2, 0, SEEK_END); |
---|
3493 | long flen = ftell (f2); |
---|
3494 | fseek (f2, 0, SEEK_SET); |
---|
3495 | char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char)); |
---|
3496 | fread (tmps1, flen, 1, f2); |
---|
3497 | fwrite (tmps1, 1, flen, f3); |
---|
3498 | fclose (f2); |
---|
3499 | fclose (f3); |
---|
3500 | unlockShm (lid); |
---|
3501 | unlink (fbkp1); |
---|
3502 | free (fbkp1); |
---|
3503 | free (tmps1); |
---|
3504 | unhandleStatus (m); |
---|
3505 | } |
---|
3506 | |
---|
3507 | freeService (&s1); |
---|
3508 | free (s1); |
---|
3509 | freeMaps (&m); |
---|
3510 | free (m); |
---|
3511 | |
---|
3512 | freeMaps (&request_input_real_format); |
---|
3513 | free (request_input_real_format); |
---|
3514 | |
---|
3515 | freeMaps (&request_output_real_format); |
---|
3516 | free (request_output_real_format); |
---|
3517 | |
---|
3518 | free (REQUEST); |
---|
3519 | free (SERVICE_URL); |
---|
3520 | #ifdef DEBUG |
---|
3521 | fprintf (stderr, "Processed response \n"); |
---|
3522 | fflush (stdout); |
---|
3523 | fflush (stderr); |
---|
3524 | #endif |
---|
3525 | |
---|
3526 | if (((int) getpid ()) != cpid || cgiSid != NULL) |
---|
3527 | { |
---|
3528 | exit (0); |
---|
3529 | } |
---|
3530 | |
---|
3531 | return 0; |
---|
3532 | } |
---|