diff options
Diffstat (limited to 'dbfp.c')
-rw-r--r-- | dbfp.c | 52 |
1 files changed, 34 insertions, 18 deletions
@@ -6,7 +6,6 @@ #include "dbfp.h" -#include <curl/curl.h> #include <errno.h> #include <expat.h> #include <stdio.h> @@ -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; |