-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttprequestworker.cpp
More file actions
338 lines (278 loc) · 10.7 KB
/
httprequestworker.cpp
File metadata and controls
338 lines (278 loc) · 10.7 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
#include "httprequestworker.h"
#include <QDateTime>
#include <QUrl>
#include <QFileInfo>
#include <QBuffer>
/*!
* \brief HttpRequestInput::HttpRequestInput() - konstruktor bezparametryczny w ciele wywołujący metode inicjalizacji.
*/
HttpRequestInput::HttpRequestInput() {
initialize();
}
/*!
* \brief HttpRequestInput::HttpRequestInput(QString v_url_str, QString v_http_method) - konstruktor parametryczny w ciele wywołujący metode inicjalizacji oraz ustawijący wartości początkowe zmiennych.
*
* - v_url_str - zmienna typu QString zawiera url.
* - v_http_method - zmienna typu QString zwiera metodę http.
*/
HttpRequestInput::HttpRequestInput(QString v_url_str, QString v_http_method) {
initialize();
url_str = v_url_str;
http_method = v_http_method;
}
/*!
* \brief HttpRequestInput::initialize() - metoda inicjalizująca parametry początkowe.
*/
void HttpRequestInput::initialize() {
var_layout = NOT_SET;
url_str = "";
http_method = "GET";
}
/*!
* \brief HttpRequestInput::add_var(QString key, QString value) - metoda dodająca pola zapytania.
*
* - key - zmienna typu QString zawierająca klucz.
* - value - zmienna typu QString zawierjąca wartość.
*/
void HttpRequestInput::add_var(QString key, QString value) {
vars[key] = value;
}
/*!
* \brief HttpRequestInput::add_file(QString variable_name, QString local_filename, QString request_filename, QString mime_type) - metoda ustawiająca pola z parametrami pliku.
*
* - variable_name - nazwa zmiennej typu QString.
* - local_filename - nazwa pliku typu QString.
* - request_filename - nazwa pliku do zapytania.
* - mime_type - typ kodowania zapytania.
*/
void HttpRequestInput::add_file(QString variable_name, QString local_filename, QString request_filename, QString mime_type) {
HttpRequestInputFileElement file;
file.variable_name = variable_name;
file.local_filename = local_filename;
file.request_filename = request_filename;
file.mime_type = mime_type;
files.append(file);
}
/*!
* \brief HttpRequestWorker::HttpRequestWorker(QObject *parent) - konstruktor parametryczny, wywołujący metode inicjalizacji oraz ustawijący wartości poczatkowe zmiennych.
*
* - parent - wskaźnik na obiek nadrzędny.
*/
HttpRequestWorker::HttpRequestWorker(QObject *parent):QObject(parent), manager(NULL)
{
qsrand(QDateTime::currentDateTime().toTime_t());
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(on_manager_finished(QNetworkReply*)));
}
/*!
* \brief HttpRequestWorker::http_attribute_encode(QString attribute_name, QString input) - metoda dekodowania odpowiedzi.
*
* - attribute_name - zmienna typu QString przechowująca atrybut.
* - input - zmienna typu QString przechowująca odpowiedź.
*/
QString 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);
}
/*!
* \brief HttpRequestWorker::execute(HttpRequestInput *input) - metoda wykonująca zapytanie.
*
* - input - wskaźnik na obiekt klasy HttpRequestInput.
*/
void HttpRequestWorker::execute(HttpRequestInput *input) {
// reset variables
QByteArray request_content = "";
response = "";
error_type = QNetworkReply::NoError;
error_str = "";
// 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;
foreach (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::number(QDateTime::currentDateTime().toTime_t())
+ QString::number(qrand());
QString boundary_delimiter = "--";
QString new_line = "\r\n";
// add variables
foreach (QString key, input->vars.keys()) {
// add boundary
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(new_line);
// add header
request_content.append("Content-Disposition: form-data; ");
request_content.append(http_attribute_encode("name", key));
request_content.append(new_line);
request_content.append("Content-Type: text/plain");
request_content.append(new_line);
// add header to body splitter
request_content.append(new_line);
// add variable content
request_content.append(input->vars.value(key));
request_content.append(new_line);
}
// add files
for (QList<HttpRequestInputFileElement>::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 == NULL || file_info->local_filename.isEmpty()
|| file_info->variable_name == NULL || 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 == NULL || 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);
request_content.append(boundary);
request_content.append(new_line);
// 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)
));
request_content.append(new_line);
if (file_info->mime_type != NULL && !file_info->mime_type.isEmpty()) {
request_content.append("Content-Type: ");
request_content.append(file_info->mime_type);
request_content.append(new_line);
}
request_content.append("Content-Transfer-Encoding: binary");
request_content.append(new_line);
// add header to body splitter
request_content.append(new_line);
// add file content
request_content.append(file.readAll());
request_content.append(new_line);
file.close();
}
// add end of body
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(boundary_delimiter);
}
// prepare connection
QNetworkRequest request = QNetworkRequest(QUrl(input->url_str));
request.setRawHeader("User-Agent", "Agent name goes here");
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 (input->http_method == "GET") {
manager->get(request);
}
else if (input->http_method == "POST") {
manager->post(request, request_content);
}
else if (input->http_method == "PUT") {
manager->put(request, request_content);
}
else if (input->http_method == "HEAD") {
manager->head(request);
}
else if (input->http_method == "DELETE") {
manager->deleteResource(request);
}
else {
QBuffer buff(&request_content);
manager->sendCustomRequest(request, input->http_method.toLatin1(), &buff);
}
}
/*!
* \brief HttpRequestWorker::on_manager_finished(QNetworkReply *reply) - metoda wychwytująca błędy i zakończająca zapytanie.
*
* - reply - wskaźnik obiekt klasy QNetworkReply.
*/
void HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
error_type = reply->error();
if (error_type == QNetworkReply::NoError) {
response = reply->readAll();
}
else {
error_str = reply->errorString();
}
reply->deleteLater();
emit on_execution_finished(this);
}