From 1df77942aee0dbd6e79e5aa019d2bcb95c59e6ec 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: Fri, 18 Jun 2021 02:25:02 +0800 Subject: Leave request as the minimum of what needs to be shared --- Makefile.am | 5 +++-- client.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- request.c | 20 -------------------- request.h | 30 +----------------------------- 4 files changed, 46 insertions(+), 53 deletions(-) delete mode 100644 request.c diff --git a/Makefile.am b/Makefile.am index 451b72d..ab46aea 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,6 @@ lib_LTLIBRARIES = libicclient.la libicclient_la_SOURCES = \ client.c \ request.h \ - request.c \ handler.c \ ord.c \ login.h \ @@ -25,7 +24,9 @@ pkginclude_HEADERS = \ icclient/ord.h \ icclient/member.h \ icclient/admin.h +#if !WASM #bin_PROGRAMS = icclient -#icclient_SOURCES = main.c client.c request.c handler.c login.c member.c admin.c +#icclient_SOURCES = main.c client.c handler.c login.c member.c admin.c #icclient_CPPFLAGS = $(TIDY_CFLAGS) #icclient_LDFLAGS = -lcurl $(TIDY_LIBS) +#endif diff --git a/client.c b/client.c index fd34d3b..b43d1c5 100644 --- a/client.c +++ b/client.c @@ -3,7 +3,22 @@ char *sampleurl; char *image_dir; +#ifdef __EMSCRIPTEN__ +emscripten_fetch_attr_t attr; +#else +CURL *curl; +size_t append(char *data, size_t size, size_t nmemb, icclient_response *response) +{ + size_t realsize = size * nmemb; + response->data = realloc(response->data, response->numBytes + realsize + 1); + memcpy(&(response->data[response->numBytes]), data, realsize); + response->numBytes += realsize; + response->data[response->numBytes] = '\0'; + return realsize; +} +#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) @@ -16,7 +31,20 @@ void icclient_init(const char *url, const char *dir, const char *certificate) strcat(sampleurl, "/"); image_dir = malloc(strlen(dir) + 1); strcpy(image_dir, dir); - init(certificate); +#ifdef __EMSCRIPTEN__ + emscripten_fetch_attr_init(&attr); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; +#else + 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_results(const char *prod_group, void (*handler)(icclient_response *), void (*callback)(struct icclient_catalog *)) @@ -67,9 +95,21 @@ void icclient_free_catalog(struct icclient_catalog *catalog) free(catalog); } +void icclient_free_response(icclient_response *response) +{ +#ifdef __EMSCRIPTEN__ + emscripten_fetch_close(response); +#else + if (response->userData) + free(response->userData); +#endif +} + void icclient_cleanup() { #ifndef __EMSCRIPTEN__ - cleanup(); + free(sampleurl); + curl_easy_cleanup(curl); + curl_global_cleanup(); #endif } diff --git a/request.c b/request.c deleted file mode 100644 index 7c92a51..0000000 --- a/request.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "request.h" - -#ifdef __EMSCRIPTEN__ -emscripten_fetch_attr_t attr; -#else -CURL *curl; -size_t append(char *data, size_t size, size_t nmemb, icclient_response *response) -{ - size_t realsize = size * nmemb; - response->data = realloc(response->data, response->numBytes + realsize + 1); - memcpy(&(response->data[response->numBytes]), data, realsize); - response->numBytes += realsize; - response->data[response->numBytes] = '\0'; - return realsize; -} -#endif - -extern inline void init(const char *); -extern inline void request(void (*)(icclient_response *), void (*)(void *), struct body *, char *, ...); -extern inline void cleanup(); diff --git a/request.h b/request.h index a946afb..561891a 100644 --- a/request.h +++ b/request.h @@ -29,24 +29,6 @@ extern char *sampleurl; size_t append(char *, size_t, size_t, icclient_response *); #endif -static inline void init(const char *certificate) -{ -#ifdef __EMSCRIPTEN__ - emscripten_fetch_attr_init(&attr); - attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; -#else - 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 -} - static inline void request(void (*handler)(icclient_response *), void (*callback)(void *), struct body *body, char *fmt, ...) { va_list ap; @@ -109,10 +91,9 @@ static inline void request(void (*handler)(icclient_response *), void (*callback #ifdef __EMSCRIPTEN__ if (handler) attr.onsuccess = handler; - char *post = NULL; if (body) { size_t length = 0; - post = malloc(1); + char *post = malloc(1); memset(post, '\0', 1); for (size_t i = 0; i < body->num_pairs; i++) { struct pair pair = body->pairs[i]; @@ -173,13 +154,4 @@ static inline void request(void (*handler)(icclient_response *), void (*callback #endif } -#ifndef __EMSCRIPTEN__ -static inline void cleanup() -{ - free(sampleurl); - curl_easy_cleanup(curl); - curl_global_cleanup(); -} -#endif - #endif -- cgit v1.2.3