aboutsummaryrefslogtreecommitdiffstats
path: root/src
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
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')
-rw-r--r--src/plugins/influxdb-reader.c2
-rw-r--r--src/plugins/influxdb-writer.c2
-rw-r--r--src/plugins/influxdb.c51
-rw-r--r--src/plugins/influxdb.h4
4 files changed, 47 insertions, 12 deletions
diff --git a/src/plugins/influxdb-reader.c b/src/plugins/influxdb-reader.c
index 98e5198..b6c9c25 100644
--- a/src/plugins/influxdb-reader.c
+++ b/src/plugins/influxdb-reader.c
@@ -225,7 +225,7 @@ static int influxdb_read(sd_event_source *s, uint64_t usec, void *userdata)
char url[URL_MAXIMUM_LENGTH]; /* Safe limit for most popular web browser */
- make_url(url, sizeof(url), r_args->host, r_args->port, "query");
+ make_url_db(url, sizeof(url), r_args->host, r_args->port, "query", DEFAULT_DB);
curl = make_curl_query_get(source->api, url);
curl_wrap_do(curl, influxdb_read_curl_cb, (void*)source->api);
diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c
index ddf8fc6..c114ab5 100644
--- a/src/plugins/influxdb-writer.c
+++ b/src/plugins/influxdb-writer.c
@@ -152,7 +152,7 @@ CURL* make_curl_write_post(afb_api_t apiHandle, const char* url, json_object* me
CURL* influxdb_write(afb_api_t apiHandle, const char* host, const char* port, json_object* metricJ)
{
char url[URL_MAXIMUM_LENGTH]; /* Safe limit for most popular web browser */
- make_url(url, sizeof(url), host, port, "write");
+ make_url_db(url, sizeof(url), host, port, "write", DEFAULT_DB);
return make_curl_write_post(apiHandle, url, metricJ);
}
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) {
diff --git a/src/plugins/influxdb.h b/src/plugins/influxdb.h
index 781ab29..33eb0a2 100644
--- a/src/plugins/influxdb.h
+++ b/src/plugins/influxdb.h
@@ -127,6 +127,8 @@ static inline void concatenate_str(char* dest, const char* source, const char* s
strncat(dest, "\"", 1);
}
-size_t make_url(char* url, size_t l_url, const char* host, const char* port, const char* endpoint);
+extern size_t make_url_db(char* url, size_t l_url, const char* host, const char* port, const char* endpoint, const char *db);
+extern size_t make_url(char* url, size_t l_url, const char* host, const char* port, const char* endpoint);
+extern char* get_url(const char* host, const char* port, const char* endpoint);
#endif