2018-06-26 15:33:51 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-01-19 22:45:33 +01:00
|
|
|
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/NetworkData.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "common/NetworkManager.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Outcome.hpp"
|
2019-08-03 11:20:19 +02:00
|
|
|
#include "common/Version.hpp"
|
2019-08-15 20:38:11 +02:00
|
|
|
#include "debug/AssertInGuiThread.hpp"
|
2018-08-07 23:46:00 +02:00
|
|
|
#include "debug/Log.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
|
|
|
#include "singletons/Paths.hpp"
|
2018-08-07 23:46:00 +02:00
|
|
|
#include "util/DebugCount.hpp"
|
2019-08-15 20:38:11 +02:00
|
|
|
#include "util/PostToThread.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
#include <QFile>
|
2018-08-10 18:56:17 +02:00
|
|
|
#include <QtConcurrent>
|
2018-08-19 16:20:14 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
#include <cassert>
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-01-19 22:45:33 +01:00
|
|
|
namespace chatterino {
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
NetworkRequest::NetworkRequest(const std::string &url,
|
|
|
|
NetworkRequestType requestType)
|
2018-07-07 13:51:01 +02:00
|
|
|
: data(new NetworkData)
|
|
|
|
, timer(new NetworkTimer)
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setUrl(QUrl(QString::fromStdString(url)));
|
|
|
|
this->data->requestType_ = requestType;
|
2019-08-03 11:20:19 +02:00
|
|
|
|
|
|
|
this->initializeDefaultValues();
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType)
|
2018-07-07 13:51:01 +02:00
|
|
|
: data(new NetworkData)
|
|
|
|
, timer(new NetworkTimer)
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setUrl(url);
|
|
|
|
this->data->requestType_ = requestType;
|
2019-08-03 11:20:19 +02:00
|
|
|
|
|
|
|
this->initializeDefaultValues();
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest::~NetworkRequest()
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 12:03:37 +02:00
|
|
|
// assert(this->executed_);
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void NetworkRequest::setRequestType(NetworkRequestType newRequestType)
|
2018-07-06 21:31:58 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->requestType_ = newRequestType;
|
2018-07-06 21:31:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void NetworkRequest::setCaller(const QObject *caller)
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->caller_ = caller;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb)
|
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->onReplyCreated_ = cb;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRequest::onError(NetworkErrorCallback cb)
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->onError_ = cb;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void NetworkRequest::onSuccess(NetworkSuccessCallback cb)
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->onSuccess_ = cb;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRequest::setRawHeader(const char *headerName, const char *value)
|
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setRawHeader(headerName, value);
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void NetworkRequest::setRawHeader(const char *headerName,
|
|
|
|
const QByteArray &value)
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setRawHeader(headerName, value);
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
|
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->request_.setRawHeader(headerName, value.toUtf8());
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRequest::setTimeout(int ms)
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
this->timer->timeoutMS_ = ms;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:56:17 +02:00
|
|
|
void NetworkRequest::setExecuteConcurrently(bool value)
|
|
|
|
{
|
|
|
|
this->data->executeConcurrently = value;
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void NetworkRequest::makeAuthorizedV5(const QString &clientID,
|
|
|
|
const QString &oauthToken)
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
|
|
|
this->setRawHeader("Client-ID", clientID);
|
|
|
|
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!oauthToken.isEmpty())
|
|
|
|
{
|
2018-07-06 22:00:03 +02:00
|
|
|
this->setRawHeader("Authorization", "OAuth " + oauthToken);
|
|
|
|
}
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void NetworkRequest::setPayload(const QByteArray &payload)
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->payload_ = payload;
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
void NetworkRequest::setUseQuickLoadCache(bool value)
|
2018-01-19 22:45:33 +01:00
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
this->data->useQuickLoadCache_ = value;
|
2018-01-19 22:45:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
void NetworkRequest::execute()
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
this->executed_ = true;
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
switch (this->data->requestType_)
|
|
|
|
{
|
|
|
|
case NetworkRequestType::Get:
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
// Get requests try to load from cache, then perform the request
|
2018-10-21 13:43:02 +02:00
|
|
|
if (this->data->useQuickLoadCache_)
|
|
|
|
{
|
|
|
|
if (this->tryLoadCachedFile())
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
// Successfully loaded from cache
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->doRequest();
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
case NetworkRequestType::Put:
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
// Put requests cannot be cached, therefore the request is called
|
|
|
|
// immediately
|
2018-07-07 13:08:57 +02:00
|
|
|
this->doRequest();
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
case NetworkRequestType::Delete:
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
// Delete requests cannot be cached, therefore the request is called
|
|
|
|
// immediately
|
2018-07-07 13:08:57 +02:00
|
|
|
this->doRequest();
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2019-01-20 16:28:25 +01:00
|
|
|
case NetworkRequestType::Post:
|
|
|
|
{
|
|
|
|
this->doRequest();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
default:
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("[Execute] Unhandled request type");
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:37:20 +02:00
|
|
|
QString NetworkRequest::urlString() const
|
|
|
|
{
|
|
|
|
return this->data->request_.url().toString();
|
|
|
|
}
|
|
|
|
|
2019-08-03 11:20:19 +02:00
|
|
|
void NetworkRequest::initializeDefaultValues()
|
|
|
|
{
|
|
|
|
const auto userAgent = QString("chatterino/%1 (%2)")
|
|
|
|
.arg(CHATTERINO_VERSION, CHATTERINO_GIT_HASH)
|
|
|
|
.toUtf8();
|
|
|
|
|
|
|
|
this->data->request_.setRawHeader("User-Agent", userAgent);
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
Outcome NetworkRequest::tryLoadCachedFile()
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-08-19 16:20:14 +02:00
|
|
|
QFile cachedFile(getPaths()->cacheDirectory() + "/" +
|
|
|
|
this->data->getHash());
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!cachedFile.exists())
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
// File didn't exist
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!cachedFile.open(QIODevice::ReadOnly))
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
// File could not be opened
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
QByteArray bytes = cachedFile.readAll();
|
|
|
|
NetworkResult result(bytes);
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
auto outcome = this->data->onSuccess_(result);
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
cachedFile.close();
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
// XXX: If success is false, we should invalidate the cache file
|
|
|
|
// somehow/somewhere
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
return outcome;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkRequest::doRequest()
|
|
|
|
{
|
2018-08-07 23:46:00 +02:00
|
|
|
DebugCount::increase("http request started");
|
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
NetworkRequester requester;
|
|
|
|
NetworkWorker *worker = new NetworkWorker;
|
|
|
|
|
|
|
|
worker->moveToThread(&NetworkManager::workerThread);
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
this->timer->start();
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
auto onUrlRequested = [data = this->data, timer = this->timer,
|
|
|
|
worker]() mutable {
|
2018-07-16 17:23:41 +02:00
|
|
|
auto reply = [&]() -> QNetworkReply * {
|
2018-10-21 13:43:02 +02:00
|
|
|
switch (data->requestType_)
|
|
|
|
{
|
2018-07-16 17:23:41 +02:00
|
|
|
case NetworkRequestType::Get:
|
2018-08-10 19:00:14 +02:00
|
|
|
return NetworkManager::accessManager.get(data->request_);
|
2018-07-16 17:23:41 +02:00
|
|
|
|
|
|
|
case NetworkRequestType::Put:
|
2018-08-10 19:00:14 +02:00
|
|
|
return NetworkManager::accessManager.put(data->request_,
|
2018-08-11 14:20:53 +02:00
|
|
|
data->payload_);
|
2018-07-16 17:23:41 +02:00
|
|
|
|
|
|
|
case NetworkRequestType::Delete:
|
2018-08-11 14:20:53 +02:00
|
|
|
return NetworkManager::accessManager.deleteResource(
|
|
|
|
data->request_);
|
2018-07-16 17:23:41 +02:00
|
|
|
|
2019-01-20 16:28:25 +01:00
|
|
|
case NetworkRequestType::Post:
|
|
|
|
return NetworkManager::accessManager.post(data->request_,
|
|
|
|
data->payload_);
|
|
|
|
|
2018-07-16 17:23:41 +02:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}();
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (reply == nullptr)
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("Unhandled request type");
|
2018-07-07 13:08:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (timer->isStarted())
|
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
timer->onTimeout(worker, [reply, data]() {
|
2018-08-11 14:20:53 +02:00
|
|
|
log("Aborted!");
|
2018-07-07 13:08:57 +02:00
|
|
|
reply->abort();
|
2018-10-21 13:43:02 +02:00
|
|
|
if (data->onError_)
|
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
data->onError_(-2);
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (data->onReplyCreated_)
|
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
data->onReplyCreated_(reply);
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 15:50:05 +02:00
|
|
|
auto handleReply = [data, timer, reply]() mutable {
|
|
|
|
// TODO(pajlada): A reply was received, kill the timeout timer
|
2018-10-21 13:43:02 +02:00
|
|
|
if (reply->error() != QNetworkReply::NetworkError::NoError)
|
|
|
|
{
|
|
|
|
if (data->onError_)
|
|
|
|
{
|
2018-07-07 13:51:01 +02:00
|
|
|
data->onError_(reply->error());
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-14 14:24:18 +02:00
|
|
|
QByteArray bytes = reply->readAll();
|
2018-07-07 13:51:01 +02:00
|
|
|
data->writeToCache(bytes);
|
2018-07-07 13:08:57 +02:00
|
|
|
|
|
|
|
NetworkResult result(bytes);
|
2018-08-07 23:46:00 +02:00
|
|
|
|
|
|
|
DebugCount::increase("http request success");
|
2018-08-11 14:20:53 +02:00
|
|
|
// log("starting {}", data->request_.url().toString());
|
2018-10-21 13:43:02 +02:00
|
|
|
if (data->onSuccess_)
|
|
|
|
{
|
2018-08-10 18:56:17 +02:00
|
|
|
if (data->executeConcurrently)
|
|
|
|
QtConcurrent::run(
|
|
|
|
[onSuccess = std::move(data->onSuccess_),
|
|
|
|
result = std::move(result)] { onSuccess(result); });
|
|
|
|
else
|
|
|
|
data->onSuccess_(result);
|
|
|
|
}
|
2018-08-11 14:20:53 +02:00
|
|
|
// log("finished {}", data->request_.url().toString());
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
};
|
|
|
|
|
2019-08-13 14:33:16 +02:00
|
|
|
// FOURTF: Not sure what this does but it doesn't work.
|
|
|
|
// if (data->caller_ != nullptr)
|
|
|
|
// {
|
|
|
|
// QObject::connect(worker, &NetworkWorker::doneUrl, data->caller_,
|
|
|
|
// handleReply);
|
|
|
|
// QObject::connect(reply, &QNetworkReply::finished, worker,
|
|
|
|
// [worker]() mutable {
|
|
|
|
// emit worker->doneUrl();
|
|
|
|
|
|
|
|
// delete worker;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
|
2019-08-15 20:38:11 +02:00
|
|
|
// auto
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-07-07 13:08:57 +02:00
|
|
|
};
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
|
|
|
|
onUrlRequested);
|
2018-07-06 17:56:11 +02:00
|
|
|
|
|
|
|
emit requester.requestUrl();
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
// Helper creator functions
|
|
|
|
NetworkRequest NetworkRequest::twitchRequest(QUrl url)
|
2018-07-06 17:56:11 +02:00
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest request(url);
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
request.makeAuthorizedV5(getDefaultClientID());
|
2018-07-06 17:56:11 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
return request;
|
2018-07-06 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:45:33 +01:00
|
|
|
} // namespace chatterino
|