#ifdef DEBUG #ifdef __ANDROID__ #include #else #include #endif #ifdef __EMSCRIPTEN__ #include #else #include #include #endif #include #include #include #ifdef __EMSCRIPTEN__ 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 #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"); attr.requestData = data; attr.requestDataSize = strlen(data); attr.userData = data; attr.onsuccess = handler; attr.onerror = clean_up; emscripten_fetch(&attr, "registration"); (void)certificate; #else curl_global_init(CURL_GLOBAL_SSL); CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); if (certificate) curl_easy_setopt(curl, CURLOPT_CAINFO, certificate); #ifdef DEBUG curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); #endif 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 }