From 860d70bd0e228ee542e3e559961bfc7e56888d77 Mon Sep 17 00:00:00 2001 From: est31 Date: Thu, 28 Jan 2016 23:53:58 +0100 Subject: [PATCH] Don't print whole json data buffer to errorstream on error `errorstream` must not be overly verbose as clientside it is directly printed onto the ingame chat window. These days, the serverlist can contain > 200k bytes, so better print it to warningstream if the data buffer is too long. --- src/convert_json.cpp | 8 +++++++- src/script/lua_api/l_util.cpp | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/convert_json.cpp b/src/convert_json.cpp index e03508e21..e548c45f5 100644 --- a/src/convert_json.cpp +++ b/src/convert_json.cpp @@ -52,7 +52,13 @@ Json::Value fetchJsonValue(const std::string &url, if (!reader.parse(stream, root)) { errorstream << "URL: " << url << std::endl; errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages(); - errorstream << "data: \"" << fetch_result.data << "\"" << std::endl; + if (fetch_result.data.size() > 100) { + errorstream << "Data (" << fetch_result.data.size() + << " bytes) printed to warningstream." << std::endl; + warningstream << "data: \"" << fetch_result.data << "\"" << std::endl; + } else { + errorstream << "data: \"" << fetch_result.data << "\"" << std::endl; + } return Json::Value(); } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 3f7e15acf..39863b987 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -161,8 +161,14 @@ int ModApiUtil::l_parse_json(lua_State *L) if (!reader.parse(stream, root)) { errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages(); - errorstream << "data: \"" << jsonstr << "\"" - << std::endl; + size_t jlen = strlen(jsonstr); + if (jlen > 100) { + errorstream << "Data (" << jlen + << " bytes) printed to warningstream." << std::endl; + warningstream << "data: \"" << jsonstr << "\"" << std::endl; + } else { + errorstream << "data: \"" << jsonstr << "\"" << std::endl; + } lua_pushnil(L); return 1; }