2014-12-27 20:03:10 +00:00
|
|
|
/*
|
2016-04-19 20:41:59 +00:00
|
|
|
* This file Copyright (C) 2014-2016 Mnemosyne LLC
|
2014-12-27 20:03:10 +00:00
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
#include <cstring>
|
2014-12-27 20:03:10 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QHostAddress>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/rpcimpl.h>
|
|
|
|
#include <libtransmission/utils.h> // tr_free
|
|
|
|
#include <libtransmission/version.h> // LONG_VERSION_STRING
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "RpcClient.h"
|
2020-07-27 04:30:58 +00:00
|
|
|
#include "VariantHelpers.h"
|
2014-12-27 20:03:10 +00:00
|
|
|
|
|
|
|
// #define DEBUG_HTTP
|
|
|
|
|
|
|
|
#define REQUEST_DATA_PROPERTY_KEY "requestData"
|
2016-04-19 20:41:59 +00:00
|
|
|
#define REQUEST_FUTUREINTERFACE_PROPERTY_KEY "requestReplyFutureInterface"
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
using ::trqt::variant_helpers::dictAdd;
|
|
|
|
using ::trqt::variant_helpers::dictFind;
|
|
|
|
using ::trqt::variant_helpers::variantInit;
|
|
|
|
|
2015-07-13 00:32:48 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void destroyVariant(tr_variant* json)
|
|
|
|
{
|
|
|
|
tr_variantFree(json);
|
|
|
|
tr_free(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
TrVariantPtr createVariant()
|
|
|
|
{
|
|
|
|
return TrVariantPtr(tr_new0(tr_variant, 1), &destroyVariant);
|
2015-07-13 00:32:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
RpcClient::RpcClient(QObject* parent) :
|
2020-05-27 21:53:12 +00:00
|
|
|
QObject(parent)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
qRegisterMetaType<TrVariantPtr>("TrVariantPtr");
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void RpcClient::stop()
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_ = nullptr;
|
|
|
|
session_id_.clear();
|
|
|
|
url_.clear();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (nam_ != nullptr)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
nam_->deleteLater();
|
|
|
|
nam_ = nullptr;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void RpcClient::start(tr_session* session)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_ = session;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void RpcClient::start(QUrl const& url)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
url_ = url;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool RpcClient::isLocal() const
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (QHostAddress(url_.host()).isLoopback())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QUrl const& RpcClient::url() const
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return url_;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponseFuture RpcClient::exec(tr_quark method, tr_variant* args)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto len = size_t{};
|
|
|
|
auto const* str = tr_quark_get_string(method, &len);
|
|
|
|
return exec(std::string_view(str, len), args);
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
RpcResponseFuture RpcClient::exec(std::string_view method, tr_variant* args)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
TrVariantPtr json = createVariant();
|
|
|
|
tr_variantInitDict(json.get(), 3);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(json.get(), TR_KEY_method, method);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (args != nullptr)
|
|
|
|
{
|
|
|
|
tr_variantDictSteal(json.get(), TR_KEY_arguments, args);
|
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return sendRequest(json);
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t RpcClient::getNextTag()
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return next_tag_++;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void RpcClient::sendNetworkRequest(TrVariantPtr json, QFutureInterface<RpcResponse> const& promise)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QNetworkRequest request;
|
2020-05-27 21:53:12 +00:00
|
|
|
request.setUrl(url_);
|
2017-04-19 12:04:45 +00:00
|
|
|
request.setRawHeader("User-Agent", (qApp->applicationName() + QLatin1Char('/') +
|
|
|
|
QString::fromUtf8(LONG_VERSION_STRING)).toUtf8());
|
|
|
|
request.setRawHeader("Content-Type", "application/json; charset=UTF-8");
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!session_id_.isEmpty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
request.setRawHeader(TR_RPC_SESSION_ID_HEADER, session_id_.toUtf8());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
size_t raw_json_data_length;
|
|
|
|
char* raw_json_data = tr_variantToStr(json.get(), TR_VARIANT_FMT_JSON_LEAN, &raw_json_data_length);
|
|
|
|
QByteArray json_data(raw_json_data, raw_json_data_length);
|
|
|
|
tr_free(raw_json_data);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QNetworkReply* reply = networkAccessManager()->post(request, json_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
reply->setProperty(REQUEST_DATA_PROPERTY_KEY, QVariant::fromValue(json));
|
|
|
|
reply->setProperty(REQUEST_FUTUREINTERFACE_PROPERTY_KEY, QVariant::fromValue(promise));
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SIGNAL(dataReadProgress()));
|
|
|
|
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SIGNAL(dataSendProgress()));
|
2014-12-27 20:03:10 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_HTTP
|
2020-05-27 21:53:12 +00:00
|
|
|
std::cerr << "sending " << "POST " << qPrintable(url_.path()) << std::endl;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QByteArray const& b : request.rawHeaderList())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
std::cerr << b.constData() << ": " << request.rawHeader(b).constData() << std::endl;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
std::cerr << "Body:\n" << json_data.constData() << std::endl;
|
2014-12-27 20:03:10 +00:00
|
|
|
#endif
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void RpcClient::sendLocalRequest(TrVariantPtr json, QFutureInterface<RpcResponse> const& promise, int64_t tag)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
local_requests_.insert(tag, promise);
|
|
|
|
tr_rpc_request_exec_json(session_, json.get(), localSessionCallback, this);
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponseFuture RpcClient::sendRequest(TrVariantPtr json)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tag = getNextTag();
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(json.get(), TR_KEY_tag, tag);
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QFutureInterface<RpcResponse> promise;
|
|
|
|
promise.setExpectedResultCount(1);
|
|
|
|
promise.setProgressRange(0, 1);
|
|
|
|
promise.setProgressValue(0);
|
|
|
|
promise.reportStarted();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendLocalRequest(json, promise, tag);
|
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (!url_.isEmpty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendNetworkRequest(json, promise);
|
|
|
|
}
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return promise.future();
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QNetworkAccessManager* RpcClient::networkAccessManager()
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (nam_ == nullptr)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
nam_ = new QNetworkAccessManager();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
connect(nam_, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkRequestFinished(QNetworkReply*)));
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
connect(nam_, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), this,
|
2017-04-19 12:04:45 +00:00
|
|
|
SIGNAL(httpAuthenticationRequired()));
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return nam_;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void RpcClient::localSessionCallback(tr_session* s, tr_variant* response, void* vself)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
Q_UNUSED(s)
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* self = static_cast<RpcClient*>(vself);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TrVariantPtr json = createVariant();
|
|
|
|
*json = *response;
|
2020-07-27 04:30:58 +00:00
|
|
|
variantInit(response, false);
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// this callback is invoked in the libtransmission thread, so we don't want
|
|
|
|
// to process the response here... let's push it over to the Qt thread.
|
|
|
|
QMetaObject::invokeMethod(self, "localRequestFinished", Qt::QueuedConnection, Q_ARG(TrVariantPtr, json));
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void RpcClient::networkRequestFinished(QNetworkReply* reply)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
reply->deleteLater();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto promise = reply->property(REQUEST_FUTUREINTERFACE_PROPERTY_KEY).
|
2019-02-10 11:05:16 +00:00
|
|
|
value<QFutureInterface<RpcResponse>>();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
#ifdef DEBUG_HTTP
|
2017-04-19 12:04:45 +00:00
|
|
|
std::cerr << "http response header: " << std::endl;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QByteArray const& b : reply->rawHeaderList())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
std::cerr << b.constData() << ": " << reply->rawHeader(b).constData() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "json:\n" << reply->peek(reply->bytesAvailable()).constData() << std::endl;
|
2014-12-27 20:03:10 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409 &&
|
|
|
|
reply->hasRawHeader(TR_RPC_SESSION_ID_HEADER))
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// we got a 409 telling us our session id has expired.
|
|
|
|
// update it and resubmit the request.
|
2020-05-27 21:53:12 +00:00
|
|
|
session_id_ = QString::fromUtf8(reply->rawHeader(TR_RPC_SESSION_ID_HEADER));
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
sendNetworkRequest(reply->property(REQUEST_DATA_PROPERTY_KEY).value<TrVariantPtr>(), promise);
|
|
|
|
return;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit networkResponse(reply->error(), reply->errorString());
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (reply->error() != QNetworkReply::NoError)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponse result;
|
|
|
|
result.networkError = reply->error();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
promise.setProgressValueAndText(1, reply->errorString());
|
|
|
|
promise.reportFinished(&result);
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponse result;
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QByteArray const json_data = reply->readAll().trimmed();
|
2017-04-19 12:04:45 +00:00
|
|
|
TrVariantPtr json = createVariant();
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (tr_variantFromJson(json.get(), json_data.constData(), json_data.size()) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
result = parseResponseData(*json);
|
|
|
|
}
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
promise.setProgressValue(1);
|
|
|
|
promise.reportFinished(&result);
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void RpcClient::localRequestFinished(TrVariantPtr response)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tag = parseResponseTag(*response);
|
|
|
|
RpcResponse result = parseResponseData(*response);
|
2020-05-27 21:53:12 +00:00
|
|
|
QFutureInterface<RpcResponse> promise = local_requests_.take(tag);
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
promise.setProgressRange(0, 1);
|
|
|
|
promise.setProgressValue(1);
|
|
|
|
promise.reportFinished(&result);
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t RpcClient::parseResponseTag(tr_variant& json)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const tag = dictFind<int>(&json, TR_KEY_tag);
|
|
|
|
return tag ? *tag : -1;
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponse RpcClient::parseResponseData(tr_variant& json)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponse ret;
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const result = dictFind<QString>(&json, TR_KEY_result);
|
|
|
|
if (result)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
ret.result = *result;
|
|
|
|
ret.success = *result == QStringLiteral("success");
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* args;
|
|
|
|
|
|
|
|
if (tr_variantDictFindDict(&json, TR_KEY_arguments, &args))
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret.args = createVariant();
|
|
|
|
*ret.args = *args;
|
2020-07-27 04:30:58 +00:00
|
|
|
variantInit(args, false);
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|