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