summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org>2022-10-05 15:49:04 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <prabowo@darapsa.org>2022-10-05 15:49:04 +0800
commit62f61ebe8a9dd3ea8c00fc7f8da9ad426dca0d56 (patch)
tree9140d7c3d23472e54dbc720fe2c699f4bcf1c873
parente9d1a5191d444fa3d0f3d72643d7f0d61a6aa190 (diff)
Reorder & complete the cleanup
-rw-r--r--midtrans.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/midtrans.c b/midtrans.c
index 5005581..c51ce5b 100644
--- a/midtrans.c
+++ b/midtrans.c
@@ -11,6 +11,7 @@
static _Bool sandbox;
static char *base_url;
static CURL *curl;
+static struct curl_slist *slist;
void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
{
@@ -20,14 +21,17 @@ void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
base_url = malloc(strlen(url_tmpl) - strlen ("%s")
+ sandbox * strlen(infix) + 1);
sprintf(base_url, url_tmpl, production ? "" : infix);
+
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
+
static const char *basic_tmpl = "%s:";
const size_t basic_len = strlen(basic_tmpl) - strlen("%s")
+ strlen(api_key);
char basic[basic_len + 1];
sprintf(basic, basic_tmpl, api_key);
+
BIO *b64 = BIO_new(BIO_f_base64());
BIO *mem = BIO_new(BIO_s_mem());
BIO_push(b64, mem);
@@ -36,14 +40,16 @@ void midtrans_init(_Bool production, const char *api_key, const char *cainfo)
long base64_len = BIO_get_mem_data(b64, &pp);
char base64[base64_len + 1];
strlcpy(base64, pp, base64_len + 1);
+ BIO_free_all(b64);
+
static const char *hdr_tmpl = "Authorization: Basic %s";
char auth[strlen(hdr_tmpl) - strlen("%s") + base64_len + 1];
sprintf(auth, hdr_tmpl, base64);
- BIO_free_all(b64);
- struct curl_slist *slist = curl_slist_append(NULL, auth);
+ slist = curl_slist_append(NULL, auth);
slist = curl_slist_append(slist, "Accept: application/json");
slist = curl_slist_append(slist, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
+
if (cainfo)
curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo);
}
@@ -61,6 +67,7 @@ void midtrans_status(const char *order_id)
void midtrans_cleanup()
{
free(base_url);
+ curl_slist_free_all(slist);
curl_easy_cleanup(curl);
curl_global_cleanup();
}