From 2f2beb8029e3d7836eac748a667c4f0c4f61ee02 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Mon, 18 Jun 2018 16:51:16 +0200 Subject: Escape special characters according to influxDB doc Change-Id: I22724b547033b40b3160c3bbe1b2141349a1ead2 Signed-off-by: Sebastien Douheret --- .vscode/launch.json | 35 +++++++++++++++++++++++++++- conf.d/cmake/config.cmake | 2 +- src/plugins/influxdb-writer.c | 7 +++--- src/plugins/influxdb.h | 53 +++++++++++++++++++++++++++++++++---------- 4 files changed, 79 insertions(+), 18 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 03c2de7..06f46b8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -60,6 +60,39 @@ "ignoreFailures": true } ] - } + }, + { + "name": "harvester (port 1234)", + "type": "cppdbg", + "request": "launch", + "program": "/opt/AGL/bin/afb-daemon", + "args": [ + "--port=1234", + "--name=afbd-harvester", + "--workdir=${workspaceRoot}/build/package/", + "--ldpaths=lib", + "--roothttp=.", + "--token=", + "--tracereq=common", + "--ws-server=unix:/tmp/harvester", + "-vvv" + ], + "additionalSOLibSearchPath": "${workspaceRoot}/build/package/lib", + "stopAtEntry": false, + "cwd": "${workspaceRoot}/build/package", + "environment": [ + ], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "showDisplayString": true, + "preLaunchTask": "Build" + } ] } diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake index 8fb6e10..c460da6 100644 --- a/conf.d/cmake/config.cmake +++ b/conf.d/cmake/config.cmake @@ -193,7 +193,7 @@ set(AFB_REMPORT "1234" CACHE PATH "Default binder listening port") # Print a helper message when every thing is finished # ---------------------------------------------------- -set(CLOSING_MESSAGE "Typical binding launch: afb-daemon --port=${AFB_REMPORT} --workdir=${CMAKE_BINARY_DIR}/package --ldpaths=lib --roothttp=htdocs --token=\"${AFB_TOKEN}\" --tracereq=common --verbose --name=afb-harvester") +set(CLOSING_MESSAGE "Typical binding launch: afb-daemon --port=${AFB_REMPORT} --workdir=${CMAKE_BINARY_DIR}/package --ldpaths=lib --roothttp=htdocs --token=\"${AFB_TOKEN}\" --tracereq=common --verbose --name=afb-harvester --ws-server unix:/tmp/harvester") set(PACKAGE_MESSAGE "Install widget file using in the target : afm-util install ${PROJECT_NAME}.wgt") # Optional schema validator about now only XML, LUA and JSON 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 +#include #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 -- cgit 1.2.3-korg