From 4bcf7322f3e8fc9f16afd7a17e1bbf68199810ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Mon, 5 Jul 2021 21:00:07 +0800 Subject: Allocate memory for POST data on Fetch API Otherwise it might get lost before it reaches the server. --- registration.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/registration.c b/registration.c index 1693f09..3da97b8 100644 --- a/registration.c +++ b/registration.c @@ -1,10 +1,21 @@ #ifdef __EMSCRIPTEN__ +#include #include #include #else #include #endif +#ifdef __EMSCRIPTEN__ + +static void cleanup(struct emscripten_fetch_t *fetch) +{ + free(fetch->userData); + emscripten_fetch_close(fetch); +} + +#endif + void sign_up(const char *brand, const char *certificate) { #ifdef __EMSCRIPTEN__ @@ -12,8 +23,13 @@ void sign_up(const char *brand, const char *certificate) emscripten_fetch_attr_init(&attr); attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; strcpy(attr.requestMethod, "POST"); - attr.requestData = brand; - attr.requestDataSize = strlen(brand) + 1; + char *data = malloc(strlen(brand) + 1); + strcpy(data, brand); + attr.requestData = data; + attr.requestDataSize = strlen(data) + 1; + attr.userData = data; + attr.onsuccess = cleanup; + attr.onerror = cleanup; emscripten_fetch(&attr, "register"); (void)certificate; #else -- cgit v1.2.3