diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-11-14 11:10:43 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-11-14 11:10:43 +0800 |
commit | 8fe15035d1d08e2e628be26f73044baa58e459c4 (patch) | |
tree | cd5a7aca507851a8c99fa64adeaee5b250deeed9 | |
parent | ddeb72bd001a0486d11c76bda995bebedf85f509 (diff) |
One handle for every thread
See libcurl-tutorial(3) manual page, "Handle the Easy libcurl".
-rw-r--r-- | client.c | 20 | ||||
-rw-r--r-- | icclient/typedefs.h | 2 | ||||
-rw-r--r-- | request.c | 15 |
3 files changed, 20 insertions, 17 deletions
@@ -1,8 +1,5 @@ #include <stdlib.h> #include <string.h> -#ifndef __EMSCRIPTEN__ -#include <curl/curl.h> -#endif #include "request.h" #include "icclient.h" @@ -11,7 +8,7 @@ char *image_dir; emscripten_fetch_attr_t attr; #else char *sampleurl; -CURL *curl; +char *cainfo = NULL; #endif extern void handle_results(icclient_response *); @@ -31,14 +28,10 @@ void icclient_init(const char *url, const char *dir, const char *certificate) if (append) strcat(sampleurl, "/"); 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 + if (certificate) { + cainfo = malloc(strlen(certificate) + 1); + strcpy(cainfo, certificate); + } #endif } @@ -98,6 +91,7 @@ void icclient_free_response(icclient_response *response) emscripten_fetch_close(response); #else free(response->data); + curl_easy_cleanup(response->curl); free(response); #endif } @@ -106,8 +100,8 @@ void icclient_cleanup() { free(image_dir); #ifndef __EMSCRIPTEN__ + free(cainfo); free(sampleurl); - curl_easy_cleanup(curl); curl_global_cleanup(); #endif } diff --git a/icclient/typedefs.h b/icclient/typedefs.h index 6876144..4bb6dcc 100644 --- a/icclient/typedefs.h +++ b/icclient/typedefs.h @@ -6,10 +6,12 @@ #include <emscripten/fetch.h> typedef emscripten_fetch_t icclient_response; #else +#include <curl/curl.h> typedef struct { void *userData; char *data; size_t numBytes; + CURL *curl; } icclient_response; #endif @@ -17,10 +17,9 @@ extern emscripten_fetch_attr_t attr; #include <curl/curl.h> extern char *sampleurl; -extern CURL *curl; +extern char *cainfo; struct container { - CURL *curl; struct curl_httppost *post; void (*handler)(icclient_response *); icclient_response *response; @@ -40,7 +39,7 @@ static int async(void *arg) { int ret = thrd_success; struct container *container = (struct container *)arg; - CURLcode res = curl_easy_perform(container->curl); + CURLcode res = curl_easy_perform(container->response->curl); if (container->post) curl_formfree(container->post); if (res == CURLE_OK && container->handler) @@ -149,6 +148,14 @@ 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 (cainfo) + curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo); +#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)); @@ -170,7 +177,7 @@ void request(void (*handler)(icclient_response *), void (*callback)(void *), str } else curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); struct container *container = malloc(sizeof(struct container)); - container->curl = curl; + response->curl = curl; container->post = post; container->handler = handler; container->response = response; |