mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-12 22:10:01 -04:00
low level js bindings
This commit is contained in:
parent
465fa2e170
commit
f0ac312f35
3 changed files with 76 additions and 8 deletions
|
@ -1,9 +1,19 @@
|
|||
#include "emscripten/emscripten.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <curl/curl.h>
|
||||
#include <emscripten.h>
|
||||
#include <string.h>
|
||||
|
||||
int write_function(void *data, size_t size, size_t nmemb, void(*data_callback)(char* chunk_ptr, int chunk_size)) {
|
||||
long real_size = size * nmemb;
|
||||
char* chunk = malloc(real_size);
|
||||
memcpy(chunk, data, real_size);
|
||||
data_callback(chunk, real_size);
|
||||
free(chunk);
|
||||
return real_size;
|
||||
}
|
||||
|
||||
int do_request(const char* url) {
|
||||
void perform_request(const char* url, void(*data_callback)(char* chunk_ptr, int chunk_size), void(*end_callback)()) {
|
||||
printf("downloading %s\n", url);
|
||||
|
||||
CURL *http_handle;
|
||||
|
@ -18,6 +28,11 @@ int do_request(const char* url) {
|
|||
curl_easy_setopt(http_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
||||
curl_easy_setopt(http_handle, CURLOPT_CAINFO, "/cacert.pem");
|
||||
curl_easy_setopt(http_handle, CURLOPT_CAPATH, "/cacert.pem");
|
||||
|
||||
//callbacks to pass the response data back to js
|
||||
curl_easy_setopt(http_handle, CURLOPT_WRITEFUNCTION, &write_function);
|
||||
curl_easy_setopt(http_handle, CURLOPT_WRITEDATA, data_callback);
|
||||
|
||||
|
||||
multi_handle = curl_multi_init();
|
||||
curl_multi_add_handle(multi_handle, http_handle);
|
||||
|
@ -42,11 +57,16 @@ int do_request(const char* url) {
|
|||
curl_easy_cleanup(http_handle);
|
||||
curl_multi_cleanup(multi_handle);
|
||||
curl_global_cleanup();
|
||||
|
||||
return 0;
|
||||
|
||||
(*end_callback)();
|
||||
}
|
||||
|
||||
char* copy_bytes(const char* ptr, const int size) {
|
||||
char* new_ptr = malloc(size);
|
||||
memcpy(new_ptr, ptr, size);
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("emscripten libcurl module loaded\n");
|
||||
do_request("https://ifconfig.me/all");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue