Status TLSConfigPlugin::genConfig(std::map<std::string, std::string>& config) { std::string json; auto s = TLSRequestHelper::go<JSONSerializer>( uri_, json, FLAGS_config_tls_max_attempts); if (!s.ok()) { return s; } if (FLAGS_tls_node_api) { // The node API embeds configuration data (JSON escaped). pt::ptree tree; try { std::stringstream input; input << json; pt::read_json(input, tree); } catch (const pt::json_parser::json_parser_error& e) { VLOG(1) << "Could not parse JSON from TLS node API"; } // Re-encode the config key into JSON. config["tls_plugin"] = unescapeUnicode(tree.get("config", "")); } else { config["tls_plugin"] = json; } return s; }
Status TLSConfigPlugin::genConfig(std::map<std::string, std::string>& config) { std::string json; JSON params; if (FLAGS_tls_node_api) { // The TLS node API morphs some verbs and variables. params.add("_get", true); } auto s = TLSRequestHelper::go<JSONSerializer>( uri_, params, json, FLAGS_config_tls_max_attempts); if (s.ok()) { if (FLAGS_tls_node_api) { // The node API embeds configuration data (JSON escaped). JSON tree; Status parse_status = tree.fromString(json); if (!parse_status.ok()) { VLOG(1) << "Could not parse JSON from TLS config node API"; } // Re-encode the config key into JSON. auto it = tree.doc().FindMember("config"); config["tls_plugin"] = unescapeUnicode(it != tree.doc().MemberEnd() && it->value.IsString() ? it->value.GetString() : ""); } else { config["tls_plugin"] = json; } } return s; }
TEST_F(ConversionsTests, test_unicode_unescape) { std::vector<std::pair<std::string, std::string> > conversions = { std::make_pair("\\u0025hi", "%hi"), std::make_pair("hi\\u0025", "hi%"), std::make_pair("\\uFFFFhi", "\\uFFFFhi"), std::make_pair("0000\\u", "0000\\u"), std::make_pair("hi", "hi"), }; for (const auto& test : conversions) { EXPECT_EQ(unescapeUnicode(test.first), test.second); } }
TEST_F(ConversionsTests, test_unicode_unescape) { std::vector<std::pair<std::string, std::string>> conversions = { std::make_pair("\\u0025hi", "%hi"), std::make_pair("hi\\u0025", "hi%"), std::make_pair("\\uFFFFhi", "\\uFFFFhi"), std::make_pair("0000\\u", "0000\\u"), std::make_pair("hi", "hi"), std::make_pair("c:\\\\users\\\\obelisk\\\\file.txt", "c:\\\\users\\\\obelisk\\\\file.txt"), std::make_pair("Edge case test\\", "Edge case test\\"), std::make_pair("Edge case test two\\\\", "Edge case test two\\\\"), }; for (const auto& test : conversions) { EXPECT_EQ(unescapeUnicode(test.first), test.second); } }