summaryrefslogtreecommitdiffstats
path: root/src/plugins/influxdb.c
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2019-07-09 00:17:39 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2019-08-01 17:46:22 +0200
commit903b0e9a002e0839943c5d734f3728114272ebc1 (patch)
tree428ed08e21cf0469bccc36c7b98204112b2c0124 /src/plugins/influxdb.c
parent6c36102bf3964808f6f2db4161f79cf458880a5a (diff)
Rework how to build dest DB URL
Change-Id: I5a351d6e2f5750dcb39bd9feff71abe930c052bb Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'src/plugins/influxdb.c')
-rw-r--r--src/plugins/influxdb.c51
1 files changed, 42 insertions, 9 deletions
diff --git a/src/plugins/influxdb.c b/src/plugins/influxdb.c
index 5cc09b0..a3b61c7 100644
--- a/src/plugins/influxdb.c
+++ b/src/plugins/influxdb.c
@@ -32,7 +32,7 @@ CTLP_ONLOAD(plugin, ret)
int err = 0;
char* result;
size_t result_size;
- CURL* request = curl_wrap_prepare_get(DEFAULT_DBHOST ":" DEFAULT_DBPORT "/ping", NULL, NULL);
+ CURL* request = curl_wrap_prepare_get(get_url(NULL, NULL, "/ping"), NULL, NULL);
struct reader_args r_args = { NULL, NULL, 1000000 };
plugin->context = (void*)&r_args;
@@ -40,7 +40,7 @@ CTLP_ONLOAD(plugin, ret)
curl_wrap_perform(request, &result, &result_size);
if (curl_wrap_response_code_get(request) != 204) {
- AFB_API_ERROR(plugin->api, "InfluxDB not reachable, please start it");
+ AFB_API_ERROR(plugin->api, "InfluxDB not reachable on '%s', please start it", get_url(NULL, NULL, "/ping"));
err = ERROR;
}
@@ -54,7 +54,7 @@ CTLP_CAPI(influxdb_ping, source, argsJ, eventJ)
char* result;
size_t result_size;
- CURL* curl_req = curl_wrap_prepare_get(DEFAULT_DBHOST ":" DEFAULT_DBPORT "/ping", NULL, NULL);
+ CURL* curl_req = curl_wrap_prepare_get(get_url(NULL, NULL, "/ping"), NULL, NULL);
curl_wrap_perform(curl_req, &result, &result_size);
@@ -78,16 +78,49 @@ size_t make_url(char* url, size_t l_url, const char* host, const char* port, con
host = host ? host : DEFAULT_DBHOST;
port = port ? port : DEFAULT_DBPORT;
- strncat(url, host, strlen(host));
+ strncat(url, host, l_url - strlen(url) - 1);
strcat(url, ":");
- strncat(url, port, strlen(port));
- strcat(url, "/");
- strncat(url, endpoint, strlen(endpoint));
- strcat(url, "?db=" DEFAULT_DB);
+ strncat(url, port, l_url - strlen(url) - 1);
+ if (endpoint[0] != '/') {
+ strncat(url, "/", l_url - strlen(url) - 1);
+ }
+ strncat(url, endpoint, l_url - strlen(url) - 1);
return strlen(url);
}
+size_t make_url_db(char* url, size_t l_url, const char* host, const char* port, const char* endpoint, const char* db)
+{
+ size_t sz = make_url(url, l_url, host, port, endpoint);
+
+ strncat(url, "?db=", l_url - strlen(url) - 1);
+ strncat(url, db ? db : DEFAULT_DB, l_url - strlen(url) - 1);
+
+ return sz + strlen(db);
+}
+
+char* get_url(const char* host, const char* port, const char* endpoint)
+{
+ static char* url = NULL;
+ static size_t urlSz = 0;
+
+ host = host ? host : DEFAULT_DBHOST;
+ port = port ? port : DEFAULT_DBPORT;
+
+ size_t newSz = strlen(host) + 1 + strlen(port) + 1 + strlen(endpoint) + 1;
+ if (urlSz < newSz) {
+ url = (char*)realloc(url, newSz);
+ if (!url) {
+ return "ERROR, cannot realloc url in get_url";
+ }
+ urlSz = newSz;
+ }
+
+ make_url(url, urlSz, host, port, endpoint);
+
+ return url;
+}
+
int create_database(afb_req_t request)
{
int ret = 0;
@@ -99,7 +132,7 @@ int create_database(afb_req_t request)
post_data[0] = "q=CREATE DATABASE \"" DEFAULT_DB "\"";
post_data[1] = NULL;
- CURL* curl_req = curl_wrap_prepare_post_unescaped(DEFAULT_DBHOST ":" DEFAULT_DBPORT "/query", NULL, " ", post_data);
+ CURL* curl_req = curl_wrap_prepare_post_unescaped(get_url(NULL, NULL, "/query"), NULL, " ", post_data);
curl_wrap_perform(curl_req, &result, &result_size);
if (curl_wrap_response_code_get(request) != 200) {