From 0ca65138cf02d6c46e1857e911a142d08cdad761 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: Thu, 17 Jun 2021 20:52:46 +0800 Subject: Container for post and callback --- icclient/typedefs.h | 15 +++++++++++++++ request.h | 22 +++++++++++++--------- 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 #endif + #ifdef __EMSCRIPTEN__ + #include + typedef emscripten_fetch_t icclient_fetch_t; +typedef char icclient_post; + #else + +#include + 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 #include #include -#ifndef __EMSCRIPTEN__ -#include -#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 -- cgit v1.2.3