From 5d0a833af478b87f4fb815e37da0beb5a09c6067 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: Sat, 10 Jul 2021 23:21:25 +0800 Subject: Use the response data to initiate client It's correct now, but the problem is, the client pushes the catalog page faster than the web server can reload to make the newly built sample URL valid. --- registration.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 14 deletions(-) (limited to 'registration.c') diff --git a/registration.c b/registration.c index f3a6a0a..5e0fe00 100644 --- a/registration.c +++ b/registration.c @@ -1,35 +1,86 @@ +#ifdef DEBUG +#ifdef __ANDROID__ +#include +#else +#include +#endif #ifdef __EMSCRIPTEN__ -#include -#include #include #else +#include #include #endif +#include +#include +#include #ifdef __EMSCRIPTEN__ -static void cleanup(struct emscripten_fetch_t *fetch) +static void clean_up(struct emscripten_fetch_t *fetch) { free(fetch->userData); emscripten_fetch_close(fetch); } +#else + +struct container { + CURL *curl; + void (*handler)(icclient_response *); + icclient_response *response; +}; + +static size_t append(char *data, size_t size, size_t nmemb, icclient_response *response) +{ + size_t realsize = size * nmemb; + response->data = realloc(response->data, response->numBytes + realsize + 1); + memcpy(&(response->data[response->numBytes]), data, realsize); + response->numBytes += realsize; + response->data[response->numBytes] = '\0'; + return realsize; +} + +static int async(void *arg) +{ + int ret = thrd_success; + struct container *container = (struct container *)arg; + CURLcode res = curl_easy_perform(container->curl); + if (res == CURLE_OK) + container->handler(container->response); + else { + ret = thrd_error; +#ifdef DEBUG + const char *error = curl_easy_strerror(res); +#ifdef __ANDROID__ + __android_log_print(ANDROID_LOG_ERROR, "namatoko.so", "%s", error); +#else + fprintf(stderr, "%s\n", error); +#endif +#endif + } + curl_easy_cleanup(container->curl); + free(container); + curl_global_cleanup(); + return ret; +} #endif -void sign_up(const char *brand, const char *certificate) +#endif + +void sign_up(const char *brand, const char *certificate, void (*handler)(icclient_response *)) { + char *data = malloc(strlen(brand) + 1); + strcpy(data, brand); #ifdef __EMSCRIPTEN__ emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; strcpy(attr.requestMethod, "POST"); - char *data = malloc(strlen(brand) + 1); - strcpy(data, brand); attr.requestData = data; - attr.requestDataSize = strlen(data) + 1; + attr.requestDataSize = strlen(data); attr.userData = data; - attr.onsuccess = cleanup; - attr.onerror = cleanup; + attr.onsuccess = handler; + attr.onerror = clean_up; emscripten_fetch(&attr, "registration"); (void)certificate; #else @@ -41,10 +92,19 @@ void sign_up(const char *brand, const char *certificate) #ifdef DEBUG curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); #endif - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, brand); - curl_easy_setopt(curl, CURLOPT_URL, SAMPLEURL"/registration"); - curl_easy_perform(curl); - curl_easy_cleanup(curl); - curl_global_cleanup(); + curl_easy_setopt(curl, CURLOPT_URL, SECURE_SERVER"/registration"); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); + icclient_response *response = malloc(sizeof(icclient_response)); + response->data = malloc(1); + response->numBytes = 0; + response->userData = data; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); + struct container *container = malloc(sizeof(struct container)); + container->curl = curl; + container->handler = handler; + container->response = response; + thrd_t thread; + thrd_create(&thread, async, container); #endif } -- cgit v1.2.3