-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpRequest.cpp
executable file
·432 lines (312 loc) · 12.2 KB
/
HttpRequest.cpp
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
#include <httq/AbstractServer.h>
#include <httq/HttpRequest.h>
#include <httq/DataStream.h>
#include <http_parser.h>
#include <httq/Logger.h>
#include <QTcpSocket>
#include <QUrl>
#include <QUrlQuery>
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
namespace httq
{
HttpRequest::HttpRequest(QTcpSocket *cli, AbstractServer *parent)
: QObject(parent)
, mCli(cli)
, mLogger(parent->getLoggerFactory()->createLogger(this))
{
mLogger->debug(QStringLiteral("HttpRequest c_tor"));
mCli->setParent(this);
// TODO: init on first read
http_parser_init(&mParser, HTTP_REQUEST);
mData.mHttpRquest = this;
mParser.data = static_cast<void *>(&mData);
connect(cli, &QTcpSocket::readyRead,
this, &HttpRequest::slotReadyRead);
// connect(cli, &QTcpSocket::disconnected,
// this, &HttpRequest::deleteLater); the developer is responsible to always delete a HttpRequest after he is done working with it
// TODO: client timeout (or developer's responsibility?)
mParserSettings.on_message_begin = &HttpRequest::on_message_begin;
mParserSettings.on_url = &HttpRequest::on_url;
mParserSettings.on_status = &HttpRequest::on_status;
mParserSettings.on_header_field = &HttpRequest::on_header_field;
mParserSettings.on_header_value = &HttpRequest::on_header_value;
mParserSettings.on_headers_complete = &HttpRequest::on_headers_complete;
mParserSettings.on_body = &HttpRequest::on_body;
mParserSettings.on_message_complete = &HttpRequest::on_message_complete;
mParserSettings.on_chunk_header = &HttpRequest::on_chunk_header;
mParserSettings.on_chunk_complete = &HttpRequest::on_chunk_complete;
}
HttpRequest::~HttpRequest()
{
mLogger->debug(QStringLiteral("~HttpRequest"));
if (mCli == nullptr)
return;
mLogger->debug(QStringLiteral("mCli flush & close"));
mCli->flush();
mCli->close();
}
QString HttpRequest::toString() const
{
return QStringLiteral("HttpRequest(url = '%1', method = %2)").arg(url().toString(), methodString());
}
int HttpRequest::on_message_begin(http_parser *parser)
{
dataLogger(parser)->debug(QStringLiteral("on_message_begin"));
return 0;
}
int HttpRequest::on_url(http_parser *parser, const char *at, size_t length)
{
dataLogger(parser)->debug(QStringLiteral("on_url '%1'").arg(QString::fromUtf8(at, length)));
// QString urlString(QString::fromUtf8(at, length));
//qWarning() << "urlString" << urlString;
http_parser_url parserUrl;
if (http_parser_parse_url(at, length, false, &parserUrl) != 0)
{
dataLogger(parser)->debug(QStringLiteral("could not determine url! %1").arg(QString::fromUtf8(at, length)));
return 1;
}
HttpRequestData *reqData = data(parser);
QUrl url(reqData->mUrl);
#define isUrlFieldSet(field) parserUrl.field_set & (1 << field)
if (isUrlFieldSet(UF_SCHEMA))
url.setScheme(QString::fromUtf8(at + parserUrl.field_data[UF_SCHEMA].off, parserUrl.field_data[UF_SCHEMA].len));
if (parserUrl.field_set & (1 << UF_HOST))
url.setHost(QString::fromUtf8(at + parserUrl.field_data[UF_HOST].off, parserUrl.field_data[UF_HOST].len));
if (parserUrl.field_set & (1 << UF_PORT))
{
bool k = false;
QString portString(QString::fromUtf8(at + parserUrl.field_data[UF_PORT].off, parserUrl.field_data[UF_PORT].len));
qint16 port = portString.toShort(&k);
if (!k)
{
dataLogger(parser)->warning(QStringLiteral("could not determine port! %1").arg(portString));
return 1;
}
url.setPort(static_cast<int>(port));
}
if (parserUrl.field_set & (1 << UF_PATH))
url.setPath(QString::fromUtf8(at + parserUrl.field_data[UF_PATH].off, parserUrl.field_data[UF_PATH].len));
if (isUrlFieldSet(UF_QUERY))
{
// Qt 5 doesn't provide a method to return a QUrlQuery, but a QString version only, so generate it one-time and save it
// TODO: I remember that there is indeed a way.
QUrlQuery q(QString::fromUtf8(at + parserUrl.field_data[UF_QUERY].off, parserUrl.field_data[UF_QUERY].len));
url.setQuery(q);
reqData->mQuery = q;
}
// if (isUrlFieldSet(UF_FRAGMENT))
// url.setFragment(QString::fromUtf8(at + parserUrl.field_data[UF_FRAGMENT].off, parserUrl.field_data[UF_FRAGMENT].len));
if (isUrlFieldSet(UF_USERINFO))
url.setUserInfo(QString::fromUtf8(at + parserUrl.field_data[UF_USERINFO].off, parserUrl.field_data[UF_USERINFO].len));
#undef isUrlFieldSet
dataLogger(parser)->debug(QStringLiteral("final url = %1").arg(url.toString()));
reqData->mUrl = url;
return 0;
}
int HttpRequest::on_status(http_parser *parser, const char */*at*/, size_t /*length*/)
{
dataLogger(parser)->debug(QStringLiteral("on_status"));
return 0;
}
int HttpRequest::on_header_field(http_parser *parser, const char *at, size_t length)
{
dataLogger(parser)->debug(QStringLiteral("on_header_field"));
data(parser)->mCurrentHeaderField = QString::fromUtf8(at, length);
return 0;
}
int HttpRequest::on_header_value(http_parser *parser, const char *at, size_t length)
{
dataLogger(parser)->debug(QStringLiteral("on_header_value"));
QString value(QString::fromUtf8(at, length));
HttpRequestData *reqdata = data(parser);
reqdata->mHeaders.insert(reqdata->mCurrentHeaderField, value);
if (reqdata->mCurrentHeaderField.toLower() == "host")
{
QStringList hostSplit(value.split(":")); // host header may also contain port (if non-standard port for scheme)
reqdata->mUrl.setHost(hostSplit.takeFirst());
if (hostSplit.count() == 1 /* 1 left - because takeFirst - means there is a port */)
{
bool k;
int port = hostSplit.takeFirst().toInt(&k);
if (!k)
{
dataLogger(parser)->error(QStringLiteral("header contains invalid port - host header was: '%1'").arg(value));
return -1;
}
reqdata->mUrl.setPort(port);
}
}
dataLogger(parser)->debug(QStringLiteral("added header: %1 = %2").arg(reqdata->mCurrentHeaderField, value));
reqdata->mCurrentHeaderField.clear(); // TODO: not really necessary
return 0;
}
int HttpRequest::on_headers_complete(http_parser *parser)
{
dataLogger(parser)->debug(QStringLiteral("on_headers_complete"));
return 0;
}
int HttpRequest::on_body(http_parser *parser, const char *at, size_t length)
{
dataLogger(parser)->debug(QStringLiteral("on_body"));
data(parser)->mBodyPartial = std::move(QByteArray(at, length));
//qWarning() << "BODY" << data(parser)->mBodyPartial;
data(parser)->mDone = true; // as the rest of the body is read from out side, the parser is done at this point!
return 0;
}
int HttpRequest::on_message_complete(http_parser *parser)
{
dataLogger(parser)->debug(QStringLiteral("on_message_complete"));
data(parser)->mDone = true;
return 0;
}
int HttpRequest::on_chunk_header(http_parser *parser)
{
dataLogger(parser)->debug(QStringLiteral("on_chunk_header"));
return 0;
}
int HttpRequest::on_chunk_complete(http_parser *parser)
{
dataLogger(parser)->debug(QStringLiteral("on_chunk_complete"));
return 0;
}
qint64 HttpRequest::availableBytes() const
{
return (mContentLength - alreadyRead);
}
DataStream *HttpRequest::createDataStreamToClient(int status, const QString &contentType, QIODevice *from, qint64 fileSize, qint64 bufferSize)
{
mLogger->debug(QStringLiteral("createDataStreamToClient"));
writeStatusHeader(status);
addHeader(QStringLiteral("Content-Type"), contentType);
addHeader(QStringLiteral("Content-Length"), QString::number(fileSize));
writeHeaders();
mCli->write("\r\n");
return new DataStream(from, mCli, {}, fileSize, bufferSize, mLogger->getLoggerFactory(), this);
}
DataStream *HttpRequest::createDataStreamFromClient(QIODevice *to, qint64 fileSize, qint64 bufferSize)
{
return new DataStream(mCli, to, mData.mBodyPartial, fileSize, bufferSize, mLogger->getLoggerFactory(), this);
}
//DataStream *HttpRequest::createDataStreamToFile(QIODevice *from, qint64 mBufferSize)
//{
// return new DataStream(from, mCli, mBufferSize, mData.mBodyPartial, this);
//}
void HttpRequest::write(int status, const QJsonValue &jsonValue, QJsonDocument::JsonFormat jsonFormat)
{
QJsonDocument doc;
if (jsonValue.isArray())
doc = QJsonDocument(jsonValue.toArray());
else
doc = QJsonDocument(jsonValue.toObject());
write(status, doc.toJson(jsonFormat), QStringLiteral("application/json"));
}
void HttpRequest::write(int status, const QByteArray &data, const QString &contentType)
{
mLogger->debug(QStringLiteral("write to client: status = %1, data (size) = %2, contentType = %3").arg(status).arg(data.size()).arg(contentType));
writeStatusHeader(status);
addHeader(QStringLiteral("Content-Type"), contentType);
addHeader(QStringLiteral("Content-Length"), QString::number(data.size()));
writeHeaders();
mCli->write("\r\n");
mCli->write(data);
}
void HttpRequest::writeStatusHeader(int status)
{
QString header(QStringLiteral("HTTP/1.1 %1 %2\r\n").arg(status).arg(QStringLiteral("OK") /* TODO: ! */));
mLogger->debug(QStringLiteral("write to client: header = %1").arg(header));
mCli->write(header.toUtf8());
}
void HttpRequest::addHeader(const QString &key, const QString &value)
{
mHeaders.insert(key, value);
}
void HttpRequest::writeHeaders()
{
QString headers;
QMapIterator<QString, QString> it(mHeaders);
while (it.hasNext())
{
it.next();
headers += QStringLiteral("%1: %2\r\n").arg(it.key(), it.value());
}
mLogger->debug(QStringLiteral("write to client: headers = %1").arg(headers));
mCli->write(headers.toUtf8());
}
void HttpRequest::slotReadyRead()
{
// read until the whole header is done
mLogger->debug(QStringLiteral("slotReadyRead"));
if (mDone)
{
mLogger->debug(QStringLiteral("already marked as done"));
return;
}
mCli->startTransaction(); // revertable, in the case that it's not a plain http request, but a ws request, which reads must be undone to be completely read again by the web socket server
//qWarning() << "ready read"));
while (mCli->bytesAvailable() && !mData.mDone)
{
mLogger->debug(QStringLiteral("bytes available"));
const QByteArray data(mCli->read(10000));//HTTP_MAX_HEADER_SIZE - 1024));
//const QByteArray data(mCli->readAll()); // TODO: TEMP!!!
int dataSize = data.size();
//qWarning() << "ready read - data" << dataSize << "avail" << mCli->bytesAvailable();
size_t size = http_parser_execute(&mParser, &mParserSettings, data.constData(), static_cast<size_t>(dataSize));
if (size < static_cast<size_t>(dataSize))
{
mLogger->error(QStringLiteral("http parser error: %1").arg(QString::fromUtf8(http_errno_name(HTTP_PARSER_ERRNO(&mParser)))));
mCli->commitTransaction(); // not a ws connection at this point, it is legitimate to read the data
deleteLater();
return;
}
}
if (mData.mDone)
{
mLogger->debug(QStringLiteral("now marked as done %1").arg(quint64(this)));
mDone = true;
if (!mData.mUrl.isValid())
{
mLogger->error(QStringLiteral("request url is invalid!"));
deleteLater();
return;
}
mLogger->debug(QStringLiteral("method = %1").arg(methodString()));
// find Upgrade
auto it = mData.mHeaders.constFind(QStringLiteral("Upgrade"));
if (it != mData.mHeaders.constEnd() && (*it).toLower() == QStringLiteral("websocket")) // ws!
{
mLogger->debug(QStringLiteral("Upgrade: ").arg(*it));
disconnect(mCli, &QTcpSocket::readyRead, nullptr, nullptr); // don't receive ready read events!
mCli->rollbackTransaction();
mCli->setParent(nullptr);
auto cli = mCli;
mCli = nullptr;
emit signalUpgrade(cli);
deleteLater();
return;
}
// find Content-Length
it = mData.mHeaders.constFind(QStringLiteral("Content-Length"));
if (it != mData.mHeaders.constEnd())
{
bool k = false;
mContentLength = (*it).toLongLong(&k);
if (!k)
{
mLogger->error(QStringLiteral("invalid content length: ").arg(*it));
deleteLater();
return;
}
}
alreadyRead = mData.mBodyPartial.size();
mLogger->debug(QStringLiteral("already read %1 / %2 bytes").arg(alreadyRead).arg(contentLength()));
mCli->commitTransaction(); // not a ws connection at this point, it is legitimate to read the data
emit signalReady();
return;
}
mLogger->debug(QStringLiteral("commit transaction"));
mCli->commitTransaction(); // not a ws connection at this point, it is legitimate to read the data
}
}