mirror-chatterino2/src/common/NetworkPrivate.cpp

257 lines
6.8 KiB
C++
Raw Normal View History

2019-08-20 20:27:16 +02:00
#include "common/NetworkPrivate.hpp"
2019-08-20 20:08:49 +02:00
#include "common/NetworkManager.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "debug/Log.hpp"
#include "singletons/Paths.hpp"
#include "util/DebugCount.hpp"
2019-08-20 20:08:49 +02:00
#include "util/PostToThread.hpp"
#include <QCryptographicHash>
#include <QFile>
2019-08-20 20:08:49 +02:00
#include <QtConcurrent>
namespace chatterino {
NetworkData::NetworkData()
2019-08-20 23:30:39 +02:00
: timer_(new QTimer())
{
2019-08-20 23:30:39 +02:00
timer_->setSingleShot(true);
DebugCount::increase("NetworkData");
}
NetworkData::~NetworkData()
{
2019-08-20 23:30:39 +02:00
this->timer_->deleteLater();
DebugCount::decrease("NetworkData");
}
QString NetworkData::getHash()
{
2019-09-14 23:23:09 +02:00
static std::mutex mu;
std::lock_guard lock(mu);
2018-10-21 13:43:02 +02:00
if (this->hash_.isEmpty())
{
QByteArray bytes;
bytes.append(this->request_.url().toString());
2018-10-21 13:43:02 +02:00
for (const auto &header : this->request_.rawHeaderList())
{
bytes.append(header);
}
2018-08-06 21:17:03 +02:00
QByteArray hashBytes(
QCryptographicHash::hash(bytes, QCryptographicHash::Sha256));
this->hash_ = hashBytes.toHex();
}
return this->hash_;
}
2019-08-20 22:06:27 +02:00
void writeToCache(const std::shared_ptr<NetworkData> &data,
const QByteArray &bytes)
{
2019-08-21 00:01:27 +02:00
if (data->cache_)
2018-10-21 13:43:02 +02:00
{
2019-08-20 22:06:27 +02:00
QtConcurrent::run([data, bytes] {
QFile cachedFile(getPaths()->cacheDirectory() + "/" +
data->getHash());
2019-08-20 22:06:27 +02:00
if (cachedFile.open(QIODevice::WriteOnly))
{
cachedFile.write(bytes);
}
});
}
}
2019-08-20 20:08:49 +02:00
void loadUncached(const std::shared_ptr<NetworkData> &data)
{
DebugCount::increase("http request started");
NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker;
worker->moveToThread(&NetworkManager::workerThread);
if (data->hasTimeout_)
{
2019-08-20 23:30:39 +02:00
data->timer_->setSingleShot(true);
data->timer_->start();
2019-08-20 20:08:49 +02:00
}
auto onUrlRequested = [data, worker]() mutable {
auto reply = [&]() -> QNetworkReply * {
switch (data->requestType_)
{
case NetworkRequestType::Get:
return NetworkManager::accessManager.get(data->request_);
case NetworkRequestType::Put:
return NetworkManager::accessManager.put(data->request_,
data->payload_);
case NetworkRequestType::Delete:
return NetworkManager::accessManager.deleteResource(
data->request_);
case NetworkRequestType::Post:
return NetworkManager::accessManager.post(data->request_,
data->payload_);
}
2019-08-20 23:54:45 +02:00
return nullptr;
2019-08-20 20:08:49 +02:00
}();
if (reply == nullptr)
{
log("Unhandled request type");
return;
}
2019-08-20 23:30:39 +02:00
if (data->timer_->isActive())
2019-08-20 20:08:49 +02:00
{
2019-08-20 23:30:39 +02:00
QObject::connect(data->timer_, &QTimer::timeout, worker,
2019-08-20 20:08:49 +02:00
[reply, data]() {
log("Aborted!");
reply->abort();
if (data->onError_)
{
data->onError_(-2);
}
});
}
if (data->onReplyCreated_)
{
data->onReplyCreated_(reply);
}
auto handleReply = [data, reply]() mutable {
2019-08-20 23:29:11 +02:00
if (data->hasCaller_ && !data->caller_.get())
{
return;
}
2019-08-20 20:08:49 +02:00
// TODO(pajlada): A reply was received, kill the timeout timer
if (reply->error() != QNetworkReply::NetworkError::NoError)
{
if (data->onError_)
{
data->onError_(reply->error());
}
return;
}
QByteArray bytes = reply->readAll();
2019-08-20 22:06:27 +02:00
writeToCache(data, bytes);
2019-08-20 20:08:49 +02:00
NetworkResult result(bytes);
DebugCount::increase("http request success");
// log("starting {}", data->request_.url().toString());
if (data->onSuccess_)
{
2019-08-21 00:01:27 +02:00
if (data->executeConcurrently_)
2019-08-20 20:08:49 +02:00
QtConcurrent::run(
[onSuccess = std::move(data->onSuccess_),
result = std::move(result)] { onSuccess(result); });
else
data->onSuccess_(result);
}
// log("finished {}", data->request_.url().toString());
reply->deleteLater();
};
QObject::connect(
reply, &QNetworkReply::finished, worker,
[data, handleReply, worker]() mutable {
2019-08-21 00:01:27 +02:00
if (data->executeConcurrently_ || isGuiThread())
2019-08-20 20:08:49 +02:00
{
handleReply();
delete worker;
}
else
{
postToThread(
[worker, cb = std::move(handleReply)]() mutable {
cb();
delete worker;
});
}
});
};
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
onUrlRequested);
emit requester.requestUrl();
}
2019-08-20 21:50:36 +02:00
// First tried to load cached, then uncached.
2019-08-20 20:08:49 +02:00
void loadCached(const std::shared_ptr<NetworkData> &data)
{
QFile cachedFile(getPaths()->cacheDirectory() + "/" + data->getHash());
if (!cachedFile.exists() || !cachedFile.open(QIODevice::ReadOnly))
{
// File didn't exist OR File could not be opened
loadUncached(data);
return;
}
else
{
// XXX: check if bytes is empty?
QByteArray bytes = cachedFile.readAll();
NetworkResult result(bytes);
if (data->onSuccess_)
{
2019-08-21 00:01:27 +02:00
if (data->executeConcurrently_ || isGuiThread())
2019-08-20 20:08:49 +02:00
{
// XXX: If outcome is Failure, we should invalidate the cache file
// somehow/somewhere
/*auto outcome =*/
2019-08-20 23:29:11 +02:00
if (data->hasCaller_ && !data->caller_.get())
{
return;
}
2019-08-20 20:08:49 +02:00
data->onSuccess_(result);
}
else
{
2019-08-20 23:29:11 +02:00
postToThread([data, result]() {
if (data->hasCaller_ && !data->caller_.get())
{
return;
}
data->onSuccess_(result);
});
2019-08-20 20:08:49 +02:00
}
}
2019-08-20 23:30:39 +02:00
}
2019-08-20 20:08:49 +02:00
}
void load(const std::shared_ptr<NetworkData> &data)
{
2019-08-21 00:01:27 +02:00
if (data->cache_)
2019-08-20 20:08:49 +02:00
{
QtConcurrent::run(loadCached, data);
}
else
{
loadUncached(data);
}
}
} // namespace chatterino