From 4c68c15d4d3102b26b824b0021901d1dd9d79e77 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 10 Mar 2016 17:42:09 +0100 Subject: dbfp: move curl handle to dbfp As the curl handle can be reused between requests, it is moved to the dbfp structure, initialized in dbfp_init and freed in dbfp_close. --- dbfp.c | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) (limited to 'dbfp.c') diff --git a/dbfp.c b/dbfp.c index 9f2dd02..1c226e5 100644 --- a/dbfp.c +++ b/dbfp.c @@ -6,7 +6,6 @@ #include "dbfp.h" -#include #include #include #include @@ -44,16 +43,37 @@ static void location_end(void *data, const char *name); int dbfp_init(struct dbfp *dbfp, char *key) { + int err = 0; + char *keydup = NULL; + CURL *curl = NULL; + if (!dbfp || !key) return EINVAL; - dbfp->key = strdup(key); - if (!dbfp->key) - return ENOMEM; + keydup = strdup(key); + if (!keydup) { + err = ENOMEM; + goto cleanup; + } curl_global_init(CURL_GLOBAL_ALL ^ CURL_GLOBAL_SSL); + curl = curl_easy_init(); + if (!curl) { + err = DBFP_ERROR_CURL; + goto cleanup; + } - return 0; + dbfp->key = keydup; + dbfp->curl = curl; + +cleanup: + if (err) { + free(keydup); + if (curl) + curl_easy_cleanup(curl); + } + + return err; } void dbfp_close(struct dbfp *dbfp) @@ -61,9 +81,14 @@ void dbfp_close(struct dbfp *dbfp) if (!dbfp) return; + if (dbfp->curl) { + curl_easy_cleanup(dbfp->curl); + dbfp->curl = NULL; + } curl_global_cleanup(); free(dbfp->key); + dbfp->key = NULL; } int dbfp_query_location_name(struct dbfp *dbfp, char *input, @@ -136,28 +161,19 @@ static int dbfp_request(struct dbfp *dbfp, char *service, char *query, { int err = 0; char *url = NULL; - CURL *curl = NULL; err = dbfp_url(dbfp, service, query, &url); if (err) goto cleanup; - curl = curl_easy_init(); - if (!curl) { - err = DBFP_ERROR_CURL; - goto cleanup; - } - - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dbfp_parse); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)parser); + curl_easy_setopt(dbfp->curl, CURLOPT_URL, url); + curl_easy_setopt(dbfp->curl, CURLOPT_WRITEFUNCTION, dbfp_parse); + curl_easy_setopt(dbfp->curl, CURLOPT_WRITEDATA, (void *)parser); - if (curl_easy_perform(curl) != CURLE_OK) + if (curl_easy_perform(dbfp->curl) != CURLE_OK) err = DBFP_ERROR_CURL; cleanup: - if (curl) - curl_easy_cleanup(curl); free(url); return err; -- cgit v1.2.3