refs #751, detect invalid JSON message

(which normally means a PHP error message in HTML)
* utility function to remove HTML parts
* adjustments
This commit is contained in:
Klaus Basan
2016-08-31 17:13:40 +02:00
committed by Roland Winklmeier
parent 2cc88d9c6a
commit 03c642d88a
4 changed files with 22 additions and 5 deletions

View File

@@ -109,12 +109,18 @@ namespace BlackCore
if (nwReply->error() == QNetworkReply::NoError)
{
QString json(nwReply->readAll());
const QString json(nwReply->readAll().trimmed());
if (json.isEmpty())
{
CLogMessage(this).error("Authentication failed, no response from %1") << urlString;
return;
}
if (!json.startsWith('{') || !json.endsWith('}'))
{
CLogMessage(this).error("Illegal JSON object: %1") << CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(json);
return;
}
QJsonObject jsonObj(Json::jsonObjectFromString(json));
CAuthenticatedUser user(CAuthenticatedUser::fromDatabaseJson(jsonObj));

View File

@@ -11,6 +11,7 @@
#include "blackmisc/logcategory.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/simulation/aircraftmodellist.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/stringutils.h"
@@ -24,6 +25,7 @@
using namespace BlackMisc;
using namespace BlackMisc::Simulation;
using namespace BlackMisc::Network;
namespace BlackMisc
{
@@ -97,8 +99,7 @@ namespace BlackMisc
// no object -> most likely some fucked up HTML string with the PHP error
if (!jsonDoc.isObject())
{
QString phpError(jsonResponse);
phpError.remove(QRegExp("<[^>]*>"));
const QString phpError(CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(jsonResponse));
messages.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, phpError));
return false;
}

View File

@@ -28,6 +28,7 @@
#include <QUrlQuery>
#include <QVariant>
#include <QtDebug>
#include <QRegularExpression>
using namespace BlackConfig;
using namespace BlackMisc;
@@ -278,5 +279,13 @@ namespace BlackMisc
}
return -1;
}
QString CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(const QString &errorMessage)
{
if (errorMessage.isEmpty()) { return errorMessage; }
QString phpError(errorMessage);
static const QRegularExpression regEx("<[^>]*>");
return phpError.remove(regEx);
}
} // namespace
} // namespacee

View File

@@ -39,7 +39,6 @@ namespace BlackMisc
class BLACKMISC_EXPORT CNetworkUtils
{
public:
//! Request type
enum RequestType
{
@@ -125,6 +124,9 @@ namespace BlackMisc
//! Last modified from reply
static qint64 lastModifiedMsSinceEpoch(QNetworkReply *nwReply);
//! Remove the HTML formatting from a PHP error message
static QString removeHtmlPartsFromPhpErrorMessage(const QString &errorMessage);
private:
//! Hidden constructor
CNetworkUtils() {}
@@ -133,4 +135,3 @@ namespace BlackMisc
} // namespace
#endif // guard