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

[C++] [Qt5] fixed cpp-client-qt5 HttpRequestWorker requests crashing on timeout #5651

Merged
merged 3 commits into from
Mar 27, 2020
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 @@ -45,14 +45,17 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
}

{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent)
: QObject(parent), manager(nullptr), _timeOut(0) {
: QObject(parent), manager(nullptr), timeOutTimer(this) {
qsrand(QDateTime::currentDateTime().toTime_t());
manager = new QNetworkAccessManager(this);
workingDirectory = QDir::currentPath();
connect(manager, &QNetworkAccessManager::finished, this, &{{prefix}}HttpRequestWorker::on_manager_finished);
timeOutTimer.setSingleShot(true);
}

{{prefix}}HttpRequestWorker::~{{prefix}}HttpRequestWorker() {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
timeOutTimer.stop();
for (const auto &item : multiPartFields) {
if (item != nullptr) {
delete item;
Expand Down Expand Up @@ -86,8 +89,11 @@ QByteArray *{{prefix}}HttpRequestWorker::getMultiPartField(const QString &fieldn
return nullptr;
}

void {{prefix}}HttpRequestWorker::setTimeOut(int timeOut) {
_timeOut = timeOut;
void {{prefix}}HttpRequestWorker::setTimeOut(int timeOutMs) {
timeOutTimer.setInterval(timeOutMs);
if(timeOutTimer.interval() == 0) {
onze marked this conversation as resolved.
Show resolved Hide resolved
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
}
}

void {{prefix}}HttpRequestWorker::setWorkingDirectory(const QString &path) {
Expand Down Expand Up @@ -347,12 +353,17 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
buffer->setParent(reply);
#endif
}
if (_timeOut > 0) {
QTimer::singleShot(_timeOut, [=]() { on_manager_timeout(reply); });
if (timeOutTimer.interval() > 0) {
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); });
timeOutTimer.start();
}
}

void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
if(timeOutTimer.isActive()) {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
timeOutTimer.stop();
}
error_type = reply->error();
error_str = reply->errorString();
if (reply->rawHeaderPairs().count() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QNetworkReply>
#include <QObject>
#include <QString>
#include <QTimer>

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

Expand Down Expand Up @@ -59,7 +60,7 @@ public:
QString http_attribute_encode(QString attribute_name, QString input);
void execute({{prefix}}HttpRequestInput *input);
static QSslConfiguration *sslDefaultConfiguration;
void setTimeOut(int tout);
void setTimeOut(int timeOutMs);
void setWorkingDirectory(const QString &path);
{{prefix}}HttpFileElement getHttpFileElement(const QString &fieldname = QString());
QByteArray *getMultiPartField(const QString &fieldname = QString());
Expand All @@ -78,7 +79,7 @@ private:
QMap<QString, {{prefix}}HttpFileElement> files;
QMap<QString, QByteArray *> multiPartFields;
QString workingDirectory;
int _timeOut;
QTimer timeOutTimer;
bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled;
void on_manager_timeout(QNetworkReply *reply);
Expand Down
21 changes: 16 additions & 5 deletions samples/client/petstore/cpp-qt5/client/PFXHttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ void PFXHttpRequestInput::add_file(QString variable_name, QString local_filename
}

PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent)
: QObject(parent), manager(nullptr), _timeOut(0) {
: QObject(parent), manager(nullptr), timeOutTimer(this) {
qsrand(QDateTime::currentDateTime().toTime_t());
manager = new QNetworkAccessManager(this);
workingDirectory = QDir::currentPath();
connect(manager, &QNetworkAccessManager::finished, this, &PFXHttpRequestWorker::on_manager_finished);
timeOutTimer.setSingleShot(true);
}

PFXHttpRequestWorker::~PFXHttpRequestWorker() {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
timeOutTimer.stop();
for (const auto &item : multiPartFields) {
if (item != nullptr) {
delete item;
Expand Down Expand Up @@ -93,8 +96,11 @@ QByteArray *PFXHttpRequestWorker::getMultiPartField(const QString &fieldname) {
return nullptr;
}

void PFXHttpRequestWorker::setTimeOut(int timeOut) {
_timeOut = timeOut;
void PFXHttpRequestWorker::setTimeOut(int timeOutMs) {
timeOutTimer.setInterval(timeOutMs);
if(timeOutTimer.interval() == 0) {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
}
}

void PFXHttpRequestWorker::setWorkingDirectory(const QString &path) {
Expand Down Expand Up @@ -354,12 +360,17 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
buffer->setParent(reply);
#endif
}
if (_timeOut > 0) {
QTimer::singleShot(_timeOut, [=]() { on_manager_timeout(reply); });
if (timeOutTimer.interval() > 0) {
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); });
timeOutTimer.start();
}
}

void PFXHttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
if(timeOutTimer.isActive()) {
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
timeOutTimer.stop();
}
error_type = reply->error();
error_str = reply->errorString();
if (reply->rawHeaderPairs().count() > 0) {
Expand Down
5 changes: 3 additions & 2 deletions samples/client/petstore/cpp-qt5/client/PFXHttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QNetworkReply>
#include <QObject>
#include <QString>
#include <QTimer>

#include "PFXHttpFileElement.h"

Expand Down Expand Up @@ -67,7 +68,7 @@ class PFXHttpRequestWorker : public QObject {
QString http_attribute_encode(QString attribute_name, QString input);
void execute(PFXHttpRequestInput *input);
static QSslConfiguration *sslDefaultConfiguration;
void setTimeOut(int tout);
void setTimeOut(int timeOutMs);
void setWorkingDirectory(const QString &path);
PFXHttpFileElement getHttpFileElement(const QString &fieldname = QString());
QByteArray *getMultiPartField(const QString &fieldname = QString());
Expand All @@ -86,7 +87,7 @@ class PFXHttpRequestWorker : public QObject {
QMap<QString, PFXHttpFileElement> files;
QMap<QString, QByteArray *> multiPartFields;
QString workingDirectory;
int _timeOut;
QTimer timeOutTimer;
bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled;
void on_manager_timeout(QNetworkReply *reply);
Expand Down