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 <event2/buffer.h>
|
|
|
|
|
|
|
|
#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"
|
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
|
|
|
|
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) :
|
|
|
|
QObject(parent),
|
|
|
|
mySession(nullptr),
|
|
|
|
myNAM(nullptr),
|
|
|
|
myNextTag(0)
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mySession = nullptr;
|
|
|
|
mySessionId.clear();
|
|
|
|
myUrl.clear();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myNAM != nullptr)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myNAM->deleteLater();
|
|
|
|
myNAM = 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mySession = 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myUrl = 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
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
if (mySession != nullptr)
|
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
|
|
|
if (QHostAddress(myUrl.host()).isLoopback())
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myUrl;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return exec(tr_quark_get_string(method, nullptr), args);
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
RpcResponseFuture RpcClient::exec(char const* 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);
|
|
|
|
tr_variantDictAddStr(json.get(), TR_KEY_method, method);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myNextTag++;
|
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;
|
|
|
|
request.setUrl(myUrl);
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!mySessionId.isEmpty())
|
|
|
|
{
|
|
|
|
request.setRawHeader(TR_RPC_SESSION_ID_HEADER, mySessionId.toUtf8());
|
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t rawJsonDataLength;
|
|
|
|
char* rawJsonData = tr_variantToStr(json.get(), TR_VARIANT_FMT_JSON_LEAN, &rawJsonDataLength);
|
|
|
|
QByteArray jsonData(rawJsonData, rawJsonDataLength);
|
|
|
|
tr_free(rawJsonData);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QNetworkReply* reply = networkAccessManager()->post(request, jsonData);
|
|
|
|
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
|
2017-04-19 12:04:45 +00:00
|
|
|
std::cerr << "sending " << "POST " << qPrintable(myUrl.path()) << std::endl;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Body:\n" << jsonData.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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myLocalRequests.insert(tag, promise);
|
|
|
|
tr_rpc_request_exec_json(mySession, 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();
|
|
|
|
tr_variantDictAddInt(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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (mySession != nullptr)
|
|
|
|
{
|
|
|
|
sendLocalRequest(json, promise, tag);
|
|
|
|
}
|
|
|
|
else if (!myUrl.isEmpty())
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
if (myNAM == nullptr)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myNAM = new QNetworkAccessManager();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkRequestFinished(QNetworkReply*)));
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(myNAM, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), this,
|
|
|
|
SIGNAL(httpAuthenticationRequired()));
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return myNAM;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_UNUSED(s);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcClient* 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;
|
|
|
|
tr_variantInitBool(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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QFutureInterface<RpcResponse> 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.
|
|
|
|
mySessionId = 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;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QByteArray const jsonData = reply->readAll().trimmed();
|
2017-04-19 12:04:45 +00:00
|
|
|
TrVariantPtr json = createVariant();
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantFromJson(json.get(), jsonData.constData(), jsonData.size()) == 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
QFutureInterface<RpcResponse> promise = myLocalRequests.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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t tag;
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindInt(&json, TR_KEY_tag, &tag))
|
|
|
|
{
|
|
|
|
tag = -1;
|
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tag;
|
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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* result;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantDictFindStr(&json, TR_KEY_result, &result, nullptr))
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret.result = QString::fromUtf8(result);
|
|
|
|
ret.success = std::strcmp(result, "success") == 0;
|
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;
|
|
|
|
tr_variantInitBool(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
|
|
|
}
|