diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-17 20:52:46 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-17 20:52:46 +0800 |
commit | 0ca65138cf02d6c46e1857e911a142d08cdad761 (patch) | |
tree | 13f43c6755f6bfde8861ff6f682206133893032b /request.h | |
parent | ac40a2ae4e9ca7a386caba1505b454a15c3dce7a (diff) |
Container for post and callback
Diffstat (limited to 'request.h')
-rw-r--r-- | request.h | 22 |
1 files changed, 13 insertions, 9 deletions
@@ -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 |