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

View File

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

View File

@@ -28,6 +28,7 @@
#include <QUrlQuery> #include <QUrlQuery>
#include <QVariant> #include <QVariant>
#include <QtDebug> #include <QtDebug>
#include <QRegularExpression>
using namespace BlackConfig; using namespace BlackConfig;
using namespace BlackMisc; using namespace BlackMisc;
@@ -278,5 +279,13 @@ namespace BlackMisc
} }
return -1; 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 } // namespace
} // namespacee } // namespacee

View File

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