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

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

Fix asynchronous support on windows platform, fix storage of responseDocument and add a sample service in Python language using the ZOO-API. Update makefile.vc and nmake.opt to remove unneeded cgic linkage. Fix for headers section to display only if needed.

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