source: trunk/zoo-project/zoo-kernel/service_internal_js.c @ 368

Last change on this file since 368 was 368, checked in by djay, 12 years ago

Many thanks to Trevor Clarke for providing patch for Python ZOO-API. This rev. shall fix ticket #74.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 21.0 KB
Line 
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
27static char dbg[1024];
28
29JSBool
30JSAlert(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
44JSBool
45JSLoadScripts(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
75int 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, "ZOOUpdateStatus", JSUpdateStatus, 2, 0))
131    return 1;
132  if (!JS_DefineFunction(cx, global, "alert", JSAlert, 2, 0))
133    return 1; 
134  if (!JS_DefineFunction(cx, global, "importScripts", JSLoadScripts, 1, 0))
135    return 1;
136
137  /**
138   * Add private context object
139   */
140  void* cxPrivate = request;
141  JS_SetContextPrivate(cx,cxPrivate);
142   
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   */
150  char *api0=(char*)malloc(strlen(tmpm1->value)+strlen(ntmp)+16);
151  sprintf(api0,"%s/%s/ZOO-proj4js.js",ntmp,tmpm1->value);
152#ifdef JS_DEBUG
153  fprintf(stderr,"Trying to load %s\n",api0);
154#endif
155  JSObject *api_script1=loadZooApiFile(cx,global,api0);
156  fflush(stderr);
157
158  char *api1=(char*)malloc(strlen(tmpm1->value)+strlen(ntmp)+11);
159  sprintf(api1,"%s/%s/ZOO-api.js",ntmp,tmpm1->value);
160#ifdef JS_DEBUG
161  fprintf(stderr,"Trying to load %s\n",api1);
162#endif
163  JSObject *api_script2=loadZooApiFile(cx,global,api1);
164  fflush(stderr);
165
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");
172
173  char *filename=(char*)malloc(strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+3);
174  sprintf(filename,"%s/%s/%s",ntmp,tmpm1->value,tmpm2->value);
175  filename[strlen(tmpm1->value)+strlen(tmpm2->value)+strlen(ntmp)+2]=0;
176#ifdef JS_DEBUG
177  fprintf(stderr,"FILENAME %s\n",filename);
178#endif
179  struct stat file_status;
180  stat(filename, &file_status);
181  char *source=(char*)malloc(file_status.st_size);
182  uint16 lineno;
183  jsval rval;
184  JSBool ok ;
185  JSObject *script = JS_CompileFile(cx, global, filename);
186  if(script!=NULL){
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];
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;
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;
220  if (ok==JS_TRUE && JSVAL_IS_OBJECT(rval1)==JS_TRUE) {
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];
229    if(strlen(dbg)==0)
230      sprintf(dbg,"No result was found after the function call");
231    sprintf(tmp1,"Unable to run %s from the JavaScript file %s : \n %s",s->name,filename,dbg);
232#ifdef JS_DEBUG
233    fprintf(stderr,"%s",tmp1);
234#endif
235    map* err=createMap("text",tmp1);
236    addToMap(err,"code","NoApplicableCode");
237    printExceptionReportResponse(*main_conf,err);
238    freeMap(&err);
239    free(err);
240    JS_DestroyContext(cx);
241    JS_DestroyRuntime(rt);
242    JS_ShutDown();
243    // Should return -1 here but the unallocation won't work from zoo_service_loader.c line 1847
244    exit(-1);
245  }
246
247  jsval t=OBJECT_TO_JSVAL(d);
248  if(JS_IsArrayObject(cx,d)){
249#ifdef JS_DEBUG
250    fprintf(stderr,"An array was returned !\n");
251#endif
252    jsuint       len;
253    if((JS_GetArrayLength(cx, d, &len)==JS_FALSE)){
254#ifdef JS_DEBUG
255      fprintf(stderr,"outputs array is empty\n");
256#endif
257    }
258    jsval tmp1;
259    JSBool hasResult=JS_GetElement(cx,d,0,&tmp1);
260    res=JSVAL_TO_INT(tmp1);
261#ifdef JS_DEBUG
262    fprintf(stderr," * %d * \n",res);
263#endif
264    jsval tmp2;
265    JSBool hasElement=JS_GetElement(cx,d,1,&tmp2);
266    if(hasElement==JS_TRUE){
267          freeMaps(outputs);
268          free(*outputs);
269      *outputs=mapsFromJSObject(cx,tmp2);
270    }
271  }
272  else{
273#ifdef JS_DEBUG
274    fprintf(stderr,"The serice didn't return an array !\n");
275#endif
276    jsval tmp1;
277    JSBool hasResult=JS_GetProperty(cx,d,"result",&tmp1);
278    res=JSVAL_TO_INT(tmp1);
279
280#ifdef JS_DEBUG
281    fprintf(stderr," * %d * \n",res);
282#endif
283    jsval tmp2;
284    JSBool hasElement=JS_GetProperty(cx,d,"outputs",&tmp2);
285#ifdef JS_DEBUG
286    if(!hasElement)
287      fprintf(stderr,"No outputs property returned\n");
288    if(JS_IsArrayObject(cx,JSVAL_TO_OBJECT(tmp2)))
289      fprintf(stderr,"outputs is array an as expected\n");
290    else
291      fprintf(stderr,"outputs is not an array as expected\n");
292#endif
293    *outputs=mapsFromJSObject(cx,tmp2);
294#ifdef JS_DEBUG
295    dumpMaps(*outputs);
296#endif
297  }
298  /* Cleanup. */
299  JS_DestroyContext(cx);
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
308JSObject * loadZooApiFile(JSContext *cx,JSObject  *global, char* filename){
309  struct stat api_status;
310  int s=stat(filename, &api_status);
311  if(s==0){
312    jsval rval;
313    JSBool ok ;
314    JSObject *script = JS_CompileFile(cx, JS_GetGlobalObject(cx), filename);
315    if(script!=NULL){
316      (void)JS_ExecuteScript(cx, JS_GetGlobalObject(cx), script, &rval);
317#ifdef JS_DEBUG
318      fprintf(stderr,"**************\n%s correctly loaded\n**************\n",filename);
319#endif
320      return script;
321    }
322#ifdef JS_DEBUG
323    else
324      fprintf(stderr,"\n**************\nUnable to run %s\n**************\n",filename);
325#endif
326  }
327#ifdef JS_DEBUG
328  else
329    fprintf(stderr,"\n**************\nUnable to load %s\n**************\n",filename);
330#endif
331  return NULL;
332}
333
334JSObject* JSObject_FromMaps(JSContext *cx,maps* t){
335
336  JSObject* res=JS_NewObject(cx, NULL, NULL, NULL);
337  //JSObject *res = JS_NewArrayObject(cx, 0, NULL);
338  if(res==NULL)
339    fprintf(stderr,"Array Object is NULL!\n");
340  maps* tmp=t;
341
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);
347    JS_SetProperty(cx, res, tmp->name, &pvalj);
348
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
357JSObject* 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;
361  map* isArray=getMap(t,"isArray");
362  map* isBinary=getMap(t,"size");
363  map* tmap=getMapType(t);
364#ifdef DEBUG
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);
369#endif
370  /* Avoid gesture of binary content which failed due to strlen function use */
371  if(isBinary!=NULL){
372    return res;
373  }
374  while(tmpm!=NULL){
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);
379#ifdef JS_DEBUG
380      fprintf(stderr,"%s => %s\n",tmpm->name,tmpm->value);
381#endif
382    }
383    tmpm=tmpm->next;
384  }
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  }
409  return res;
410}
411
412maps* mapsFromJSObject(JSContext *cx,jsval t){
413  maps *res=NULL;
414  maps *tres=NULL;
415  jsint oi=0;
416  JSObject* tt=JSVAL_TO_OBJECT(t);
417  if(JS_IsArrayObject(cx,tt)){
418#ifdef JS_DEBUG
419    fprintf(stderr,"Is finally an array !\n");
420#endif
421  }
422  else{
423#ifdef JS_DEBUG
424    fprintf(stderr,"Is not an array !\n");
425#endif
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;
451        if((JS_GetProperty(cx, tt, JS_EncodeString(cx,jsmsg), &nvp)==JS_FALSE)){
452#ifdef JS_DEBUG
453          fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_EncodeString(cx,jsmsg));
454#endif
455        }
456       
457        JSObject *nvp1=JSVAL_TO_OBJECT(JSVAL_NULL);
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
477  jsuint len;
478  JSBool hasLen=JS_GetArrayLength(cx, tt, &len);
479  if(hasLen==JS_FALSE){
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
487  for(oi=0;oi < len;oi++){
488#ifdef JS_DEBUG
489    fprintf(stderr,"outputs array length : %d step %d \n",len,oi);
490#endif
491    jsval tmp1;
492    JSBool hasElement=JS_GetElement(cx,tt,oi,&tmp1);
493    JSObject *otmp1=JSVAL_TO_OBJECT(tmp1);
494    JSIdArray *idp=JS_Enumerate(cx,otmp1);
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
501      tres=(maps*)malloc(MAPS_SIZE);
502      tres->name=NULL;
503      tres->content=NULL;
504      tres->next=NULL;
505
506      for (index=0,argNum=idp->length;index<argNum;index++) { 
507        jsval id = idp->vector[index];
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
517        fprintf(stderr,"Enumerate id : %d => %s\n",oi,JS_EncodeString(cx,jsmsg));
518#endif
519        jsval nvp=JSVAL_NULL;
520        if((JS_GetProperty(cx, JSVAL_TO_OBJECT(tmp1), JS_EncodeString(cx,jsmsg), &nvp)==JS_FALSE)){
521#ifdef JS_DEBUG
522          fprintf(stderr,"Enumerate id : %d => %s => No more value\n",oi,JS_EncodeString(cx,jsmsg));
523#endif
524        }
525       
526        if(JSVAL_IS_OBJECT(nvp)){
527#ifdef JS_DEBUG
528          fprintf(stderr,"JSVAL NVP IS OBJECT\n");
529#endif
530        }
531
532        JSObject *nvp1=JSVAL_TO_OBJECT(JSVAL_NULL);
533        JS_ValueToObject(cx,nvp,&nvp1);
534        jsval nvp1j=OBJECT_TO_JSVAL(nvp1);
535        if(JSVAL_IS_OBJECT(nvp1j)){
536          JSString *jsmsg1;
537          JSObject *nvp2=JSVAL_TO_OBJECT(JSVAL_NULL);
538          jsmsg1 = JS_ValueToString(cx,nvp1j);
539          len1 = JS_GetStringLength(jsmsg1);
540#ifdef JS_DEBUG
541          fprintf(stderr,"JSVAL NVP1J IS OBJECT %s = %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
542#endif
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          }
547          else
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            }
557        }
558#ifdef JS_DEBUG
559        else
560          fprintf(stderr,"JSVAL NVP1J IS NOT OBJECT !!\n");
561#endif
562
563      }
564#ifdef JS_DEBUG
565      dumpMaps(tres);
566#endif
567      if(res==NULL)
568        res=dupMaps(&tres);
569      else
570        addMapsToMaps(&res,tres);
571      freeMaps(&tres);
572      free(tres);
573      tres=NULL;
574
575    }
576  }
577#ifdef JS_DEBUG
578  dumpMaps(res);
579#endif
580  return res;
581}
582
583map* mapFromJSObject(JSContext *cx,jsval t){
584  map *res=NULL;
585  JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t));
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++) { 
596      jsval id = idp->vector[index];
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;
606      JS_GetProperty(cx, JSVAL_TO_OBJECT(t), JS_EncodeString(cx,jsmsg), &nvp);
607      jsmsg1 = JS_ValueToString(cx,nvp);
608      len1 = JS_GetStringLength(jsmsg1);
609#ifdef JS_DEBUG
610      fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
611#endif
612      if(res!=NULL){
613#ifdef JS_DEBUG
614        fprintf(stderr,"%s - %s\n",JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
615#endif
616        addToMap(res,JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
617      }
618      else{
619        res=createMap(JS_EncodeString(cx,jsmsg),JS_EncodeString(cx,jsmsg1));
620        res->next=NULL;
621      }
622#ifdef JS_DEBUG
623      dumpMap(res);
624#endif
625    }
626  }
627#ifdef JS_DEBUG
628  dumpMap(res);
629#endif
630  return res;
631}
632
633/* The error reporter callback. */
634void 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
646char* 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
673HINTERNET 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
706JSBool
707JSRequest(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}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png