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

Last change on this file since 784 was 784, checked in by djay, 8 years ago

Give the capability to store the main.cfg file in sysconfdir and the services in servicePath if defined in the [main] section.

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