mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
refactor: extract-method is_valid_id() in rpcimpl (#7907)
This commit is contained in:
@@ -129,11 +129,29 @@ namespace
|
||||
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] constexpr bool is_valid_id(tr_variant const& val)
|
||||
{
|
||||
switch (val.index())
|
||||
{
|
||||
// https://www.jsonrpc.org/specification#request_object
|
||||
// An identifier established by the Client that MUST contain a String,
|
||||
// Number, or NULL value if included. .. The value SHOULD normally not
|
||||
// be Null and Numbers SHOULD NOT contain fractional parts
|
||||
case tr_variant::StringIndex:
|
||||
case tr_variant::IntIndex:
|
||||
case tr_variant::DoubleIndex:
|
||||
case tr_variant::NullIndex:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// https://www.jsonrpc.org/specification#response_object
|
||||
[[nodiscard]] tr_variant::Map build_response(Error::Code code, tr_variant id, tr_variant::Map&& body)
|
||||
{
|
||||
if (id.index() != tr_variant::StringIndex && id.index() != tr_variant::IntIndex && id.index() != tr_variant::DoubleIndex &&
|
||||
id.index() != tr_variant::NullIndex)
|
||||
if (!is_valid_id(id))
|
||||
{
|
||||
TR_ASSERT(false);
|
||||
id = nullptr;
|
||||
@@ -2893,20 +2911,12 @@ void tr_rpc_request_exec_impl(tr_session* session, tr_variant const& request, tr
|
||||
|
||||
auto data = tr_rpc_idle_data{};
|
||||
data.session = session;
|
||||
if (is_jsonrpc)
|
||||
|
||||
if (auto iter = map->find(is_jsonrpc ? TR_KEY_id : TR_KEY_tag); iter != std::end(*map))
|
||||
{
|
||||
if (auto it_id = map->find(TR_KEY_id); it_id != std::end(*map))
|
||||
tr_variant const& id = iter->second;
|
||||
if (is_jsonrpc && !is_valid_id(id))
|
||||
{
|
||||
auto const& id = it_id->second;
|
||||
switch (id.index())
|
||||
{
|
||||
case tr_variant::StringIndex:
|
||||
case tr_variant::IntIndex:
|
||||
case tr_variant::DoubleIndex:
|
||||
case tr_variant::NullIndex:
|
||||
data.id.merge(id); // copy
|
||||
break;
|
||||
default:
|
||||
callback(
|
||||
session,
|
||||
build_response(
|
||||
@@ -2915,12 +2925,9 @@ void tr_rpc_request_exec_impl(tr_session* session, tr_variant const& request, tr
|
||||
Error::build_data("id type must be String, Number, or Null"sv, {})));
|
||||
return;
|
||||
}
|
||||
data.id.merge(id);
|
||||
}
|
||||
}
|
||||
else if (auto tag = map->value_if<int64_t>(TR_KEY_tag); tag)
|
||||
{
|
||||
data.id = *tag;
|
||||
}
|
||||
|
||||
data.callback = std::move(callback);
|
||||
|
||||
auto const is_notification = is_jsonrpc && !data.id.has_value();
|
||||
|
||||
Reference in New Issue
Block a user