summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-17 20:52:46 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-06-17 20:52:46 +0800
commit0ca65138cf02d6c46e1857e911a142d08cdad761 (patch)
tree13f43c6755f6bfde8861ff6f682206133893032b
parentac40a2ae4e9ca7a386caba1505b454a15c3dce7a (diff)
Container for post and callback
-rw-r--r--icclient/typedefs.h15
-rw-r--r--request.h22
2 files changed, 28 insertions, 9 deletions
diff --git a/icclient/typedefs.h b/icclient/typedefs.h
index 2b0ec21..fbfa0d0 100644
--- a/icclient/typedefs.h
+++ b/icclient/typedefs.h
@@ -4,15 +4,30 @@
#ifdef __cplusplus
#include <cstddef>
#endif
+
#ifdef __EMSCRIPTEN__
+
#include <emscripten/fetch.h>
+
typedef emscripten_fetch_t icclient_fetch_t;
+typedef char icclient_post;
+
#else
+
+#include <curl/curl.h>
+
typedef struct {
void *userData;
char *data;
size_t numBytes;
} icclient_fetch_t;
+typedef struct curl_httppost icclient_post;
+
#endif
+struct icclient_post_callback {
+ icclient_post *post;
+ void (*callback)(icclient_fetch_t *);
+};
+
#endif
diff --git a/request.h b/request.h
index e3b81c8..1327ef3 100644
--- a/request.h
+++ b/request.h
@@ -8,9 +8,6 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
-#ifndef __EMSCRIPTEN__
-#include <curl/curl.h>
-#endif
#include "icclient/typedefs.h"
struct body {
@@ -129,7 +126,10 @@ static inline void request(void (*handler)(icclient_fetch_t *), void *callback,
attr.requestHeaders = headers;
attr.requestData = post;
attr.requestDataSize = length + 1;
- attr.userData = post;
+ struct icclient_post_callback *post_callback = malloc(sizeof(struct icclient_post_callback));
+ post_callback->post = post;
+ post_callback->callback = callback;
+ attr.userData = post_callback;
} else {
strcpy(attr.requestMethod, "GET");
attr.userData = callback;
@@ -138,8 +138,7 @@ static inline void request(void (*handler)(icclient_fetch_t *), void *callback,
#else
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append);
- icclient_fetch_t fetch = { .userData = callback, .numBytes = 0 };
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fetch);
+ icclient_fetch_t fetch = { .numBytes = 0 };
struct curl_httppost *post, *last = NULL;
if (body) {
for (size_t i = 0; i < body->num_pairs; i++) {
@@ -153,11 +152,16 @@ static inline void request(void (*handler)(icclient_fetch_t *), void *callback,
}
last = NULL;
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
- } else
+ struct icclient_post_callback *post_callback = malloc(sizeof(struct icclient_post_callback));
+ post_callback->post = post;
+ post_callback->callback = callback;
+ fetch.userData = post_callback;
+ } else {
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
+ fetch.userData = callback;
+ }
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fetch);
CURLcode res = curl_easy_perform(curl);
- if (post)
- curl_formfree(post);
if (res == CURLE_OK && handler)
handler(&fetch);
#ifdef DEBUG