1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-30 19:03:04 +00:00

refactor: reuse one QRequestHeader across requests (#1373)

Minor optimization: since we don't actually change our request headers
very often, we can lazy-build the request once and reuse that instead of
having to rebuild it each time.
This commit is contained in:
Charles Kerr 2020-07-28 10:56:40 -05:00 committed by GitHub
parent 0b042d85dd
commit 910944733e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 16 deletions

View file

@ -25,9 +25,6 @@
// #define DEBUG_HTTP
#define REQUEST_DATA_PROPERTY_KEY "requestData"
#define REQUEST_FUTUREINTERFACE_PROPERTY_KEY "requestReplyFutureInterface"
using ::trqt::variant_helpers::dictAdd;
using ::trqt::variant_helpers::dictFind;
using ::trqt::variant_helpers::variantInit;
@ -35,6 +32,9 @@ using ::trqt::variant_helpers::variantInit;
namespace
{
char const constexpr* const RequestDataPropertyKey { "requestData" };
char const constexpr* const RequestFutureinterfacePropertyKey { "requestReplyFutureInterface" };
void destroyVariant(tr_variant* json)
{
tr_variantFree(json);
@ -59,6 +59,7 @@ void RpcClient::stop()
session_ = nullptr;
session_id_.clear();
url_.clear();
request_.reset();
if (nam_ != nullptr)
{
@ -75,6 +76,7 @@ void RpcClient::start(tr_session* session)
void RpcClient::start(QUrl const& url)
{
url_ = url;
request_.reset();
}
bool RpcClient::isLocal() const
@ -125,15 +127,19 @@ int64_t RpcClient::getNextTag()
void RpcClient::sendNetworkRequest(TrVariantPtr json, QFutureInterface<RpcResponse> const& promise)
{
QNetworkRequest request;
request.setUrl(url_);
request.setRawHeader("User-Agent", (qApp->applicationName() + QLatin1Char('/') +
QString::fromUtf8(LONG_VERSION_STRING)).toUtf8());
request.setRawHeader("Content-Type", "application/json; charset=UTF-8");
if (!session_id_.isEmpty())
if (!request_)
{
request.setRawHeader(TR_RPC_SESSION_ID_HEADER, session_id_.toUtf8());
QNetworkRequest request;
request.setUrl(url_);
request.setRawHeader("User-Agent", (qApp->applicationName() + QLatin1Char('/') + QString::fromUtf8(
LONG_VERSION_STRING)).toUtf8());
request.setRawHeader("Content-Type", "application/json; charset=UTF-8");
if (!session_id_.isEmpty())
{
request.setRawHeader(TR_RPC_SESSION_ID_HEADER, session_id_.toUtf8());
}
request_ = request;
}
size_t raw_json_data_length;
@ -141,9 +147,9 @@ void RpcClient::sendNetworkRequest(TrVariantPtr json, QFutureInterface<RpcRespon
QByteArray json_data(raw_json_data, raw_json_data_length);
tr_free(raw_json_data);
QNetworkReply* reply = networkAccessManager()->post(request, json_data);
reply->setProperty(REQUEST_DATA_PROPERTY_KEY, QVariant::fromValue(json));
reply->setProperty(REQUEST_FUTUREINTERFACE_PROPERTY_KEY, QVariant::fromValue(promise));
QNetworkReply* reply = networkAccessManager()->post(*request_, json_data);
reply->setProperty(RequestDataPropertyKey, QVariant::fromValue(json));
reply->setProperty(RequestFutureinterfacePropertyKey, QVariant::fromValue(promise));
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SIGNAL(dataReadProgress()));
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SIGNAL(dataSendProgress()));
@ -223,7 +229,7 @@ void RpcClient::networkRequestFinished(QNetworkReply* reply)
{
reply->deleteLater();
auto promise = reply->property(REQUEST_FUTUREINTERFACE_PROPERTY_KEY).
auto promise = reply->property(RequestFutureinterfacePropertyKey).
value<QFutureInterface<RpcResponse>>();
#ifdef DEBUG_HTTP
@ -243,8 +249,9 @@ void RpcClient::networkRequestFinished(QNetworkReply* reply)
// we got a 409 telling us our session id has expired.
// update it and resubmit the request.
session_id_ = QString::fromUtf8(reply->rawHeader(TR_RPC_SESSION_ID_HEADER));
request_.reset();
sendNetworkRequest(reply->property(REQUEST_DATA_PROPERTY_KEY).value<TrVariantPtr>(), promise);
sendNetworkRequest(reply->property(RequestDataPropertyKey).value<TrVariantPtr>(), promise);
return;
}

View file

@ -9,11 +9,13 @@
#pragma once
#include <memory>
#include <optional>
#include <string_view>
#include <QFuture>
#include <QFutureInterface>
#include <QHash>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QObject>
#include <QString>
@ -87,6 +89,8 @@ private:
static void localSessionCallback(tr_session* s, tr_variant* response, void* vself);
std::optional<QNetworkRequest> request_;
tr_session* session_ = {};
QString session_id_;
QUrl url_;