summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-19 19:14:26 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-19 19:14:26 +0800
commitded922b3f8cceabcdadf519b1ad233774d9f0a45 (patch)
treef811ddcb270bbba7703806f17c5d2f199396e4ac
parent01dd138ede339e0722956de2c116599a596fcf32 (diff)
One handle for the whole session
-rw-r--r--client.c28
-rw-r--r--request.c23
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 <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