summaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-06-18 16:51:16 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2018-07-10 23:41:15 +0200
commit2f2beb8029e3d7836eac748a667c4f0c4f61ee02 (patch)
treec8591f35e0589d9e8582641327133b91e3af4bf4 /src/plugins
parentfe866cb7d63dd26ef271157430f9a7b3635ce40d (diff)
Escape special characters according to influxDB doc
Change-Id: I22724b547033b40b3160c3bbe1b2141349a1ead2 Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/influxdb-writer.c7
-rw-r--r--src/plugins/influxdb.h53
2 files changed, 44 insertions, 16 deletions
diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c
index dca5dab..c1f8abd 100644
--- a/src/plugins/influxdb-writer.c
+++ b/src/plugins/influxdb-writer.c
@@ -76,9 +76,9 @@ static size_t format_write_args(char* query, struct series_t* serie)
else
concatenate(query, it->key, ",");
if (json_object_is_type(it->value, json_type_string))
- concatenate(query, json_object_get_string(it->value), "=");
+ concatenate_str(query, json_object_get_string(it->value), "=");
else
- concatenate(query, json_object_to_json_string(it->value), "=");
+ concatenate_str(query, json_object_to_json_string(it->value), "=");
i++;
}
}
@@ -165,8 +165,7 @@ CTLP_CAPI(write_to_influxdb, source, argsJ, eventJ)
if (wrap_json_unpack(req_args, "{s?s,s?o,so!}",
"host", &host,
"port", &portJ,
- "metric", &metric)
- || !metric)
+ "metric", &metric) || !metric)
AFB_ReqFail(request, "Failed", "Error processing arguments. Miss metric\
JSON object or malformed");
else
diff --git a/src/plugins/influxdb.h b/src/plugins/influxdb.h
index 88ea2bc..8fb734d 100644
--- a/src/plugins/influxdb.h
+++ b/src/plugins/influxdb.h
@@ -20,6 +20,7 @@
#define _GNU_SOURCE
#include <string.h>
+#include <stdbool.h>
#include "../utils/list.h"
#include "ctl-plugin.h"
@@ -41,20 +42,28 @@ int create_database(AFB_ReqT request);
int unpack_metric_from_api(json_object* m, struct series_t* serie);
-static inline int should_escape(char c)
+static inline int should_escape(char c, bool quoteOnly)
{
- switch (c) {
- case ',':
- case '=':
- case ' ':
- case '"':
- return 1;
- break;
+ if (quoteOnly) {
+ return (c == '"');
+ } else {
+ switch (c) {
+ case ',':
+ case '=':
+ case ' ':
+ case '"':
+ return 1;
+ break;
+ }
}
return 0;
}
-static inline char* escape_chr(const char* src)
+/*
+ Escape special characters according to influxDB doc
+ https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_reference/#special-characters
+*/
+static inline char* escape_chr(const char* src, bool quoteOnly)
{
int j, i = 0;
size_t len, src_len;
@@ -62,7 +71,7 @@ static inline char* escape_chr(const char* src)
len = src_len = strlen(src);
while (i < src_len) {
- if (should_escape(src[i++]))
+ if (should_escape(src[i++], quoteOnly))
len++;
}
@@ -73,7 +82,11 @@ static inline char* escape_chr(const char* src)
if (res) {
i = j = 0;
while (i < src_len) {
- if (should_escape(src[i]))
+ if (src[i] == '\\') {
+ i++;
+ continue;
+ }
+ if (should_escape(src[i], quoteOnly))
res[j++] = '\\';
res[j++] = src[i++];
}
@@ -90,7 +103,7 @@ static inline void concatenate(char* dest, const char* source, const char* sep)
if (sep)
strncat(dest, sep, strlen(sep));
- esc_source = escape_chr(source);
+ esc_source = escape_chr(source, FALSE);
strncat(dest, esc_source, strlen(esc_source));
@@ -98,6 +111,22 @@ static inline void concatenate(char* dest, const char* source, const char* sep)
free(esc_source);
}
+static inline void concatenate_str(char* dest, const char* source, const char* sep)
+{
+ char* esc_source;
+
+ if (sep)
+ strncat(dest, sep, strlen(sep));
+ strncat(dest, "\"", 1);
+
+ esc_source = escape_chr(source, TRUE);
+ strncat(dest, esc_source, strlen(esc_source));
+ if (esc_source)
+ free(esc_source);
+
+ strncat(dest, "\"", 1);
+}
+
size_t make_url(char* url, size_t l_url, const char* host, const char* port, const char* endpoint);
#endif