Correctly parse multi-buffer JSON messages (RIPD-1306):

When attempting to parse a BufferSequence as a JSON object,
if the sequence contained more than buffer, the JSON parser
would incorrectly attempt to decode each buffer as a separate
JSON object, instead of one complete object.
This commit is contained in:
Nik Bougalis
2016-09-30 18:41:36 -07:00
committed by Vinnie Falco
parent 41851022d3
commit 69b47890e6
2 changed files with 4 additions and 8 deletions

View File

@@ -176,13 +176,11 @@ bool
Reader::parse(Value& root, BufferSequence const& bs) Reader::parse(Value& root, BufferSequence const& bs)
{ {
using namespace boost::asio; using namespace boost::asio;
std::string s;
s.reserve (buffer_size(bs));
for (auto const& b : bs) for (auto const& b : bs)
{ s.append(buffer_cast<char const*>(b), buffer_size(b));
auto begin = buffer_cast<const char*>(b); return parse(s, root);
if(! parse(begin, begin + buffer_size(b), root))
return false;
}
return true;
} }
/** \brief Read from 'sin' into 'root'. /** \brief Read from 'sin' into 'root'.

View File

@@ -208,11 +208,9 @@ struct Regression_test : public beast::unit_test::suite
std::string const request = R"json({"command":"path_find","id":19,"subcommand":"create","source_account":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","destination_account":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","destination_amount":"1000000","source_currencies":[{"currency":"0000000000000000000000000000000000000000"},{"currency":"0000000000000000000000005553440000000000"},{"currency":"0000000000000000000000004254430000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004254430000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004254430000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004555520000000000"},{"currency":"0000000000000000000000004554480000000000"},{"currency":"0000000000000000000000004A50590000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"000000000000000000000000434E590000000000"},{"currency":"0000000000000000000000004742490000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004341440000000000"}]})json"; std::string const request = R"json({"command":"path_find","id":19,"subcommand":"create","source_account":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","destination_account":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","destination_amount":"1000000","source_currencies":[{"currency":"0000000000000000000000000000000000000000"},{"currency":"0000000000000000000000005553440000000000"},{"currency":"0000000000000000000000004254430000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004254430000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004254430000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004555520000000000"},{"currency":"0000000000000000000000004554480000000000"},{"currency":"0000000000000000000000004A50590000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"000000000000000000000000434E590000000000"},{"currency":"0000000000000000000000004742490000000000"},{"issuer":"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh","currency":"0000000000000000000000004341440000000000"}]})json";
Json::Value jvRequest; Json::Value jvRequest;
Json::Reader jrReader; Json::Reader jrReader;
std::vector<boost::asio::const_buffer> buffers; std::vector<boost::asio::const_buffer> buffers;
buffers.emplace_back(buffer(request, 1024)); buffers.emplace_back(buffer(request, 1024));
buffers.emplace_back(buffer(request.data() + 1024, request.length() - 1024)); buffers.emplace_back(buffer(request.data() + 1024, request.length() - 1024));