diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-06-01 15:08:52 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-07-10 23:41:15 +0200 |
commit | 9f65896316418d257670bc65e83aa87c5683579d (patch) | |
tree | 9bfbb971e39dc3d918743f8bd15519987965ddb6 /src | |
parent | 7234b8adf3627338e9f34e2ce0f31b7e8754628d (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')
-rw-r--r-- | src/plugins/influxdb-writer.c | 4 | ||||
-rw-r--r-- | src/plugins/influxdb.h | 75 |
2 files changed, 66 insertions, 13 deletions
diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c index aaa8991..be02e34 100644 --- a/src/plugins/influxdb-writer.c +++ b/src/plugins/influxdb-writer.c @@ -56,7 +56,7 @@ static size_t format_write_args(char *query, struct series_t *serie) struct list *tags = serie->serie_columns.tags; struct list *fields = serie->serie_columns.fields; - strncat(query, serie->name, strlen(serie->name)); + concatenate(query, serie->name, NULL); if(tags) { while(tags != NULL) { concatenate(query, tags->key, ","); @@ -132,6 +132,8 @@ CURL *make_curl_write_post(AFB_ApiT apiHandle, const char *url, json_object *met } } + AFB_ApiDebug(apiHandle, "influx curl: url=%s data=%s", url, (const char*) *post_data); + /* Check that we just do not broke the for loop before trying preparing CURL request object */ curl = i == lpd ? 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 |