From ded922b3f8cceabcdadf519b1ad233774d9f0a45 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, 19 Jun 2021 19:14:26 +0800 Subject: One handle for the whole session --- client.c | 28 ++++++++++++++++++---------- request.c | 23 ++++------------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/client.c b/client.c index 865fe9e..b46f973 100644 --- a/client.c +++ b/client.c @@ -7,29 +7,38 @@ #include "icclient.h" char *image_dir; -#ifndef __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ +emscripten_fetch_attr_t attr; +#else char *sampleurl; -char *certificate = NULL; +CURL *curl; #endif extern void handle_results(icclient_response *); -void icclient_init(const char *url, const char *dir, const char *cert) +void icclient_init(const char *url, const char *dir, const char *certificate) { image_dir = malloc(strlen(dir) + 1); strcpy(image_dir, dir); -#ifndef __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ + emscripten_fetch_attr_init(&attr); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; +#else size_t length = strlen(url); size_t append = url[length - 1] != '/'; sampleurl = malloc(length + append + 1); strcpy(sampleurl, url); if (append) strcat(sampleurl, "/"); - if (certificate) { - certificate = malloc(strlen(certificate) + 1); - strcpy(certificate, cert); - } curl_global_init(CURL_GLOBAL_SSL); + curl = curl_easy_init(); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); + if (certificate) + curl_easy_setopt(curl, CURLOPT_CAINFO, certificate); +#ifdef DEBUG + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); +#endif #endif } @@ -98,8 +107,7 @@ void icclient_cleanup() free(image_dir); #ifndef __EMSCRIPTEN__ free(sampleurl); - if (certificate) - free(certificate); + curl_easy_cleanup(curl); curl_global_cleanup(); #endif } diff --git a/request.c b/request.c index cee61a1..e2fed5a 100644 --- a/request.c +++ b/request.c @@ -6,15 +6,13 @@ #include #include #include "request.h" - -#ifndef __EMSCRIPTEN__ - +#ifdef __EMSCRIPTEN__ +extern emscripten_fetch_attr_t attr; +#else #include #include - extern char *sampleurl; -extern char *certificate; - +extern CURL *curl; static size_t append(char *data, size_t size, size_t nmemb, icclient_response *response) { size_t realsize = size * nmemb; @@ -24,7 +22,6 @@ static size_t append(char *data, size_t size, size_t nmemb, icclient_response *r response->data[response->numBytes] = '\0'; return realsize; } - #endif void request(void (*handler)(icclient_response *), void (*callback)(void *), struct body *body, char *fmt, ...) @@ -87,9 +84,6 @@ void request(void (*handler)(icclient_response *), void (*callback)(void *), str va_end(ap); #ifdef __EMSCRIPTEN__ - emscripten_fetch_attr_t attr; - emscripten_fetch_attr_init(&attr); - attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; if (handler) attr.onsuccess = handler; if (body) { @@ -118,14 +112,6 @@ void request(void (*handler)(icclient_response *), void (*callback)(void *), str } emscripten_fetch(&attr, url); #else - CURL *curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); - 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, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); icclient_response *response = malloc(sizeof(icclient_response)); @@ -151,7 +137,6 @@ void request(void (*handler)(icclient_response *), void (*callback)(void *), str CURLcode res = curl_easy_perform(curl); if (post) curl_formfree(post); - curl_easy_cleanup(curl); if (res == CURLE_OK && handler) handler(response); #ifdef DEBUG -- cgit v1.2.3