diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-19 18:39:17 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-19 18:39:17 +0800 |
commit | 6374c26a020f7c0f551ee62078f7cfac61e14128 (patch) | |
tree | d888981df48f59d1d6c2e42985ccdf8fbbf03a23 | |
parent | 3eb1ed77e876778e846e2e15515716ea36dc1457 (diff) |
request not inline any more
-rw-r--r-- | Makefile.am | 5 | ||||
-rw-r--r-- | client.c | 33 | ||||
-rw-r--r-- | ord.c | 1 | ||||
-rw-r--r-- | request.c | 168 | ||||
-rw-r--r-- | request.h | 159 |
5 files changed, 190 insertions, 176 deletions
diff --git a/Makefile.am b/Makefile.am index ab46aea..8d4dfc8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,10 +1,9 @@ lib_LTLIBRARIES = libicclient.la libicclient_la_SOURCES = \ client.c \ - request.h \ + request.c \ handler.c \ ord.c \ - login.h \ login.c \ member.c \ admin.c @@ -26,7 +25,7 @@ pkginclude_HEADERS = \ icclient/admin.h #if !WASM #bin_PROGRAMS = icclient -#icclient_SOURCES = main.c client.c handler.c login.c member.c admin.c +#icclient_SOURCES = main.c client.c request.c handler.c login.c member.c admin.c #icclient_CPPFLAGS = $(TIDY_CFLAGS) #icclient_LDFLAGS = -lcurl $(TIDY_LIBS) #endif @@ -1,40 +1,34 @@ +#include <stdlib.h> +#include <string.h> +#ifndef __EMSCRIPTEN__ +#include <curl/curl.h> +#endif #include "request.h" #include "icclient.h" -char *sampleurl; char *image_dir; +#ifndef __EMSCRIPTEN__ +char *sampleurl; char *certificate = NULL; +#endif -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; - 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 - void icclient_init(const char *url, const char *dir, const char *cert) { + image_dir = malloc(strlen(dir) + 1); + strcpy(image_dir, dir); +#ifndef __EMSCRIPTEN__ size_t length = strlen(url); size_t append = url[length - 1] != '/'; sampleurl = malloc(length + append + 1); strcpy(sampleurl, url); if (append) strcat(sampleurl, "/"); - image_dir = malloc(strlen(dir) + 1); - strcpy(image_dir, dir); if (certificate) { certificate = malloc(strlen(certificate) + 1); strcpy(certificate, cert); } -#ifndef __EMSCRIPTEN__ curl_global_init(CURL_GLOBAL_SSL); #endif } @@ -93,14 +87,17 @@ void icclient_free_response(icclient_response *response) free(response->userData); #ifdef __EMSCRIPTEN__ emscripten_fetch_close(response); +#else + free(response->data); + free(response); #endif } void icclient_cleanup() { + free(image_dir); #ifndef __EMSCRIPTEN__ free(sampleurl); - free(image_dir); if (certificate) free(certificate); curl_global_cleanup(); @@ -1,4 +1,5 @@ #include <stdlib.h> +#include <string.h> #include <stdbool.h> #include "request.h" #include "icclient.h" diff --git a/request.c b/request.c new file mode 100644 index 0000000..72cbf1c --- /dev/null +++ b/request.c @@ -0,0 +1,168 @@ +#if defined __ANDROID__ && defined DEBUG +#include <android/log.h> +#endif +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include "request.h" + +#ifndef __EMSCRIPTEN__ + +#include <threads.h> +#include <curl/curl.h> + +extern char *sampleurl; +extern char *certificate; + +static 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 + +void request(void (*handler)(icclient_response *), void (*callback)(void *), struct body *body, char *fmt, ...) +{ + va_list ap; + char *p, *sval; + unsigned int ival; + size_t length = +#ifndef __EMSCRIPTEN__ + strlen(sampleurl) + +#endif + strlen(fmt); + + va_start(ap, fmt); + for (p = fmt; *p; p++) { + if (*p != '%') + continue; + switch(*++p) { + case 's': + sval = va_arg(ap, char *); + length += strlen(sval) - 2; + break; + case 'd': + ival = va_arg(ap, unsigned int); + do { + length++; + } while ((ival /= 10)); + length -= 2; + break; + default: + break; + } + } + va_end(ap); + + char url[length + 1]; +#ifdef __EMSCRIPTEN__ + memset(url, '\0', length + 1); +#else + strcpy(url, sampleurl); +#endif + + va_start(ap, fmt); + for (p = fmt; *p; p++) { + if (*p != '%') + continue; + switch(*++p) { + case 's': + sval = va_arg(ap, char *); + strcat(url, sval); + break; + case 'd': + ival = va_arg(ap, unsigned int); + sprintf(url, "%s%d", url, ival); + break; + default: + break; + } + } + 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) { + size_t length = 0; + char *post = malloc(1); + memset(post, '\0', 1); + for (size_t i = 0; i < body->num_pairs; i++) { + struct pair pair = body->pairs[i]; + if (!pair.value) + continue; + length += strlen(pair.key) + strlen(pair.value) + (i ? 1 : 0) + 1; + post = realloc(post, length + 1); + if (i) + strcat(post, "&"); + sprintf(post, "%s%s=%s", post, pair.key, pair.value); + } + strcpy(attr.requestMethod, "POST"); + const char *headers[] = { "Content-Type", "application/x-www-form-urlencoded", NULL }; + attr.requestHeaders = headers; + attr.requestData = post; + attr.requestDataSize = length + 1; + attr.userData = post; + } else { + strcpy(attr.requestMethod, "GET"); + attr.userData = 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 = malloc(sizeof(icclient_response)); + response->data = malloc(1); + response->userData = callback; + response->numBytes = 0; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); + struct curl_httppost *post, *last = NULL; + if (body) { + for (size_t i = 0; i < body->num_pairs; i++) { + struct pair pair = body->pairs[i]; + if (!pair.value) + continue; + curl_formadd(&post, &last, + CURLFORM_COPYNAME, pair.key, + CURLFORM_PTRCONTENTS, pair.value, + CURLFORM_END); + } + last = NULL; + curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); + } else + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + CURLcode res = curl_easy_perform(curl); + if (post) + curl_formfree(post); + if (res == CURLE_OK && handler) + handler(response); +#ifdef DEBUG + else { + const char *error = curl_easy_strerror(res); +#ifdef __ANDROID__ + __android_log_print(ANDROID_LOG_ERROR, "libicclient.so", "%s", error); +#else + fprintf(stderr, "%s\n", error); +#endif + } +#endif + curl_easy_cleanup(curl); +#endif +} @@ -1,17 +1,7 @@ -#ifndef ICCLIENT_REQUEST_H -#define ICCLIENT_REQUEST_H +#ifndef REQUEST_H +#define REQUEST_H -#if defined __ANDROID__ && defined DEBUG -#include <android/log.h> -#endif -#include <stdio.h> -#include <stdlib.h> -#include <stdarg.h> -#include <string.h> -#ifndef __EMSCRIPTEN__ -#include <threads.h> -#include <curl/curl.h> -#endif +#include <stddef.h> #include "icclient/typedefs.h" struct body { @@ -22,147 +12,6 @@ struct body { } pairs[16]; }; -#ifndef __EMSCRIPTEN__ -extern char *sampleurl; -extern char *certificate; -size_t append(char *, size_t, size_t, icclient_response *); -#endif - -static inline void request(void (*handler)(icclient_response *), void (*callback)(void *), struct body *body, char *fmt, ...) -{ - va_list ap; - char *p, *sval; - unsigned int ival; - size_t length = -#ifndef __EMSCRIPTEN__ - strlen(sampleurl) + -#endif - strlen(fmt); - - va_start(ap, fmt); - for (p = fmt; *p; p++) { - if (*p != '%') - continue; - switch(*++p) { - case 's': - sval = va_arg(ap, char *); - length += strlen(sval) - 2; - break; - case 'd': - ival = va_arg(ap, unsigned int); - do { - length++; - } while ((ival /= 10)); - length -= 2; - break; - default: - break; - } - } - va_end(ap); - - char url[length + 1]; -#ifdef __EMSCRIPTEN__ - memset(url, '\0', length + 1); -#else - strcpy(url, sampleurl); -#endif - - va_start(ap, fmt); - for (p = fmt; *p; p++) { - if (*p != '%') - continue; - switch(*++p) { - case 's': - sval = va_arg(ap, char *); - strcat(url, sval); - break; - case 'd': - ival = va_arg(ap, unsigned int); - sprintf(url, "%s%d", url, ival); - break; - default: - break; - } - } - 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) { - size_t length = 0; - char *post = malloc(1); - memset(post, '\0', 1); - for (size_t i = 0; i < body->num_pairs; i++) { - struct pair pair = body->pairs[i]; - if (!pair.value) - continue; - length += strlen(pair.key) + strlen(pair.value) + (i ? 1 : 0) + 1; - post = realloc(post, length + 1); - if (i) - strcat(post, "&"); - sprintf(post, "%s%s=%s", post, pair.key, pair.value); - } - strcpy(attr.requestMethod, "POST"); - const char *headers[] = { "Content-Type", "application/x-www-form-urlencoded", NULL }; - attr.requestHeaders = headers; - attr.requestData = post; - attr.requestDataSize = length + 1; - attr.userData = post; - } else { - strcpy(attr.requestMethod, "GET"); - attr.userData = 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 }; - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); - struct curl_httppost *post, *last = NULL; - if (body) { - for (size_t i = 0; i < body->num_pairs; i++) { - struct pair pair = body->pairs[i]; - if (!pair.value) - continue; - curl_formadd(&post, &last, - CURLFORM_COPYNAME, pair.key, - CURLFORM_PTRCONTENTS, pair.value, - CURLFORM_END); - } - last = NULL; - curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); - } else - curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); - CURLcode res = curl_easy_perform(curl); - if (post) - curl_formfree(post); - if (res == CURLE_OK && handler) - handler(&response); -#ifdef DEBUG - else { - const char *error = curl_easy_strerror(res); -#ifdef __ANDROID__ - __android_log_print(ANDROID_LOG_ERROR, "libicclient.so", "%s", error); -#else - fprintf(stderr, "%s\n", error); -#endif - } -#endif - curl_easy_cleanup(curl); -#endif -} +void request(void (*)(icclient_response *), void (*)(void *), struct body *, char *, ...); #endif |