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 ++++++++++++++++++++++++++++++++++------------------ dbfp.h | 2 ++ dbfp_check.c | 20 ++++++++++++++++++++ man/dbfp.3 | 16 ++++++++++++---- 4 files changed, 68 insertions(+), 22 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 #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; diff --git a/dbfp.h b/dbfp.h index b2537ad..7871372 100644 --- a/dbfp.h +++ b/dbfp.h @@ -7,6 +7,7 @@ #ifndef DBFP_H_ #define DBFP_H_ +#include #include enum dbfp_error { @@ -19,6 +20,7 @@ enum dbfp_error { struct dbfp { char *key; + CURL *curl; }; struct dbfp_location { diff --git a/dbfp_check.c b/dbfp_check.c index 5e8e225..a9da26c 100644 --- a/dbfp_check.c +++ b/dbfp_check.c @@ -69,6 +69,26 @@ START_TEST(test_dbfp_location_simple) free(locations[i].id); } free(locations); + locations = NULL; + + err = dbfp_query_location_name(&dbfp, "freib", &n, &locations); + ck_assert_int_eq(err, 0); + + ck_assert_int_gt(n, 0); + ck_assert_ptr_ne(locations, NULL); + + ck_assert_str_eq(locations[0].name, "Freiburg(Breisgau) Hbf"); + ck_assert_str_eq(locations[0].id, "008000107"); + ck_assert(locations[0].lon == 7.841173f); + ck_assert(locations[0].lat == 47.997696f); + + for (i = 0; i < n; i++) { + free(locations[i].name); + free(locations[i].id); + } + free(locations); + locations = NULL; + dbfp_close(&dbfp); } END_TEST diff --git a/man/dbfp.3 b/man/dbfp.3 index e423f38..6e9cc2a 100644 --- a/man/dbfp.3 +++ b/man/dbfp.3 @@ -8,6 +8,8 @@ dbfp, dbfp_init, dbfp_close \- handle metadata for connections to the DB timetab .B struct dbfp { .RS .B char *key; +.br +.B CURL *curl; .RE .B }; .HP @@ -19,12 +21,14 @@ dbfp, dbfp_init, dbfp_close \- handle metadata for connections to the DB timetab stores the metadata for connections to the DB timetable API. .B key is the API key used for API queries. +.B curl +is the curl handle used for the API requests. .PP .B dbfp_init -initializes a dbfp structure and sets the API key to the given value. dbfp and -key may not be NULL. dbfp must already be allocated. The key is copied to the -dbfp structure. Every structure that was initialized with this function must -be closed with dbfp_close. +initializes a dbfp structure with the given API key. dbfp and key may not be +NULL. dbfp must already be allocated. The key is copied to the dbfp structure. +Every structure that was initialized with this function must be closed with +dbfp_close. .PP .B dbfp_close closes a dbfp structure and releases its allocated resources. dbfp itself is @@ -43,6 +47,10 @@ ENOMEM .RS if there is not enough memory to copy the key in dbfp_init .RE +DBFP_ERROR_CURL +.RS +if the curl handle could not be created +.RE .SH EXAMPLE #include .br -- cgit v1.2.3