aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Kellermann2018-05-14 20:33:59 -0400
committerAntony Kellermann2018-05-14 20:33:59 -0400
commitc2d3b996408b785d180ce6e8daf30573bce70b7c (patch)
tree2ffba87e2b75b2f1d777fe70b37024b74b8ddc4b
parent1d63a515a541a56a97beb8a7ab11e734f06d048a (diff)
downloadaur-c2d3b996408b785d180ce6e8daf30573bce70b7c.tar.gz
Moved curl init to main for multithread safety
-rw-r--r--api.c44
-rw-r--r--main.c4
2 files changed, 25 insertions, 23 deletions
diff --git a/api.c b/api.c
index d1facefc55b1..e454af5bc588 100644
--- a/api.c
+++ b/api.c
@@ -32,30 +32,28 @@ String* api_curl_data(const char* url, const char* post_field) {
String* pString = string_init();
CURL* curl = curl_easy_init();
CURLcode res;
- curl_global_init(CURL_GLOBAL_DEFAULT);
- if (curl) {
- curl_easy_setopt(curl, CURLOPT_URL, url); // Set URL
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Needed for HTTPS
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, api_string_writefunc); // Specify writefunc for return data
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &pString->data); // Specify object for return data
- struct curl_slist* list = NULL;
- if (url[12] == 'g') { //if using Google Urlshortener, a post field is needed
- list = curl_slist_append(list, "Content-Type: application/json");
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_field);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(post_field));
- }
- res = curl_easy_perform(curl);
- if (url[12] == 'g')
- curl_slist_free_all(list);
- curl_easy_cleanup(curl);
- if (res != CURLE_OK) {
- puts("Error curling data.");
- string_destroy(&pString);// Free and return NULL
- }
- } else {
- puts("Error initializing curl.");
+ if (!curl) { // Error creating curl object
+ string_destroy(&pString);
+ RETNULL_MSG("Error initializing curl.")
+ }
+ curl_easy_setopt(curl, CURLOPT_URL, url); // Set URL
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Needed for HTTPS
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, api_string_writefunc); // Specify writefunc for return data
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &pString->data); // Specify object for return data
+ struct curl_slist* list = NULL;
+ if (url[12] == 'g') { //if using Google Urlshortener, a post field is needed
+ list = curl_slist_append(list, "Content-Type: application/json");
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_field);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(post_field));
+ }
+ res = curl_easy_perform(curl);
+ if (url[12] == 'g')
+ curl_slist_free_all(list);
+ curl_easy_cleanup(curl);
+ if (res != CURLE_OK) {
string_destroy(&pString);// Free and return NULL
+ RETNULL_MSG("Error curling data.")
}
return pString;
}
diff --git a/main.c b/main.c
index c3290c02cfed..3e7248cc668b 100644
--- a/main.c
+++ b/main.c
@@ -21,6 +21,9 @@ int main(int argc, char* argv[]) {
// Init portfolio path
portfolio_file_init();
+ // Init cURL
+ curl_global_init(CURL_GLOBAL_ALL);
+
// Portfolio modify operation
int modop = -1;
@@ -108,5 +111,6 @@ int main(int argc, char* argv[]) {
}
free(portfolio_file);
free(sym);
+ curl_global_cleanup();
return 0;
} \ No newline at end of file