1 | /** |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright (c) 2009-2012 GeoLabs SARL |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "service_internal.h" |
---|
26 | |
---|
27 | #ifndef JSCLASS_GLOBAL_FLAGS |
---|
28 | #define JSCLSAS_GLOBAL_FLAGS 0 |
---|
29 | #endif |
---|
30 | |
---|
31 | static char dbg[1024]; |
---|
32 | |
---|
33 | JSBool |
---|
34 | JSAlert (JSContext * cx, uintN argc, jsval * argv1) |
---|
35 | { |
---|
36 | jsval *argv = JS_ARGV (cx, argv1); |
---|
37 | int i = 0; |
---|
38 | JS_MaybeGC (cx); |
---|
39 | for (i = 0; i < argc; i++) |
---|
40 | { |
---|
41 | JSString *jsmsg = JS_ValueToString (cx, argv[i]); |
---|
42 | char *tmp = JS_EncodeString (cx, jsmsg); |
---|
43 | fprintf (stderr, "[ZOO-API:JS] %s\n", tmp); |
---|
44 | free (tmp); |
---|
45 | } |
---|
46 | JS_MaybeGC (cx); |
---|
47 | |
---|
48 | return JS_TRUE; |
---|
49 | } |
---|
50 | |
---|
51 | JSBool |
---|
52 | JSLoadScripts (JSContext * cx, uintN argc, jsval * argv1) |
---|
53 | { |
---|
54 | //map* request = JS_GetContextPrivate(cx); |
---|
55 | //map* tmpm1=getMap(request,"metapath"); |
---|
56 | JS_MaybeGC (cx); |
---|
57 | |
---|
58 | char ntmp[1024]; |
---|
59 | getcwd (ntmp, 1024); |
---|
60 | |
---|
61 | jsval *argv = JS_ARGV (cx, argv1); |
---|
62 | int i = 0; |
---|
63 | JS_MaybeGC (cx); |
---|
64 | for (i = 0; i < argc; i++) |
---|
65 | { |
---|
66 | JSString *jsmsg = JS_ValueToString (cx, argv[i]); |
---|
67 | char *filename = JSValToChar (cx, &argv[i]); |
---|
68 | char *api0 = |
---|
69 | (char *) malloc ((strlen (ntmp) + strlen (filename) + 2) * |
---|
70 | sizeof (char)); |
---|
71 | sprintf (api0, "%s/%s", ntmp, filename); |
---|
72 | #ifdef JS_DEBUG |
---|
73 | fprintf (stderr, "Trying to load %s\n", api0); |
---|
74 | fflush (stderr); |
---|
75 | #endif |
---|
76 | JSObject *api_script1 = |
---|
77 | loadZooApiFile (cx, JS_GetGlobalObject (cx), api0); |
---|
78 | free (api0); |
---|
79 | } |
---|
80 | JS_MaybeGC (cx); |
---|
81 | JS_SET_RVAL (cx, argv1, JSVAL_VOID); |
---|
82 | |
---|
83 | return JS_TRUE; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | int |
---|
88 | zoo_js_support (maps ** main_conf, map * request, service * s, |
---|
89 | maps ** inputs, maps ** outputs) |
---|
90 | { |
---|
91 | maps *main = *main_conf; |
---|
92 | maps *_inputs = *inputs; |
---|
93 | maps *_outputs = *outputs; |
---|
94 | |
---|
95 | /* The class of the global object. */ |
---|
96 | JSClass global_class = { |
---|
97 | "global", JSCLASS_GLOBAL_FLAGS, |
---|
98 | JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, |
---|
99 | JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, |
---|
100 | JSCLASS_NO_OPTIONAL_MEMBERS |
---|
101 | }; |
---|
102 | |
---|
103 | /* JS variables. */ |
---|
104 | JSRuntime *rt; |
---|
105 | JSContext *cx; |
---|
106 | JSObject *global; |
---|
107 | |
---|
108 | /* Create a JS runtime. */ |
---|
109 | rt = JS_NewRuntime (8L * 1024L * 1024L); |
---|
110 | if (rt == NULL) |
---|
111 | return 1; |
---|
112 | |
---|
113 | /* Create a context. */ |
---|
114 | cx = JS_NewContext (rt, 8192); |
---|
115 | if (cx == NULL) |
---|
116 | { |
---|
117 | return 1; |
---|
118 | } |
---|
119 | JS_SetOptions (cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); |
---|
120 | JS_SetVersion (cx, JSVERSION_LATEST); |
---|
121 | JS_SetErrorReporter (cx, reportError); |
---|
122 | |
---|
123 | /* Create the global object. */ |
---|
124 | global = JS_NewCompartmentAndGlobalObject (cx, &global_class, NULL); |
---|
125 | |
---|
126 | /* Populate the global object with the standard globals, |
---|
127 | like Object and Array. */ |
---|
128 | if (!JS_InitStandardClasses (cx, global)) |
---|
129 | { |
---|
130 | return 1; |
---|
131 | } |
---|
132 | |
---|
133 | /* Define specific function and global variable to share with JS runtime |
---|
134 | */ |
---|
135 | jsval tmp = INT_TO_JSVAL (3); |
---|
136 | if (!JS_SetProperty (cx, global, "SERVICE_SUCCEEDED", &tmp)) |
---|
137 | return 1; |
---|
138 | tmp = INT_TO_JSVAL (4); |
---|
139 | if (!JS_SetProperty (cx, global, "SERVICE_FAILED", &tmp)) |
---|
140 | return 1; |
---|
141 | if (!JS_DefineFunction (cx, global, "ZOORequest", JSRequest, 4, 0)) |
---|
142 | return 1; |
---|
143 | if (!JS_DefineFunction (cx, global, "ZOOTranslate", JSTranslate, 4, 0)) |
---|
144 | return 1; |
---|
145 | if (!JS_DefineFunction |
---|
146 | (cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0)) |
---|
147 | return 1; |
---|
148 | if (!JS_DefineFunction (cx, global, "alert", JSAlert, 2, 0)) |
---|
149 | return 1; |
---|
150 | if (!JS_DefineFunction (cx, global, "importScripts", JSLoadScripts, 1, 0)) |
---|
151 | return 1; |
---|
152 | |
---|
153 | /** |
---|
154 | * Add private context object |
---|
155 | */ |
---|
156 | void *cxPrivate = request; |
---|
157 | JS_SetContextPrivate (cx, cxPrivate); |
---|
158 | |
---|
159 | char ntmp[1024]; |
---|
160 | getcwd (ntmp, 1024); |
---|
161 | |
---|
162 | /** |
---|
163 | * Load the first part of the ZOO-API |
---|
164 | */ |
---|
165 | char *api0 = (char *) malloc ((strlen (ntmp) + 17) * sizeof (char)); |
---|
166 | sprintf (api0, "%s/ZOO-proj4js.js", ntmp); |
---|
167 | #ifdef JS_DEBUG |
---|
168 | fprintf (stderr, "Trying to load %s\n", api0); |
---|
169 | #endif |
---|
170 | JSObject *api_script1 = loadZooApiFile (cx, global, api0); |
---|
171 | free (api0); |
---|
172 | fflush (stderr); |
---|
173 | |
---|
174 | char *api1 = (char *) malloc ((strlen (ntmp) + 13) * sizeof (char)); |
---|
175 | sprintf (api1, "%s/ZOO-api.js", ntmp); |
---|
176 | #ifdef JS_DEBUG |
---|
177 | fprintf (stderr, "Trying to load %s\n", api1); |
---|
178 | #endif |
---|
179 | JSObject *api_script2 = loadZooApiFile (cx, global, api1); |
---|
180 | free (api1); |
---|
181 | fflush (stderr); |
---|
182 | |
---|
183 | /* Your application code here. This may include JSAPI calls |
---|
184 | to create your own custom JS objects and run scripts. */ |
---|
185 | maps *out = *outputs; |
---|
186 | int res = SERVICE_FAILED; |
---|
187 | maps *mc = *main_conf; |
---|
188 | map *tmpm2 = getMap (s->content, "serviceProvider"); |
---|
189 | char *tmp_path = (char *) malloc ((strlen (s->zcfg) + 1) * sizeof (char *)); |
---|
190 | sprintf (tmp_path, "%s", s->zcfg); |
---|
191 | char *dir = dirname (tmp_path); |
---|
192 | char *filename = |
---|
193 | (char *) malloc ((strlen (dir) + strlen (tmpm2->value) + 2) * |
---|
194 | sizeof (char *)); |
---|
195 | sprintf (filename, "%s/%s", dir, tmpm2->value); |
---|
196 | free (tmp_path); |
---|
197 | #ifdef JS_DEBUG |
---|
198 | fprintf (stderr, "FILENAME %s\n", filename); |
---|
199 | #endif |
---|
200 | struct stat file_status; |
---|
201 | stat (filename, &file_status); |
---|
202 | //char *source=(char*)malloc(file_status.st_size); |
---|
203 | uint16 lineno; |
---|
204 | jsval rval; |
---|
205 | JSBool ok; |
---|
206 | JSObject *script = JS_CompileFile (cx, global, filename); |
---|
207 | if (script != NULL) |
---|
208 | { |
---|
209 | (void) JS_ExecuteScript (cx, global, script, &rval); |
---|
210 | } |
---|
211 | else |
---|
212 | { |
---|
213 | char tmp1[1024]; |
---|
214 | sprintf (tmp1, "Unable to load JavaScript file %s", filename); |
---|
215 | free (filename); |
---|
216 | map *err = createMap ("text", tmp1); |
---|
217 | addToMap (err, "code", "NoApplicableCode"); |
---|
218 | printExceptionReportResponse (mc, err); |
---|
219 | freeMap (&err); |
---|
220 | free (err); |
---|
221 | JS_MaybeGC (cx); |
---|
222 | JS_DestroyContext (cx); |
---|
223 | JS_DestroyRuntime (rt); |
---|
224 | JS_ShutDown (); |
---|
225 | return -1; |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | /* Call a function in obj's scope. */ |
---|
230 | jsval argv[3]; |
---|
231 | JSObject *jsargv1 = JSObject_FromMaps (cx, *main_conf); |
---|
232 | argv[0] = OBJECT_TO_JSVAL (jsargv1); |
---|
233 | JSObject *jsargv2 = JSObject_FromMaps (cx, *inputs); |
---|
234 | argv[1] = OBJECT_TO_JSVAL (jsargv2); |
---|
235 | JSObject *jsargv3 = JSObject_FromMaps (cx, *outputs); |
---|
236 | argv[2] = OBJECT_TO_JSVAL (jsargv3); |
---|
237 | jsval rval1 = JSVAL_NULL; |
---|
238 | #ifdef JS_DEBUG |
---|
239 | fprintf (stderr, "object %p\n", (void *) argv[2]); |
---|
240 | #endif |
---|
241 | |
---|
242 | ok = JS_CallFunctionName (cx, global, s->name, 3, argv, &rval1); |
---|
243 | |
---|
244 | #ifdef JS_DEBUG |
---|
245 | fprintf (stderr, "object %p\n", (void *) argv[2]); |
---|
246 | #endif |
---|
247 | |
---|
248 | JSObject *d; |
---|
249 | if (ok == JS_TRUE && JSVAL_IS_OBJECT (rval1) == JS_TRUE) |
---|
250 | { |
---|
251 | #ifdef JS_DEBUG |
---|
252 | fprintf (stderr, "Function run sucessfully !\n"); |
---|
253 | #endif |
---|
254 | /* Should get a number back from the service function call. */ |
---|
255 | ok = JS_ValueToObject (cx, rval1, &d); |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | /* Unable to run JS function */ |
---|
260 | char tmp1[1024]; |
---|
261 | if (strlen (dbg) == 0) |
---|
262 | sprintf (dbg, "No result was found after the function call"); |
---|
263 | sprintf (tmp1, "Unable to run %s from the JavaScript file %s : \n %s", |
---|
264 | s->name, filename, dbg); |
---|
265 | #ifdef JS_DEBUG |
---|
266 | fprintf (stderr, "%s", tmp1); |
---|
267 | #endif |
---|
268 | map *err = createMap ("text", tmp1); |
---|
269 | addToMap (err, "code", "NoApplicableCode"); |
---|
270 | printExceptionReportResponse (*main_conf, err); |
---|
271 | freeMap (&err); |
---|
272 | free (err); |
---|
273 | free (filename); |
---|
274 | JS_MaybeGC (cx); |
---|
275 | JS_DestroyContext (cx); |
---|
276 | JS_DestroyRuntime (rt); |
---|
277 | JS_ShutDown (); |
---|
278 | // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847 |
---|
279 | return -1; |
---|
280 | } |
---|
281 | |
---|
282 | jsval t = OBJECT_TO_JSVAL (d); |
---|
283 | if (JS_IsArrayObject (cx, d)) |
---|
284 | { |
---|
285 | #ifdef JS_DEBUG |
---|
286 | fprintf (stderr, "An array was returned !\n"); |
---|
287 | #endif |
---|
288 | jsuint len; |
---|
289 | if ((JS_GetArrayLength (cx, d, &len) == JS_FALSE)) |
---|
290 | { |
---|
291 | #ifdef JS_DEBUG |
---|
292 | fprintf (stderr, "outputs array is empty\n"); |
---|
293 | #endif |
---|
294 | } |
---|
295 | jsval tmp1; |
---|
296 | JSBool hasResult = JS_GetElement (cx, d, 0, &tmp1); |
---|
297 | res = JSVAL_TO_INT (tmp1); |
---|
298 | #ifdef JS_DEBUG |
---|
299 | fprintf (stderr, " * %d * \n", res); |
---|
300 | #endif |
---|
301 | if (res == SERVICE_SUCCEEDED) |
---|
302 | { |
---|
303 | jsval tmp2; |
---|
304 | JSBool hasElement = JS_GetElement (cx, d, 1, &tmp2); |
---|
305 | if (hasElement == JS_TRUE) |
---|
306 | { |
---|
307 | freeMaps (outputs); |
---|
308 | free (*outputs); |
---|
309 | *outputs = mapsFromJSObject (cx, tmp2); |
---|
310 | } |
---|
311 | } |
---|
312 | else |
---|
313 | { |
---|
314 | jsval tmp3; |
---|
315 | JSBool hasConf = JS_GetElement (cx, d, 1, &tmp3); |
---|
316 | if (hasConf == JS_TRUE) |
---|
317 | { |
---|
318 | freeMaps (main_conf); |
---|
319 | free (*main_conf); |
---|
320 | *main_conf = mapsFromJSObject (cx, tmp3); |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | } |
---|
325 | else |
---|
326 | { |
---|
327 | #ifdef JS_DEBUG |
---|
328 | fprintf (stderr, "The service didn't return an array !\n"); |
---|
329 | #endif |
---|
330 | /** |
---|
331 | * Extract result |
---|
332 | */ |
---|
333 | jsval tmp1; |
---|
334 | JSBool hasResult = JS_GetProperty (cx, d, "result", &tmp1); |
---|
335 | res = JSVAL_TO_INT (tmp1); |
---|
336 | |
---|
337 | #ifdef JS_DEBUG |
---|
338 | fprintf (stderr, " * %d * \n", res); |
---|
339 | #endif |
---|
340 | /** |
---|
341 | * Extract outputs when available. |
---|
342 | */ |
---|
343 | jsval tmp2; |
---|
344 | JSBool hasElement = JS_GetProperty (cx, d, "outputs", &tmp2); |
---|
345 | if (!JSVAL_IS_VOID (tmp2) && hasElement == JS_TRUE) |
---|
346 | { |
---|
347 | freeMaps (outputs); |
---|
348 | free (*outputs); |
---|
349 | *outputs = mapsFromJSObject (cx, tmp2); |
---|
350 | } |
---|
351 | JS_MaybeGC (cx); |
---|
352 | #ifdef JS_DEBUG |
---|
353 | if (JSVAL_IS_VOID (tmp2)) |
---|
354 | fprintf (stderr, "No outputs property returned\n"); |
---|
355 | else |
---|
356 | { |
---|
357 | if (JS_IsArrayObject (cx, JSVAL_TO_OBJECT (tmp2))) |
---|
358 | fprintf (stderr, "outputs is an array as expected\n"); |
---|
359 | else |
---|
360 | fprintf (stderr, "outputs is not an array as expected\n"); |
---|
361 | } |
---|
362 | JS_MaybeGC (cx); |
---|
363 | #endif |
---|
364 | |
---|
365 | /** |
---|
366 | * Extract conf when available. |
---|
367 | */ |
---|
368 | jsval tmp3; |
---|
369 | JSBool hasConf = JS_GetProperty (cx, d, "conf", &tmp3); |
---|
370 | if (!JSVAL_IS_VOID (tmp3) && hasConf == JS_TRUE) |
---|
371 | { |
---|
372 | freeMaps (main_conf); |
---|
373 | free (*main_conf); |
---|
374 | *main_conf = mapsFromJSObject (cx, tmp3); |
---|
375 | } |
---|
376 | JS_MaybeGC (cx); |
---|
377 | |
---|
378 | #ifdef JS_DEBUG |
---|
379 | dumpMaps (*outputs); |
---|
380 | #endif |
---|
381 | } |
---|
382 | /* Cleanup. */ |
---|
383 | JS_MaybeGC (cx); |
---|
384 | JS_DestroyContext (cx); |
---|
385 | JS_DestroyRuntime (rt); |
---|
386 | JS_ShutDown (); |
---|
387 | free (filename); |
---|
388 | #ifdef JS_DEBUG |
---|
389 | fprintf (stderr, "Returned value %d\n", res); |
---|
390 | #endif |
---|
391 | return res; |
---|
392 | } |
---|
393 | |
---|
394 | JSObject * |
---|
395 | loadZooApiFile (JSContext * cx, JSObject * global, char *filename) |
---|
396 | { |
---|
397 | struct stat api_status; |
---|
398 | int s = stat (filename, &api_status); |
---|
399 | if (s == 0) |
---|
400 | { |
---|
401 | jsval rval; |
---|
402 | JSBool ok; |
---|
403 | JSObject *script = |
---|
404 | JS_CompileFile (cx, JS_GetGlobalObject (cx), filename); |
---|
405 | if (script != NULL) |
---|
406 | { |
---|
407 | (void) JS_ExecuteScript (cx, JS_GetGlobalObject (cx), script, |
---|
408 | &rval); |
---|
409 | #ifdef JS_DEBUG |
---|
410 | fprintf (stderr, |
---|
411 | "**************\n%s correctly loaded\n**************\n", |
---|
412 | filename); |
---|
413 | #endif |
---|
414 | return script; |
---|
415 | } |
---|
416 | #ifdef JS_DEBUG |
---|
417 | else |
---|
418 | fprintf (stderr, |
---|
419 | "\n**************\nUnable to run %s\n**************\n", |
---|
420 | filename); |
---|
421 | #endif |
---|
422 | } |
---|
423 | #ifdef JS_DEBUG |
---|
424 | else |
---|
425 | fprintf (stderr, "\n**************\nUnable to load %s\n**************\n", |
---|
426 | filename); |
---|
427 | #endif |
---|
428 | return NULL; |
---|
429 | } |
---|
430 | |
---|
431 | JSObject * |
---|
432 | JSObject_FromMaps (JSContext * cx, maps * t) |
---|
433 | { |
---|
434 | |
---|
435 | JSObject *res = JS_NewObject (cx, NULL, NULL, NULL); |
---|
436 | //JSObject *res = JS_NewArrayObject(cx, 0, NULL); |
---|
437 | if (res == NULL) |
---|
438 | fprintf (stderr, "Array Object is NULL!\n"); |
---|
439 | maps *tmp = t; |
---|
440 | while (tmp != NULL) |
---|
441 | { |
---|
442 | jsuint len; |
---|
443 | JSObject *res1 = JS_NewObject (cx, NULL, NULL, NULL); |
---|
444 | JSObject *pval = JSObject_FromMap (cx, tmp->content); |
---|
445 | jsval pvalj = OBJECT_TO_JSVAL (pval); |
---|
446 | JS_SetProperty (cx, res, tmp->name, &pvalj); |
---|
447 | #ifdef JS_DEBUG |
---|
448 | fprintf (stderr, "Length of the Array %d, element : %s added \n", len, |
---|
449 | tmp->name); |
---|
450 | #endif |
---|
451 | tmp = tmp->next; |
---|
452 | } |
---|
453 | return res; |
---|
454 | } |
---|
455 | |
---|
456 | JSObject * |
---|
457 | JSObject_FromMap (JSContext * cx, map * t) |
---|
458 | { |
---|
459 | JSObject *res = JS_NewObject (cx, NULL, NULL, NULL); |
---|
460 | jsval resf = OBJECT_TO_JSVAL (res); |
---|
461 | map *tmpm = t; |
---|
462 | map *isArray = getMap (t, "isArray"); |
---|
463 | map *isBinary = getMap (t, "size"); |
---|
464 | map *tmap = getMapType (t); |
---|
465 | #ifdef JS_DEBUG |
---|
466 | if (tmap == NULL) |
---|
467 | fprintf (stderr, "tmap is null !\n"); |
---|
468 | else |
---|
469 | fprintf (stderr, "tmap is not null ! (%s = %s)\n", tmap->name, |
---|
470 | tmap->value); |
---|
471 | #endif |
---|
472 | while (tmpm != NULL) |
---|
473 | { |
---|
474 | jsval jsstr; |
---|
475 | if ((isArray == NULL && isBinary != NULL |
---|
476 | && strncasecmp (tmpm->name, "value", 5) == 0)) |
---|
477 | jsstr = |
---|
478 | STRING_TO_JSVAL (JS_NewStringCopyN |
---|
479 | (cx, tmpm->value, atoi (isBinary->value))); |
---|
480 | else |
---|
481 | jsstr = |
---|
482 | STRING_TO_JSVAL (JS_NewStringCopyN |
---|
483 | (cx, tmpm->value, strlen (tmpm->value))); |
---|
484 | JS_SetProperty (cx, res, tmpm->name, &jsstr); |
---|
485 | #ifdef JS_DEBUG |
---|
486 | fprintf (stderr, "[JS] %s => %s\n", tmpm->name, tmpm->value); |
---|
487 | #endif |
---|
488 | tmpm = tmpm->next; |
---|
489 | } |
---|
490 | if (isArray != NULL) |
---|
491 | { |
---|
492 | map *len = getMap (t, "length"); |
---|
493 | int cnt = atoi (len->value); |
---|
494 | JSObject *values = JS_NewArrayObject (cx, cnt, NULL); |
---|
495 | JSObject *mvalues = JS_NewArrayObject (cx, cnt, NULL); |
---|
496 | map *tmpm1, *tmpm2; |
---|
497 | int i = 0; |
---|
498 | for (i = 0; i < cnt; i++) |
---|
499 | { |
---|
500 | tmpm1 = getMapArray (t, "value", i); |
---|
501 | tmpm2 = getMapArray (t, tmap->name, i); |
---|
502 | if (tmpm1 != NULL) |
---|
503 | { |
---|
504 | jsval jsstr = |
---|
505 | STRING_TO_JSVAL (JS_NewStringCopyN |
---|
506 | (cx, tmpm1->value, strlen (tmpm1->value))); |
---|
507 | JS_SetElement (cx, values, i, &jsstr); |
---|
508 | } |
---|
509 | if (tmpm2 != NULL) |
---|
510 | { |
---|
511 | jsval jsstr = |
---|
512 | STRING_TO_JSVAL (JS_NewStringCopyN |
---|
513 | (cx, tmpm2->value, strlen (tmpm2->value))); |
---|
514 | JS_SetElement (cx, mvalues, i, &jsstr); |
---|
515 | } |
---|
516 | } |
---|
517 | jsval jvalues = OBJECT_TO_JSVAL (values); |
---|
518 | jsval jmvalues = OBJECT_TO_JSVAL (mvalues); |
---|
519 | JS_SetProperty (cx, res, "value", &jvalues); |
---|
520 | JS_SetProperty (cx, res, tmap->name, &jmvalues); |
---|
521 | } |
---|
522 | return res; |
---|
523 | } |
---|
524 | |
---|
525 | maps * |
---|
526 | mapsFromJSObject (JSContext * cx, jsval t) |
---|
527 | { |
---|
528 | maps *res = NULL; |
---|
529 | maps *tres = NULL; |
---|
530 | jsint oi = 0; |
---|
531 | JSObject *tt = JSVAL_TO_OBJECT (t); |
---|
532 | if (JS_IsArrayObject (cx, tt)) |
---|
533 | { |
---|
534 | #ifdef JS_DEBUG |
---|
535 | fprintf (stderr, "Is finally an array !\n"); |
---|
536 | #endif |
---|
537 | } |
---|
538 | else |
---|
539 | { |
---|
540 | #ifdef JS_DEBUG |
---|
541 | fprintf (stderr, "Is not an array !\n"); |
---|
542 | #endif |
---|
543 | JSIdArray *idp = JS_Enumerate (cx, tt); |
---|
544 | if (idp != NULL) |
---|
545 | { |
---|
546 | int index; |
---|
547 | jsdouble argNum; |
---|
548 | #ifdef JS_DEBUG |
---|
549 | fprintf (stderr, "Properties length : %d \n", idp->length); |
---|
550 | #endif |
---|
551 | |
---|
552 | for (index = 0, argNum = idp->length; index < argNum; index++) |
---|
553 | { |
---|
554 | jsval id = idp->vector[index]; |
---|
555 | jsval vp; |
---|
556 | JSString *str; |
---|
557 | JS_IdToValue (cx, id, &vp); |
---|
558 | char *c, *tmp; |
---|
559 | JSString *jsmsg; |
---|
560 | size_t len1; |
---|
561 | jsmsg = JS_ValueToString (cx, vp); |
---|
562 | len1 = JS_GetStringLength (jsmsg); |
---|
563 | |
---|
564 | tmp = JS_EncodeString (cx, jsmsg); |
---|
565 | tres = (maps *) malloc (MAPS_SIZE); |
---|
566 | tres->name = zStrdup (tmp); |
---|
567 | tres->content = NULL; |
---|
568 | tres->next = NULL; |
---|
569 | |
---|
570 | jsval nvp = JSVAL_NULL; |
---|
571 | if ((JS_GetProperty (cx, tt, tmp, &nvp) == JS_FALSE)) |
---|
572 | { |
---|
573 | #ifdef JS_DEBUG |
---|
574 | fprintf (stderr, |
---|
575 | "Enumerate id : %d => %s => No more value\n", oi, |
---|
576 | tmp); |
---|
577 | #endif |
---|
578 | } |
---|
579 | free (tmp); |
---|
580 | JSObject *nvp1 = JSVAL_TO_OBJECT (JSVAL_NULL); |
---|
581 | JS_ValueToObject (cx, nvp, &nvp1); |
---|
582 | jsval nvp1j = OBJECT_TO_JSVAL (nvp1); |
---|
583 | if (JSVAL_IS_OBJECT (nvp1j)) |
---|
584 | { |
---|
585 | tres->content = mapFromJSObject (cx, nvp1j); |
---|
586 | } |
---|
587 | |
---|
588 | if (res == NULL) |
---|
589 | res = dupMaps (&tres); |
---|
590 | else |
---|
591 | addMapsToMaps (&res, tres); |
---|
592 | freeMaps (&tres); |
---|
593 | free (tres); |
---|
594 | tres = NULL; |
---|
595 | |
---|
596 | } |
---|
597 | JS_DestroyIdArray (cx, idp); |
---|
598 | } |
---|
599 | } |
---|
600 | |
---|
601 | jsuint len; |
---|
602 | JSBool hasLen = JS_GetArrayLength (cx, tt, &len); |
---|
603 | #ifdef JS_DEBUG |
---|
604 | if (hasLen == JS_FALSE) |
---|
605 | { |
---|
606 | fprintf (stderr, "outputs array is empty\n"); |
---|
607 | } |
---|
608 | fprintf (stderr, "outputs array length : %d\n", len); |
---|
609 | #endif |
---|
610 | for (oi = 0; oi < len; oi++) |
---|
611 | { |
---|
612 | #ifdef JS_DEBUG |
---|
613 | fprintf (stderr, "outputs array length : %d step %d \n", len, oi); |
---|
614 | #endif |
---|
615 | jsval tmp1; |
---|
616 | JSBool hasElement = JS_GetElement (cx, tt, oi, &tmp1); |
---|
617 | JSObject *otmp1 = JSVAL_TO_OBJECT (tmp1); |
---|
618 | JSIdArray *idp = JS_Enumerate (cx, otmp1); |
---|
619 | if (idp != NULL) |
---|
620 | { |
---|
621 | int index; |
---|
622 | jsdouble argNum; |
---|
623 | #ifdef JS_DEBUG |
---|
624 | fprintf (stderr, "Properties length : %d \n", idp->length); |
---|
625 | #endif |
---|
626 | tres = (maps *) malloc (MAPS_SIZE); |
---|
627 | tres->name = NULL; |
---|
628 | tres->content = NULL; |
---|
629 | tres->next = NULL; |
---|
630 | |
---|
631 | for (index = 0, argNum = idp->length; index < argNum; index++) |
---|
632 | { |
---|
633 | jsval id = idp->vector[index]; |
---|
634 | jsval vp; |
---|
635 | JSString *str; |
---|
636 | JS_IdToValue (cx, id, &vp); |
---|
637 | char *c, *tmp; |
---|
638 | JSString *jsmsg; |
---|
639 | size_t len1; |
---|
640 | jsmsg = JS_ValueToString (cx, vp); |
---|
641 | len1 = JS_GetStringLength (jsmsg); |
---|
642 | tmp = JS_EncodeString (cx, jsmsg); |
---|
643 | #ifdef JS_DEBUG |
---|
644 | fprintf (stderr, "Enumerate id : %d => %s\n", oi, tmp); |
---|
645 | #endif |
---|
646 | jsval nvp = JSVAL_NULL; |
---|
647 | if ((JS_GetProperty (cx, JSVAL_TO_OBJECT (tmp1), tmp, &nvp) == |
---|
648 | JS_FALSE)) |
---|
649 | { |
---|
650 | #ifdef JS_DEBUG |
---|
651 | fprintf (stderr, |
---|
652 | "Enumerate id : %d => %s => No more value\n", oi, |
---|
653 | tmp); |
---|
654 | #endif |
---|
655 | } |
---|
656 | free (tmp); |
---|
657 | if (JSVAL_IS_OBJECT (nvp)) |
---|
658 | { |
---|
659 | #ifdef JS_DEBUG |
---|
660 | fprintf (stderr, "JSVAL NVP IS OBJECT\n"); |
---|
661 | #endif |
---|
662 | } |
---|
663 | |
---|
664 | JSObject *nvp1 = JSVAL_TO_OBJECT (JSVAL_NULL); |
---|
665 | JS_ValueToObject (cx, nvp, &nvp1); |
---|
666 | jsval nvp1j = OBJECT_TO_JSVAL (nvp1); |
---|
667 | if (JSVAL_IS_OBJECT (nvp1j)) |
---|
668 | { |
---|
669 | JSString *jsmsg1; |
---|
670 | char *tmp1, *tmp2; |
---|
671 | JSObject *nvp2 = JSVAL_TO_OBJECT (JSVAL_NULL); |
---|
672 | jsmsg1 = JS_ValueToString (cx, nvp1j); |
---|
673 | len1 = JS_GetStringLength (jsmsg1); |
---|
674 | tmp1 = JS_EncodeString (cx, jsmsg1); |
---|
675 | tmp2 = JS_EncodeString (cx, jsmsg); |
---|
676 | #ifdef JS_DEBUG |
---|
677 | fprintf (stderr, "JSVAL NVP1J IS OBJECT %s = %s\n", |
---|
678 | JS_EncodeString (cx, jsmsg), tmp1); |
---|
679 | #endif |
---|
680 | if (strcasecmp (tmp1, "[object Object]") == 0) |
---|
681 | { |
---|
682 | tres->name = zStrdup (tmp2); |
---|
683 | tres->content = mapFromJSObject (cx, nvp1j); |
---|
684 | } |
---|
685 | else if (strcasecmp (tmp2, "name") == 0) |
---|
686 | { |
---|
687 | tres->name = zStrdup (tmp1); |
---|
688 | } |
---|
689 | else |
---|
690 | { |
---|
691 | if (tres->content == NULL) |
---|
692 | tres->content = createMap (tmp2, tmp1); |
---|
693 | else |
---|
694 | addToMap (tres->content, tmp2, tmp1); |
---|
695 | } |
---|
696 | free (tmp1); |
---|
697 | free (tmp2); |
---|
698 | } |
---|
699 | #ifdef JS_DEBUG |
---|
700 | else |
---|
701 | fprintf (stderr, "JSVAL NVP1J IS NOT OBJECT !!\n"); |
---|
702 | #endif |
---|
703 | } |
---|
704 | #ifdef JS_DEBUG |
---|
705 | dumpMaps (tres); |
---|
706 | #endif |
---|
707 | if (res == NULL) |
---|
708 | res = dupMaps (&tres); |
---|
709 | else |
---|
710 | addMapsToMaps (&res, tres); |
---|
711 | freeMaps (&tres); |
---|
712 | free (tres); |
---|
713 | tres = NULL; |
---|
714 | JS_DestroyIdArray (cx, idp); |
---|
715 | } |
---|
716 | } |
---|
717 | #ifdef JS_DEBUG |
---|
718 | dumpMaps (res); |
---|
719 | #endif |
---|
720 | return res; |
---|
721 | } |
---|
722 | |
---|
723 | map * |
---|
724 | mapFromJSObject (JSContext * cx, jsval t) |
---|
725 | { |
---|
726 | map *res = NULL; |
---|
727 | JSIdArray *idp = JS_Enumerate (cx, JSVAL_TO_OBJECT (t)); |
---|
728 | #ifdef JS_DEBUG |
---|
729 | fprintf (stderr, "Properties %p\n", (void *) t); |
---|
730 | #endif |
---|
731 | if (idp != NULL) |
---|
732 | { |
---|
733 | int index; |
---|
734 | jsdouble argNum; |
---|
735 | #ifdef JS_DEBUG |
---|
736 | fprintf (stderr, "Properties length : %d \n", idp->length); |
---|
737 | #endif |
---|
738 | for (index = 0, argNum = idp->length; index < argNum; index++) |
---|
739 | { |
---|
740 | jsval id = idp->vector[index]; |
---|
741 | jsval vp; |
---|
742 | JSString *str; |
---|
743 | JS_IdToValue (cx, id, &vp); |
---|
744 | char *c, *tmp, *tmp1; |
---|
745 | JSString *jsmsg, *jsmsg1; |
---|
746 | size_t len, len1; |
---|
747 | jsmsg = JS_ValueToString (cx, vp); |
---|
748 | len = JS_GetStringLength (jsmsg); |
---|
749 | jsval nvp; |
---|
750 | tmp = JS_EncodeString (cx, jsmsg); |
---|
751 | JS_GetProperty (cx, JSVAL_TO_OBJECT (t), tmp, &nvp); |
---|
752 | jsmsg1 = JS_ValueToString (cx, nvp); |
---|
753 | len1 = JS_GetStringLength (jsmsg1); |
---|
754 | tmp1 = JS_EncodeString (cx, jsmsg1); |
---|
755 | #ifdef JS_DEBUG |
---|
756 | fprintf (stderr, "Enumerate id : %d [ %s => %s ]\n", index, tmp, |
---|
757 | tmp1); |
---|
758 | #endif |
---|
759 | if (res != NULL) |
---|
760 | { |
---|
761 | #ifdef JS_DEBUG |
---|
762 | fprintf (stderr, "%s - %s\n", tmp, tmp1); |
---|
763 | #endif |
---|
764 | addToMap (res, tmp, tmp1); |
---|
765 | } |
---|
766 | else |
---|
767 | { |
---|
768 | res = createMap (tmp, tmp1); |
---|
769 | res->next = NULL; |
---|
770 | } |
---|
771 | free (tmp); |
---|
772 | free (tmp1); |
---|
773 | #ifdef JS_DEBUG |
---|
774 | dumpMap (res); |
---|
775 | #endif |
---|
776 | } |
---|
777 | JS_DestroyIdArray (cx, idp); |
---|
778 | } |
---|
779 | #ifdef JS_DEBUG |
---|
780 | dumpMap (res); |
---|
781 | #endif |
---|
782 | return res; |
---|
783 | } |
---|
784 | |
---|
785 | /* The error reporter callback. */ |
---|
786 | void |
---|
787 | reportError (JSContext * cx, const char *message, JSErrorReport * report) |
---|
788 | { |
---|
789 | sprintf (dbg, "%s:%u:%s\n", |
---|
790 | report->filename ? report->filename : "<no filename>", |
---|
791 | (unsigned int) report->lineno, message); |
---|
792 | #ifdef JS_DEBUG |
---|
793 | fprintf (stderr, "%s", dbg); |
---|
794 | #endif |
---|
795 | fflush (stderr); |
---|
796 | } |
---|
797 | |
---|
798 | char * |
---|
799 | JSValToChar (JSContext * context, jsval * arg) |
---|
800 | { |
---|
801 | char *c; |
---|
802 | char *tmp; |
---|
803 | JSString *jsmsg; |
---|
804 | size_t len; |
---|
805 | int i; |
---|
806 | if (!JSVAL_IS_STRING (*arg)) |
---|
807 | { |
---|
808 | return NULL; |
---|
809 | } |
---|
810 | jsmsg = JS_ValueToString (context, *arg); |
---|
811 | len = JS_GetStringLength (jsmsg); |
---|
812 | tmp = JS_EncodeString (context, jsmsg); |
---|
813 | c = (char *) malloc ((len + 1) * sizeof (char)); |
---|
814 | c[len] = '\0'; |
---|
815 | #ifdef ULINET_DEBUG |
---|
816 | fprintf (stderr, "%d \n", len); |
---|
817 | #endif |
---|
818 | for (i = 0; i < len; i++) |
---|
819 | { |
---|
820 | c[i] = tmp[i]; |
---|
821 | c[i + 1] = 0; |
---|
822 | } |
---|
823 | #ifdef ULINET_DEBUG |
---|
824 | fprintf (stderr, "%s \n", c); |
---|
825 | #endif |
---|
826 | return c; |
---|
827 | } |
---|
828 | |
---|
829 | HINTERNET |
---|
830 | setHeader (HINTERNET * handle, JSContext * cx, JSObject * header) |
---|
831 | { |
---|
832 | jsuint length = 0; |
---|
833 | jsint i = 0; |
---|
834 | char *tmp1; |
---|
835 | #ifdef ULINET_DEBUG |
---|
836 | fprintf (stderr, "setHeader\n"); |
---|
837 | #endif |
---|
838 | if (JS_IsArrayObject (cx, header)) |
---|
839 | { |
---|
840 | #ifdef ULINET_DEBUG |
---|
841 | fprintf (stderr, "header is an array\n"); |
---|
842 | #endif |
---|
843 | JS_GetArrayLength (cx, header, &length); |
---|
844 | #ifdef ULINET_DEBUG |
---|
845 | fprintf (stderr, "header is an array of %d elements\n", length); |
---|
846 | #endif |
---|
847 | handle->ihandle[handle->nb].header = NULL; |
---|
848 | for (i = 0; i < length; i++) |
---|
849 | { |
---|
850 | jsval tmp; |
---|
851 | JS_GetElement (cx, header, i, &tmp); |
---|
852 | tmp1 = JSValToChar (cx, &tmp); |
---|
853 | #ifdef ULINET_DEBUG |
---|
854 | curl_easy_setopt (handle->ihandle[handle->nb].handle, |
---|
855 | CURLOPT_VERBOSE, 1); |
---|
856 | fprintf (stderr, "Element of array n° %d, value : %s\n", i, tmp1); |
---|
857 | #endif |
---|
858 | handle->ihandle[handle->nb].header = |
---|
859 | curl_slist_append (handle->ihandle[handle->nb].header, tmp1); |
---|
860 | free (tmp1); |
---|
861 | } |
---|
862 | } |
---|
863 | else |
---|
864 | { |
---|
865 | fprintf (stderr, "not an array !!!!!!!\n"); |
---|
866 | } |
---|
867 | return *handle; |
---|
868 | } |
---|
869 | |
---|
870 | JSBool |
---|
871 | JSTranslate (JSContext * cx, uintN argc, jsval * argv1) |
---|
872 | { |
---|
873 | jsval *argv = JS_ARGV (cx, argv1); |
---|
874 | char *str = JSValToChar (cx, &argv[0]); |
---|
875 | char *tmpValue = _ss (str); |
---|
876 | JS_SET_RVAL (cx, argv1, |
---|
877 | STRING_TO_JSVAL (JS_NewStringCopyN |
---|
878 | (cx, tmpValue, strlen (tmpValue)))); |
---|
879 | JS_MaybeGC (cx); |
---|
880 | return JS_TRUE; |
---|
881 | } |
---|
882 | |
---|
883 | JSBool |
---|
884 | JSRequest (JSContext * cx, uintN argc, jsval * argv1) |
---|
885 | { |
---|
886 | jsval *argv = JS_ARGV (cx, argv1); |
---|
887 | HINTERNET hInternet; |
---|
888 | HINTERNET res; |
---|
889 | HINTERNET res1; |
---|
890 | JSObject *header; |
---|
891 | char *url; |
---|
892 | char *method; |
---|
893 | char *tmpValue; |
---|
894 | size_t dwRead; |
---|
895 | int i = 0; |
---|
896 | JS_MaybeGC (cx); |
---|
897 | hInternet = InternetOpen ("ZooWPSClient\0", |
---|
898 | INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); |
---|
899 | if (!CHECK_INET_HANDLE (hInternet)) |
---|
900 | return JS_FALSE; |
---|
901 | if (argc >= 2) |
---|
902 | { |
---|
903 | method = JSValToChar (cx, &argv[0]); |
---|
904 | url = JSValToChar (cx, &argv[1]); |
---|
905 | } |
---|
906 | else |
---|
907 | { |
---|
908 | method = zStrdup ("GET"); |
---|
909 | url = JSValToChar (cx, argv); |
---|
910 | } |
---|
911 | hInternet.waitingRequests[hInternet.nb] = strdup (url); |
---|
912 | if (argc == 4) |
---|
913 | { |
---|
914 | char *body; |
---|
915 | body = JSValToChar (cx, &argv[2]); |
---|
916 | header = JSVAL_TO_OBJECT (argv[3]); |
---|
917 | #ifdef ULINET_DEBUG |
---|
918 | fprintf (stderr, "URL (%s) \nBODY (%s)\n", url, body); |
---|
919 | #endif |
---|
920 | if (JS_IsArrayObject (cx, header)) |
---|
921 | setHeader (&hInternet, cx, header); |
---|
922 | #ifdef ULINET_DEBUG |
---|
923 | fprintf (stderr, "BODY (%s)\n", body); |
---|
924 | #endif |
---|
925 | InternetOpenUrl (&hInternet, hInternet.waitingRequests[hInternet.nb], |
---|
926 | body, strlen (body), INTERNET_FLAG_NO_CACHE_WRITE, 0); |
---|
927 | processDownloads (&hInternet); |
---|
928 | free (body); |
---|
929 | } |
---|
930 | else |
---|
931 | { |
---|
932 | if (argc == 3) |
---|
933 | { |
---|
934 | char *body = JSValToChar (cx, &argv[2]); |
---|
935 | InternetOpenUrl (&hInternet, |
---|
936 | hInternet.waitingRequests[hInternet.nb], body, |
---|
937 | strlen (body), INTERNET_FLAG_NO_CACHE_WRITE, 0); |
---|
938 | processDownloads (&hInternet); |
---|
939 | free (body); |
---|
940 | } |
---|
941 | else |
---|
942 | { |
---|
943 | InternetOpenUrl (&hInternet, |
---|
944 | hInternet.waitingRequests[hInternet.nb], NULL, 0, |
---|
945 | INTERNET_FLAG_NO_CACHE_WRITE, 0); |
---|
946 | processDownloads (&hInternet); |
---|
947 | } |
---|
948 | } |
---|
949 | tmpValue = |
---|
950 | (char *) malloc ((hInternet.ihandle[0].nDataLen + 1) * sizeof (char)); |
---|
951 | InternetReadFile (hInternet.ihandle[0], (LPVOID) tmpValue, |
---|
952 | hInternet.ihandle[0].nDataLen, &dwRead); |
---|
953 | #ifdef ULINET_DEBUG |
---|
954 | fprintf (stderr, "content downloaded (%d) (%s) \n", dwRead, tmpValue); |
---|
955 | #endif |
---|
956 | if (dwRead == 0) |
---|
957 | { |
---|
958 | JS_SET_RVAL (cx, argv1, |
---|
959 | STRING_TO_JSVAL (JS_NewStringCopyN |
---|
960 | (cx, "Unable to access the file.", |
---|
961 | strlen ("Unable to access the file.")))); |
---|
962 | return JS_TRUE; |
---|
963 | } |
---|
964 | |
---|
965 | #ifdef ULINET_DEBUG |
---|
966 | fprintf (stderr, "content downloaded (%d) (%s) \n", dwRead, tmpValue); |
---|
967 | #endif |
---|
968 | JS_SET_RVAL (cx, argv1, |
---|
969 | STRING_TO_JSVAL (JS_NewStringCopyN |
---|
970 | (cx, tmpValue, strlen (tmpValue)))); |
---|
971 | free (url); |
---|
972 | if (argc >= 2) |
---|
973 | free (method); |
---|
974 | InternetCloseHandle (&hInternet); |
---|
975 | JS_MaybeGC (cx); |
---|
976 | return JS_TRUE; |
---|
977 | } |
---|