diff options
-rw-r--r-- | Makefile.am | 5 | ||||
-rw-r--r-- | client.c | 44 | ||||
-rw-r--r-- | request.c | 20 | ||||
-rw-r--r-- | request.h | 30 |
4 files changed, 46 insertions, 53 deletions
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 @@ -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(); @@ -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 |