WebSockets: also json-escape strings in state messages

This commit is contained in:
Luciano Iam 2020-04-22 18:17:13 +02:00 committed by Robin Gareus
parent 946094b25c
commit dbb26daafa
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
7 changed files with 82 additions and 29 deletions

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2020 Luciano Iam <lucianito@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the/GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <iomanip>
#include <sstream>
#include "json.h"
/* adapted from https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c
CC BY-SA 4.0 license */
std::string
Json::escape (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

@ -0,0 +1,30 @@
/*
* Copyright (C) 2020 Luciano Iam <lucianito@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the/GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef _ardour_surface_json_h_
#define _ardour_surface_json_h_
class Json
{
public:
static std::string escape (const std::string&);
};
#endif // _ardour_surface_json_h_

View File

@ -26,6 +26,7 @@
#include "manifest.h"
#include "resources.h"
#include "json.h"
static const char* const manifest_filename = "manifest.xml";
@ -76,10 +77,10 @@ SurfaceManifest::to_json ()
std::stringstream ss;
ss << "{"
<< "\"path\":\"" << ServerResources::escape_json (Glib::path_get_basename (_path)) << "\""
<< ",\"name\":\"" << ServerResources::escape_json (_name) << "\""
<< ",\"description\":\"" << ServerResources::escape_json (_description) << "\""
<< ",\"version\":\"" << ServerResources::escape_json (_version) << "\""
<< "\"path\":\"" << Json::escape (Glib::path_get_basename (_path)) << "\""
<< ",\"name\":\"" << Json::escape (_name) << "\""
<< ",\"description\":\"" << Json::escape (_description) << "\""
<< ",\"version\":\"" << Json::escape (_version) << "\""
<< "}";
return ss.str ();

View File

@ -26,6 +26,7 @@
#include <sstream>
#include "message.h"
#include "json.h"
// JSON does not support Infinity or NaN
#define XSTR(s) STR (s)
@ -166,7 +167,7 @@ NodeStateMessage::serialize (void* buf, size_t len) const
break;
}
case TypedValue::String:
ss << '"' << static_cast<std::string> (val) << '"';
ss << '"' << Json::escape (static_cast<std::string> (val)) << '"';
break;
default:
break;

View File

@ -16,7 +16,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <iomanip>
#include <iostream>
#include <sstream>
@ -27,6 +26,7 @@
#include "pbd/file_utils.h"
#include "resources.h"
#include "json.h"
static const char* const data_dir_env_var = "ARDOUR_WEBSURFACES_PATH";
static const char* const data_dir_name = "web_surfaces";
@ -46,23 +46,6 @@ ServerResources::ServerResources ()
{
}
/* adapted from https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c
CC BY-SA 4.0 license */
std::string
ServerResources::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 ();
}
const std::string&
ServerResources::index_dir ()
{
@ -102,8 +85,8 @@ ServerResources::scan ()
SurfaceManifestVector builtin = read_manifests (builtin_dir_str);
ss << "[{"
<< "\"filesystemPath\":\"" << escape_json (builtin_dir_str) << "\""
<< ",\"path\":\"" << escape_json (builtin_dir_name) << "\""
<< "\"filesystemPath\":\"" << Json::escape (builtin_dir_str) << "\""
<< ",\"path\":\"" << Json::escape (builtin_dir_name) << "\""
<< ",\"surfaces\":"
<< "[";
@ -118,8 +101,8 @@ ServerResources::scan ()
SurfaceManifestVector user = read_manifests (user_dir_str);
ss << "]},{"
<< "\"filesystemPath\":\"" << escape_json(user_dir_str) << "\""
<< ",\"path\":\"" << escape_json(user_dir_name) << "\""
<< "\"filesystemPath\":\"" << Json::escape (user_dir_str) << "\""
<< ",\"path\":\"" << Json::escape (user_dir_name) << "\""
<< ",\"surfaces\":"
<< "[";

View File

@ -30,8 +30,6 @@ class ServerResources
{
public:
ServerResources ();
static std::string escape_json (const std::string&);
const std::string& index_dir ();
const std::string& builtin_dir ();

View File

@ -29,6 +29,7 @@ def build(bld):
dispatcher.cc
manifest.cc
resources.cc
json.cc
'''
obj.export_includes = ['.']
obj.defines = [ 'PACKAGE="ardour_websockets"' ]