forked from loafle/openapi-generator-original
[C++] [Qt5] fixed cpp-client-qt5 HttpRequestWorker requests crashing on timeout... (#5651)
* - fixed cpp-client-qt5 HttpRequestWorker requests crashing on timeout when they have actually NOT timed out (were calling back into a deleted struct). * #minor fixes after review * Regenerate changed files Co-authored-by: valentin Bisson <valentin@inrosoftware.com> Co-authored-by: etherealjoy <sunn.ssb@live.com>
This commit is contained in:
@@ -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;
|
||||
@@ -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) {
|
||||
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void {{prefix}}HttpRequestWorker::setWorkingDirectory(const QString &path) {
|
||||
@@ -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) {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <QNetworkReply>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
|
||||
#include "{{prefix}}HttpFileElement.h"
|
||||
|
||||
@@ -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());
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
@@ -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) {
|
||||
@@ -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) {
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <QNetworkReply>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
|
||||
#include "PFXHttpFileElement.h"
|
||||
|
||||
@@ -67,7 +68,7 @@ public:
|
||||
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());
|
||||
@@ -86,7 +87,7 @@ private:
|
||||
QMap<QString, PFXHttpFileElement> files;
|
||||
QMap<QString, QByteArray *> multiPartFields;
|
||||
QString workingDirectory;
|
||||
int _timeOut;
|
||||
QTimer timeOutTimer;
|
||||
bool isResponseCompressionEnabled;
|
||||
bool isRequestCompressionEnabled;
|
||||
void on_manager_timeout(QNetworkReply *reply);
|
||||
|
||||
Reference in New Issue
Block a user