-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractBodyHandler.cpp
More file actions
65 lines (52 loc) · 1.32 KB
/
AbstractBodyHandler.cpp
File metadata and controls
65 lines (52 loc) · 1.32 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
#include <httq/AbstractBodyHandler.h>
#include <httq/BodyReader.h>
#include <httq/Logger.h>
#include <QTimer>
namespace httq
{
AbstractBodyHandler::AbstractBodyHandler(const std::optional<int> &bodySize, const std::optional<int> &timeoutMs)
: AbstractHandler()
, mBodySize(bodySize)
{
if (!bodySize || !timeoutMs)
return;
QTimer *t = new QTimer(this);
t->setInterval(timeoutMs.value());
connect(t, &QTimer::timeout,
this, [this]()
{
logger()->debug(QStringLiteral("timeout"));
deleteLater();
});
t->start();
}
void AbstractBodyHandler::handle()
{
if (!mBodySize)
{
bodyHandle();
return;
}
BodyReader *br = new BodyReader(request()); // TODO: use body size
connect(br, &BodyReader::signalDone,
this, &AbstractBodyHandler::slotBodyHandle);
connect(br, &BodyReader::signalError,
this, &AbstractBodyHandler::slotBodyHandle);
connect(br, &BodyReader::signalError,
this, [this]()
{
logger()->debug(QStringLiteral("body reader failed"));
});
}
void AbstractBodyHandler::slotBodyHandle()
{
BodyReader *br = qobject_cast<BodyReader *>(sender()); // the sender is guaranteed to be of type BodyReader
mBody = std::move(br->body());
br->deleteLater();
bodyHandle();
}
const QByteArray &AbstractBodyHandler::body() const
{
return mBody;
}
}