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.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
void
|
|
|
|
destroyVariant (tr_variant * json)
|
|
|
|
{
|
|
|
|
tr_variantFree (json);
|
|
|
|
tr_free (json);
|
|
|
|
}
|
|
|
|
|
|
|
|
TrVariantPtr
|
|
|
|
createVariant ()
|
|
|
|
{
|
|
|
|
return TrVariantPtr (tr_new0 (tr_variant, 1), &destroyVariant);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
RpcClient::RpcClient (QObject * parent):
|
|
|
|
QObject (parent),
|
|
|
|
mySession (nullptr),
|
2016-04-19 20:41:59 +00:00
|
|
|
myNAM (nullptr),
|
|
|
|
myNextTag (0)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2015-07-13 00:32:48 +00:00
|
|
|
qRegisterMetaType<TrVariantPtr> ("TrVariantPtr");
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RpcClient::stop ()
|
|
|
|
{
|
|
|
|
mySession = nullptr;
|
|
|
|
mySessionId.clear ();
|
|
|
|
myUrl.clear ();
|
|
|
|
|
|
|
|
if (myNAM != nullptr)
|
|
|
|
{
|
|
|
|
myNAM->deleteLater ();
|
|
|
|
myNAM = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RpcClient::start (tr_session * session)
|
|
|
|
{
|
|
|
|
mySession = session;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RpcClient::start (const QUrl& url)
|
|
|
|
{
|
|
|
|
myUrl = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RpcClient::isLocal () const
|
|
|
|
{
|
|
|
|
if (mySession != 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2015-01-29 21:53:05 +00:00
|
|
|
if (myUrl.host () == QLatin1String ("127.0.0.1") ||
|
|
|
|
myUrl.host ().compare (QLatin1String ("localhost"), Qt::CaseInsensitive) == 0)
|
2014-12-27 20:03:10 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
if (QHostAddress (myUrl.host ()).isLoopback ())
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QUrl&
|
|
|
|
RpcClient::url () const
|
|
|
|
{
|
|
|
|
return myUrl;
|
|
|
|
}
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcResponseFuture
|
|
|
|
RpcClient::exec (tr_quark method, tr_variant * args)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2016-04-19 20:41:59 +00:00
|
|
|
return exec (tr_quark_get_string (method, nullptr), args);
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcResponseFuture
|
|
|
|
RpcClient::exec (const char * method, tr_variant * args)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2015-07-13 00:32:48 +00:00
|
|
|
TrVariantPtr json = createVariant ();
|
|
|
|
tr_variantInitDict (json.get (), 3);
|
|
|
|
tr_variantDictAddStr (json.get (), TR_KEY_method, method);
|
2014-12-27 20:03:10 +00:00
|
|
|
if (args != nullptr)
|
2015-07-13 00:32:48 +00:00
|
|
|
tr_variantDictSteal (json.get (), TR_KEY_arguments, args);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
return sendRequest (json);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
RpcClient::getNextTag ()
|
|
|
|
{
|
|
|
|
return myNextTag++;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcClient::sendNetworkRequest (TrVariantPtr json, const QFutureInterface<RpcResponse> &promise)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2016-04-19 20:41:59 +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");
|
|
|
|
|
|
|
|
if (!mySessionId.isEmpty ())
|
|
|
|
request.setRawHeader (TR_RPC_SESSION_ID_HEADER, mySessionId.toUtf8 ());
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2016-04-19 20:41:59 +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
|
|
|
|
2016-04-19 20:41:59 +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
|
|
|
|
2016-04-19 20:41:59 +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
|
2016-04-19 20:41:59 +00:00
|
|
|
std::cerr << "sending " << "POST " << qPrintable (myUrl.path ()) << std::endl;
|
|
|
|
for (const QByteArray& b: request.rawHeaderList ())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RpcClient::sendLocalRequest (TrVariantPtr json, const QFutureInterface<RpcResponse> &promise, int64_t tag)
|
|
|
|
{
|
|
|
|
myLocalRequests.insert (tag, promise);
|
|
|
|
tr_rpc_request_exec_json (mySession, json.get (), localSessionCallback, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
RpcResponseFuture
|
|
|
|
RpcClient::sendRequest (TrVariantPtr json)
|
|
|
|
{
|
|
|
|
int64_t tag = getNextTag ();
|
|
|
|
tr_variantDictAddInt (json.get (), TR_KEY_tag, tag);
|
|
|
|
|
|
|
|
QFutureInterface<RpcResponse> promise;
|
|
|
|
promise.setExpectedResultCount (1);
|
|
|
|
promise.setProgressRange (0, 1);
|
|
|
|
promise.setProgressValue (0);
|
|
|
|
promise.reportStarted ();
|
|
|
|
|
|
|
|
if (mySession != nullptr)
|
|
|
|
sendLocalRequest (json, promise, tag);
|
|
|
|
else if (!myUrl.isEmpty ())
|
|
|
|
sendNetworkRequest (json, promise);
|
|
|
|
|
|
|
|
return promise.future ();
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkAccessManager *
|
|
|
|
RpcClient::networkAccessManager ()
|
|
|
|
{
|
|
|
|
if (myNAM == 0)
|
|
|
|
{
|
|
|
|
myNAM = new QNetworkAccessManager ();
|
|
|
|
|
|
|
|
connect (myNAM, SIGNAL (finished (QNetworkReply *)),
|
2016-04-19 20:41:59 +00:00
|
|
|
this, SLOT (networkRequestFinished (QNetworkReply * )));
|
2014-12-27 20:03:10 +00:00
|
|
|
|
|
|
|
connect (myNAM, SIGNAL (authenticationRequired (QNetworkReply *,QAuthenticator *)),
|
|
|
|
this, SIGNAL (httpAuthenticationRequired ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return myNAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-13 00:32:48 +00:00
|
|
|
RpcClient::localSessionCallback (tr_session * s, tr_variant * response, void * vself)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED (s);
|
|
|
|
|
|
|
|
RpcClient * self = static_cast<RpcClient*> (vself);
|
|
|
|
|
2015-07-13 00:32:48 +00:00
|
|
|
TrVariantPtr json = createVariant ();
|
|
|
|
*json = *response;
|
|
|
|
tr_variantInitBool (response, false);
|
|
|
|
|
2014-12-27 20:03:10 +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.
|
2016-04-19 20:41:59 +00:00
|
|
|
QMetaObject::invokeMethod (self, "localRequestFinished", Qt::QueuedConnection, Q_ARG (TrVariantPtr, json));
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcClient::networkRequestFinished (QNetworkReply *reply)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2016-04-19 20:41:59 +00:00
|
|
|
reply->deleteLater ();
|
|
|
|
|
|
|
|
QFutureInterface<RpcResponse> promise = reply->property (REQUEST_FUTUREINTERFACE_PROPERTY_KEY).value<QFutureInterface<RpcResponse>> ();
|
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
#ifdef DEBUG_HTTP
|
|
|
|
std::cerr << "http response header: " << std::endl;
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const QByteArray& b: reply->rawHeaderList ())
|
2014-12-27 20:03:10 +00:00
|
|
|
std::cerr << b.constData ()
|
|
|
|
<< ": "
|
|
|
|
<< reply->rawHeader (b).constData ()
|
|
|
|
<< std::endl;
|
|
|
|
std::cerr << "json:\n" << reply->peek (reply->bytesAvailable ()).constData () << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (reply->attribute (QNetworkRequest::HttpStatusCodeAttribute).toInt () == 409 &&
|
|
|
|
reply->hasRawHeader (TR_RPC_SESSION_ID_HEADER))
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
emit networkResponse (reply->error(), reply->errorString());
|
|
|
|
|
|
|
|
if (reply->error () != QNetworkReply::NoError)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcResponse result;
|
|
|
|
result.networkError = reply->error ();
|
|
|
|
|
|
|
|
promise.setProgressValueAndText (1, reply->errorString ());
|
|
|
|
promise.reportFinished (&result);
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcResponse result;
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
const QByteArray jsonData = reply->readAll ().trimmed ();
|
2015-07-13 00:32:48 +00:00
|
|
|
TrVariantPtr json = createVariant ();
|
|
|
|
if (tr_variantFromJson (json.get (), jsonData.constData (), jsonData.size ()) == 0)
|
2016-04-19 20:41:59 +00:00
|
|
|
result = parseResponseData (*json);
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
promise.setProgressValue (1);
|
|
|
|
promise.reportFinished (&result);
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcClient::localRequestFinished (TrVariantPtr response)
|
|
|
|
{
|
|
|
|
int64_t tag = parseResponseTag (*response);
|
|
|
|
RpcResponse result = parseResponseData (*response);
|
|
|
|
QFutureInterface<RpcResponse> promise = myLocalRequests.take (tag);
|
|
|
|
|
|
|
|
promise.setProgressRange (0, 1);
|
|
|
|
promise.setProgressValue (1);
|
|
|
|
promise.reportFinished (&result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
RpcClient::parseResponseTag (tr_variant& json)
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
|
|
|
int64_t tag;
|
2016-04-19 20:41:59 +00:00
|
|
|
|
|
|
|
if (!tr_variantDictFindInt (&json, TR_KEY_tag, &tag))
|
2014-12-27 20:03:10 +00:00
|
|
|
tag = -1;
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
RpcResponse
|
|
|
|
RpcClient::parseResponseData (tr_variant& json)
|
|
|
|
{
|
|
|
|
RpcResponse ret;
|
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
const char * result;
|
2016-04-19 20:41:59 +00:00
|
|
|
if (tr_variantDictFindStr (&json, TR_KEY_result, &result, nullptr))
|
|
|
|
{
|
|
|
|
ret.result = QString::fromUtf8 (result);
|
|
|
|
ret.success = std::strcmp (result, "success") == 0;
|
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
|
|
|
tr_variant * args;
|
2016-04-19 20:41:59 +00:00
|
|
|
if (tr_variantDictFindDict (&json, TR_KEY_arguments, &args))
|
|
|
|
{
|
|
|
|
ret.args = createVariant ();
|
|
|
|
*ret.args = *args;
|
|
|
|
tr_variantInitBool (args, false);
|
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
return ret;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|