forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.cpp.mustache
More file actions
573 lines (506 loc) · 20.5 KB
/
HttpRequest.cpp.mustache
File metadata and controls
573 lines (506 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
{{>licenseInfo}}
#include <QBuffer>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QTimer>
#include <QUrl>
#include <QUuid>
#include <QtGlobal>
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
#define SKIP_EMPTY_PARTS Qt::SkipEmptyParts
#else
#define SKIP_EMPTY_PARTS QString::SkipEmptyParts
#endif{{#contentCompression}}
#include <zlib.h>{{/contentCompression}}
#include "{{prefix}}HttpRequest.h"
{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}
{{prefix}}HttpRequestInput::{{prefix}}HttpRequestInput() {
initialize();
}
{{prefix}}HttpRequestInput::{{prefix}}HttpRequestInput(QString v_url_str, QString v_http_method) {
initialize();
url_str = v_url_str;
http_method = v_http_method;
}
void {{prefix}}HttpRequestInput::initialize() {
var_layout = NOT_SET;
url_str = "";
http_method = "GET";
}
void {{prefix}}HttpRequestInput::add_var(QString key, QString value) {
vars[key] = value;
}
void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_filename, QString request_filename, QString mime_type) {
{{prefix}}HttpFileElement file;
file.variable_name = variable_name;
file.local_filename = local_filename;
file.request_filename = request_filename;
file.mime_type = mime_type;
files.append(file);
}
{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
randomGenerator = QRandomGenerator(QDateTime::currentDateTime().toSecsSinceEpoch());
#else
qsrand(QDateTime::currentDateTime().toTime_t());
#endif
if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
workingDirectory = QDir::currentPath();
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;
}
}
}
QMap<QString, QString> {{prefix}}HttpRequestWorker::getResponseHeaders() const {
return headers;
}
{{prefix}}HttpFileElement {{prefix}}HttpRequestWorker::getHttpFileElement(const QString &fieldname) {
if (!files.isEmpty()) {
if (fieldname.isEmpty()) {
return files.first();
} else if (files.contains(fieldname)) {
return files[fieldname];
}
}
return {{prefix}}HttpFileElement();
}
QByteArray *{{prefix}}HttpRequestWorker::getMultiPartField(const QString &fieldname) {
if (!multiPartFields.isEmpty()) {
if (fieldname.isEmpty()) {
return multiPartFields.first();
} else if (multiPartFields.contains(fieldname)) {
return multiPartFields[fieldname];
}
}
return nullptr;
}
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) {
if (!path.isEmpty()) {
workingDirectory = path;
}
}
void {{prefix}}HttpRequestWorker::setResponseCompressionEnabled(bool enable) {
isResponseCompressionEnabled = enable;
}
void {{prefix}}HttpRequestWorker::setRequestCompressionEnabled(bool enable) {
isRequestCompressionEnabled = enable;
}
int {{prefix}}HttpRequestWorker::getHttpResponseCode() const{
return httpResponseCode;
}
QString {{prefix}}HttpRequestWorker::http_attribute_encode(QString attribute_name, QString input) {
// result structure follows RFC 5987
bool need_utf_encoding = false;
QString result = "";
QByteArray input_c = input.toLocal8Bit();
char c;
for (int i = 0; i < input_c.length(); i++) {
c = input_c.at(i);
if (c == '\\' || c == '/' || c == '\0' || c < ' ' || c > '~') {
// ignore and request utf-8 version
need_utf_encoding = true;
} else if (c == '"') {
result += "\\\"";
} else {
result += c;
}
}
if (result.length() == 0) {
need_utf_encoding = true;
}
if (!need_utf_encoding) {
// return simple version
return QString("%1=\"%2\"").arg(attribute_name, result);
}
QString result_utf8 = "";
for (int i = 0; i < input_c.length(); i++) {
c = input_c.at(i);
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
result_utf8 += c;
} else {
result_utf8 += "%" + QString::number(static_cast<unsigned char>(input_c.at(i)), 16).toUpper();
}
}
// return enhanced version with UTF-8 support
return QString("%1=\"%2\"; %1*=utf-8''%3").arg(attribute_name, result, result_utf8);
}
void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
// reset variables
QNetworkReply *reply = nullptr;
QByteArray request_content = "";
response = "";
error_type = QNetworkReply::NoError;
error_str = "";
bool isFormData = false;
// decide on the variable layout
if (input->files.length() > 0) {
input->var_layout = MULTIPART;
}
if (input->var_layout == NOT_SET) {
input->var_layout = input->http_method == "GET" || input->http_method == "HEAD" ? ADDRESS : URL_ENCODED;
}
// prepare request content
QString boundary = "";
if (input->var_layout == ADDRESS || input->var_layout == URL_ENCODED) {
// variable layout is ADDRESS or URL_ENCODED
if (input->vars.count() > 0) {
bool first = true;
isFormData = true;
for (QString key : input->vars.keys()) {
if (!first) {
request_content.append("&");
}
first = false;
request_content.append(QUrl::toPercentEncoding(key));
request_content.append("=");
request_content.append(QUrl::toPercentEncoding(input->vars.value(key)));
}
if (input->var_layout == ADDRESS) {
input->url_str += "?" + request_content;
request_content = "";
}
}
} else {
// variable layout is MULTIPART
boundary = QString("__-----------------------%1%2")
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
.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
for (QString key : input->vars.keys()) {
// add boundary
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).toUtf8());
request_content.append(new_line.toUtf8());
request_content.append("Content-Type: text/plain");
request_content.append(new_line.toUtf8());
// add header to body splitter
request_content.append(new_line.toUtf8());
// add variable content
request_content.append(input->vars.value(key).toUtf8());
request_content.append(new_line.toUtf8());
}
// add files
for (QList<{{prefix}}HttpFileElement>::iterator file_info = input->files.begin(); file_info != input->files.end(); file_info++) {
QFileInfo fi(file_info->local_filename);
// ensure necessary variables are available
if (file_info->local_filename == nullptr
|| file_info->local_filename.isEmpty()
|| file_info->variable_name == nullptr
|| file_info->variable_name.isEmpty()
|| !fi.exists()
|| !fi.isFile()
|| !fi.isReadable()) {
// silent abort for the current file
continue;
}
QFile file(file_info->local_filename);
if (!file.open(QIODevice::ReadOnly)) {
// silent abort for the current file
continue;
}
// ensure filename for the request
if (file_info->request_filename == nullptr || file_info->request_filename.isEmpty()) {
file_info->request_filename = fi.fileName();
if (file_info->request_filename.isEmpty()) {
file_info->request_filename = "file";
}
}
// add boundary
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)).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.toUtf8());
request_content.append(new_line.toUtf8());
}
request_content.append("Content-Transfer-Encoding: binary");
request_content.append(new_line.toUtf8());
// add header to body splitter
request_content.append(new_line.toUtf8());
// add file content
request_content.append(file.readAll());
request_content.append(new_line.toUtf8());
file.close();
}
// add end of body
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(boundary_delimiter.toUtf8());
}
if (input->request_body.size() > 0) {
qDebug() << "got a request body";
request_content.clear();
if(!isFormData && (input->var_layout != MULTIPART) && isRequestCompressionEnabled){
request_content.append(compress(input->request_body, 7, {{prefix}}CompressionType::Gzip));
} else {
request_content.append(input->request_body);
}
}
// prepare connection
QNetworkRequest request = QNetworkRequest(QUrl(input->url_str));
if ({{prefix}}HttpRequestWorker::sslDefaultConfiguration != nullptr) {
request.setSslConfiguration(*{{prefix}}HttpRequestWorker::sslDefaultConfiguration);
}
request.setRawHeader("User-Agent", "{{httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{apiVersion}}/cpp-qt{{/httpUserAgent}}");
for (QString key : input->headers.keys()) { request.setRawHeader(key.toStdString().c_str(), input->headers.value(key).toStdString().c_str()); }
if (request_content.size() > 0 && !isFormData && (input->var_layout != MULTIPART)) {
if (!input->headers.contains("Content-Type")) {
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
} else {
request.setHeader(QNetworkRequest::ContentTypeHeader, input->headers.value("Content-Type"));
}
if(isRequestCompressionEnabled){
request.setRawHeader("Content-Encoding", "gzip");
}
} else if (input->var_layout == URL_ENCODED) {
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
} else if (input->var_layout == MULTIPART) {
request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + boundary);
}
if(isResponseCompressionEnabled){
request.setRawHeader("Accept-Encoding", "gzip");
} else {
request.setRawHeader("Accept-Encoding", "identity");
}
if (input->http_method == "GET") {
reply = manager->get(request);
} else if (input->http_method == "POST") {
reply = manager->post(request, request_content);
} else if (input->http_method == "PUT") {
reply = manager->put(request, request_content);
} else if (input->http_method == "HEAD") {
reply = manager->head(request);
} else if (input->http_method == "DELETE") {
reply = manager->deleteResource(request);
} else {
#if (QT_VERSION >= 0x050800)
reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content);
#else
QBuffer *buffer = new QBuffer;
buffer->setData(request_content);
buffer->open(QIODevice::ReadOnly);
reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer);
buffer->setParent(reply);
#endif
}
if (reply != nullptr) {
reply->setParent(this);
connect(reply, &QNetworkReply::finished, this, [this, reply] {
on_reply_finished(reply);
});
}
if (timeOutTimer.interval() > 0) {
QObject::connect(&timeOutTimer, &QTimer::timeout, this, [this, reply] {
on_reply_timeout(reply);
});
timeOutTimer.start();
}
}
void {{prefix}}HttpRequestWorker::on_reply_finished(QNetworkReply *reply) {
bool codeSts = false;
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) {
for (const auto &item : reply->rawHeaderPairs()) {
headers.insert(item.first, item.second);
}
}
auto rescode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(&codeSts);
if(codeSts){
httpResponseCode = rescode;
} else{
httpResponseCode = -1;
}
process_response(reply);
reply->deleteLater();
Q_EMIT on_execution_finished(this);
}
void {{prefix}}HttpRequestWorker::on_reply_timeout(QNetworkReply *reply) {
error_type = QNetworkReply::TimeoutError;
response = "";
error_str = "Timed out waiting for response";
disconnect(reply, nullptr, nullptr, nullptr);
reply->abort();
reply->deleteLater();
Q_EMIT on_execution_finished(this);
}
void {{prefix}}HttpRequestWorker::process_response(QNetworkReply *reply) {
QString contentDispositionHdr;
QString contentTypeHdr;
QString contentEncodingHdr;
for(auto hdr: getResponseHeaders().keys()){
if(hdr.compare(QString("Content-Disposition"), Qt::CaseInsensitive) == 0){
contentDispositionHdr = getResponseHeaders().value(hdr);
}
if(hdr.compare(QString("Content-Type"), Qt::CaseInsensitive) == 0){
contentTypeHdr = getResponseHeaders().value(hdr);
}
if(hdr.compare(QString("Content-Encoding"), Qt::CaseInsensitive) == 0){
contentEncodingHdr = getResponseHeaders().value(hdr);
}
}
if (!contentDispositionHdr.isEmpty()) {
auto contentDisposition = contentDispositionHdr.split(QString(";"), SKIP_EMPTY_PARTS);
auto contentType =
!contentTypeHdr.isEmpty() ? contentTypeHdr.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("="), SKIP_EMPTY_PARTS).at(1);
break;
}
}
{{prefix}}HttpFileElement felement;
felement.saveToFile(QString(), workingDirectory + QDir::separator() + filename, filename, contentType, reply->readAll());
files.insert(filename, felement);
}
} else if (!contentTypeHdr.isEmpty()) {
auto contentType = contentTypeHdr.split(QString(";"), SKIP_EMPTY_PARTS);
if ((contentType.count() > 0) && (contentType.first() == QString("multipart/form-data"))) {
// TODO : Handle Multipart responses
} else {
if(!contentEncodingHdr.isEmpty()){
auto encoding = contentEncodingHdr.split(QString(";"), SKIP_EMPTY_PARTS);
if(encoding.count() > 0){
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)){
response = reply->readAll();
}
}
}
else {
response = reply->readAll();
}
}
}
}
QByteArray {{prefix}}HttpRequestWorker::decompress(const QByteArray& data){
{{#contentCompression}}QByteArray result;
bool sts = false;
do{
z_stream strm{};
static const int CHUNK_SIZE = 8*1024;
char out[CHUNK_SIZE];
if (data.size() <= 4) {
break;
}
strm.avail_in = data.size();
strm.next_in = (Bytef*)(data.data());
if(Z_OK != inflateInit2(&strm, 15 + 32)){
break;
}
do {
sts = false;
strm.avail_out = CHUNK_SIZE;
strm.next_out = (Bytef*)(out);
if(inflate(&strm, Z_NO_FLUSH) < Z_OK){
break;
}
result.append(out, CHUNK_SIZE - strm.avail_out);
sts = true;
} while (strm.avail_out == 0);
inflateEnd(&strm);
} while(false);
return sts ? result : QByteArray();{{/contentCompression}}{{^contentCompression}}
Q_UNUSED(data);
return QByteArray();{{/contentCompression}}
}
QByteArray {{prefix}}HttpRequestWorker::compress(const QByteArray& input, int level, {{prefix}}CompressionType compressType) {
{{#contentCompression}}QByteArray output;
static const int GZIP_WINDOW_BIT = 15+16;
static const int ZLIB_WINDOW_BIT = 15;
static const int CHUNK_SIZE = 8*1024;
int windowBits;
if( compressType == {{prefix}}CompressionType::Gzip ){
windowBits = GZIP_WINDOW_BIT;
} else if ( compressType == {{prefix}}CompressionType::Zlib ){
windowBits = ZLIB_WINDOW_BIT;
}
do{
int flush = 0, ret = 0;
bool error_sts = false;
z_stream strm{};
if(input.length() <= 0) {
break;
}
if (deflateInit2(&strm, qMax(-1, qMin(9, level)), Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY) != Z_OK){
break;
}
output.clear();
auto input_data = input.data();
int input_data_left = input.length();
do {
int chunk_size = qMin(CHUNK_SIZE, input_data_left);
strm.next_in = (unsigned char*)input_data;
strm.avail_in = chunk_size;
input_data += chunk_size;
input_data_left -= chunk_size;
flush = (input_data_left <= 0 ? Z_FINISH : Z_NO_FLUSH);
do {
char out[CHUNK_SIZE];
strm.next_out = (unsigned char*)out;
strm.avail_out = CHUNK_SIZE;
ret = deflate(&strm, flush);
if(ret == Z_STREAM_ERROR) {
error_sts = true;
break;
}
auto have = (CHUNK_SIZE - strm.avail_out);
if(have > 0){
output.append((char*)out, have);
}
} while (strm.avail_out == 0);
} while ((flush != Z_FINISH) && !(error_sts));
deflateEnd(&strm);
} while(false);
return output;{{/contentCompression}}{{^contentCompression}}
Q_UNUSED(input);
Q_UNUSED(level);
Q_UNUSED(compressType);
return QByteArray();{{/contentCompression}}
}
QSslConfiguration *{{prefix}}HttpRequestWorker::sslDefaultConfiguration;
{{#cppNamespaceDeclarations}}
} // namespace {{this}}
{{/cppNamespaceDeclarations}}