diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-19 19:14:26 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-19 19:14:26 +0800 |
commit | ded922b3f8cceabcdadf519b1ad233774d9f0a45 (patch) | |
tree | f811ddcb270bbba7703806f17c5d2f199396e4ac | |
parent | 01dd138ede339e0722956de2c116599a596fcf32 (diff) |
One handle for the whole session
-rw-r--r-- | client.c | 28 | ||||
-rw-r--r-- | request.c | 23 |
2 files changed, 22 insertions, 29 deletions
@@ -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 } @@ -6,15 +6,13 @@ #include <stdarg.h> #include <string.h> #include "request.h" - -#ifndef __EMSCRIPTEN__ - +#ifdef __EMSCRIPTEN__ +extern emscripten_fetch_attr_t attr; +#else #include <threads.h> #include <curl/curl.h> - 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 |