From 72e2f0bf18b8ade701a55ffe40521d598d598059 Mon Sep 17 00:00:00 2001 From: redPanther Date: Sun, 26 Feb 2017 15:30:10 +0100 Subject: [PATCH] check config when writing (#405) * check config when writing * integrate #403 --- CompileHowto.md | 4 ++-- libsrc/jsonserver/JsonClientConnection.cpp | 4 ++-- libsrc/webconfig/CgiHandler.cpp | 27 +++++++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CompileHowto.md b/CompileHowto.md index 80bed57e..74f56b94 100644 --- a/CompileHowto.md +++ b/CompileHowto.md @@ -107,8 +107,8 @@ To generate make files on OS X: After which you can run cmake with the correct qt5 path: ``` -cmake -DCMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.7.0 -DCMAKE_BUILD_TYPE=Release .. -``` +export QVER=$(find /usr/local/Cellar/qt5 -type d -name "5.*" | sort -n | head -n1) +cmake -DCMAKE_PREFIX_PATH=$QVER -DCMAKE_BUILD_TYPE=Release ..``` ### Run make to build Hyperion The `-j $(nproc)` specifies the amount of CPU cores to use. diff --git a/libsrc/jsonserver/JsonClientConnection.cpp b/libsrc/jsonserver/JsonClientConnection.cpp index 6fa626ae..1cc52db8 100644 --- a/libsrc/jsonserver/JsonClientConnection.cpp +++ b/libsrc/jsonserver/JsonClientConnection.cpp @@ -1028,7 +1028,7 @@ void JsonClientConnection::handleSchemaGetCommand(const QJsonObject& message, co // read the hyperion json schema from the resource QFile schemaData(":/hyperion-schema-"+QString::number(_hyperion->getConfigVersionId())); - + if (!schemaData.open(QIODevice::ReadOnly)) { std::stringstream error; @@ -1039,7 +1039,7 @@ void JsonClientConnection::handleSchemaGetCommand(const QJsonObject& message, co QByteArray schema = schemaData.readAll(); QJsonDocument doc = QJsonDocument::fromJson(schema, &error); schemaData.close(); - + if (error.error != QJsonParseError::NoError) { // report to the user the failure and their locations in the document. diff --git a/libsrc/webconfig/CgiHandler.cpp b/libsrc/webconfig/CgiHandler.cpp index ff09192f..3796f135 100644 --- a/libsrc/webconfig/CgiHandler.cpp +++ b/libsrc/webconfig/CgiHandler.cpp @@ -101,7 +101,32 @@ void CgiHandler::cmd_cfg_set() if (error.error == QJsonParseError::NoError) { QJsonObject hyperionConfigJsonObj = hyperionConfig.object(); - QJsonFactory::writeJson(QString::fromStdString(_hyperion->getConfigFileName()), hyperionConfigJsonObj); + try + { + // make sure the resources are loaded (they may be left out after static linking) + Q_INIT_RESOURCE(resource); + QJsonObject schemaJson = QJsonFactory::readSchema(":/hyperion-schema-"+QString::number(_hyperion->getConfigVersionId())); + QJsonSchemaChecker schemaChecker; + schemaChecker.setSchema(schemaJson); + if ( schemaChecker.validate(hyperionConfigJsonObj) ) + { + QJsonFactory::writeJson(QString::fromStdString(_hyperion->getConfigFileName()), hyperionConfigJsonObj); + } + else + { + std::string errorMsg = "ERROR: Json validation failed: \n"; + for (std::list::const_iterator i = schemaChecker.getMessages().begin(); i != schemaChecker.getMessages().end(); ++i) + { + Error(_log, "config write validation: %s", (*i).c_str()); + errorMsg += *i + "\n"; + } + throw std::runtime_error(errorMsg.c_str()); + } + } + catch(const std::runtime_error& validate_error) + { + _reply->appendRawData (QString(validate_error.what()).toUtf8()); + } } else {