mirror-chatterino2/src/common/NetworkRequest.hpp

281 lines
8.1 KiB
C++
Raw Normal View History

2018-01-19 22:45:33 +01:00
#pragma once
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2018-06-26 15:33:51 +02:00
#include "common/NetworkManager.hpp"
#include "common/NetworkRequester.hpp"
#include "common/NetworkWorker.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Paths.hpp"
2018-01-19 22:45:33 +01:00
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
2018-01-19 22:45:33 +01:00
#include <QCryptographicHash>
#include <QFile>
namespace chatterino {
static QJsonObject parseJSONFromData(const QByteArray &data)
{
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
if (jsonDoc.isNull()) {
return QJsonObject();
}
return jsonDoc.object();
}
static rapidjson::Document parseJSONFromData2(const QByteArray &data)
{
rapidjson::Document ret(rapidjson::kNullType);
rapidjson::ParseResult result = ret.Parse(data.data(), data.length());
if (result.Code() != rapidjson::kParseErrorNone) {
2018-06-26 17:06:17 +02:00
Log("JSON parse error: {} ({})", rapidjson::GetParseError_En(result.Code()),
2018-06-26 17:20:03 +02:00
result.Offset());
2018-01-19 22:45:33 +01:00
return ret;
}
return ret;
}
static rapidjson::Document parseJSONFromReply2(QNetworkReply *reply)
{
rapidjson::Document ret(rapidjson::kNullType);
if (reply->error() != QNetworkReply::NetworkError::NoError) {
return ret;
}
QByteArray data = reply->readAll();
rapidjson::ParseResult result = ret.Parse(data.data(), data.length());
if (result.Code() != rapidjson::kParseErrorNone) {
2018-06-26 17:06:17 +02:00
Log("JSON parse error: {} ({})", rapidjson::GetParseError_En(result.Code()),
2018-06-26 17:20:03 +02:00
result.Offset());
2018-01-19 22:45:33 +01:00
return ret;
}
return ret;
}
class NetworkRequest
{
public:
enum RequestType {
2018-05-12 19:50:22 +02:00
GetRequest,
PostRequest,
PutRequest,
DeleteRequest,
};
private:
2018-01-19 22:45:33 +01:00
struct Data {
QNetworkRequest request;
const QObject *caller = nullptr;
std::function<void(QNetworkReply *)> onReplyCreated;
int timeoutMS = -1;
bool useQuickLoadCache = false;
std::function<bool(int)> onError;
std::function<bool(const rapidjson::Document &)> onSuccess;
NetworkRequest::RequestType requestType;
QByteArray payload;
2018-01-19 22:45:33 +01:00
QString getHash()
{
if (this->hash.isEmpty()) {
QByteArray bytes;
bytes.append(this->request.url().toString());
for (const auto &header : this->request.rawHeaderList()) {
bytes.append(header);
}
QByteArray hashBytes(QCryptographicHash::hash(bytes, QCryptographicHash::Sha256));
this->hash = hashBytes.toHex();
}
return this->hash;
}
void writeToCache(const QByteArray &bytes);
private:
QString hash;
} data;
public:
NetworkRequest() = delete;
explicit NetworkRequest(const char *url);
explicit NetworkRequest(const std::string &url);
explicit NetworkRequest(const QString &url);
2018-07-06 17:56:11 +02:00
void setRequestType(RequestType newRequestType);
template <typename Func>
void onError(Func cb)
{
this->data.onError = cb;
}
template <typename Func>
void onSuccess(Func cb)
{
this->data.onSuccess = cb;
}
void setPayload(const QByteArray &payload)
{
this->data.payload = payload;
}
2018-01-19 22:45:33 +01:00
void setUseQuickLoadCache(bool value);
void setCaller(const QObject *caller);
2018-07-06 17:56:11 +02:00
void setOnReplyCreated(std::function<void(QNetworkReply *)> f);
void setRawHeader(const char *headerName, const char *value);
void setRawHeader(const char *headerName, const QByteArray &value);
void setRawHeader(const char *headerName, const QString &value);
void setTimeout(int ms);
void makeAuthorizedV5(const QString &clientID, const QString &oauthToken);
2018-01-19 22:45:33 +01:00
template <typename FinishedCallback>
void get(FinishedCallback onFinished)
{
if (this->data.useQuickLoadCache) {
auto app = getApp();
2018-01-19 22:45:33 +01:00
2018-06-21 13:02:34 +02:00
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data.getHash());
2018-01-19 22:45:33 +01:00
if (cachedFile.exists()) {
if (cachedFile.open(QIODevice::ReadOnly)) {
QByteArray bytes = cachedFile.readAll();
// qDebug() << "Loaded cached resource" << this->data.request.url();
2018-01-19 23:41:02 +01:00
bool success = onFinished(bytes);
2018-01-19 22:45:33 +01:00
cachedFile.close();
if (!success) {
// The images were not successfully loaded from the file
// XXX: Invalidate the cache file so we don't attempt to load it again next
// time
}
2018-01-19 22:45:33 +01:00
}
}
}
QTimer *timer = nullptr;
if (this->data.timeoutMS > 0) {
timer = new QTimer;
}
NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker;
worker->moveToThread(&NetworkManager::workerThread);
if (this->data.caller != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller,
2018-06-26 17:20:03 +02:00
[ onFinished, data = this->data ](auto reply) mutable {
2018-01-19 22:45:33 +01:00
if (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data.onError) {
data.onError(reply->error());
}
2018-01-19 22:45:33 +01:00
return;
}
QByteArray readBytes = reply->readAll();
QByteArray bytes;
bytes.setRawData(readBytes.data(), readBytes.size());
2018-01-19 22:45:33 +01:00
data.writeToCache(bytes);
2018-01-19 23:41:02 +01:00
onFinished(bytes);
2018-01-19 22:45:33 +01:00
reply->deleteLater();
});
}
if (timer != nullptr) {
timer->start(this->data.timeoutMS);
}
QObject::connect(
&requester, &NetworkRequester::requestUrl, worker,
2018-06-26 17:20:03 +02:00
[ timer, data = std::move(this->data), worker, onFinished{std::move(onFinished)} ]() {
2018-01-19 22:45:33 +01:00
QNetworkReply *reply = NetworkManager::NaM.get(data.request);
if (timer != nullptr) {
QObject::connect(timer, &QTimer::timeout, worker, [reply, timer]() {
2018-06-26 17:06:17 +02:00
Log("Aborted!");
2018-01-19 22:45:33 +01:00
reply->abort();
timer->deleteLater();
});
}
if (data.onReplyCreated) {
data.onReplyCreated(reply);
}
2018-06-26 17:20:03 +02:00
QObject::connect(reply, &QNetworkReply::finished, worker, [
data = std::move(data), worker, reply, onFinished = std::move(onFinished)
]() mutable {
if (data.caller == nullptr) {
QByteArray bytes = reply->readAll();
data.writeToCache(bytes);
onFinished(bytes);
reply->deleteLater();
} else {
emit worker->doneUrl(reply);
}
2018-06-26 17:20:03 +02:00
delete worker;
});
2018-01-19 22:45:33 +01:00
});
emit requester.requestUrl();
}
template <typename FinishedCallback>
void getJSON(FinishedCallback onFinished)
{
2018-06-26 17:20:03 +02:00
this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes)->bool {
2018-01-19 22:45:33 +01:00
auto object = parseJSONFromData(bytes);
onFinished(object);
// XXX: Maybe return onFinished? For now I don't want to force onFinished to have a
// return value
return true;
2018-01-19 22:45:33 +01:00
});
}
template <typename FinishedCallback>
void getJSON2(FinishedCallback onFinished)
{
2018-06-26 17:20:03 +02:00
this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes)->bool {
2018-01-19 22:45:33 +01:00
auto object = parseJSONFromData2(bytes);
onFinished(object);
// XXX: Maybe return onFinished? For now I don't want to force onFinished to have a
// return value
return true;
2018-01-19 22:45:33 +01:00
});
}
2018-07-06 17:56:11 +02:00
void execute();
private:
2018-07-06 17:56:11 +02:00
void useCache();
void doRequest();
void executeGet();
void executePut();
void executeDelete();
2018-05-16 15:42:45 +02:00
};
2018-01-19 22:45:33 +01:00
} // namespace chatterino