From 2d3fffa502e808452848b7396b4e121917ddc787 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 10 Mar 2016 17:48:52 +0100 Subject: dbfp: URL-encode the input for the location query In dbfp_query_location_name, URL-encode the input string using curl before sending the HTTP request. This fixes the issue that input strings with non-URL characters leed to empty results. --- dbfp.c | 22 ++++++++++++++-------- dbfp_check.c | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/dbfp.c b/dbfp.c index 1c226e5..b36a17c 100644 --- a/dbfp.c +++ b/dbfp.c @@ -94,7 +94,8 @@ void dbfp_close(struct dbfp *dbfp) int dbfp_query_location_name(struct dbfp *dbfp, char *input, size_t *n, struct dbfp_location **locations) { - int err; + int err = 0; + char *input_enc = NULL; char *query = NULL; XML_Parser parser = NULL; int len; @@ -107,14 +108,17 @@ int dbfp_query_location_name(struct dbfp *dbfp, char *input, *n = 0; *locations = NULL; - /* - * TODO(robin.krahl): improve query building and implement HTML - * encoding. - */ + input_enc = curl_easy_escape(dbfp->curl, input, 0); + if (!input_enc) { + err = DBFP_ERROR_CURL; + goto cleanup; + } query = malloc(DBFP_URL_LEN); - if (!query) - return ENOMEM; - len = snprintf(query, DBFP_URL_LEN, "input=%s", input); + if (!query) { + err = ENOMEM; + goto cleanup; + } + len = snprintf(query, DBFP_URL_LEN, "input=%s", input_enc); if (len < 0 || len >= DBFP_URL_LEN) { err = DBFP_ERROR_FORMAT; goto cleanup; @@ -136,6 +140,8 @@ int dbfp_query_location_name(struct dbfp *dbfp, char *input, cleanup: if (err) free(ld.locations); + if (input_enc) + curl_free(input_enc); XML_ParserFree(parser); free(query); diff --git a/dbfp_check.c b/dbfp_check.c index a9da26c..29db4a3 100644 --- a/dbfp_check.c +++ b/dbfp_check.c @@ -89,6 +89,24 @@ START_TEST(test_dbfp_location_simple) free(locations); locations = NULL; + err = dbfp_query_location_name(&dbfp, "freiburg im", &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 -- cgit v1.2.3