WebSockets: json-escape user strings loaded from manifest.xml

This commit is contained in:
Luciano Iam 2020-04-19 20:57:57 +02:00 committed by Robin Gareus
parent b7cdb63a95
commit 3c423d9265
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
2 changed files with 22 additions and 3 deletions

View file

@ -16,6 +16,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#include <iomanip>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -76,9 +77,9 @@ SurfaceManifest::to_json ()
ss << "{" ss << "{"
<< "\"path\":\"" << Glib::path_get_basename (_path) << "\"" << "\"path\":\"" << Glib::path_get_basename (_path) << "\""
<< ",\"name\":\"" << _name << "\"" << ",\"name\":\"" << escape_json (_name) << "\""
<< ",\"description\":\"" << _description << "\"" << ",\"description\":\"" << escape_json (_description) << "\""
<< ",\"version\":\"" << _version << "\"" << ",\"version\":\"" << escape_json (_version) << "\""
<< "}"; << "}";
return ss.str (); return ss.str ();
@ -90,3 +91,20 @@ SurfaceManifest::exists_at_path (std::string path)
std::string xml_path = Glib::build_filename (path, manifest_filename); std::string xml_path = Glib::build_filename (path, manifest_filename);
return Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS); return Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS);
} }
/* adapted from https://stackoverflow.com/questions/10789740/passing-stdstring-by-value-or-reference
CC BY-SA 4.0 license */
std::string
SurfaceManifest::escape_json (const std::string &s) {
std::ostringstream o;
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
if (*it == '"' || *it == '\\' || ('\x00' <= *it && *it <= '\x1f')) {
o << "\\u" << std::hex << std::setw (4) << std::setfill ('0') << static_cast<int>(*it);
} else {
o << *it;
}
}
return o.str ();
}

View file

@ -37,6 +37,7 @@ public:
std::string to_json (); std::string to_json ();
static bool exists_at_path (std::string); static bool exists_at_path (std::string);
static std::string escape_json (const std::string&);
private: private:
bool _valid; bool _valid;