Skip to content

Commit 7d06241

Browse files
onzevalentin Bissonetherealjoy
authored andcommitted
[C++] [Qt5] fixed cpp-client-qt5 HttpRequestWorker requests crashing on timeout... (OpenAPITools#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 <[email protected]> Co-authored-by: etherealjoy <[email protected]>
1 parent 9443c7f commit 7d06241

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpRequest.cpp.mustache

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
4545
}
4646

4747
{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent)
48-
: QObject(parent), manager(nullptr), _timeOut(0) {
48+
: QObject(parent), manager(nullptr), timeOutTimer(this) {
4949
qsrand(QDateTime::currentDateTime().toTime_t());
5050
manager = new QNetworkAccessManager(this);
5151
workingDirectory = QDir::currentPath();
5252
connect(manager, &QNetworkAccessManager::finished, this, &{{prefix}}HttpRequestWorker::on_manager_finished);
53+
timeOutTimer.setSingleShot(true);
5354
}
5455

5556
{{prefix}}HttpRequestWorker::~{{prefix}}HttpRequestWorker() {
57+
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
58+
timeOutTimer.stop();
5659
for (const auto &item : multiPartFields) {
5760
if (item != nullptr) {
5861
delete item;
@@ -86,8 +89,11 @@ QByteArray *{{prefix}}HttpRequestWorker::getMultiPartField(const QString &fieldn
8689
return nullptr;
8790
}
8891

89-
void {{prefix}}HttpRequestWorker::setTimeOut(int timeOut) {
90-
_timeOut = timeOut;
92+
void {{prefix}}HttpRequestWorker::setTimeOut(int timeOutMs) {
93+
timeOutTimer.setInterval(timeOutMs);
94+
if(timeOutTimer.interval() == 0) {
95+
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
96+
}
9197
}
9298

9399
void {{prefix}}HttpRequestWorker::setWorkingDirectory(const QString &path) {
@@ -347,12 +353,17 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
347353
buffer->setParent(reply);
348354
#endif
349355
}
350-
if (_timeOut > 0) {
351-
QTimer::singleShot(_timeOut, [=]() { on_manager_timeout(reply); });
356+
if (timeOutTimer.interval() > 0) {
357+
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); });
358+
timeOutTimer.start();
352359
}
353360
}
354361

355362
void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
363+
if(timeOutTimer.isActive()) {
364+
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
365+
timeOutTimer.stop();
366+
}
356367
error_type = reply->error();
357368
error_str = reply->errorString();
358369
if (reply->rawHeaderPairs().count() > 0) {

modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpRequest.h.mustache

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <QNetworkReply>
1414
#include <QObject>
1515
#include <QString>
16+
#include <QTimer>
1617

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

@@ -59,7 +60,7 @@ public:
5960
QString http_attribute_encode(QString attribute_name, QString input);
6061
void execute({{prefix}}HttpRequestInput *input);
6162
static QSslConfiguration *sslDefaultConfiguration;
62-
void setTimeOut(int tout);
63+
void setTimeOut(int timeOutMs);
6364
void setWorkingDirectory(const QString &path);
6465
{{prefix}}HttpFileElement getHttpFileElement(const QString &fieldname = QString());
6566
QByteArray *getMultiPartField(const QString &fieldname = QString());
@@ -78,7 +79,7 @@ private:
7879
QMap<QString, {{prefix}}HttpFileElement> files;
7980
QMap<QString, QByteArray *> multiPartFields;
8081
QString workingDirectory;
81-
int _timeOut;
82+
QTimer timeOutTimer;
8283
bool isResponseCompressionEnabled;
8384
bool isRequestCompressionEnabled;
8485
void on_manager_timeout(QNetworkReply *reply);

samples/client/petstore/cpp-qt5/client/PFXHttpRequest.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ void PFXHttpRequestInput::add_file(QString variable_name, QString local_filename
5252
}
5353

5454
PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent)
55-
: QObject(parent), manager(nullptr), _timeOut(0) {
55+
: QObject(parent), manager(nullptr), timeOutTimer(this) {
5656
qsrand(QDateTime::currentDateTime().toTime_t());
5757
manager = new QNetworkAccessManager(this);
5858
workingDirectory = QDir::currentPath();
5959
connect(manager, &QNetworkAccessManager::finished, this, &PFXHttpRequestWorker::on_manager_finished);
60+
timeOutTimer.setSingleShot(true);
6061
}
6162

6263
PFXHttpRequestWorker::~PFXHttpRequestWorker() {
64+
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
65+
timeOutTimer.stop();
6366
for (const auto &item : multiPartFields) {
6467
if (item != nullptr) {
6568
delete item;
@@ -93,8 +96,11 @@ QByteArray *PFXHttpRequestWorker::getMultiPartField(const QString &fieldname) {
9396
return nullptr;
9497
}
9598

96-
void PFXHttpRequestWorker::setTimeOut(int timeOut) {
97-
_timeOut = timeOut;
99+
void PFXHttpRequestWorker::setTimeOut(int timeOutMs) {
100+
timeOutTimer.setInterval(timeOutMs);
101+
if(timeOutTimer.interval() == 0) {
102+
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
103+
}
98104
}
99105

100106
void PFXHttpRequestWorker::setWorkingDirectory(const QString &path) {
@@ -354,12 +360,17 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
354360
buffer->setParent(reply);
355361
#endif
356362
}
357-
if (_timeOut > 0) {
358-
QTimer::singleShot(_timeOut, [=]() { on_manager_timeout(reply); });
363+
if (timeOutTimer.interval() > 0) {
364+
QObject::connect(&timeOutTimer, &QTimer::timeout, [=]() { on_manager_timeout(reply); });
365+
timeOutTimer.start();
359366
}
360367
}
361368

362369
void PFXHttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
370+
if(timeOutTimer.isActive()) {
371+
QObject::disconnect(&timeOutTimer, &QTimer::timeout, nullptr, nullptr);
372+
timeOutTimer.stop();
373+
}
363374
error_type = reply->error();
364375
error_str = reply->errorString();
365376
if (reply->rawHeaderPairs().count() > 0) {

samples/client/petstore/cpp-qt5/client/PFXHttpRequest.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QNetworkReply>
2424
#include <QObject>
2525
#include <QString>
26+
#include <QTimer>
2627

2728
#include "PFXHttpFileElement.h"
2829

@@ -67,7 +68,7 @@ class PFXHttpRequestWorker : public QObject {
6768
QString http_attribute_encode(QString attribute_name, QString input);
6869
void execute(PFXHttpRequestInput *input);
6970
static QSslConfiguration *sslDefaultConfiguration;
70-
void setTimeOut(int tout);
71+
void setTimeOut(int timeOutMs);
7172
void setWorkingDirectory(const QString &path);
7273
PFXHttpFileElement getHttpFileElement(const QString &fieldname = QString());
7374
QByteArray *getMultiPartField(const QString &fieldname = QString());
@@ -86,7 +87,7 @@ class PFXHttpRequestWorker : public QObject {
8687
QMap<QString, PFXHttpFileElement> files;
8788
QMap<QString, QByteArray *> multiPartFields;
8889
QString workingDirectory;
89-
int _timeOut;
90+
QTimer timeOutTimer;
9091
bool isResponseCompressionEnabled;
9192
bool isRequestCompressionEnabled;
9293
void on_manager_timeout(QNetworkReply *reply);

0 commit comments

Comments
 (0)