Skip to content
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 @@ -57,9 +57,13 @@ QJsonValue {{prefix}}HttpFileElement::asJsonValue() const {
if (!result) {
qDebug() << "Error opening file " << local_filename;
}
return QJsonDocument::fromBinaryData(bArray.data()).object();
#if QT_VERSION >= 0x051500
return QJsonDocument::fromJson(bArray.data()).object();
#else
return QJsonDocument::fromBinaryData(bArray.data()).object();
#endif
}

bool {{prefix}}HttpFileElement::fromStringValue(const QString &instr) {
QFile file(local_filename);
bool result = false;
Expand All @@ -82,7 +86,11 @@ bool {{prefix}}HttpFileElement::fromJsonValue(const QJsonValue &jval) {
file.remove();
}
result = file.open(QIODevice::WriteOnly);
#if QT_VERSION >= 0x051500
file.write(QJsonDocument(jval.toObject()).toJson());
#else
file.write(QJsonDocument(jval.toObject()).toBinaryData());
#endif
file.close();
if (!result) {
qDebug() << "Error creating file " << local_filename;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
#include <QTimer>
#include <QUrl>
#include <QUuid>
#include <QtGlobal>{{#contentCompression}}
#include <QtGlobal>
#if QT_VERSION >= 0x051500
#define SKIP_EMPTY_PARTS Qt::SkipEmptyParts
#else
#define SKIP_EMPTY_PARTS QString::SkipEmptyParts
#endif{{#contentCompression}}
#include <zlib.h>{{/contentCompression}}

#include "{{prefix}}HttpRequest.h"
Expand Down Expand Up @@ -46,7 +51,13 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f

{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {

#if QT_VERSION >= 0x051500
randomGenerator = QRandomGenerator(QDateTime::currentDateTime().toSecsSinceEpoch());
#else
qsrand(QDateTime::currentDateTime().toTime_t());
#endif

if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
Expand Down Expand Up @@ -205,31 +216,36 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
// variable layout is MULTIPART

boundary = QString("__-----------------------%1%2")
.arg(QDateTime::currentDateTime().toTime_t())
.arg(qrand());
#if QT_VERSION >= 0x051500
.arg(QDateTime::currentDateTime().toSecsSinceEpoch())
.arg(randomGenerator.generate());
#else
.arg(QDateTime::currentDateTime().toTime_t())
.arg(qrand());
#endif
QString boundary_delimiter = "--";
QString new_line = "\r\n";

// add variables
foreach (QString key, input->vars.keys()) {
// add boundary
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(new_line);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(new_line.toUtf8());

// add header
request_content.append("Content-Disposition: form-data; ");
request_content.append(http_attribute_encode("name", key));
request_content.append(new_line);
request_content.append(http_attribute_encode("name", key).toUtf8());
request_content.append(new_line.toUtf8());
request_content.append("Content-Type: text/plain");
request_content.append(new_line);
request_content.append(new_line.toUtf8());

// add header to body splitter
request_content.append(new_line);
request_content.append(new_line.toUtf8());

// add variable content
request_content.append(input->vars.value(key));
request_content.append(new_line);
request_content.append(input->vars.value(key).toUtf8());
request_content.append(new_line.toUtf8());
}

// add files
Expand Down Expand Up @@ -263,38 +279,38 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
}

// add boundary
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(new_line);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(new_line.toUtf8());

// add header
request_content.append(
QString("Content-Disposition: form-data; %1; %2").arg(http_attribute_encode("name", file_info->variable_name), http_attribute_encode("filename", file_info->request_filename)));
request_content.append(new_line);
QString("Content-Disposition: form-data; %1; %2").arg(http_attribute_encode("name", file_info->variable_name), http_attribute_encode("filename", file_info->request_filename)).toUtf8());
request_content.append(new_line.toUtf8());

if (file_info->mime_type != nullptr && !file_info->mime_type.isEmpty()) {
request_content.append("Content-Type: ");
request_content.append(file_info->mime_type);
request_content.append(new_line);
request_content.append(file_info->mime_type.toUtf8());
request_content.append(new_line.toUtf8());
}

request_content.append("Content-Transfer-Encoding: binary");
request_content.append(new_line);
request_content.append(new_line.toUtf8());

// add header to body splitter
request_content.append(new_line);
request_content.append(new_line.toUtf8());

// add file content
request_content.append(file.readAll());
request_content.append(new_line);
request_content.append(new_line.toUtf8());

file.close();
}

// add end of body
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(boundary_delimiter);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(boundary_delimiter.toUtf8());
}

if (input->request_body.size() > 0) {
Expand Down Expand Up @@ -408,14 +424,14 @@ void {{prefix}}HttpRequestWorker::on_reply_timeout(QNetworkReply *reply) {

void {{prefix}}HttpRequestWorker::process_response(QNetworkReply *reply) {
if (getResponseHeaders().contains(QString("Content-Disposition"))) {
auto contentDisposition = getResponseHeaders().value(QString("Content-Disposition").toUtf8()).split(QString(";"), QString::SkipEmptyParts);
auto contentDisposition = getResponseHeaders().value(QString("Content-Disposition").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS);
auto contentType =
getResponseHeaders().contains(QString("Content-Type")) ? getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), QString::SkipEmptyParts).first() : QString();
getResponseHeaders().contains(QString("Content-Type")) ? getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS).first() : QString();
if ((contentDisposition.count() > 0) && (contentDisposition.first() == QString("attachment"))) {
QString filename = QUuid::createUuid().toString();
for (const auto &file : contentDisposition) {
if (file.contains(QString("filename"))) {
filename = file.split(QString("="), QString::SkipEmptyParts).at(1);
filename = file.split(QString("="), SKIP_EMPTY_PARTS).at(1);
break;
}
}
Expand All @@ -425,14 +441,14 @@ void {{prefix}}HttpRequestWorker::process_response(QNetworkReply *reply) {
}

} else if (getResponseHeaders().contains(QString("Content-Type"))) {
auto contentType = getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), QString::SkipEmptyParts);
auto contentType = getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS);
if ((contentType.count() > 0) && (contentType.first() == QString("multipart/form-data"))) {
// TODO : Handle Multipart responses
} else {
if(headers.contains("Content-Encoding")){
auto encoding = headers.value("Content-Encoding").split(QString(";"), QString::SkipEmptyParts);
auto encoding = headers.value("Content-Encoding").split(QString(";"), SKIP_EMPTY_PARTS);
if(encoding.count() > 0){
auto compressionTypes = encoding.first().split(',', QString::SkipEmptyParts);
auto compressionTypes = encoding.first().split(',', SKIP_EMPTY_PARTS);
if(compressionTypes.contains("gzip", Qt::CaseInsensitive) || compressionTypes.contains("deflate", Qt::CaseInsensitive)){
response = decompress(reply->readAll());
} else if(compressionTypes.contains("identity", Qt::CaseInsensitive)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include <QObject>
#include <QString>
#include <QTimer>
#if QT_VERSION >= 0x051500
#include <QRandomGenerator>
#endif

#include "{{prefix}}HttpFileElement.h"

Expand Down Expand Up @@ -86,6 +89,9 @@ private:
bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled;
int httpResponseCode;
#if QT_VERSION >= 0x051500
QRandomGenerator randomGenerator;
#endif

void on_reply_timeout(QNetworkReply *reply);
void on_reply_finished(QNetworkReply *reply);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName
{{/isBasicBearer}}{{#isBasicBasic}}
if(!_username.isEmpty() && !_password.isEmpty()){
QByteArray b64;
b64.append(_username + ":" + _password);
b64.append(_username.toUtf8() + ":" + _password.toUtf8());
addHeaders("Authorization","Basic " + b64.toBase64());
}{{/isBasicBasic}}{{/authMethods}}
{{#queryParams}}{{^collectionFormat}}
Expand Down Expand Up @@ -230,7 +230,7 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName
QString output({{paramName}});{{/isString}}{{#isByteArray}}QString output({{paramName}});{{/isByteArray}}{{^isString}}{{^isByteArray}}{{^isFile}}
QString output = {{paramName}}.asJson();{{/isFile}}{{/isByteArray}}{{/isString}}{{#isFile}}{{#hasConsumes}}input.headers.insert("Content-Type", {{#consumes}}{{^-first}}, {{/-first}}"{{mediaType}}"{{/consumes}});{{/hasConsumes}}
QByteArray output = {{paramName}}.asByteArray();{{/isFile}}
input.request_body.append(output);
input.request_body.append(output.toUtf8());
{{/isContainer}}{{/bodyParams}}{{#headerParams}}
if (!::{{cppNamespace}}::toStringValue({{paramName}}).isEmpty()) {
input.headers.insert("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}));
Expand Down
12 changes: 10 additions & 2 deletions samples/client/petstore/cpp-qt5/client/PFXHttpFileElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ QJsonValue PFXHttpFileElement::asJsonValue() const {
if (!result) {
qDebug() << "Error opening file " << local_filename;
}
return QJsonDocument::fromBinaryData(bArray.data()).object();
#if QT_VERSION >= 0x051500
return QJsonDocument::fromJson(bArray.data()).object();
#else
return QJsonDocument::fromBinaryData(bArray.data()).object();
#endif
}

bool PFXHttpFileElement::fromStringValue(const QString &instr) {
QFile file(local_filename);
bool result = false;
Expand All @@ -90,7 +94,11 @@ bool PFXHttpFileElement::fromJsonValue(const QJsonValue &jval) {
file.remove();
}
result = file.open(QIODevice::WriteOnly);
#if QT_VERSION >= 0x051500
file.write(QJsonDocument(jval.toObject()).toJson());
#else
file.write(QJsonDocument(jval.toObject()).toBinaryData());
#endif
file.close();
if (!result) {
qDebug() << "Error creating file " << local_filename;
Expand Down
Loading