From c455fbee1965477216a7c35a56b31f68f82bda63 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 30 Jan 2023 16:31:33 +0100 Subject: [PATCH] [xhr] Code for synchronous POSTs --- src/ecmascript/mujs/xhr.cpp | 13 ++++++++++++- src/ecmascript/quickjs/xhr.cpp | 16 +++++++++++++++- src/ecmascript/spidermonkey/xhr.cpp | 15 ++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/ecmascript/mujs/xhr.cpp b/src/ecmascript/mujs/xhr.cpp index c26fcb6e..315a903e 100644 --- a/src/ecmascript/mujs/xhr.cpp +++ b/src/ecmascript/mujs/xhr.cpp @@ -830,7 +830,7 @@ mjs_xhr_send(js_State *J) const char *body = NULL; - if (xhr->method == POST && !js_isundefined(J, 1)) { + if (xhr->async && xhr->method == POST && !js_isundefined(J, 1)) { body = js_tostring(J, 1); if (body) { @@ -881,6 +881,17 @@ mjs_xhr_send(js_State *J) curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, xhr); + + if (!js_isundefined(J, 1)) { + const char *body = js_tostring(J, 1); + size_t size = strlen(body); + + if (body) { + curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, (long) size); + curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, body); + } + } + curl_easy_perform(curl_handle); curl_easy_cleanup(curl_handle); xhr->readyState = DONE; diff --git a/src/ecmascript/quickjs/xhr.cpp b/src/ecmascript/quickjs/xhr.cpp index c22e17ea..5b9ddef0 100644 --- a/src/ecmascript/quickjs/xhr.cpp +++ b/src/ecmascript/quickjs/xhr.cpp @@ -1352,7 +1352,7 @@ xhr_send(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) if (!x->sent) { JSValue arg = argv[0]; - if (x->method == POST && JS_IsString(arg)) { + if (x->async && x->method == POST && JS_IsString(arg)) { size_t size; const char *body = JS_ToCStringLen(ctx, &size, arg); @@ -1413,6 +1413,20 @@ xhr_send(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) 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); + + JSValue arg = argv[0]; + if (JS_IsString(arg)) { + size_t size; + const char *body = JS_ToCStringLen(ctx, &size, arg); + + if (body) { + curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, (long) size); + curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, body); + + JS_FreeCString(ctx, body); + } + } + /* get it! */ curl_easy_perform(curl_handle); /* cleanup curl stuff */ diff --git a/src/ecmascript/spidermonkey/xhr.cpp b/src/ecmascript/spidermonkey/xhr.cpp index 1f13850d..b1a00131 100644 --- a/src/ecmascript/spidermonkey/xhr.cpp +++ b/src/ecmascript/spidermonkey/xhr.cpp @@ -988,7 +988,7 @@ xhr_send(JSContext *ctx, unsigned int argc, JS::Value *rval) char *body = NULL; - if (xhr->method == POST && argc == 1) { + if (xhr->async && xhr->method == POST && argc == 1) { body = jsval_to_string(ctx, args[0]); if (body) { @@ -1041,6 +1041,19 @@ xhr_send(JSContext *ctx, unsigned int argc, JS::Value *rval) curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, xhr); + + if (argc > 0) { + char *body = jsval_to_string(ctx, args[0]); + + if (body) { + size_t size = strlen(body); + + curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, (long) size); + curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, body); + + mem_free(body); + } + } curl_easy_perform(curl_handle); curl_easy_cleanup(curl_handle); xhr->readyState = DONE;