diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-06-18 16:51:16 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-07-10 23:41:15 +0200 |
commit | 2f2beb8029e3d7836eac748a667c4f0c4f61ee02 (patch) | |
tree | c8591f35e0589d9e8582641327133b91e3af4bf4 /src/plugins/influxdb.h | |
parent | fe866cb7d63dd26ef271157430f9a7b3635ce40d (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/influxdb.h')
-rw-r--r-- | src/plugins/influxdb.h | 53 |
1 files changed, 41 insertions, 12 deletions
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 |