1
0
Fork 0

[xhr] Code for synchronous POSTs

This commit is contained in:
Witold Filipczyk 2023-01-30 16:31:33 +01:00
parent c8aba2ef8c
commit c455fbee19
3 changed files with 41 additions and 3 deletions

View File

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

View File

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

View File

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