diff options
author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-12 14:44:30 +0800 |
---|---|---|
committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2021-06-12 14:44:30 +0800 |
commit | 3947ac9a60018f395558731027e26db8953090bb (patch) | |
tree | 0f77a63d06127c43a5fcddd3cad963d4a0f2330d | |
parent | a7f4dba7c9c0738545c8b41b4185bd5ec9feb099 (diff) |
Move all libcurl related stuff to request
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | admin.c | 108 | ||||
-rw-r--r-- | client.c | 37 | ||||
-rw-r--r-- | login.h | 48 | ||||
-rw-r--r-- | main.c | 104 | ||||
-rw-r--r-- | member.c | 118 | ||||
-rw-r--r-- | ord.c | 87 | ||||
-rw-r--r-- | request.c | 11 | ||||
-rw-r--r-- | request.h | 68 |
9 files changed, 178 insertions, 405 deletions
diff --git a/Makefile.am b/Makefile.am index b43a6f7..7f218cf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -36,7 +36,7 @@ endif #if !WASM #bin_PROGRAMS = icclient -#icclient_SOURCES = main.c client.c catalog.c product.c request.c +#icclient_SOURCES = main.c client.c catalog.c product.c request.c login.c member.c #icclient_CPPFLAGS = -I${prefix}/include $(DEPS_CFLAGS) #icclient_LDFLAGS = $(DEPS_LIBS) -lcurl #endif @@ -13,92 +13,36 @@ icclient_admin *icclient_admin_login(const char *username, const char *password, admin->name = NULL; admin->username = NULL; admin->super = false; - login(username, password, NULL, "MMLogin", successpage, nextpage, failpage, - handler, admin); + login(username, password, NULL, "MMLogin", successpage, nextpage, failpage, handler, admin); return admin; } -void icclient_admin_newitem(const char *description, const char *comment, - const char *price, const char *image_path) +void icclient_admin_newitem(const char *description, const char *comment, const char *price, const char *image_path) { - struct curl_httppost *post, *last = NULL; - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_click", - CURLFORM_COPYCONTENTS, "process_filter", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_data_fields", - CURLFORM_COPYCONTENTS, "sku description prod_group category comment inactive price wholesale image thumb image_large weight nontaxable gift_cert", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_ui", - CURLFORM_COPYCONTENTS, "1", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "ui_new_item", - CURLFORM_COPYCONTENTS, "1", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_todo", - CURLFORM_COPYCONTENTS, "set", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_update_empty", - CURLFORM_COPYCONTENTS, "1", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_action", - CURLFORM_COPYCONTENTS, "set", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_data_table", - CURLFORM_COPYCONTENTS, "products", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_data_function", - CURLFORM_COPYCONTENTS, "insert", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_data_key", - CURLFORM_COPYCONTENTS, "sku", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_return_table", - CURLFORM_COPYCONTENTS, "products", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "sku", - CURLFORM_PTRCONTENTS, image_path, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "description", - CURLFORM_PTRCONTENTS, description, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "comment", - CURLFORM_PTRCONTENTS, comment, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "price", - CURLFORM_PTRCONTENTS, price, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "image", - CURLFORM_FILE, image_path, - CURLFORM_CONTENTTYPE, "image/jpeg", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_data_file_field", - CURLFORM_COPYCONTENTS, "image", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_data_file_path", - CURLFORM_COPYCONTENTS, "images/items", - CURLFORM_END); - last = NULL; - request(NULL, NULL, post, "%s", "admin/item_edit"); - curl_formfree(post); - post = NULL; + request(NULL, NULL, &(struct icclient_request_data){ 15, { + { "mv_click", "process_filter" }, + { "mv_data_fields", "sku description prod_group category comment inactive price wholesale image thumb image_large weight nontaxable gift_cert" }, + { "mv_ui", "1" }, + { "ui_new_item", "1" }, + { "mv_todo", "set" }, + { "mv_update_empty", "1" }, + { "mv_action", "set" }, + { "mv_data_table", "products" }, + { "mv_data_function", "insert" }, + { "mv_data_key", "sku" }, + { "mv_return_table", "products" }, + { "sku", image_path }, + { "description", description }, + { "comment", comment }, + { "price", price } + /* + { "image", + CURLFORM_FILE, image_path, + CURLFORM_CONTENTTYPE, "image/jpeg" }, + { "mv_data_file_field", "image" }, + { "mv_data_file_path", "images/items" } + */ + }}, "%s", "admin/item_edit"); } void icclient_admin_logout(icclient_admin *admin) @@ -1,5 +1,4 @@ #include <stdbool.h> -#include <stdlib.h> #include <string.h> #include "request.h" #include "icclient/product.h" @@ -7,41 +6,15 @@ #include "icclient/client.h" #ifdef __EMSCRIPTEN__ -emscripten_fetch_attr_t attr; extern void icclient_catalog_results(emscripten_fetch_t *); #else -CURL *curl = NULL; -char *server_url = NULL; extern size_t icclient_catalog_results(void *, size_t, size_t, void *); #endif bool icclient_init(const char *url, const char *certificate) { icclient_catalog_init(); -#ifdef __EMSCRIPTEN__ - emscripten_fetch_attr_init(&attr); - attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; - return true; -#else - curl_global_init(CURL_GLOBAL_SSL); - curl = curl_easy_init(); - if (curl) { - 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 - size_t length = strlen(url); - bool append = !(bool)(url[length - 1] == '/'); - server_url = malloc(length + (size_t)append + 1); - strcpy(server_url, url); - if (append) - strcat(server_url, "/"); - } - return (bool)curl; -#endif + return icclient_request_init(url, certificate); } void icclient_results(const char *prod_group, @@ -70,11 +43,5 @@ void icclient_page(const char *path, icclient_handler handler, void **dataptr) void icclient_cleanup() { -#ifndef __EMSCRIPTEN__ - if (curl) { - free(server_url); - curl_easy_cleanup(curl); - } - curl_global_cleanup(); -#endif + icclient_request_cleanup(); } @@ -6,43 +6,15 @@ inline void login(const char *username, const char *password, const char *verify, const char *click, const char *successpage, const char *nextpage, const char *failpage, icclient_handler handler, void *user) { - struct curl_httppost *post, *last = NULL; - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_username", - CURLFORM_PTRCONTENTS, username, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_password", - CURLFORM_PTRCONTENTS, password, - CURLFORM_END); - if (verify) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_verify", - CURLFORM_PTRCONTENTS, verify, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_click", - CURLFORM_PTRCONTENTS, click, - CURLFORM_END); - if (successpage) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_successpage", - CURLFORM_PTRCONTENTS, successpage, - CURLFORM_END); - if (nextpage) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_nextpage", - CURLFORM_PTRCONTENTS, nextpage, - CURLFORM_END); - if (failpage) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_failpage", - CURLFORM_PTRCONTENTS, failpage, - CURLFORM_END); - last = NULL; - request(handler, user, post, "%s", "process"); - curl_formfree(post); - post = NULL; + request(handler, user, &(struct icclient_request_data){ 7, { + { "mv_username", username }, + { "mv_password", password }, + { "mv_verify", verify }, + { "mv_click", click }, + { "mv_successpage", successpage }, + { "mv_nextpage", nextpage }, + { "mv_failpage", failpage } + }}, "%s", "process"); } -#endif // ICCLIENT_LOGIN_H +#endif @@ -2,15 +2,13 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> -#include <json.h> #include "icclient/typedefs.h" #include "icclient/product.h" #include "icclient/catalog.h" +#include "icclient/member.h" #include "icclient/client.h" -static json_tokener *tokener; - -static void callback(struct icclient_catalog *catalog) +static void print_catalog(struct icclient_catalog *catalog) { for (size_t i = 0; i < catalog->length; i++) { struct icclient_product *product = catalog->products[i]; @@ -30,98 +28,52 @@ static void callback(struct icclient_catalog *catalog) } } -static size_t handler_results(void *contents, size_t size, size_t nmemb, void *userData) +static size_t print_user(void *contents, size_t size, size_t nmemb, void *userData) { size_t realsize = size * nmemb; char data[realsize + 1]; memcpy(data, contents, realsize); data[realsize] = '\0'; - json_object *products = json_tokener_parse_ex(tokener, data, realsize); - enum json_tokener_error error = json_tokener_get_error(tokener); - if (!products) { - if (error == json_tokener_continue) - return realsize; - else { - json_tokener_reset(tokener); - return realsize; - } - } else if (!json_object_is_type(products, json_type_array) || error != json_tokener_success) - return realsize; - size_t length = json_object_array_length(products); - struct icclient_catalog_callback *catalog_callback = (struct icclient_catalog_callback *)userData; - struct icclient_catalog **catalogptr = catalog_callback->catalog; - *catalogptr = malloc(sizeof(struct icclient_catalog) + sizeof(struct icclient_product *[length])); - struct icclient_catalog *catalog = *catalogptr; - catalog->length = length; - for (size_t i = 0; i < length; i++) { - catalog->products[i] = malloc(sizeof(struct icclient_product)); - struct icclient_product *product = catalog->products[i]; - icclient_product_init(product); - json_object *object = json_object_array_get_idx(products, i); - struct json_object_iterator iterator = json_object_iter_begin(object); - struct json_object_iterator iterator_end = json_object_iter_end(object); - while (!json_object_iter_equal(&iterator, &iterator_end)) { - const char *key = json_object_iter_peek_name(&iterator); - json_object *val = json_object_iter_peek_value(&iterator); - if (!strcmp(key, "price")) - product->price = json_object_get_double(val); - else { - int len = json_object_get_string_len(val); - if (len) { - char *value = malloc(len + 1); - strcpy(value, json_object_get_string(val)); - if (!strcmp(key, "sku")) - product->sku = value; - else if (!strcmp(key, "thumb")) - product->thumb = value; - else if (!strcmp(key, "image")) - product->image = value; - else if (!strcmp(key, "description")) - product->description = value; - else if (!strcmp(key, "prod_group")) - product->prod_group = value; - } - } - json_object_iter_next(&iterator); - } - } - catalog_callback->callback(catalog); - free(catalog_callback); + printf("%s\n", data); return realsize; } int main(int argc, char *argv[]) { - tokener = json_tokener_new(); - /* - char *url_line = NULL, *name_line = NULL, *pass_line = NULL; - printf("URL: "); + char *url_line = NULL; + printf("\nURL: "); ssize_t url_nread = getline(&url_line, &(size_t){0}, stdin); - printf("Name: "); - ssize_t name_nread = getline(&name_line, &(size_t){0}, stdin); - printf("Pass: "); - ssize_t pass_nread = getline(&pass_line, &(size_t){0}, stdin); - char *url = malloc(--url_nread + 1), *name = malloc(--name_nread + 1), *pass = malloc(--pass_nread + 1); + char *url = malloc(--url_nread + 1); strncpy(url, url_line, url_nread); free(url_line); + + printf("\n"); + icclient_init(url, NULL); + free(url); + + struct icclient_catalog *catalog; + icclient_allproducts(print_catalog, &catalog, NULL); + icclient_catalog_free(catalog); + + char *name_line = NULL; + printf("\nName: "); + ssize_t name_nread = getline(&name_line, &(size_t){0}, stdin); + char *name = malloc(--name_nread + 1); strncpy(name, name_line, name_nread); free(name_line); + + char *pass_line = NULL; + printf("Pass: "); + ssize_t pass_nread = getline(&pass_line, &(size_t){0}, stdin); + char *pass = malloc(--pass_nread + 1); strncpy(pass, pass_line, pass_nread); free(pass_line); - */ - icclient_init(argv[1], NULL); - /* - free(url); + pass[pass_nread] = '\0'; - icclient_login(NULL, NULL, name, pass, NULL, NULL, NULL); + struct icclient_member *member = icclient_member_login(name, pass, NULL, NULL, NULL, print_user); free(name); free(pass); + //icclient_member_logout(member); - icclient_logout(); - */ - struct icclient_catalog *catalog; - icclient_allproducts(callback, &catalog, handler_results); - icclient_catalog_free(catalog); icclient_cleanup(); - json_tokener_free(tokener); } @@ -63,8 +63,7 @@ icclient_member *icclient_member_newaccount(const char *username, const char *pa const char *failpage, icclient_handler handler) { icclient_member *member = initialise(username, password); - login(username, password, verify, "NewAccount", successpage, nextpage, failpage, - handler, member); + login(username, password, verify, "NewAccount", successpage, nextpage, failpage, handler, member); return member; } @@ -72,8 +71,7 @@ icclient_member *icclient_member_login(const char *username, const char *passwor const char *successpage, const char *nextpage, const char *failpage, icclient_handler handler) { icclient_member *member = initialise(username, password); - login(username, password, NULL, "Login", successpage, nextpage, failpage, handler, - member); + login(username, password, NULL, "Login", successpage, nextpage, failpage, handler, member); return member; } @@ -81,97 +79,33 @@ void icclient_member_account(const char *fname, const char *lname, const char *a const char *address2, const char *city, const char *state, const char *zip, const char *email, const char *phone_day) { - struct curl_httppost *post, *last = NULL; - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_form_profile", - CURLFORM_COPYCONTENTS, "account_change", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_todo", - CURLFORM_COPYCONTENTS, "return", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_nextpage", - CURLFORM_COPYCONTENTS, "member/account", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_check", - CURLFORM_COPYCONTENTS, "Save_database", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "fname", - CURLFORM_PTRCONTENTS, fname, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "lname", - CURLFORM_PTRCONTENTS, lname, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "address1", - CURLFORM_PTRCONTENTS, address1, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "address2", - CURLFORM_PTRCONTENTS, address2, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "city", - CURLFORM_PTRCONTENTS, city, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "state", - CURLFORM_PTRCONTENTS, state, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "zip", - CURLFORM_PTRCONTENTS, zip, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "email", - CURLFORM_PTRCONTENTS, email, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "phone_day", - CURLFORM_PTRCONTENTS, phone_day, - CURLFORM_END); - last = NULL; - request(NULL, NULL, post, "%s", "process"); - curl_formfree(post); - post = NULL; + request(NULL, NULL, &(struct icclient_request_data){ 13, { + { "mv_form_profile", "account_change" }, + { "mv_todo", "return" }, + { "mv_nextpage", "member/account" }, + { "mv_check", "Save_database" }, + { "fname", fname }, + { "lname", lname }, + { "address1", address1 }, + { "address2", address2 }, + { "city", city }, + { "state", state }, + { "zip", zip }, + { "email", email }, + { "phone_day", phone_day } + }}, "%s", "process"); } -void icclient_member_changepassword(const char *password_old, const char *password, - const char *verify) +void icclient_member_changepassword(const char *password_old, const char *password, const char *verify) { - struct curl_httppost *post, *last = NULL; - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_action", - CURLFORM_COPYCONTENTS, "return", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_check", - CURLFORM_COPYCONTENTS, "Change_password", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_successpage", - CURLFORM_COPYCONTENTS, "member/service", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_password_old", - CURLFORM_PTRCONTENTS, password_old, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_password", - CURLFORM_PTRCONTENTS, password, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_verify", - CURLFORM_PTRCONTENTS, verify, - CURLFORM_END); - last = NULL; - request(NULL, NULL, post, "%s", "member/change_password"); - curl_formfree(post); - post = NULL; + request(NULL, NULL, &(struct icclient_request_data){ 6, { + { "mv_action", "return" }, + { "mv_check", "Change_password" }, + { "mv_successpage", "member/service" }, + { "mv_password_old", password_old }, + { "mv_password", password }, + { "mv_verify", verify } + }}, "%s", "member/change_password"); } void icclient_member_logout(icclient_member *member) @@ -87,77 +87,22 @@ void icclient_ord_order(const char *sku, const icclient_catalog *catalog, void icclient_ord_checkout(struct icclient_ord_order *order, struct icclient_member *member) { - struct curl_httppost *post, *last = NULL; - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_todo", - CURLFORM_COPYCONTENTS, "submit", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_action", - CURLFORM_COPYCONTENTS, "refresh", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_order_profile", - CURLFORM_PTRCONTENTS, order->profile, - CURLFORM_END); - if (member->fname) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "fname", - CURLFORM_PTRCONTENTS, member->fname, - CURLFORM_END); - if (member->lname) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "lname", - CURLFORM_PTRCONTENTS, member->lname, - CURLFORM_END); - if (member->address1) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "address1", - CURLFORM_PTRCONTENTS, member->address1, - CURLFORM_END); - if (member->address2) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "address2", - CURLFORM_PTRCONTENTS, member->address2, - CURLFORM_END); - if (member->city) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "city", - CURLFORM_PTRCONTENTS, member->city, - CURLFORM_END); - if (member->state) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "state", - CURLFORM_PTRCONTENTS, member->state, - CURLFORM_END); - if (member->zip) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "zip", - CURLFORM_PTRCONTENTS, member->zip, - CURLFORM_END); - if (member->email) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "email", - CURLFORM_PTRCONTENTS, member->email, - CURLFORM_END); - if (member->phone_day) - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "phone_day", - CURLFORM_PTRCONTENTS, member->phone_day, - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "mv_same_billing", - CURLFORM_COPYCONTENTS, - member->preferences->mv_same_billing? "1" : "0", - CURLFORM_END); - curl_formadd(&post, &last, - CURLFORM_COPYNAME, "email_copy", - CURLFORM_COPYCONTENTS, member->preferences->email_copy? "1" : "0", - CURLFORM_END); - last = NULL; - request(NULL, NULL, post, "%s", "ord/checkout"); - curl_formfree(post); - post = NULL; + request(NULL, NULL, &(struct icclient_request_data){ 14, { + { "mv_todo", "submit" }, + { "mv_action", "refresh" }, + { "mv_order_profile", order->profile }, + { "fname", member->fname }, + { "lname", member->lname }, + { "address1", member->address1 }, + { "address2", member->address2 }, + { "city", member->city }, + { "state", member->state }, + { "zip", member->zip }, + { "email", member->email }, + { "phone_day", member->phone_day }, + { "mv_same_billing", member->preferences->mv_same_billing? "1" : "0" }, + { "email_copy", member->preferences->email_copy? "1" : "0" } + }}, "%s", "ord/checkout"); } void icclient_ord_free(struct icclient_ord_order *order) @@ -1,9 +1,12 @@ #include "request.h" -extern inline void request(icclient_handler, void *, #ifdef __EMSCRIPTEN__ - int +emscripten_fetch_attr_t attr; #else - struct curl_httppost * +CURL *curl; +char *server_url; #endif - , char *, ...); + +extern inline bool icclient_request_init(const char *, const char *); +extern inline void request(icclient_handler, void *, struct icclient_request_data *, char *, ...); +extern inline void icclient_request_cleanup(); @@ -7,11 +7,21 @@ #include <string.h> #include <stdarg.h> #include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> #ifndef __EMSCRIPTEN__ #include <curl/curl.h> #endif #include "icclient/typedefs.h" +struct icclient_request_data { + size_t num_pairs; + struct icclient_request_pair { + const char *key; + const char *value; + } pairs[16]; +}; + #ifdef __EMSCRIPTEN__ extern emscripten_fetch_attr_t attr; #else @@ -19,13 +29,35 @@ extern CURL *curl; extern char *server_url; #endif -inline void request(icclient_handler writefunction, void *writedata, +inline bool icclient_request_init(const char *url, const char *certificate) +{ #ifdef __EMSCRIPTEN__ - int + emscripten_fetch_attr_init(&attr); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; + return true; #else - struct curl_httppost * + curl_global_init(CURL_GLOBAL_SSL); + curl = curl_easy_init(); + if (curl) { + 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 + size_t length = strlen(url); + bool append = !(bool)(url[length - 1] == '/'); + server_url = malloc(length + (size_t)append + 1); + strcpy(server_url, url); + if (append) + strcat(server_url, "/"); + } + return (bool)curl; #endif - post, char *fmt, ...) +} + +inline void request(icclient_handler writefunction, void *writedata, struct icclient_request_data *body, char *fmt, ...) { va_list ap; char *p, *sval; @@ -97,14 +129,27 @@ inline void request(icclient_handler writefunction, void *writedata, curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); else curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); - if (post) + struct curl_httppost *post, *last = NULL; + if (body) { + for (size_t i = 0; i < body->num_pairs; i++) { + struct icclient_request_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 + } else curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); #ifdef DEBUG CURLcode res = #endif curl_easy_perform(curl); + if (post) + curl_formfree(post); #ifdef DEBUG if (res != CURLE_OK) { const char *error = curl_easy_strerror(res); @@ -118,4 +163,15 @@ inline void request(icclient_handler writefunction, void *writedata, #endif } +inline void icclient_request_cleanup() +{ +#ifndef __EMSCRIPTEN__ + if (curl) { + free(server_url); + curl_easy_cleanup(curl); + } + curl_global_cleanup(); +#endif +} + #endif |