add support for saving and loading cookies

This commit is contained in:
ading2210 2024-03-19 18:41:54 -04:00
parent d23703ff9a
commit f8e55ea307
7 changed files with 65 additions and 12 deletions

View file

@ -14,6 +14,7 @@ get_error_str
http_set_options
http_get_info
http_set_cookie_jar
websocket_set_options
recv_from_websocket
@ -30,7 +31,4 @@ tls_socket_set_options
recv_from_socket
send_to_socket
ftp_set_options
ftp_set_cmd
free

View file

@ -1,8 +1,41 @@
class HTTPSession extends CurlSession {
constructor(options) {
constructor(options={}) {
super();
this.options = options;
this.set_connections(50, 40);
this.import_cookies();
}
import_cookies() {
if (this.options.enable_cookies) {
this.cookie_filename = `/cookies_${Math.random()}.txt`;
if (this.options.cookie_jar) {
FS.writeFile(this.cookie_filename, this.options.cookie_jar);
}
}
}
export_cookies() {
if (!this.cookie_filename) return "";
try {
return FS.readFile(this.cookie_filename, {encoding: "utf8"});
}
catch (e) {
if (e.errno === 44) return "";
throw e;
}
}
close() {
if (this.cookie_filename) {
try {
FS.unlink(this.cookie_filename);
}
catch (e) {}
}
super.close();
}
request_async(url, params, body) {
@ -38,6 +71,10 @@ class HTTPSession extends CurlSession {
http_handle = this.stream_response(url, headers_callback, finish_callback, params.signal);
c_func(_http_set_options, [http_handle, params_json, body, body_length]);
if (this.cookie_filename && params.credentials !== "omit") {
c_func(_http_set_cookie_jar, [http_handle, this.cookie_filename]);
}
this.start_request(http_handle);
});
}

View file

@ -146,8 +146,6 @@ class CurlSession {
headers_callback(stream);
}
console.log(error);
try {
stream_controller.close();
}

View file

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cjson/cJSON.h"
#include "curl/curl.h"
@ -65,6 +66,11 @@ void http_set_options(CURL* http_handle, const char* json_params, const char* bo
request_info->headers_list = headers_list;
}
void http_set_cookie_jar(CURL* http_handle, const char* filename) {
curl_easy_setopt(http_handle, CURLOPT_COOKIEFILE, filename);
curl_easy_setopt(http_handle, CURLOPT_COOKIEJAR, filename);
}
char* http_get_info(CURL* http_handle) {
struct RequestInfo *request_info = get_request_info(http_handle);

View file

@ -16,7 +16,7 @@ git clone -b v5.6.6-stable --depth=1 https://github.com/wolfSSL/wolfssl wolfssl
cd wolfssl
autoreconf -fi
CFLAGS="-Oz -DSP_WORD_SIZE=32" emconfigure ./configure --prefix=$PREFIX --enable-curl --enable-static --disable-shared --host=i686-linux --disable-examples --disable-asm --enable-sni --enable-alpn --enable-truncatedhmac --enable-oldtls --enable-tlsv12 --enable-all-crypto --disable-asyncthreads --disable-threadlocal --enable-tlsx
CFLAGS="-Oz -DSP_WORD_SIZE=32 -DWOLFSSL_NO_ATOMICS" emconfigure ./configure --prefix=$PREFIX --enable-curl --enable-static --disable-shared --host=i686-linux --disable-examples --disable-asm --enable-sni --enable-alpn --enable-truncatedhmac --enable-oldtls --enable-tlsv12 --enable-all-crypto --disable-asyncthreads --disable-threadlocal --enable-tlsx
emmake make -j$CORE_COUNT
make install