From e39dff5001bdd36bd2a2ca546ff19ea39965f79c 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: Sat, 19 Jun 2021 17:23:52 +0800 Subject: One handle for one request --- client.c | 36 ++++++++++++++---------------------- handler.c | 2 +- request.h | 19 +++++++++++++++---- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/client.c b/client.c index b42bc16..b66470f 100644 --- a/client.c +++ b/client.c @@ -3,10 +3,12 @@ char *sampleurl; char *image_dir; -#ifdef __EMSCRIPTEN__ -emscripten_fetch_attr_t attr; -#else -CURL *curl; +char *certificate = NULL; + +extern inline void request(void (*)(icclient_response *), void (*)(void *), struct body *, char *, ...); +extern void handle_results(icclient_response *); + +#ifndef __EMSCRIPTEN__ size_t append(char *data, size_t size, size_t nmemb, icclient_response *response) { size_t realsize = size * nmemb; @@ -18,10 +20,7 @@ size_t append(char *data, size_t size, size_t nmemb, icclient_response *response } #endif -extern inline void request(void (*)(icclient_response *), void (*)(void *), struct body *, char *, ...); -extern void handle_results(icclient_response *); - -void icclient_init(const char *url, const char *dir, const char *certificate) +void icclient_init(const char *url, const char *dir, const char *cert) { size_t length = strlen(url); size_t append = url[length - 1] != '/'; @@ -31,20 +30,11 @@ void icclient_init(const char *url, const char *dir, const char *certificate) strcat(sampleurl, "/"); image_dir = malloc(strlen(dir) + 1); strcpy(image_dir, dir); -#ifdef __EMSCRIPTEN__ - emscripten_fetch_attr_init(&attr); - attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; -#else + if (certificate) { + certificate = malloc(strlen(certificate) + 1); + strcpy(certificate, cert); + } 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 -#endif } void icclient_catalog(const char *prod_group, void (*handler)(icclient_response *), void (*callback)(struct icclient_catalog *)) @@ -108,7 +98,9 @@ void icclient_cleanup() { #ifndef __EMSCRIPTEN__ free(sampleurl); - curl_easy_cleanup(curl); + free(image_dir); + if (certificate) + free(certificate); curl_global_cleanup(); #endif } diff --git a/handler.c b/handler.c index 5648731..a5833b8 100644 --- a/handler.c +++ b/handler.c @@ -34,7 +34,7 @@ static void recurse(TidyDoc doc, TidyNode tnod, struct icclient_catalog **catalo sprintf(prefix, "%s%s", image_dir, subdir); size_t prefix_len = strlen(prefix); for (TidyAttr attr = tidyAttrFirst(child); attr; attr = tidyAttrNext(attr)) { - ctmbstr name = tidyAttrName(attr); + name = tidyAttrName(attr); ctmbstr value = tidyAttrValue(attr); if (!strcmp(name, "src")) { if (strncmp(value, prefix, prefix_len)) diff --git a/request.h b/request.h index 561891a..11444de 100644 --- a/request.h +++ b/request.h @@ -9,6 +9,7 @@ #include #include #ifndef __EMSCRIPTEN__ +#include #include #endif #include "icclient/typedefs.h" @@ -21,11 +22,9 @@ struct body { } pairs[16]; }; -#ifdef __EMSCRIPTEN__ -extern emscripten_fetch_attr_t attr; -#else -extern CURL *curl; +#ifndef __EMSCRIPTEN__ extern char *sampleurl; +extern char *certificate; size_t append(char *, size_t, size_t, icclient_response *); #endif @@ -89,6 +88,9 @@ static inline void request(void (*handler)(icclient_response *), void (*callback va_end(ap); #ifdef __EMSCRIPTEN__ + emscripten_fetch_attr_t attr; + emscripten_fetch_attr_init(&attr); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; if (handler) attr.onsuccess = handler; if (body) { @@ -117,6 +119,14 @@ static inline void request(void (*handler)(icclient_response *), void (*callback } emscripten_fetch(&attr, url); #else + CURL *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 curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); icclient_response response = { .userData = callback, .numBytes = 0 }; @@ -151,6 +161,7 @@ static inline void request(void (*handler)(icclient_response *), void (*callback #endif } #endif + curl_easy_cleanup(curl); #endif } -- cgit v1.2.3