[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 | |
---|
[383] | 27 | #ifndef JSCLASS_GLOBAL_FLAGS |
---|
| 28 | #define JSCLSAS_GLOBAL_FLAGS 0 |
---|
| 29 | #endif |
---|
| 30 | |
---|
[1] | 31 | static char dbg[1024]; |
---|
| 32 | |
---|
[274] | 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]); |
---|
[471] | 41 | char *tmp=JS_EncodeString(cx,jsmsg); |
---|
| 42 | fprintf(stderr,"[ZOO-API:JS] %s\n",tmp); |
---|
| 43 | free(tmp); |
---|
[274] | 44 | } |
---|
| 45 | JS_MaybeGC(cx); |
---|
| 46 | |
---|
| 47 | return JS_TRUE; |
---|
| 48 | } |
---|
| 49 | |
---|
[336] | 50 | JSBool |
---|
| 51 | JSLoadScripts(JSContext *cx, uintN argc, jsval *argv1) |
---|
| 52 | { |
---|
[364] | 53 | //map* request = JS_GetContextPrivate(cx); |
---|
| 54 | //map* tmpm1=getMap(request,"metapath"); |
---|
[336] | 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]); |
---|
[364] | 66 | char *api0=(char*)malloc((strlen(ntmp)+strlen(filename)+2)*sizeof(char)); |
---|
| 67 | sprintf(api0,"%s/%s",ntmp,filename); |
---|
[336] | 68 | #ifdef JS_DEBUG |
---|
| 69 | fprintf(stderr,"Trying to load %s\n",api0); |
---|
[364] | 70 | fflush(stderr); |
---|
[336] | 71 | #endif |
---|
| 72 | JSObject *api_script1=loadZooApiFile(cx,JS_GetGlobalObject(cx),api0); |
---|
[471] | 73 | free(api0); |
---|
[336] | 74 | } |
---|
| 75 | JS_MaybeGC(cx); |
---|
| 76 | JS_SET_RVAL(cx, argv1, JSVAL_VOID); |
---|
| 77 | |
---|
| 78 | return JS_TRUE; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
[1] | 82 | int zoo_js_support(maps** main_conf,map* request,service* s, |
---|
| 83 | maps **inputs,maps **outputs) |
---|
| 84 | { |
---|
[9] | 85 | maps* main=*main_conf; |
---|
| 86 | maps* _inputs=*inputs; |
---|
| 87 | maps* _outputs=*outputs; |
---|
| 88 | |
---|
[1] | 89 | /* The class of the global object. */ |
---|
[383] | 90 | JSClass global_class= { |
---|
[1] | 91 | "global", JSCLASS_GLOBAL_FLAGS, |
---|
[364] | 92 | JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, |
---|
[1] | 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; |
---|
[9] | 106 | |
---|
[1] | 107 | /* Create a context. */ |
---|
[384] | 108 | cx = JS_NewContext(rt,8192); |
---|
[1] | 109 | if (cx == NULL){ |
---|
| 110 | return 1; |
---|
| 111 | } |
---|
[383] | 112 | JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT); |
---|
[1] | 113 | JS_SetVersion(cx, JSVERSION_LATEST); |
---|
| 114 | JS_SetErrorReporter(cx, reportError); |
---|
| 115 | |
---|
| 116 | /* Create the global object. */ |
---|
[328] | 117 | global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); |
---|
[1] | 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 | } |
---|
[274] | 124 | |
---|
[403] | 125 | /* Define specific function and global variable to share with JS runtime |
---|
| 126 | */ |
---|
[368] | 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; |
---|
[1] | 133 | if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0)) |
---|
| 134 | return 1; |
---|
[377] | 135 | if (!JS_DefineFunction(cx, global, "ZOOTranslate", JSTranslate, 4, 0)) |
---|
| 136 | return 1; |
---|
[26] | 137 | if (!JS_DefineFunction(cx, global, "ZOOUpdateStatus", JSUpdateStatus, 2, 0)) |
---|
| 138 | return 1; |
---|
[274] | 139 | if (!JS_DefineFunction(cx, global, "alert", JSAlert, 2, 0)) |
---|
[336] | 140 | return 1; |
---|
| 141 | if (!JS_DefineFunction(cx, global, "importScripts", JSLoadScripts, 1, 0)) |
---|
[274] | 142 | return 1; |
---|
[1] | 143 | |
---|
[336] | 144 | /** |
---|
| 145 | * Add private context object |
---|
| 146 | */ |
---|
| 147 | void* cxPrivate = request; |
---|
| 148 | JS_SetContextPrivate(cx,cxPrivate); |
---|
[505] | 149 | |
---|
[42] | 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 | */ |
---|
[505] | 157 | char *api0=(char*)malloc((strlen(ntmp)+17)*sizeof(char)); |
---|
| 158 | sprintf(api0,"%s/ZOO-proj4js.js",ntmp); |
---|
[274] | 159 | #ifdef JS_DEBUG |
---|
[42] | 160 | fprintf(stderr,"Trying to load %s\n",api0); |
---|
[274] | 161 | #endif |
---|
| 162 | JSObject *api_script1=loadZooApiFile(cx,global,api0); |
---|
[471] | 163 | free(api0); |
---|
[42] | 164 | fflush(stderr); |
---|
| 165 | |
---|
[505] | 166 | char *api1=(char*)malloc((strlen(ntmp)+13)*sizeof(char)); |
---|
| 167 | sprintf(api1,"%s/ZOO-api.js",ntmp); |
---|
[274] | 168 | #ifdef JS_DEBUG |
---|
[42] | 169 | fprintf(stderr,"Trying to load %s\n",api1); |
---|
[274] | 170 | #endif |
---|
| 171 | JSObject *api_script2=loadZooApiFile(cx,global,api1); |
---|
[471] | 172 | free(api1); |
---|
[42] | 173 | fflush(stderr); |
---|
| 174 | |
---|
[1] | 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"); |
---|
[42] | 181 | |
---|
[364] | 182 | char *filename=(char*)malloc(strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+3); |
---|
[329] | 183 | sprintf(filename,"%s/%s/%s",ntmp,tmpm1->value,tmpm2->value); |
---|
| 184 | filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+2]=0; |
---|
[274] | 185 | #ifdef JS_DEBUG |
---|
[26] | 186 | fprintf(stderr,"FILENAME %s\n",filename); |
---|
[274] | 187 | #endif |
---|
[1] | 188 | struct stat file_status; |
---|
| 189 | stat(filename, &file_status); |
---|
[471] | 190 | //char *source=(char*)malloc(file_status.st_size); |
---|
[1] | 191 | uint16 lineno; |
---|
| 192 | jsval rval; |
---|
| 193 | JSBool ok ; |
---|
[274] | 194 | JSObject *script = JS_CompileFile(cx, global, filename); |
---|
[9] | 195 | if(script!=NULL){ |
---|
[1] | 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); |
---|
[471] | 201 | free(filename); |
---|
[1] | 202 | map* err=createMap("text",tmp1); |
---|
[471] | 203 | addToMap(err,"code","NoApplicableCode"); |
---|
[1] | 204 | printExceptionReportResponse(mc,err); |
---|
[471] | 205 | freeMap(&err); |
---|
| 206 | free(err); |
---|
| 207 | JS_MaybeGC(cx); |
---|
[1] | 208 | JS_DestroyContext(cx); |
---|
| 209 | JS_DestroyRuntime(rt); |
---|
| 210 | JS_ShutDown(); |
---|
[471] | 211 | return -1; |
---|
[1] | 212 | } |
---|
[471] | 213 | |
---|
[383] | 214 | |
---|
[1] | 215 | /* Call a function in obj's scope. */ |
---|
| 216 | jsval argv[3]; |
---|
[9] | 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; |
---|
[1] | 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; |
---|
[9] | 235 | if (ok==JS_TRUE && JSVAL_IS_OBJECT(rval1)==JS_TRUE) { |
---|
[1] | 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]; |
---|
[9] | 244 | if(strlen(dbg)==0) |
---|
| 245 | sprintf(dbg,"No result was found after the function call"); |
---|
[274] | 246 | sprintf(tmp1,"Unable to run %s from the JavaScript file %s : \n %s",s->name,filename,dbg); |
---|
| 247 | #ifdef JS_DEBUG |
---|
[1] | 248 | fprintf(stderr,"%s",tmp1); |
---|
[274] | 249 | #endif |
---|
[1] | 250 | map* err=createMap("text",tmp1); |
---|
[9] | 251 | addToMap(err,"code","NoApplicableCode"); |
---|
[1] | 252 | printExceptionReportResponse(*main_conf,err); |
---|
[9] | 253 | freeMap(&err); |
---|
| 254 | free(err); |
---|
[471] | 255 | free(filename); |
---|
| 256 | JS_MaybeGC(cx); |
---|
[1] | 257 | JS_DestroyContext(cx); |
---|
| 258 | JS_DestroyRuntime(rt); |
---|
| 259 | JS_ShutDown(); |
---|
[9] | 260 | // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847 |
---|
[471] | 261 | return -1; |
---|
[1] | 262 | } |
---|
| 263 | |
---|
| 264 | jsval t=OBJECT_TO_JSVAL(d); |
---|
[9] | 265 | if(JS_IsArrayObject(cx,d)){ |
---|
[1] | 266 | #ifdef JS_DEBUG |
---|
| 267 | fprintf(stderr,"An array was returned !\n"); |
---|
| 268 | #endif |
---|
[364] | 269 | jsuint len; |
---|
[9] | 270 | if((JS_GetArrayLength(cx, d, &len)==JS_FALSE)){ |
---|
[1] | 271 | #ifdef JS_DEBUG |
---|
| 272 | fprintf(stderr,"outputs array is empty\n"); |
---|
| 273 | #endif |
---|
| 274 | } |
---|
| 275 | jsval tmp1; |
---|
[9] | 276 | JSBool hasResult=JS_GetElement(cx,d,0,&tmp1); |
---|
[1] | 277 | res=JSVAL_TO_INT(tmp1); |
---|
| 278 | #ifdef JS_DEBUG |
---|
| 279 | fprintf(stderr," * %d * \n",res); |
---|
| 280 | #endif |
---|
[383] | 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 | } |
---|
[9] | 297 | } |
---|
[383] | 298 | |
---|
[1] | 299 | } |
---|
| 300 | else{ |
---|
[9] | 301 | #ifdef JS_DEBUG |
---|
[383] | 302 | fprintf(stderr,"The service didn't return an array !\n"); |
---|
[9] | 303 | #endif |
---|
[383] | 304 | /** |
---|
| 305 | * Extract result |
---|
| 306 | */ |
---|
[1] | 307 | jsval tmp1; |
---|
[9] | 308 | JSBool hasResult=JS_GetProperty(cx,d,"result",&tmp1); |
---|
[1] | 309 | res=JSVAL_TO_INT(tmp1); |
---|
| 310 | |
---|
[9] | 311 | #ifdef JS_DEBUG |
---|
[1] | 312 | fprintf(stderr," * %d * \n",res); |
---|
[9] | 313 | #endif |
---|
[383] | 314 | /** |
---|
| 315 | * Extract outputs when available. |
---|
| 316 | */ |
---|
[1] | 317 | jsval tmp2; |
---|
[9] | 318 | JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2); |
---|
[383] | 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); |
---|
[274] | 325 | #ifdef JS_DEBUG |
---|
[383] | 326 | if(JSVAL_IS_VOID(tmp2)) |
---|
[1] | 327 | fprintf(stderr,"No outputs property returned\n"); |
---|
[383] | 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); |
---|
[274] | 335 | #endif |
---|
[383] | 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 | |
---|
[1] | 349 | #ifdef JS_DEBUG |
---|
[364] | 350 | dumpMaps(*outputs); |
---|
[1] | 351 | #endif |
---|
| 352 | } |
---|
| 353 | /* Cleanup. */ |
---|
[383] | 354 | JS_MaybeGC(cx); |
---|
[274] | 355 | JS_DestroyContext(cx); |
---|
[1] | 356 | JS_DestroyRuntime(rt); |
---|
| 357 | JS_ShutDown(); |
---|
[471] | 358 | free(filename); |
---|
[1] | 359 | #ifdef JS_DEBUG |
---|
| 360 | fprintf(stderr,"Returned value %d\n",res); |
---|
| 361 | #endif |
---|
| 362 | return res; |
---|
| 363 | } |
---|
| 364 | |
---|
[274] | 365 | JSObject * loadZooApiFile(JSContext *cx,JSObject *global, char* filename){ |
---|
[42] | 366 | struct stat api_status; |
---|
| 367 | int s=stat(filename, &api_status); |
---|
| 368 | if(s==0){ |
---|
| 369 | jsval rval; |
---|
| 370 | JSBool ok ; |
---|
[274] | 371 | JSObject *script = JS_CompileFile(cx, JS_GetGlobalObject(cx), filename); |
---|
[42] | 372 | if(script!=NULL){ |
---|
[274] | 373 | (void)JS_ExecuteScript(cx, JS_GetGlobalObject(cx), script, &rval); |
---|
| 374 | #ifdef JS_DEBUG |
---|
[42] | 375 | fprintf(stderr,"**************\n%s correctly loaded\n**************\n",filename); |
---|
[274] | 376 | #endif |
---|
[42] | 377 | return script; |
---|
| 378 | } |
---|
[274] | 379 | #ifdef JS_DEBUG |
---|
[42] | 380 | else |
---|
| 381 | fprintf(stderr,"\n**************\nUnable to run %s\n**************\n",filename); |
---|
[274] | 382 | #endif |
---|
[42] | 383 | } |
---|
[274] | 384 | #ifdef JS_DEBUG |
---|
[42] | 385 | else |
---|
| 386 | fprintf(stderr,"\n**************\nUnable to load %s\n**************\n",filename); |
---|
[274] | 387 | #endif |
---|
[42] | 388 | return NULL; |
---|
| 389 | } |
---|
| 390 | |
---|
[1] | 391 | JSObject* JSObject_FromMaps(JSContext *cx,maps* t){ |
---|
[328] | 392 | |
---|
| 393 | JSObject* res=JS_NewObject(cx, NULL, NULL, NULL); |
---|
| 394 | //JSObject *res = JS_NewArrayObject(cx, 0, NULL); |
---|
[9] | 395 | if(res==NULL) |
---|
| 396 | fprintf(stderr,"Array Object is NULL!\n"); |
---|
[1] | 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); |
---|
[328] | 403 | JS_SetProperty(cx, res, tmp->name, &pvalj); |
---|
[1] | 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; |
---|
[360] | 416 | map* isArray=getMap(t,"isArray"); |
---|
| 417 | map* isBinary=getMap(t,"size"); |
---|
| 418 | map* tmap=getMapType(t); |
---|
[403] | 419 | #ifdef JS_DEBUG |
---|
[360] | 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); |
---|
[364] | 424 | #endif |
---|
[410] | 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); |
---|
[1] | 432 | #ifdef JS_DEBUG |
---|
[410] | 433 | fprintf(stderr,"[JS] %s => %s\n",tmpm->name,tmpm->value); |
---|
[1] | 434 | #endif |
---|
[410] | 435 | tmpm=tmpm->next; |
---|
[1] | 436 | } |
---|
[410] | 437 | if(isArray!=NULL){ |
---|
[360] | 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 | } |
---|
[1] | 461 | return res; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | maps* mapsFromJSObject(JSContext *cx,jsval t){ |
---|
| 465 | maps *res=NULL; |
---|
| 466 | maps *tres=NULL; |
---|
[9] | 467 | jsint oi=0; |
---|
| 468 | JSObject* tt=JSVAL_TO_OBJECT(t); |
---|
[333] | 469 | if(JS_IsArrayObject(cx,tt)){ |
---|
[1] | 470 | #ifdef JS_DEBUG |
---|
| 471 | fprintf(stderr,"Is finally an array !\n"); |
---|
[333] | 472 | #endif |
---|
[1] | 473 | } |
---|
[333] | 474 | else{ |
---|
| 475 | #ifdef JS_DEBUG |
---|
[1] | 476 | fprintf(stderr,"Is not an array !\n"); |
---|
| 477 | #endif |
---|
[333] | 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); |
---|
[471] | 496 | |
---|
| 497 | tmp=JS_EncodeString(cx,jsmsg); |
---|
[333] | 498 | tres=(maps*)malloc(MAPS_SIZE); |
---|
[471] | 499 | tres->name=zStrdup(tmp); |
---|
[333] | 500 | tres->content=NULL; |
---|
| 501 | tres->next=NULL; |
---|
| 502 | |
---|
| 503 | jsval nvp=JSVAL_NULL; |
---|
[471] | 504 | if((JS_GetProperty(cx, tt, tmp, &nvp)==JS_FALSE)){ |
---|
[333] | 505 | #ifdef JS_DEBUG |
---|
[471] | 506 | fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,tmp); |
---|
[333] | 507 | #endif |
---|
| 508 | } |
---|
[471] | 509 | free(tmp); |
---|
[364] | 510 | JSObject *nvp1=JSVAL_TO_OBJECT(JSVAL_NULL); |
---|
[333] | 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; |
---|
[403] | 524 | |
---|
[333] | 525 | } |
---|
[471] | 526 | JS_DestroyIdArray(cx,idp); |
---|
[333] | 527 | } |
---|
| 528 | } |
---|
| 529 | |
---|
[364] | 530 | jsuint len; |
---|
[9] | 531 | JSBool hasLen=JS_GetArrayLength(cx, tt, &len); |
---|
[383] | 532 | #ifdef JS_DEBUG |
---|
[9] | 533 | if(hasLen==JS_FALSE){ |
---|
[1] | 534 | fprintf(stderr,"outputs array is empty\n"); |
---|
| 535 | } |
---|
| 536 | fprintf(stderr,"outputs array length : %d\n",len); |
---|
| 537 | #endif |
---|
[9] | 538 | for(oi=0;oi < len;oi++){ |
---|
[1] | 539 | #ifdef JS_DEBUG |
---|
[9] | 540 | fprintf(stderr,"outputs array length : %d step %d \n",len,oi); |
---|
[1] | 541 | #endif |
---|
| 542 | jsval tmp1; |
---|
[9] | 543 | JSBool hasElement=JS_GetElement(cx,tt,oi,&tmp1); |
---|
| 544 | JSObject *otmp1=JSVAL_TO_OBJECT(tmp1); |
---|
| 545 | JSIdArray *idp=JS_Enumerate(cx,otmp1); |
---|
[1] | 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 |
---|
[274] | 552 | tres=(maps*)malloc(MAPS_SIZE); |
---|
| 553 | tres->name=NULL; |
---|
| 554 | tres->content=NULL; |
---|
| 555 | tres->next=NULL; |
---|
| 556 | |
---|
[1] | 557 | for (index=0,argNum=idp->length;index<argNum;index++) { |
---|
[9] | 558 | jsval id = idp->vector[index]; |
---|
[1] | 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); |
---|
[471] | 567 | tmp=JS_EncodeString(cx,jsmsg); |
---|
[1] | 568 | #ifdef JS_DEBUG |
---|
[471] | 569 | fprintf(stderr,"Enumerate id : %d => %s\n",oi,tmp); |
---|
[1] | 570 | #endif |
---|
[9] | 571 | jsval nvp=JSVAL_NULL; |
---|
[471] | 572 | if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), tmp, &nvp)==JS_FALSE)){ |
---|
[1] | 573 | #ifdef JS_DEBUG |
---|
[471] | 574 | fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,tmp); |
---|
[1] | 575 | #endif |
---|
[274] | 576 | } |
---|
[471] | 577 | free(tmp); |
---|
[1] | 578 | if(JSVAL_IS_OBJECT(nvp)){ |
---|
| 579 | #ifdef JS_DEBUG |
---|
| 580 | fprintf(stderr,"JSVAL NVP IS OBJECT\n"); |
---|
| 581 | #endif |
---|
| 582 | } |
---|
[274] | 583 | |
---|
[364] | 584 | JSObject *nvp1=JSVAL_TO_OBJECT(JSVAL_NULL); |
---|
[1] | 585 | JS_ValueToObject(cx,nvp,&nvp1); |
---|
| 586 | jsval nvp1j=OBJECT_TO_JSVAL(nvp1); |
---|
| 587 | if(JSVAL_IS_OBJECT(nvp1j)){ |
---|
[274] | 588 | JSString *jsmsg1; |
---|
[471] | 589 | char *tmp1, *tmp2; |
---|
[364] | 590 | JSObject *nvp2=JSVAL_TO_OBJECT(JSVAL_NULL); |
---|
[274] | 591 | jsmsg1 = JS_ValueToString(cx,nvp1j); |
---|
| 592 | len1 = JS_GetStringLength(jsmsg1); |
---|
[471] | 593 | tmp1=JS_EncodeString(cx,jsmsg1); |
---|
| 594 | tmp2=JS_EncodeString(cx,jsmsg); |
---|
[277] | 595 | #ifdef JS_DEBUG |
---|
[471] | 596 | fprintf(stderr,"JSVAL NVP1J IS OBJECT %s = %s\n",JS_EncodeString(cx,jsmsg),tmp1); |
---|
[277] | 597 | #endif |
---|
[471] | 598 | if(strcasecmp(tmp1,"[object Object]")==0){ |
---|
| 599 | tres->name=zStrdup(tmp2); |
---|
[274] | 600 | tres->content=mapFromJSObject(cx,nvp1j); |
---|
| 601 | } |
---|
[1] | 602 | else |
---|
[471] | 603 | if(strcasecmp(tmp2,"name")==0){ |
---|
| 604 | tres->name=zStrdup(tmp1); |
---|
[274] | 605 | } |
---|
| 606 | else{ |
---|
| 607 | if(tres->content==NULL) |
---|
[471] | 608 | tres->content=createMap(tmp2,tmp1); |
---|
[274] | 609 | else |
---|
[471] | 610 | addToMap(tres->content,tmp2,tmp1); |
---|
[274] | 611 | } |
---|
[471] | 612 | free(tmp1); |
---|
| 613 | free(tmp2); |
---|
[1] | 614 | } |
---|
| 615 | #ifdef JS_DEBUG |
---|
| 616 | else |
---|
| 617 | fprintf(stderr,"JSVAL NVP1J IS NOT OBJECT !!\n"); |
---|
| 618 | #endif |
---|
| 619 | } |
---|
[277] | 620 | #ifdef JS_DEBUG |
---|
[274] | 621 | dumpMaps(tres); |
---|
[277] | 622 | #endif |
---|
[274] | 623 | if(res==NULL) |
---|
| 624 | res=dupMaps(&tres); |
---|
| 625 | else |
---|
| 626 | addMapsToMaps(&res,tres); |
---|
| 627 | freeMaps(&tres); |
---|
| 628 | free(tres); |
---|
| 629 | tres=NULL; |
---|
[471] | 630 | JS_DestroyIdArray(cx,idp); |
---|
[1] | 631 | } |
---|
| 632 | } |
---|
[277] | 633 | #ifdef JS_DEBUG |
---|
[1] | 634 | dumpMaps(res); |
---|
[277] | 635 | #endif |
---|
[1] | 636 | return res; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | map* mapFromJSObject(JSContext *cx,jsval t){ |
---|
| 640 | map *res=NULL; |
---|
[9] | 641 | JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t)); |
---|
[1] | 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++) { |
---|
[9] | 652 | jsval id = idp->vector[index]; |
---|
[1] | 653 | jsval vp; |
---|
| 654 | JSString* str; |
---|
| 655 | JS_IdToValue(cx,id,&vp); |
---|
[471] | 656 | char *c, *tmp, *tmp1; |
---|
[1] | 657 | JSString *jsmsg,*jsmsg1; |
---|
| 658 | size_t len,len1; |
---|
| 659 | jsmsg = JS_ValueToString(cx,vp); |
---|
| 660 | len = JS_GetStringLength(jsmsg); |
---|
| 661 | jsval nvp; |
---|
[471] | 662 | tmp=JS_EncodeString(cx,jsmsg); |
---|
| 663 | JS_GetProperty(cx, JSVAL_TO_OBJECT(t), tmp, &nvp); |
---|
[1] | 664 | jsmsg1 = JS_ValueToString(cx,nvp); |
---|
| 665 | len1 = JS_GetStringLength(jsmsg1); |
---|
[471] | 666 | tmp1=JS_EncodeString(cx,jsmsg1); |
---|
[1] | 667 | #ifdef JS_DEBUG |
---|
[471] | 668 | fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,tmp,tmp1); |
---|
[1] | 669 | #endif |
---|
[9] | 670 | if(res!=NULL){ |
---|
[26] | 671 | #ifdef JS_DEBUG |
---|
[471] | 672 | fprintf(stderr,"%s - %s\n",tmp,tmp1); |
---|
[26] | 673 | #endif |
---|
[471] | 674 | addToMap(res,tmp,tmp1); |
---|
[9] | 675 | } |
---|
| 676 | else{ |
---|
[471] | 677 | res=createMap(tmp,tmp1); |
---|
[9] | 678 | res->next=NULL; |
---|
| 679 | } |
---|
[471] | 680 | free(tmp); |
---|
| 681 | free(tmp1); |
---|
[26] | 682 | #ifdef JS_DEBUG |
---|
[9] | 683 | dumpMap(res); |
---|
[26] | 684 | #endif |
---|
[1] | 685 | } |
---|
[471] | 686 | JS_DestroyIdArray(cx,idp); |
---|
[1] | 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 | |
---|
[368] | 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 | |
---|
[492] | 734 | HINTERNET setHeader(HINTERNET* handle,JSContext *cx,JSObject *header){ |
---|
[368] | 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 |
---|
[492] | 749 | handle->ihandle[handle->nb].header=NULL; |
---|
[368] | 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 |
---|
[492] | 755 | curl_easy_setopt(handle->ihandle[handle->nb].handle,CURLOPT_VERBOSE,1); |
---|
[368] | 756 | fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1); |
---|
| 757 | #endif |
---|
[492] | 758 | handle->ihandle[handle->nb].header=curl_slist_append(handle->ihandle[handle->nb].header, tmp1); |
---|
[368] | 759 | free(tmp1); |
---|
| 760 | } |
---|
| 761 | } |
---|
| 762 | else{ |
---|
| 763 | fprintf(stderr,"not an array !!!!!!!\n"); |
---|
| 764 | } |
---|
[492] | 765 | return *handle; |
---|
[368] | 766 | } |
---|
| 767 | |
---|
| 768 | JSBool |
---|
[377] | 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 |
---|
[368] | 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{ |
---|
[453] | 803 | method=zStrdup("GET"); |
---|
[368] | 804 | url=JSValToChar(cx,argv); |
---|
| 805 | } |
---|
[492] | 806 | hInternet.waitingRequests[hInternet.nb]=strdup(url); |
---|
[368] | 807 | if(argc==4){ |
---|
| 808 | char *body; |
---|
| 809 | body=JSValToChar(cx,&argv[2]); |
---|
| 810 | header=JSVAL_TO_OBJECT(argv[3]); |
---|
| 811 | #ifdef ULINET_DEBUG |
---|
| 812 | fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body); |
---|
| 813 | #endif |
---|
| 814 | if(JS_IsArrayObject(cx,header)) |
---|
[492] | 815 | setHeader(&hInternet,cx,header); |
---|
[368] | 816 | #ifdef ULINET_DEBUG |
---|
| 817 | fprintf(stderr,"BODY (%s)\n",body); |
---|
| 818 | #endif |
---|
[492] | 819 | InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],body,strlen(body), |
---|
| 820 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 821 | processDownloads(&hInternet); |
---|
[368] | 822 | free(body); |
---|
| 823 | }else{ |
---|
| 824 | if(argc==3){ |
---|
| 825 | char *body=JSValToChar(cx,&argv[2]); |
---|
[492] | 826 | InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],body,strlen(body), |
---|
| 827 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 828 | processDownloads(&hInternet); |
---|
[368] | 829 | free(body); |
---|
[492] | 830 | }else{ |
---|
| 831 | InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],NULL,0, |
---|
| 832 | INTERNET_FLAG_NO_CACHE_WRITE,0); |
---|
| 833 | processDownloads(&hInternet); |
---|
[368] | 834 | } |
---|
| 835 | } |
---|
[492] | 836 | tmpValue=(char*)malloc((hInternet.ihandle[0].nDataLen+1)*sizeof(char)); |
---|
| 837 | InternetReadFile(hInternet.ihandle[0],(LPVOID)tmpValue,hInternet.ihandle[0].nDataLen,&dwRead); |
---|
[368] | 838 | #ifdef ULINET_DEBUG |
---|
| 839 | fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue); |
---|
| 840 | #endif |
---|
| 841 | if(dwRead==0){ |
---|
| 842 | JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,"Unable to access the file.",strlen("Unable to access the file.")))); |
---|
| 843 | return JS_TRUE; |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | #ifdef ULINET_DEBUG |
---|
| 847 | fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue); |
---|
| 848 | #endif |
---|
| 849 | JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue)))); |
---|
| 850 | free(url); |
---|
| 851 | if(argc>=2) |
---|
| 852 | free(method); |
---|
[492] | 853 | InternetCloseHandle(&hInternet); |
---|
[368] | 854 | JS_MaybeGC(cx); |
---|
| 855 | return JS_TRUE; |
---|
| 856 | } |
---|