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

Last change on this file since 634 was 586, checked in by djay, 9 years ago

Add the PHP ZOO-API, fix issue if ZTS is not defined, correct loading of the PHP script depending of the ZOO-Kernel location, use a ZEND_HANDLE_FD rather than the ZEND_HANDLE_FP used before, correct location of the hello.php service.

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