Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpp-qt5] client response headers and validation of properties for client and server #1508

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
{{prefix}}HttpRequestWorker::~{{prefix}}HttpRequestWorker() {
}

QMap<QByteArray, QByteArray> {{prefix}}HttpRequestWorker::getResponseHeaders() const {
return headers;
}

QString {{prefix}}HttpRequestWorker::http_attribute_encode(QString attribute_name, QString input) {
// result structure follows RFC 5987
bool need_utf_encoding = false;
Expand Down Expand Up @@ -305,7 +309,11 @@ void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
error_type = reply->error();
response = reply->readAll();
error_str = reply->errorString();

if(reply->rawHeaderPairs().count() > 0){
for(const auto& item: reply->rawHeaderPairs()){
headers.insert(item.first, item.second);
}
}
reply->deleteLater();

emit on_execution_finished(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public:
QNetworkReply::NetworkError error_type;
QString error_str;

explicit {{prefix}}HttpRequestWorker(QObject *parent = 0);
explicit {{prefix}}HttpRequestWorker(QObject *parent = nullptr);
virtual ~{{prefix}}HttpRequestWorker();

QMap<QByteArray, QByteArray> getResponseHeaders() const;
QString http_attribute_encode(QString attribute_name, QString input);
void execute({{prefix}}HttpRequestInput *input);
static QSslConfiguration* sslDefaultConfiguration;
Expand All @@ -73,7 +74,7 @@ signals:

private:
QNetworkAccessManager *manager;

QMap<QByteArray, QByteArray> headers;
private slots:
void on_manager_finished(QNetworkReply *reply);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ toStringValue(const bool &value) {

QString
toStringValue(const float &value){
return QString::number(value);
return QString::number(static_cast<double>(value));
}

QString
Expand Down Expand Up @@ -92,7 +92,7 @@ toJsonValue(const bool &value){

QJsonValue
toJsonValue(const float &value){
return QJsonValue(value);
return QJsonValue(static_cast<double>(value));
}

QJsonValue
Expand Down Expand Up @@ -193,88 +193,126 @@ fromStringValue(const QString &inStr, double &value){
return ok;
}

void
bool
fromJsonValue(QString &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(!jval.isUndefined() && !jval.isNull()){
if(jval.isString()){
value = jval.toString();
} else if(jval.isBool()) {
value = jval.toBool() ? "true" : "false";
} else if(jval.isDouble()){
value = QString::number(jval.toDouble());
} else {
ok = false;
}
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue(QDateTime &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(!jval.isUndefined() && !jval.isNull() && jval.isString()){
value = QDateTime::fromString(jval.toString(), Qt::ISODate);
ok = value.isValid();
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue(QByteArray &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(!jval.isUndefined() && !jval.isNull() && jval.isString()) {
value = QByteArray::fromBase64(QByteArray::fromStdString(jval.toString().toStdString()));
ok = value.size() > 0 ;
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue(QDate &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(!jval.isUndefined() && !jval.isNull() && jval.isString()){
value = QDate::fromString(jval.toString(), Qt::ISODate);
ok = value.isValid();
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue(qint32 &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(!jval.isUndefined() && !jval.isNull() && !jval.isObject() && !jval.isArray()){
value = jval.toInt();
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue(qint64 &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(!jval.isUndefined() && !jval.isNull() && !jval.isObject() && !jval.isArray()){
value = jval.toVariant().toLongLong();
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue(bool &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(jval.isBool()){
value = jval.toBool();
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue(float &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(jval.isDouble()){
value = static_cast<float>(jval.toDouble());
} else {
ok = false;
}
return ok;
}

void
bool
fromJsonValue(double &value, const QJsonValue &jval){
if(!(jval.isUndefined() || jval.isNull())){
bool ok = true;
if(jval.isDouble()){
value = jval.toDouble();
} else {
ok = false;
}

return ok;
}

void
bool
fromJsonValue({{prefix}}Object &value, const QJsonValue &jval){
bool ok = true;
if(jval.isObject()){
value.fromJsonObject(jval.toObject());
ok = value.isValid();
} else {
ok = false;
}
return ok;
}

{{#cppNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace {{this}} {
template <typename T>
QString toStringValue(const QList<T> &val) {
QString strArray;
for(auto item : val) {
for(const auto& item : val) {
strArray.append(toStringValue(item) + ",");
}
if(val.count() > 0) {
Expand All @@ -53,7 +53,7 @@ namespace {{this}} {
template <typename T>
QJsonValue toJsonValue(const QList<T> &val) {
QJsonArray jArray;
for(auto item : val) {
for(const auto& item : val) {
jArray.append(toJsonValue(item));
}
return jArray;
Expand All @@ -62,7 +62,7 @@ namespace {{this}} {
template <typename T>
QJsonValue toJsonValue(const QMap<QString, T> &val) {
QJsonObject jObject;
for(auto itemkey : val.keys()) {
for(const auto& itemkey : val.keys()) {
jObject.insert(itemkey, toJsonValue(val.value(itemkey)));
}
return jObject;
Expand All @@ -79,56 +79,69 @@ namespace {{this}} {
bool fromStringValue(const QString &inStr, double &value);

template <typename T>
void fromStringValue(const QList<QString> &inStr, QList<T> &val) {
for(auto item: inStr){
bool fromStringValue(const QList<QString> &inStr, QList<T> &val) {
bool ok = (inStr.count() > 0);
for(const auto& item: inStr){
T itemVal;
fromStringValue(item, itemVal);
ok &= fromStringValue(item, itemVal);
val.push_back(itemVal);
}
return ok;
}

template <typename T>
void fromStringValue(const QMap<QString, QString> &inStr, QMap<QString, T> &val) {
for(auto itemkey : inStr.keys()){
bool fromStringValue(const QMap<QString, QString> &inStr, QMap<QString, T> &val) {
bool ok = (inStr.count() > 0);
for(const auto& itemkey : inStr.keys()){
T itemVal;
fromStringValue(inStr.value(itemkey), itemVal);
ok &= fromStringValue(inStr.value(itemkey), itemVal);
val.insert(itemkey, itemVal);
}
return ok;
}

void fromJsonValue(QString &value, const QJsonValue &jval);
void fromJsonValue(QDateTime &value, const QJsonValue &jval);
void fromJsonValue(QByteArray &value, const QJsonValue &jval);
void fromJsonValue(QDate &value, const QJsonValue &jval);
void fromJsonValue(qint32 &value, const QJsonValue &jval);
void fromJsonValue(qint64 &value, const QJsonValue &jval);
void fromJsonValue(bool &value, const QJsonValue &jval);
void fromJsonValue(float &value, const QJsonValue &jval);
void fromJsonValue(double &value, const QJsonValue &jval);
void fromJsonValue({{prefix}}Object &value, const QJsonValue &jval);
bool fromJsonValue(QString &value, const QJsonValue &jval);
bool fromJsonValue(QDateTime &value, const QJsonValue &jval);
bool fromJsonValue(QByteArray &value, const QJsonValue &jval);
bool fromJsonValue(QDate &value, const QJsonValue &jval);
bool fromJsonValue(qint32 &value, const QJsonValue &jval);
bool fromJsonValue(qint64 &value, const QJsonValue &jval);
bool fromJsonValue(bool &value, const QJsonValue &jval);
bool fromJsonValue(float &value, const QJsonValue &jval);
bool fromJsonValue(double &value, const QJsonValue &jval);
bool fromJsonValue({{prefix}}Object &value, const QJsonValue &jval);

template <typename T>
void fromJsonValue(QList<T> &val, const QJsonValue &jval) {
bool fromJsonValue(QList<T> &val, const QJsonValue &jval) {
bool ok = true;
if(jval.isArray()){
for(const QJsonValue &jitem : jval.toArray()){
for(const auto& jitem : jval.toArray()){
T item;
fromJsonValue(item, jitem);
ok &= fromJsonValue(item, jitem);
val.push_back(item);
}
} else {
ok = false;
}
}
return ok;
}

template <typename T>
void fromJsonValue(QMap<QString, T> &val, const QJsonValue &jval) {
auto varmap = jval.toObject().toVariantMap();
if(varmap.count() > 0){
for(auto itemkey : varmap.keys() ){
T itemVal;
fromJsonValue(itemVal, QJsonValue::fromVariant(varmap.value(itemkey)));
val.insert(itemkey, itemVal);
bool fromJsonValue(QMap<QString, T> &val, const QJsonValue &jval) {
bool ok = true;
if(jval.isObject()){
auto varmap = jval.toObject().toVariantMap();
if(varmap.count() > 0){
for(const auto& itemkey : varmap.keys() ){
T itemVal;
ok &= fromJsonValue(itemVal, QJsonValue::fromVariant(varmap.value(itemkey)));
val.insert(itemkey, itemVal);
}
}
} else {
ok = false;
}
return;
return ok;
}

{{#cppNamespaceDeclarations}}
Expand Down
Loading