Don't print whole json data buffer to errorstream on error
authorest31 <MTest31@outlook.com>
Thu, 28 Jan 2016 22:53:58 +0000 (23:53 +0100)
committerest31 <MTest31@outlook.com>
Thu, 28 Jan 2016 22:53:58 +0000 (23:53 +0100)
`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
src/script/lua_api/l_util.cpp

index e03508e21fe2d0a77a76013d3f3a79a3168c9a0c..e548c45f54ea61a697844b883f53e595832d281f 100644 (file)
@@ -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();
        }
 
index 3f7e15acfa79e5fd3cf9bd1934094fce41b00929..39863b987a7dea893847519e727fcd864f19b463 100644 (file)
@@ -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;
                }