source: branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/service_internal_js.c @ 549

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

Search

Context Navigation

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