From 2e33d2c1ca2a28282c15bc419c9a57841a399031 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sun, 29 Jan 2023 18:29:07 +0100 Subject: [PATCH] [quickjs] curl for synchronous ajax GET method --- meson.build | 5 ++++ src/ecmascript/ecmascript.cpp | 4 +++ src/ecmascript/quickjs/xhr.cpp | 55 ++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/meson.build b/meson.build index 024c34ab..e5dc503c 100644 --- a/meson.build +++ b/meson.build @@ -401,6 +401,11 @@ if conf_data.get('CONFIG_ECMASCRIPT_SMJS') or conf_data.get('CONFIG_QUICKJS') or conf_data.set('CONFIG_XML', true) endif +if conf_data.get('CONFIG_ECMASCRIPT_SMJS') or conf_data.get('CONFIG_QUICKJS') or conf_data.get('CONFIG_MUJS') + curldeps = dependency('libcurl', static: st) + deps += curldeps +endif + if conf_data.get('CONFIG_SCRIPTING_LUA') luadeps = dependency(luapkg, static: st) deps += luadeps diff --git a/src/ecmascript/ecmascript.cpp b/src/ecmascript/ecmascript.cpp index 51158098..2ca42087 100644 --- a/src/ecmascript/ecmascript.cpp +++ b/src/ecmascript/ecmascript.cpp @@ -46,6 +46,8 @@ #include "viewer/text/form.h" /* <-ecmascript_reset_state() */ #include "viewer/text/vs.h" +#include + #include #include #include @@ -802,11 +804,13 @@ init_ecmascript_module(struct module *module) #endif } ecmascript_enabled = get_opt_bool("ecmascript.enable", NULL); + curl_global_init(CURL_GLOBAL_ALL); } static void done_ecmascript_module(struct module *module) { + curl_global_cleanup(); free_string_list(&allowed_urls); free_string_list(&disallowed_urls); mem_free_if(console_log_filename); diff --git a/src/ecmascript/quickjs/xhr.cpp b/src/ecmascript/quickjs/xhr.cpp index be943b5d..f930bcf3 100644 --- a/src/ecmascript/quickjs/xhr.cpp +++ b/src/ecmascript/quickjs/xhr.cpp @@ -72,6 +72,8 @@ #include "viewer/text/link.h" #include "viewer/text/vs.h" +#include + #include #include #include @@ -1311,6 +1313,30 @@ xhr_overridemimetype(JSContext *ctx, JSValueConst this_val, int argc, JSValueCon return JS_ThrowTypeError(ctx, "unsupported"); } +static size_t +write_data(void *ptr, size_t size, size_t nmemb, void *stream) +{ + Xhr *x = (Xhr *)stream; + + size_t length = 0; + + if (x->response_text) { + length = strlen(x->response_text); + } + + char *n = (char *)mem_realloc(x->response_text, length + size * nmemb + 1); + + if (n) { + x->response_text = n; + } else { + return 0; + } + memcpy(x->response_text + length, ptr, (size * nmemb)); + x->response_text[length + size * nmemb] = '\0'; + + return nmemb; +} + static JSValue xhr_send(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { @@ -1369,6 +1395,35 @@ xhr_send(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) if (x->uri->protocol == PROTOCOL_FILE && !get_opt_bool("ecmascript.allow_xhr_file", NULL)) { return JS_UNDEFINED; } + + if (!x->async) { + char *url = get_uri_string(x->uri, URI_DIR_LOCATION | URI_PATH | URI_USER | URI_PASSWORD); + + if (!url) { + return JS_UNDEFINED; + } + + x->sent = true; + /* init the curl session */ + CURL *curl_handle = curl_easy_init(); + /* set URL to get here */ + curl_easy_setopt(curl_handle, CURLOPT_URL, url); + curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L); + /* disable progress meter, set to 0L to enable it */ + curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); + /* send all data to this function */ + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); + /* write the page body to this file handle */ + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, x); + /* get it! */ + curl_easy_perform(curl_handle); + /* cleanup curl stuff */ + curl_easy_cleanup(curl_handle); + x->ready_state = XHR_RSTATE_DONE; + x->status = 200; + mem_free(url); + return JS_UNDEFINED; + } x->sent = true; x->download.data = x; x->download.callback = (download_callback_T *)x_loading_callback;