server player req: parse malformed json (#4512)
This commit is contained in:
@@ -80,7 +80,18 @@ void McClient::parseResponse()
|
|||||||
Q_UNUSED(readVarInt(m_resp)); // json length
|
Q_UNUSED(readVarInt(m_resp)); // json length
|
||||||
|
|
||||||
// 'resp' should now be the JSON string
|
// 'resp' should now be the JSON string
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(m_resp);
|
QJsonParseError parseError;
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(m_resp, &parseError);
|
||||||
|
if (parseError.error != QJsonParseError::NoError) {
|
||||||
|
QByteArray validJson = m_resp.left(parseError.offset);
|
||||||
|
doc = QJsonDocument::fromJson(validJson, &parseError);
|
||||||
|
|
||||||
|
if (parseError.error != QJsonParseError::NoError) {
|
||||||
|
qDebug() << "Failed to parse JSON:" << parseError.errorString();
|
||||||
|
emitFail(parseError.errorString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
emitSucceed(doc.object());
|
emitSucceed(doc.object());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
|
|
||||||
#include <Json.h>
|
#include <Json.h>
|
||||||
|
#include "Exception.h"
|
||||||
#include "McClient.h"
|
#include "McClient.h"
|
||||||
#include "McResolver.h"
|
#include "McResolver.h"
|
||||||
#include "ServerPingTask.h"
|
#include "ServerPingTask.h"
|
||||||
|
|
||||||
unsigned getOnlinePlayers(QJsonObject data)
|
unsigned getOnlinePlayers(QJsonObject data)
|
||||||
{
|
{
|
||||||
return Json::requireInteger(Json::requireObject(data, "players"), "online");
|
try {
|
||||||
|
return Json::requireInteger(Json::requireObject(data, "players"), "online");
|
||||||
|
} catch (Exception& e) {
|
||||||
|
qWarning() << "server ping failed to parse response" << e.what();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerPingTask::executeTask()
|
void ServerPingTask::executeTask()
|
||||||
|
|||||||
Reference in New Issue
Block a user