aboutsummaryrefslogtreecommitdiff
path: root/dbfp.c
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2016-03-10 17:42:09 +0100
committerRobin Krahl <me@robin-krahl.de>2016-03-10 17:42:09 +0100
commit4c68c15d4d3102b26b824b0021901d1dd9d79e77 (patch)
tree47688168607b1c25002ab45eeb197a3bfaa21567 /dbfp.c
parentde9f0aa8f8d248292815e5571e1fa3fb068de8c3 (diff)
downloaddbfp-4c68c15d4d3102b26b824b0021901d1dd9d79e77.tar.gz
dbfp-4c68c15d4d3102b26b824b0021901d1dd9d79e77.tar.bz2
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.
Diffstat (limited to 'dbfp.c')
-rw-r--r--dbfp.c52
1 files changed, 34 insertions, 18 deletions
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 <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;