-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequestWorker.cpp
More file actions
executable file
·485 lines (426 loc) · 15.2 KB
/
Copy pathHttpRequestWorker.cpp
File metadata and controls
executable file
·485 lines (426 loc) · 15.2 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
/****************************************************************************
**
** Copyright (C) 2007-2015 Speedovation
** Copyright (C) 2015 creativepulse.gr
** Copyright (C) qt-rest-client contributors
**
** A lightweight REST/HTTP client built on QNetworkAccessManager.
**
** Authors:
** creativepulse.gr
** https://github.com/codeyash
** https://github.com/fro0m
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** See the LICENSE file for details.
**
** All rights are reserved.
*/
#include "HttpRequestWorker.h"
#include <QBuffer>
#include <QDateTime>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QRandomGenerator>
#include <QTimer>
#include <QUrl>
HttpRequestInput::HttpRequestInput(const QString& url, HttpMethod method)
: urlStr(url), httpMethod(method)
{
}
void HttpRequestInput::addVar(const QString& key, const QByteArray& value)
{
vars[key] = value;
}
void HttpRequestInput::addHeader(const QByteArray& header, const QByteArray& value)
{
headers[header] = value;
}
void HttpRequestInput::addFile(const QString& variableName, const QString& localFilename,
const QString& requestFilename, const QByteArray& mimeType)
{
HttpRequestInputFilePathElement file;
file.variableName = variableName;
file.localFilename = localFilename;
file.requestFilename = requestFilename;
file.mimeType = mimeType;
files.append(file);
}
void HttpRequestInput::addFile(const QString& variableName,
const QByteArray& localFileData,
const QString& requestFilename, const QByteArray& mimeType)
{
HttpRequestInputFileDataElement file;
file.variableName = variableName;
file.localFileData = localFileData;
file.requestFilename = requestFilename;
file.mimeType = mimeType;
filesData.append(file);
}
HttpRequestWorker::HttpRequestWorker(QObject* parent)
: QObject(parent), manager(new QNetworkAccessManager(this))
{
connect(manager, &QNetworkAccessManager::finished,
this, &HttpRequestWorker::managerFinished);
}
HttpRequestWorker::~HttpRequestWorker() = default;
QByteArray HttpRequestWorker::httpAttributeEncode(const QString& attributeName,
const QString& input)
{
// result structure follows RFC 5987
// Iterate over UTF-16 code points; percent-encode anything that isn't a
// bare ASCII attr-char so the comparison ranges are never applied to a
// signed char (which would be UB for bytes >= 0x80).
bool needUtfEncoding = false;
QString simple;
simple.reserve(input.size());
for (const QChar ch : input)
{
const ushort uc = ch.unicode();
if (uc == 0)
{
needUtfEncoding = true;
}
else if (uc < 0x20 || uc > 0x7e)
{
needUtfEncoding = true;
}
else if (uc == '"')
{
simple += QStringLiteral("\\\"");
}
else
{
simple += ch;
}
}
if (simple.isEmpty())
needUtfEncoding = true;
if (!needUtfEncoding)
return QString::fromLatin1("%1=\"%2\"").arg(attributeName, simple).toLatin1();
// Build the percent-encoded UTF-8 form for the extended syntax.
const QByteArray utf8 = input.toUtf8();
QString encoded;
encoded.reserve(static_cast<int>(utf8.size() * 3));
for (const unsigned char b : utf8)
{
const char c = static_cast<char>(b);
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
encoded += QLatin1Char(c);
}
else
{
encoded += QLatin1Char('%');
encoded += QString::number(b, 16).toUpper().rightJustified(2, '0');
}
}
return QString::fromLatin1("%1=\"%2\"; %1*=utf-8''%3")
.arg(attributeName, simple, encoded)
.toLatin1();
}
void HttpRequestWorker::buildUrlEncoded(QByteArray& body, HttpRequestInput* input, bool asAddress)
{
if (input->vars.isEmpty())
return;
body.reserve(body.size() + input->vars.size() * 16);
bool first = true;
for (auto it = input->vars.cbegin(); it != input->vars.cend(); ++it)
{
if (!first)
body.append('&');
first = false;
body.append(QUrl::toPercentEncoding(it.key()));
body.append('=');
body.append(QUrl::toPercentEncoding(it.value()));
}
if (asAddress)
{
input->urlStr += '?' + QString::fromLatin1(body);
body.clear();
}
}
void HttpRequestWorker::appendMultipartFile(QByteArray& body, const QByteArray& boundary,
const QString& variableName,
const QString& requestFilename,
const QByteArray& mimeType,
const QByteArray& content)
{
const QByteArray boundaryDelimiter = "--";
const QByteArray newLine = "\r\n";
body.append(boundaryDelimiter);
body.append(boundary);
body.append(newLine);
body.append(QString("Content-Disposition: form-data; %1; %2")
.arg(QString::fromLatin1(httpAttributeEncode("name", variableName)),
QString::fromLatin1(httpAttributeEncode("filename", requestFilename)))
.toLatin1());
body.append(newLine);
if (!mimeType.isNull() && !mimeType.isEmpty())
{
body.append("Content-Type: ");
body.append(mimeType);
body.append(newLine);
}
body.append("Content-Transfer-Encoding: binary");
body.append(newLine);
body.append(newLine);
body.append(content);
body.append(newLine);
}
void HttpRequestWorker::buildMultipart(QByteArray& body, const QByteArray& boundary,
HttpRequestInput* input)
{
const QByteArray boundaryDelimiter = "--";
const QByteArray newLine = "\r\n";
// rough size hint so the body isn't reallocated on every append
qint64 hint = input->vars.size() * 128;
for (const auto& f : input->files)
hint += QFileInfo(f.localFilename).size();
for (const auto& f : input->filesData)
hint += f.localFileData.size();
if (hint > 0)
body.reserve(static_cast<int>(qMin(hint, qint64(INT_MAX))));
// add variables
for (auto it = input->vars.cbegin(); it != input->vars.cend(); ++it)
{
body.append(boundaryDelimiter);
body.append(boundary);
body.append(newLine);
//! The Content-Disposition response-header field has been proposed
//! as a means for the origin server to suggest a default filename
//! if the user requests that the content is saved to a file. This
//! usage is derived from the definition of Content-Disposition
//! in RFC 1806
body.append("Content-Disposition: form-data; ");
body.append(httpAttributeEncode("name", it.key()));
body.append(newLine);
body.append("Content-Type: text/plain");
body.append(newLine);
body.append(newLine);
body.append(it.value());
body.append(newLine);
}
// add files referenced by path
for (const HttpRequestInputFilePathElement& fileInfo : input->files)
{
QFileInfo fi(fileInfo.localFilename);
if (fileInfo.localFilename.isEmpty() || fileInfo.variableName.isEmpty() ||
!fi.exists() || !fi.isFile() || !fi.isReadable())
{
qDebug() << "problem with file in request";
continue;
}
QFile file(fileInfo.localFilename);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "could not open file in request:" << fileInfo.localFilename;
continue;
}
QString requestFilename = fileInfo.requestFilename;
if (requestFilename.isEmpty())
{
requestFilename = fi.fileName();
if (requestFilename.isEmpty())
requestFilename = QStringLiteral("file");
}
appendMultipartFile(body, boundary, fileInfo.variableName, requestFilename,
fileInfo.mimeType, file.readAll());
file.close();
}
// add files supplied as raw data
for (const HttpRequestInputFileDataElement& fileInfo : input->filesData)
{
if (fileInfo.localFileData.isEmpty() || fileInfo.variableName.isEmpty())
{
qDebug() << "problem with file data in request";
continue;
}
QString requestFilename = fileInfo.requestFilename;
if (requestFilename.isEmpty())
requestFilename = QStringLiteral("file");
appendMultipartFile(body, boundary, fileInfo.variableName, requestFilename,
fileInfo.mimeType, fileInfo.localFileData);
}
// closing boundary
body.append(boundaryDelimiter);
body.append(boundary);
body.append(boundaryDelimiter);
}
void HttpRequestWorker::buildJson(QByteArray& body, HttpRequestInput* input)
{
// Each variable value is parsed as JSON; if it is a valid object or
// array it is embedded as-is, otherwise it is stored as a JSON string.
// This means "123" becomes a number and "{\"a\":1}" becomes an object —
// callers that want literal strings for such values should pre-encode.
QJsonObject obj;
for (auto it = input->vars.cbegin(); it != input->vars.cend(); ++it)
{
const QJsonDocument doc = QJsonDocument::fromJson(it.value());
if (doc.isObject())
obj[it.key()] = doc.object();
else if (doc.isArray())
obj[it.key()] = doc.array();
else
obj[it.key()] = QString::fromUtf8(it.value());
}
QJsonDocument doc;
doc.setObject(obj);
body.append(doc.toJson(QJsonDocument::Compact));
}
QNetworkReply* HttpRequestWorker::dispatch(QNetworkRequest& request, const QByteArray& body,
HttpRequestInput* input)
{
QNetworkReply* reply = nullptr;
switch (input->httpMethod)
{
case HttpMethod::Get:
reply = manager->get(request);
break;
case HttpMethod::Post:
reply = manager->post(request, body);
break;
case HttpMethod::Put:
reply = manager->put(request, body);
break;
case HttpMethod::Head:
reply = manager->head(request);
break;
case HttpMethod::Delete:
reply = manager->deleteResource(request);
break;
case HttpMethod::Custom:
{
// The buffer must outlive the call; parent it to the reply so it is
// destroyed together with the reply rather than at scope exit.
QBuffer* buffer = new QBuffer;
buffer->setData(body);
buffer->open(QIODevice::ReadOnly);
reply = manager->sendCustomRequest(request, input->customMethod, buffer);
if (reply)
buffer->setParent(reply);
else
delete buffer;
break;
}
}
Q_ASSERT_X(reply, "HttpRequestWorker::dispatch",
"QNetworkAccessManager returned a null reply");
return reply;
}
QNetworkReply::NetworkError HttpRequestWorker::execute(HttpRequestInput* input)
{
Q_ASSERT_X(input, "HttpRequestWorker::execute", "input must not be null");
// reset result fields
response.clear();
errorType = QNetworkReply::NoError;
errorStr.clear();
statusCode = 0;
// decide on the variable layout
if (input->files.size() > 0 || input->filesData.size() > 0)
{
input->varLayout = HttpRequestVarLayout::Multipart;
}
else if (input->varLayout == HttpRequestVarLayout::NotSet)
{
input->varLayout = (input->httpMethod == HttpMethod::Get ||
input->httpMethod == HttpMethod::Head)
? HttpRequestVarLayout::Address
: HttpRequestVarLayout::UrlEncoded;
}
// build the request body
QByteArray body;
QByteArray boundary;
switch (input->varLayout)
{
case HttpRequestVarLayout::Address:
buildUrlEncoded(body, input, /*asAddress=*/true);
break;
case HttpRequestVarLayout::UrlEncoded:
buildUrlEncoded(body, input, /*asAddress=*/false);
break;
case HttpRequestVarLayout::Multipart:
{
// random-ish boundary; the leading -- is part of the delimiter, the
// extra dashes just make it unlikely to collide with body content
boundary = "-------------------------" +
QString::number(QDateTime::currentSecsSinceEpoch()).toLatin1() +
QString::number(QRandomGenerator::system()->generate()).toLatin1();
buildMultipart(body, boundary, input);
break;
}
case HttpRequestVarLayout::Json:
buildJson(body, input);
break;
case HttpRequestVarLayout::NotSet:
break;
}
// prepare the connection
const QUrl url(input->urlStr);
if (!url.isValid())
{
errorType = QNetworkReply::UnknownNetworkError;
errorStr = QStringLiteral("Invalid URL: ") + input->urlStr;
QTimer::singleShot(0, this, [this] { emit executionFinished(this); });
return errorType;
}
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("qt-rest-client"));
switch (input->varLayout)
{
case HttpRequestVarLayout::UrlEncoded:
request.setHeader(QNetworkRequest::ContentTypeHeader,
QStringLiteral("application/x-www-form-urlencoded"));
break;
case HttpRequestVarLayout::Json:
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
break;
case HttpRequestVarLayout::Multipart:
request.setHeader(QNetworkRequest::ContentTypeHeader,
"multipart/form-data; boundary=" + boundary);
break;
default:
break;
}
// apply caller-supplied headers
for (auto it = input->headers.cbegin(); it != input->headers.cend(); ++it)
request.setRawHeader(it.key(), it.value());
QNetworkReply* reply = dispatch(request, body, input);
if (!reply)
{
errorType = QNetworkReply::UnknownNetworkError;
errorStr = QStringLiteral("Could not start the request");
emit executionFinished(this);
return errorType;
}
return reply->error();
}
void HttpRequestWorker::setReadResponseOnError(bool readResponseOnError)
{
m_readResponseOnError = readResponseOnError;
}
void HttpRequestWorker::setReadErrorString(bool readErrorString)
{
m_readErrorString = readErrorString;
}
void HttpRequestWorker::managerFinished(QNetworkReply* reply)
{
Q_ASSERT_X(reply, "HttpRequestWorker::managerFinished", "reply must not be null");
if (!reply)
{
emit executionFinished(this);
return;
}
statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
errorType = reply->error();
if (errorType == QNetworkReply::NoError || m_readResponseOnError)
response = reply->readAll();
if (m_readErrorString)
errorStr = reply->errorString();
reply->deleteLater();
emit executionFinished(this);
}