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/ulinet.c

    r364 r368  
    374374}
    375375
    376 #ifdef USE_JS
    377 #define XP_WIN 1
    378 #include "jsapi.h"
    379 
    380 char* JSValToChar(JSContext* context, jsval* arg) {
    381   char *c;
    382   char *tmp;
    383   JSString *jsmsg;
    384   size_t len;
    385   int i;
    386   if(!JSVAL_IS_STRING(*arg)) {
    387     return NULL;
    388   }
    389   jsmsg = JS_ValueToString(context,*arg);
    390   len = JS_GetStringLength(jsmsg);
    391   tmp = JS_EncodeString(context,jsmsg);
    392   c = (char*)malloc((len+1)*sizeof(char));
    393   c[len] = '\0';
    394 #ifdef ULINET_DEBUG
    395   fprintf(stderr,"%d \n",len);
    396 #endif
    397   for(i = 0;i < len;i++) {
    398     c[i] = tmp[i];
    399     c[i+1] = 0;
    400   }
    401 #ifdef ULINET_DEBUG
    402   fprintf(stderr,"%s \n",c);
    403 #endif
    404   return c;
    405 }
    406 
    407 HINTERNET setHeader(HINTERNET handle,JSContext *cx,JSObject *header){
    408   jsuint length=0;
    409   jsint i=0;
    410   char *tmp1;
    411 #ifdef ULINET_DEBUG
    412   fprintf(stderr,"setHeader\n");
    413 #endif
    414   if(JS_IsArrayObject(cx,header)){
    415 #ifdef ULINET_DEBUG
    416     fprintf(stderr,"header is an array\n");
    417 #endif
    418     JS_GetArrayLength(cx,header,&length);
    419 #ifdef ULINET_DEBUG
    420     fprintf(stderr,"header is an array of %d elements\n",length);
    421 #endif
    422     handle.header=NULL;
    423     for(i=0;i<length;i++){
    424       jsval tmp;
    425       JS_GetElement(cx,header,i,&tmp);
    426       tmp1=JSValToChar(cx,&tmp);
    427 #ifdef ULINET_DEBUG
    428       fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1);
    429 #endif
    430       handle.header=curl_slist_append(handle.header, tmp1);
    431       free(tmp1);
    432     }
    433   }
    434   else{
    435     fprintf(stderr,"not an array !!!!!!!\n");
    436   }
    437   return handle;
    438 }
    439 
    440 JSBool
    441 JSRequest(JSContext *cx, uintN argc, jsval *argv1)
    442 {
    443   jsval *argv = JS_ARGV(cx,argv1);
    444   HINTERNET hInternet;
    445   HINTERNET res;
    446   HINTERNET res1;
    447   JSObject *header;
    448   char *url;
    449   char *method;
    450   char* tmpValue;
    451   size_t dwRead;
    452   int i=0;
    453   JS_MaybeGC(cx);
    454   hInternet=InternetOpen((LPCTSTR)"ZooWPSClient\0",
    455                          INTERNET_OPEN_TYPE_PRECONFIG,
    456                          NULL,NULL, 0);
    457   if(!CHECK_INET_HANDLE(hInternet))
    458     return JS_FALSE;
    459   if(argc>=2){
    460     method=JSValToChar(cx,&argv[0]);
    461     url=JSValToChar(cx,&argv[1]);
    462   }
    463   else{
    464     method=strdup("GET");
    465     url=JSValToChar(cx,argv);
    466   }
    467   if(argc==4){
    468     char *body;
    469     body=JSValToChar(cx,&argv[2]);
    470     header=JSVAL_TO_OBJECT(argv[3]);
    471 #ifdef ULINET_DEBUG
    472     fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body);
    473 #endif
    474     if(JS_IsArrayObject(cx,header))
    475       res1=setHeader(hInternet,cx,header);
    476 #ifdef ULINET_DEBUG
    477     fprintf(stderr,"BODY (%s)\n",body);
    478 #endif
    479     res=InternetOpenUrl(res1,url,body,strlen(body),
    480                         INTERNET_FLAG_NO_CACHE_WRITE,0);   
    481     free(body);
    482   }else{
    483     if(argc==3){
    484       char *body=JSValToChar(cx,&argv[2]);
    485       res=InternetOpenUrl(hInternet,url,body,strlen(body),
    486                           INTERNET_FLAG_NO_CACHE_WRITE,0);
    487       free(body);
    488     }
    489     res=InternetOpenUrl(hInternet,url,NULL,0,
    490                         INTERNET_FLAG_NO_CACHE_WRITE,0);
    491   }
    492   tmpValue=(char*)malloc((res.nDataLen+1)*sizeof(char));
    493   InternetReadFile(res,(LPVOID)tmpValue,res.nDataLen,&dwRead);
    494   fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
    495   if(dwRead==0){
    496     JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,"Unable to access the file.",strlen("Unable to access the file."))));
    497     return JS_TRUE;
    498   }
    499 
    500 #ifdef ULINET_DEBUG
    501   fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);
    502 #endif
    503   JS_SET_RVAL(cx, argv1,STRING_TO_JSVAL(JS_NewStringCopyN(cx,tmpValue,strlen(tmpValue))));
    504   free(url);
    505   if(argc>=2)
    506     free(method);
    507   if(argc==4 && res.header!=NULL){
    508     curl_slist_free_all(res.header);
    509   }
    510   InternetCloseHandle(hInternet);
    511   JS_MaybeGC(cx);
    512   return JS_TRUE;
    513 }
    514 #endif
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