mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-12 14:00:01 -04:00
use curl multi interface for async support
This commit is contained in:
parent
643fc8e463
commit
465fa2e170
3 changed files with 52 additions and 28 deletions
|
@ -5,14 +5,19 @@ LIB_DIR="build/curl-wasm/lib/"
|
|||
CACERT_FILE="cacert.pem"
|
||||
OUT_FILE="out/libcurl.js"
|
||||
|
||||
EXPORTED_FUNCS="_main"
|
||||
COMPILER_OPTIONS="-o $OUT_FILE -Os -lcurl -lssl -lcrypto -I $INCLUDE_DIR -L $LIB_DIR"
|
||||
EMSCRIPTEN_OPTIONS="-lwebsocket.js -sWEBSOCKET_URL=wss://debug.ading.dev/ws -pthread -sPROXY_TO_PTHREAD -sEXPORTED_FUNCTIONS=$EXPORTED_FUNCS --preload-file $CACERT_FILE"
|
||||
EXPORTED_FUNCS="_main,_do_request"
|
||||
COMPILER_OPTIONS="-o $OUT_FILE -lcurl -lssl -lcrypto -I $INCLUDE_DIR -L $LIB_DIR"
|
||||
EMSCRIPTEN_OPTIONS="-lwebsocket.js -sWEBSOCKET_URL=wss://debug.ading.dev/ws -sASYNCIFY -sALLOW_UNIMPLEMENTED_SYSCALLS -sEXPORTED_FUNCTIONS=$EXPORTED_FUNCS --preload-file $CACERT_FILE"
|
||||
|
||||
if [ ! -f $CACERT_FILE ]; then
|
||||
wget "https://curl.se/ca/cacert.pem" -O $CACERT_FILE
|
||||
fi
|
||||
|
||||
if [ ! -d $INCLUDE_DIR ]; then
|
||||
tools/openssl.sh
|
||||
tools/curl.sh
|
||||
fi
|
||||
|
||||
COMPILE_CMD="emcc main.c $COMPILER_OPTIONS $EMSCRIPTEN_OPTIONS"
|
||||
echo $COMPILE_CMD
|
||||
$COMPILE_CMD
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
<head>
|
||||
<script src="./out/libcurl.js"></script>
|
||||
<script src="./main.js"></script>
|
||||
<link rel="icon" href="data:;base64,=">
|
||||
</head>
|
||||
<body>
|
||||
<p>emscripten tests</p>
|
||||
|
|
|
@ -1,34 +1,52 @@
|
|||
#include "emscripten/emscripten.h"
|
||||
#include <stdio.h>
|
||||
#include <curl/curl.h>
|
||||
#include <emscripten.h>
|
||||
|
||||
int main() {
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
|
||||
char* url = "https://ading.dev/";
|
||||
|
||||
int do_request(const char* url) {
|
||||
printf("downloading %s\n", url);
|
||||
|
||||
CURL *http_handle;
|
||||
CURLM *multi_handle;
|
||||
int still_running = 1;
|
||||
|
||||
curl = curl_easy_init();
|
||||
if (curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
curl_easy_setopt(curl, CURLOPT_PROXY, "socks5h://127.0.0.1:1234");
|
||||
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
||||
curl_easy_setopt(curl, CURLOPT_CAINFO, "/cacert.pem");
|
||||
curl_easy_setopt(curl, CURLOPT_CAPATH, "/cacert.pem");
|
||||
|
||||
/* example.com is redirected, so we tell libcurl to follow redirection */
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
http_handle = curl_easy_init();
|
||||
|
||||
/* Perform the request, res will get the return code */
|
||||
res = curl_easy_perform(curl);
|
||||
/* Check for errors */
|
||||
if(res != CURLE_OK)
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
||||
curl_easy_strerror(res));
|
||||
curl_easy_setopt(http_handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(http_handle, CURLOPT_PROXY, "socks5h://127.0.0.1:1234");
|
||||
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");
|
||||
|
||||
multi_handle = curl_multi_init();
|
||||
curl_multi_add_handle(multi_handle, http_handle);
|
||||
|
||||
do {
|
||||
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
|
||||
|
||||
if(!mc)
|
||||
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
|
||||
|
||||
if(mc) {
|
||||
fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc);
|
||||
break;
|
||||
}
|
||||
|
||||
//ensure we dont block the main thread
|
||||
emscripten_sleep(0);
|
||||
|
||||
} while(still_running);
|
||||
|
||||
curl_multi_remove_handle(multi_handle, http_handle);
|
||||
curl_easy_cleanup(http_handle);
|
||||
curl_multi_cleanup(multi_handle);
|
||||
curl_global_cleanup();
|
||||
|
||||
/* always cleanup */
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
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