Changeset 368 for trunk/zoo-project/zoo-kernel/ulinet.c
- Timestamp:
- Oct 4, 2012, 5:04:31 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/ulinet.c
r364 r368 374 374 } 375 375 376 #ifdef USE_JS377 #define XP_WIN 1378 #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_DEBUG395 fprintf(stderr,"%d \n",len);396 #endif397 for(i = 0;i < len;i++) {398 c[i] = tmp[i];399 c[i+1] = 0;400 }401 #ifdef ULINET_DEBUG402 fprintf(stderr,"%s \n",c);403 #endif404 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_DEBUG412 fprintf(stderr,"setHeader\n");413 #endif414 if(JS_IsArrayObject(cx,header)){415 #ifdef ULINET_DEBUG416 fprintf(stderr,"header is an array\n");417 #endif418 JS_GetArrayLength(cx,header,&length);419 #ifdef ULINET_DEBUG420 fprintf(stderr,"header is an array of %d elements\n",length);421 #endif422 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_DEBUG428 fprintf(stderr,"Element of array n° %d, value : %s\n",i,tmp1);429 #endif430 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 JSBool441 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_DEBUG472 fprintf(stderr,"URL (%s) \nBODY (%s)\n",url,body);473 #endif474 if(JS_IsArrayObject(cx,header))475 res1=setHeader(hInternet,cx,header);476 #ifdef ULINET_DEBUG477 fprintf(stderr,"BODY (%s)\n",body);478 #endif479 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_DEBUG501 fprintf(stderr,"content downloaded (%d) (%s) \n",dwRead,tmpValue);502 #endif503 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.