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