Ignore:
Timestamp:
Oct 4, 2012, 5:04:31 PM (12 years ago)
Author:
djay
Message:

Many thanks to Trevor Clarke for providing patch for Python ZOO-API. This rev. shall fix ticket #74.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-kernel/service_internal_js.c

    r364 r368  
    120120  }
    121121
     122  jsval tmp=INT_TO_JSVAL(3);
     123  if (!JS_SetProperty(cx, global, "SERVICE_SUCCEEDED", &tmp))
     124    return 1;
     125  tmp=INT_TO_JSVAL(4);
     126  if (!JS_SetProperty(cx, global, "SERVICE_FAILED", &tmp))
     127    return 1;
    122128  if (!JS_DefineFunction(cx, global, "ZOORequest", JSRequest, 4, 0))
    123129    return 1;
     
    638644}
    639645
     646char* JSValToChar(JSContext* context, jsval* arg) {
     647  char *c;
     648  char *tmp;
     649  JSString *jsmsg;
     650  size_t len;
     651  int i;
     652  if(!JSVAL_IS_STRING(*arg)) {
     653    return NULL;
     654  }
     655  jsmsg = JS_ValueToString(context,*arg);
     656  len = JS_GetStringLength(jsmsg);
     657  tmp = JS_EncodeString(context,jsmsg);
     658  c = (char*)malloc((len+1)*sizeof(char));
     659  c[len] = '\0';
     660#ifdef ULINET_DEBUG
     661  fprintf(stderr,"%d \n",len);
     662#endif
     663  for(i = 0;i < len;i++) {
     664    c[i] = tmp[i];
     665    c[i+1] = 0;
     666  }
     667#ifdef ULINET_DEBUG
     668  fprintf(stderr,"%s \n",c);
     669#endif
     670  return c;
     671}
     672
     673HINTERNET setHeader(HINTERNET handle,JSContext *cx,JSObject *header){
     674  jsuint length=0;
     675  jsint i=0;
     676  char *tmp1;
     677#ifdef ULINET_DEBUG
     678  fprintf(stderr,"setHeader\n");
     679#endif
     680  if(JS_IsArrayObject(cx,header)){
     681#ifdef ULINET_DEBUG
     682    fprintf(stderr,"header is an array\n");
     683#endif
     684    JS_GetArrayLength(cx,header,&length);
     685#ifdef ULINET_DEBUG
     686    fprintf(stderr,"header is an array of %d elements\n",length);
     687#endif
     688    handle.header=NULL;
     689    for(i=0;i<length;i++){
     690      jsval tmp;
     691      JS_GetElement(cx,header,i,&tmp);
     692      tmp1=JSValToChar(cx,&tmp);
     693#ifdef ULINET_DEBUG
     694      fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1);
     695#endif
     696      handle.header=curl_slist_append(handle.header, tmp1);
     697      free(tmp1);
     698    }
     699  }
     700  else{
     701    fprintf(stderr,"not an array !!!!!!!\n");
     702  }
     703  return handle;
     704}
     705
     706JSBool
     707JSRequest(JSContext *cx, uintN argc, jsval *argv1)
     708{
     709  jsval *argv = JS_ARGV(cx,argv1);
     710  HINTERNET hInternet;
     711  HINTERNET res;
     712  HINTERNET res1;
     713  JSObject *header;
     714  char *url;
     715  char *method;
     716  char* tmpValue;
     717  size_t dwRead;
     718  int i=0;
     719  JS_MaybeGC(cx);
     720  hInternet=InternetOpen("ZooWPSClient\0",
     721                         INTERNET_OPEN_TYPE_PRECONFIG,
     722                         NULL,NULL, 0);
     723  if(!CHECK_INET_HANDLE(hInternet))
     724    return JS_FALSE;
     725  if(argc>=2){
     726    method=JSValToChar(cx,&argv[0]);
     727    url=JSValToChar(cx,&argv[1]);
     728  }
     729  else{
     730    method=strdup("GET");
     731    url=JSValToChar(cx,argv);
     732  }
     733  if(argc==4){
     734    char *body;
     735    body=JSValToChar(cx,&argv[2]);
     736    header=JSVAL_TO_OBJECT(argv[3]);
     737#ifdef ULINET_DEBUG
     738    fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body);
     739#endif
     740    if(JS_IsArrayObject(cx,header))
     741      res1=setHeader(hInternet,cx,header);
     742#ifdef ULINET_DEBUG
     743    fprintf(stderr,"BODY (%s)\n",body);
     744#endif
     745    res=InternetOpenUrl(res1,url,body,strlen(body),
     746                        INTERNET_FLAG_NO_CACHE_WRITE,0);   
     747    free(body);
     748  }else{
     749    if(argc==3){
     750      char *body=JSValToChar(cx,&argv[2]);
     751      res=InternetOpenUrl(hInternet,url,body,strlen(body),
     752                          INTERNET_FLAG_NO_CACHE_WRITE,0);
     753      free(body);
     754    }
     755    res=InternetOpenUrl(hInternet,url,NULL,0,
     756                        INTERNET_FLAG_NO_CACHE_WRITE,0);
     757  }
     758  tmpValue=(char*)malloc((res.nDataLen+1)*sizeof(char));
     759  InternetReadFile(res,(LPVOID)tmpValue,res.nDataLen,&dwRead);
     760#ifdef ULINET_DEBUG
     761  fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
     762#endif
     763  if(dwRead==0){
     764    JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,"Unable to access the file.",strlen("Unable to access the file."))));
     765    return JS_TRUE;
     766  }
     767
     768#ifdef ULINET_DEBUG
     769  fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
     770#endif
     771  JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue))));
     772  free(url);
     773  if(argc>=2)
     774    free(method);
     775  if(argc==4 && res.header!=NULL){
     776    curl_slist_free_all(res.header);
     777  }
     778  InternetCloseHandle(hInternet);
     779  JS_MaybeGC(cx);
     780  return JS_TRUE;
     781}
Note: See TracChangeset for help on using the changeset viewer.

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