Skip to content

Commit

Permalink
escape bytes attributes in DebugString (envoyproxy#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizan authored and qiwzhang committed Aug 29, 2017
1 parent fb3e1b7 commit c6fbd2e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion mixerclient/src/attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,31 @@
*/

#include "include/attribute.h"
#include <iomanip>
#include <locale>
#include <sstream>

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<uint16_t>(c);
}
}
return ss.str();
}
}

const std::string Attributes::kQuotaName = "quota.name";
const std::string Attributes::kQuotaAmount = "quota.amount";

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c6fbd2e

Please sign in to comment.