1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-15 23:35:34 +00:00

[quickjs] curl for synchronous ajax

GET method
This commit is contained in:
Witold Filipczyk 2023-01-29 18:29:07 +01:00
parent 2faa4a6ff3
commit 2e33d2c1ca
3 changed files with 64 additions and 0 deletions

View File

@ -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

View File

@ -46,6 +46,8 @@
#include "viewer/text/form.h" /* <-ecmascript_reset_state() */
#include "viewer/text/vs.h"
#include <curl/curl.h>
#include <libxml/tree.h>
#include <libxml/HTMLparser.h>
#include <libxml++/libxml++.h>
@ -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);

View File

@ -72,6 +72,8 @@
#include "viewer/text/link.h"
#include "viewer/text/vs.h"
#include <curl/curl.h>
#include <iostream>
#include <map>
#include <sstream>
@ -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;