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