mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-12 22:10: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"
|
CACERT_FILE="cacert.pem"
|
||||||
OUT_FILE="out/libcurl.js"
|
OUT_FILE="out/libcurl.js"
|
||||||
|
|
||||||
EXPORTED_FUNCS="_main"
|
EXPORTED_FUNCS="_main,_do_request"
|
||||||
COMPILER_OPTIONS="-o $OUT_FILE -Os -lcurl -lssl -lcrypto -I $INCLUDE_DIR -L $LIB_DIR"
|
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 -pthread -sPROXY_TO_PTHREAD -sEXPORTED_FUNCTIONS=$EXPORTED_FUNCS --preload-file $CACERT_FILE"
|
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
|
if [ ! -f $CACERT_FILE ]; then
|
||||||
wget "https://curl.se/ca/cacert.pem" -O $CACERT_FILE
|
wget "https://curl.se/ca/cacert.pem" -O $CACERT_FILE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ ! -d $INCLUDE_DIR ]; then
|
||||||
|
tools/openssl.sh
|
||||||
|
tools/curl.sh
|
||||||
|
fi
|
||||||
|
|
||||||
COMPILE_CMD="emcc main.c $COMPILER_OPTIONS $EMSCRIPTEN_OPTIONS"
|
COMPILE_CMD="emcc main.c $COMPILER_OPTIONS $EMSCRIPTEN_OPTIONS"
|
||||||
echo $COMPILE_CMD
|
echo $COMPILE_CMD
|
||||||
$COMPILE_CMD
|
$COMPILE_CMD
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
<head>
|
<head>
|
||||||
<script src="./out/libcurl.js"></script>
|
<script src="./out/libcurl.js"></script>
|
||||||
<script src="./main.js"></script>
|
<script src="./main.js"></script>
|
||||||
|
<link rel="icon" href="data:;base64,=">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<p>emscripten tests</p>
|
<p>emscripten tests</p>
|
||||||
|
|
|
@ -1,34 +1,52 @@
|
||||||
|
#include "emscripten/emscripten.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
#include <emscripten.h>
|
||||||
|
|
||||||
int main() {
|
int do_request(const char* url) {
|
||||||
CURL *curl;
|
|
||||||
CURLcode res;
|
|
||||||
|
|
||||||
char* url = "https://ading.dev/";
|
|
||||||
|
|
||||||
printf("downloading %s\n", url);
|
printf("downloading %s\n", url);
|
||||||
|
|
||||||
curl = curl_easy_init();
|
CURL *http_handle;
|
||||||
if (curl) {
|
CURLM *multi_handle;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
int still_running = 1;
|
||||||
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_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
http_handle = curl_easy_init();
|
||||||
|
|
||||||
/* Perform the request, res will get the return code */
|
curl_easy_setopt(http_handle, CURLOPT_URL, url);
|
||||||
res = curl_easy_perform(curl);
|
curl_easy_setopt(http_handle, CURLOPT_PROXY, "socks5h://127.0.0.1:1234");
|
||||||
/* Check for errors */
|
curl_easy_setopt(http_handle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
|
||||||
if(res != CURLE_OK)
|
curl_easy_setopt(http_handle, CURLOPT_CAINFO, "/cacert.pem");
|
||||||
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
curl_easy_setopt(http_handle, CURLOPT_CAPATH, "/cacert.pem");
|
||||||
curl_easy_strerror(res));
|
|
||||||
|
|
||||||
/* always cleanup */
|
multi_handle = curl_multi_init();
|
||||||
curl_easy_cleanup(curl);
|
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();
|
||||||
|
|
||||||
return 0;
|
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