2018-07-07 13:08:57 +02:00
|
|
|
#include "common/NetworkData.hpp"
|
|
|
|
|
2019-08-20 20:08:49 +02:00
|
|
|
#include "common/NetworkManager.hpp"
|
|
|
|
#include "common/NetworkRequester.hpp"
|
|
|
|
#include "common/NetworkResult.hpp"
|
|
|
|
#include "common/NetworkWorker.hpp"
|
|
|
|
#include "common/Outcome.hpp"
|
|
|
|
#include "debug/AssertInGuiThread.hpp"
|
|
|
|
#include "debug/Log.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
2018-07-16 17:23:41 +02:00
|
|
|
#include "util/DebugCount.hpp"
|
2019-08-20 20:08:49 +02:00
|
|
|
#include "util/PostToThread.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QFile>
|
2019-08-20 20:08:49 +02:00
|
|
|
#include <QtConcurrent>
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
2018-07-16 17:23:41 +02:00
|
|
|
NetworkData::NetworkData()
|
|
|
|
{
|
|
|
|
DebugCount::increase("NetworkData");
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkData::~NetworkData()
|
|
|
|
{
|
|
|
|
DebugCount::decrease("NetworkData");
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
QString NetworkData::getHash()
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (this->hash_.isEmpty())
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
QByteArray bytes;
|
|
|
|
|
|
|
|
bytes.append(this->request_.url().toString());
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &header : this->request_.rawHeaderList())
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
bytes.append(header);
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
QByteArray hashBytes(
|
|
|
|
QCryptographicHash::hash(bytes, QCryptographicHash::Sha256));
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
this->hash_ = hashBytes.toHex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->hash_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkData::writeToCache(const QByteArray &bytes)
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (this->useQuickLoadCache_)
|
|
|
|
{
|
2018-08-19 16:20:14 +02:00
|
|
|
QFile cachedFile(getPaths()->cacheDirectory() + "/" + this->getHash());
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (cachedFile.open(QIODevice::WriteOnly))
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
cachedFile.write(bytes);
|
|
|
|
|
|
|
|
cachedFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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_)
|
|
|
|
{
|
|
|
|
data->timer_.setSingleShot(true);
|
|
|
|
data->timer_.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (reply == nullptr)
|
|
|
|
{
|
|
|
|
log("Unhandled request type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->timer_.isActive())
|
|
|
|
{
|
|
|
|
QObject::connect(&data->timer_, &QTimer::timeout, worker,
|
|
|
|
[reply, data]() {
|
|
|
|
log("Aborted!");
|
|
|
|
reply->abort();
|
|
|
|
if (data->onError_)
|
|
|
|
{
|
|
|
|
data->onError_(-2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->onReplyCreated_)
|
|
|
|
{
|
|
|
|
data->onReplyCreated_(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto handleReply = [data, reply]() mutable {
|
|
|
|
// 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();
|
|
|
|
data->writeToCache(bytes);
|
|
|
|
|
|
|
|
NetworkResult result(bytes);
|
|
|
|
|
|
|
|
DebugCount::increase("http request success");
|
|
|
|
// log("starting {}", data->request_.url().toString());
|
|
|
|
if (data->onSuccess_)
|
|
|
|
{
|
|
|
|
if (data->executeConcurrently)
|
|
|
|
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 {
|
|
|
|
if (data->executeConcurrently || isGuiThread())
|
|
|
|
{
|
|
|
|
handleReply();
|
|
|
|
|
|
|
|
delete worker;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
postToThread(
|
|
|
|
[worker, cb = std::move(handleReply)]() mutable {
|
|
|
|
cb();
|
|
|
|
delete worker;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
|
|
|
|
onUrlRequested);
|
|
|
|
|
|
|
|
emit requester.requestUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to load the cached file and loads fro
|
|
|
|
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_)
|
|
|
|
{
|
|
|
|
if (data->executeConcurrently || isGuiThread())
|
|
|
|
{
|
|
|
|
// XXX: If outcome is Failure, we should invalidate the cache file
|
|
|
|
// somehow/somewhere
|
|
|
|
/*auto outcome =*/
|
|
|
|
data->onSuccess_(result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
postToThread([data, result]() { data->onSuccess_(result); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace chatterino
|
|
|
|
}
|
|
|
|
|
|
|
|
void load(const std::shared_ptr<NetworkData> &data)
|
|
|
|
{
|
|
|
|
if (data->useQuickLoadCache_)
|
|
|
|
{
|
|
|
|
QtConcurrent::run(loadCached, data);
|
|
|
|
loadCached(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
loadUncached(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
} // namespace chatterino
|