summaryrefslogtreecommitdiffstats
path: root/src/plugins/influxdb.h
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-06-01 15:08:52 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2018-07-10 23:41:15 +0200
commit9f65896316418d257670bc65e83aa87c5683579d (patch)
tree9bfbb971e39dc3d918743f8bd15519987965ddb6 /src/plugins/influxdb.h
parent7234b8adf3627338e9f34e2ce0f31b7e8754628d (diff)
Escaped special characters for measurement, tags and fields
See https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_reference/#special-characters for more info. Change-Id: Iad2f7d00b7d6c42dbea33df03a75353e9cac97a9 Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'src/plugins/influxdb.h')
-rw-r--r--src/plugins/influxdb.h75
1 files changed, 63 insertions, 12 deletions
diff --git a/src/plugins/influxdb.h b/src/plugins/influxdb.h
index 0f06dda..88ea2bc 100644
--- a/src/plugins/influxdb.h
+++ b/src/plugins/influxdb.h
@@ -21,32 +21,83 @@
#define _GNU_SOURCE
#include <string.h>
+#include "../utils/list.h"
#include "ctl-plugin.h"
-#include "wrap-json.h"
#include "tsdb.h"
-#include "../utils/list.h"
+#include "wrap-json.h"
struct serie_columns_t {
- struct list *tags;
- struct list *fields;
+ struct list* tags;
+ struct list* fields;
};
struct series_t {
- const char *name;
- struct serie_columns_t serie_columns;
- uint64_t timestamp;
+ const char* name;
+ struct serie_columns_t serie_columns;
+ uint64_t timestamp;
};
int create_database(AFB_ReqT request);
-int unpack_metric_from_api(json_object *m, struct series_t *serie);
+int unpack_metric_from_api(json_object* m, struct series_t* serie);
+
+static inline int should_escape(char c)
+{
+ switch (c) {
+ case ',':
+ case '=':
+ case ' ':
+ case '"':
+ return 1;
+ break;
+ }
+ return 0;
+}
+
+static inline char* escape_chr(const char* src)
+{
+ int j, i = 0;
+ size_t len, src_len;
+ char* res;
+
+ len = src_len = strlen(src);
+ while (i < src_len) {
+ if (should_escape(src[i++]))
+ len++;
+ }
+
+ if (len == src_len) {
+ res = strdup(src);
+ } else {
+ res = malloc(len + 1);
+ if (res) {
+ i = j = 0;
+ while (i < src_len) {
+ if (should_escape(src[i]))
+ res[j++] = '\\';
+ res[j++] = src[i++];
+ }
+ }
+ res[j] = '\0';
+ }
+ return res;
+}
-static inline void concatenate(char* dest, const char* source, const char *sep)
+static inline void concatenate(char* dest, const char* source, const char* sep)
{
- strncat(dest, sep, strlen(sep));
- strncat(dest, source, strlen(source));
+ char* esc_source;
+
+ if (sep)
+ strncat(dest, sep, strlen(sep));
+
+ esc_source = escape_chr(source);
+
+ strncat(dest, esc_source, strlen(esc_source));
+
+ if (esc_source)
+ free(esc_source);
}
-size_t make_url(char *url, size_t l_url, const char *host, const char *port, const char *endpoint);
+size_t make_url(char* url, size_t l_url, const char* host, const char* port, const char* endpoint);
#endif