From c6fbd2e710bb0f0949d12d1d489c59d942fa0886 Mon Sep 17 00:00:00 2001 From: Lizan Zhou Date: Tue, 29 Aug 2017 16:45:51 -0700 Subject: [PATCH] escape bytes attributes in DebugString (#103) --- mixerclient/src/attribute.cc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/mixerclient/src/attribute.cc b/mixerclient/src/attribute.cc index 0f888a7e2c50..6f104f51365f 100644 --- a/mixerclient/src/attribute.cc +++ b/mixerclient/src/attribute.cc @@ -14,11 +14,31 @@ */ #include "include/attribute.h" +#include +#include #include namespace istio { namespace mixer_client { +namespace { + +std::string escape(const std::string& source) { + std::stringstream ss; + static const std::locale& loc = std::locale::classic(); + + for (const auto& c : source) { + if (std::isprint(c, loc)) { + ss << c; + } else { + ss << "\\x" << std::setfill('0') << std::setw(2) << std::hex + << static_cast(c); + } + } + return ss.str(); +} +} + const std::string Attributes::kQuotaName = "quota.name"; const std::string Attributes::kQuotaAmount = "quota.amount"; @@ -120,7 +140,7 @@ std::string Attributes::DebugString() const { ss << "(STRING): " << it.second.str_v; break; case Attributes::Value::ValueType::BYTES: - ss << "(BYTES): " << it.second.str_v; + ss << "(BYTES): " << escape(it.second.str_v); break; case Attributes::Value::ValueType::INT64: ss << "(INT64): " << it.second.value.int64_v;