diff options
-rw-r--r-- | icclient/typedefs.h | 15 | ||||
-rw-r--r-- | request.h | 22 |
2 files changed, 9 insertions, 28 deletions
diff --git a/icclient/typedefs.h b/icclient/typedefs.h index 73d8adf..80e09b1 100644 --- a/icclient/typedefs.h +++ b/icclient/typedefs.h @@ -4,30 +4,15 @@ #ifdef __cplusplus #include <cstddef> #endif - #ifdef __EMSCRIPTEN__ - #include <emscripten/fetch.h> - typedef emscripten_fetch_t icclient_response; -typedef char icclient_post; - #else - -#include <curl/curl.h> - typedef struct { void *userData; char *data; size_t numBytes; } icclient_response; -typedef struct curl_httppost icclient_post; - #endif -struct icclient_post_callback { - icclient_post *post; - void (*callback)(void *); -}; - #endif @@ -8,6 +8,9 @@ #include <stdlib.h> #include <stdarg.h> #include <string.h> +#ifndef __EMSCRIPTEN__ +#include <curl/curl.h> +#endif #include "icclient/typedefs.h" struct body { @@ -126,10 +129,7 @@ static inline void request(void (*handler)(icclient_response *), void (*callback attr.requestHeaders = headers; attr.requestData = post; attr.requestDataSize = length + 1; - struct icclient_post_callback *post_callback = malloc(sizeof(struct icclient_post_callback)); - post_callback->post = post; - post_callback->callback = callback; - attr.userData = post_callback; + attr.userData = post; } else { strcpy(attr.requestMethod, "GET"); attr.userData = callback; @@ -138,7 +138,8 @@ static inline void request(void (*handler)(icclient_response *), void (*callback #else curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); - icclient_response response = { .numBytes = 0 }; + icclient_response response = { .userData = callback, .numBytes = 0 }; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); struct curl_httppost *post, *last = NULL; if (body) { for (size_t i = 0; i < body->num_pairs; i++) { @@ -152,16 +153,11 @@ static inline void request(void (*handler)(icclient_response *), void (*callback } last = NULL; curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); - struct icclient_post_callback *post_callback = malloc(sizeof(struct icclient_post_callback)); - post_callback->post = post; - post_callback->callback = callback; - response.userData = post_callback; - } else { + } else curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - response.userData = callback; - } - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); CURLcode res = curl_easy_perform(curl); + if (post) + curl_formfree(post); if (res == CURLE_OK && handler) handler(&response); #ifdef DEBUG |