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