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

Last change on this file since 403 was 403, checked in by djay, 11 years ago

Fixing issue pointed by Luca today about JS service.

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