diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-10 23:21:25 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-07-10 23:21:25 +0800 |
commit | 5d0a833af478b87f4fb815e37da0beb5a09c6067 (patch) | |
tree | 00a1e021aea50625d0c57ddeb4321af47a3c0641 /registration.c | |
parent | 873f8a55d01ecbd11c957d140eaf2a0bb30ae51f (diff) |
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.
Diffstat (limited to 'registration.c')
-rw-r--r-- | registration.c | 88 |
1 files changed, 74 insertions, 14 deletions
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 <android/log.h> +#else +#include <stdio.h> +#endif #ifdef __EMSCRIPTEN__ -#include <stdlib.h> -#include <string.h> #include <emscripten/fetch.h> #else +#include <threads.h> #include <curl/curl.h> #endif +#include <stdlib.h> +#include <string.h> +#include <icclient/typedefs.h> #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 } |