From 5792ed1ae26096a3dba67bf68719168bc201fb81 Mon Sep 17 00:00:00 2001 From: kannibalox Date: Fri, 6 Dec 2024 21:16:44 -0500 Subject: [PATCH] Apply clang-format --- src/rpc/xmlrpc_tinyxml2.cc | 141 +++++++++++++++---------------------- 1 file changed, 56 insertions(+), 85 deletions(-) diff --git a/src/rpc/xmlrpc_tinyxml2.cc b/src/rpc/xmlrpc_tinyxml2.cc index 593793fe..9169e5ed 100644 --- a/src/rpc/xmlrpc_tinyxml2.cc +++ b/src/rpc/xmlrpc_tinyxml2.cc @@ -1,39 +1,3 @@ -// rTorrent - BitTorrent client -// Copyright (C) 2005-2011, Jari Sundell -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// In addition, as a special exception, the copyright holders give -// permission to link the code of portions of this program with the -// OpenSSL library under certain conditions as described in each -// individual source file, and distribute linked combinations -// including the two. -// -// You must obey the GNU General Public License in all respects for -// all of the code used other than OpenSSL. If you modify file(s) -// with this exception, you may extend this exception to your version -// of the file(s), but you are not obligated to do so. If you do not -// wish to do so, delete this exception statement from your version. -// If you delete this exception statement from all source files in the -// program, then also delete it here. -// -// Contact: Jari Sundell -// -// Skomakerveien 33 -// 3185 Skoppum, NORWAY - #include "config.h" #ifdef HAVE_XMLRPC_TINYXML2 @@ -42,46 +6,47 @@ #endif #include -#include #include +#include #include #include -#include #include +#include +#include "parse_commands.h" #include "rpc/tinyxml2/tinyxml2.h" #include "utils/base64.h" #include "xmlrpc.h" -#include "parse_commands.h" namespace rpc { // Taken from xmlrpc-c -const int XMLRPC_INTERNAL_ERROR = -500; -const int XMLRPC_TYPE_ERROR = -501; -const int XMLRPC_INDEX_ERROR = -502; -const int XMLRPC_PARSE_ERROR = -503; -const int XMLRPC_NETWORK_ERROR = -504; -const int XMLRPC_TIMEOUT_ERROR = -505; -const int XMLRPC_NO_SUCH_METHOD_ERROR = -506; -const int XMLRPC_REQUEST_REFUSED_ERROR = -507; +const int XMLRPC_INTERNAL_ERROR = -500; +const int XMLRPC_TYPE_ERROR = -501; +const int XMLRPC_INDEX_ERROR = -502; +const int XMLRPC_PARSE_ERROR = -503; +const int XMLRPC_NETWORK_ERROR = -504; +const int XMLRPC_TIMEOUT_ERROR = -505; +const int XMLRPC_NO_SUCH_METHOD_ERROR = -506; +const int XMLRPC_REQUEST_REFUSED_ERROR = -507; const int XMLRPC_INTROSPECTION_DISABLED_ERROR = -508; -const int XMLRPC_LIMIT_EXCEEDED_ERROR = -509; -const int XMLRPC_INVALID_UTF8_ERROR = -510; +const int XMLRPC_LIMIT_EXCEEDED_ERROR = -509; +const int XMLRPC_INVALID_UTF8_ERROR = -510; class xmlrpc_error : public torrent::base_error { public: - xmlrpc_error(int type, std::string msg) : m_type(type), m_msg(msg) {} + xmlrpc_error(int type, std::string msg) : + m_type(type), m_msg(msg) {} virtual ~xmlrpc_error() throw() {} virtual int type() const throw() { return m_type; } virtual const char* what() const throw() { return m_msg.c_str(); } private: - int m_type; - std::string m_msg; + int m_type; + std::string m_msg; }; const tinyxml2::XMLElement* @@ -103,7 +68,7 @@ element_to_int(const tinyxml2::XMLNode* elem) { if (elem->FirstChild() == nullptr) { throw xmlrpc_error(XMLRPC_TYPE_ERROR, "unable to parse empty integer"); } - auto str = elem->FirstChild()->ToText()->Value(); + auto str = elem->FirstChild()->ToText()->Value(); auto result = std::strtoll(str, &pos, 10); if (pos == str || *pos != '\0') throw xmlrpc_error(XMLRPC_TYPE_ERROR, "unable to parse integer value"); @@ -119,7 +84,7 @@ xml_value_to_object(const tinyxml2::XMLNode* elem) { throw xmlrpc_error(XMLRPC_INTERNAL_ERROR, "received non-value element to convert"); } auto root_element = elem->FirstChild(); - auto root_type = root_element->Value(); + auto root_type = root_element->Value(); if (std::strncmp(root_type, "string", sizeof("string")) == 0) { auto child_element = root_element->FirstChild(); if (child_element == nullptr) { @@ -139,9 +104,9 @@ xml_value_to_object(const tinyxml2::XMLNode* elem) { } throw xmlrpc_error(XMLRPC_TYPE_ERROR, "unknown boolean value: " + boolean_text); } else if (std::strncmp(root_type, "array", sizeof("array")) == 0) { - auto array_raw = torrent::Object::create_list(); - auto& array = array_raw.as_list(); - auto data_element = root_element->ToElement()->FirstChildElement("data"); + auto array_raw = torrent::Object::create_list(); + auto& array = array_raw.as_list(); + auto data_element = root_element->ToElement()->FirstChildElement("data"); if (data_element == nullptr) throw xmlrpc_error(XMLRPC_PARSE_ERROR, "could not find expected data element in array"); for (auto child = data_element->FirstChildElement("value"); child; child = child->NextSiblingElement("value")) { @@ -149,8 +114,8 @@ xml_value_to_object(const tinyxml2::XMLNode* elem) { } return array_raw; } else if (std::strncmp(root_type, "struct", sizeof("struct")) == 0) { - auto map_raw = torrent::Object::create_map(); - auto& map = map_raw.as_map(); + auto map_raw = torrent::Object::create_map(); + auto& map = map_raw.as_map(); for (auto child = root_element->FirstChildElement("member"); child; child = child->NextSiblingElement("member")) { auto key = child->FirstChildElement("name")->GetText(); map[key] = std::move(xml_value_to_object(child->FirstChildElement("value"))); @@ -235,15 +200,15 @@ print_object_xml(const torrent::Object& obj, tinyxml2::XMLPrinter* printer) { } } - -torrent::Object execute_command(std::string method_name, const tinyxml2::XMLElement* params_element) { +torrent::Object +execute_command(std::string method_name, const tinyxml2::XMLElement* params_element) { CommandMap::iterator cmd_itr = commands.find(method_name.c_str()); if (cmd_itr == commands.end() || !(cmd_itr->second.m_flags & CommandMap::flag_public_xmlrpc)) { throw xmlrpc_error(XMLRPC_NO_SUCH_METHOD_ERROR, "method '" + method_name + "' not defined"); } - torrent::Object params_raw = torrent::Object::create_list(); - torrent::Object::list_type& params = params_raw.as_list(); - rpc::target_type target = rpc::make_target(); + torrent::Object params_raw = torrent::Object::create_list(); + torrent::Object::list_type& params = params_raw.as_list(); + rpc::target_type target = rpc::make_target(); if (params_element != nullptr) { if (std::strncmp(params_element->Name(), "params", sizeof("params")) == 0) { // Parse out the target if available @@ -284,15 +249,15 @@ process_document(const tinyxml2::XMLDocument* doc, tinyxml2::XMLPrinter* printer throw xmlrpc_error(XMLRPC_PARSE_ERROR, "methodCall element not found"); if (doc->FirstChildElement("methodCall")->FirstChildElement("methodName") == nullptr) throw xmlrpc_error(XMLRPC_PARSE_ERROR, "methodName element not found"); - auto method_name = doc->FirstChildElement("methodCall")->FirstChildElement("methodName")->GetText(); + auto method_name = doc->FirstChildElement("methodCall")->FirstChildElement("methodName")->GetText(); torrent::Object result; // Add a shim here for system.multicall to allow better code reuse, and // because system.multicall is one of the few methods that doesn't take a target if (method_name == std::string("system.multicall")) { - result = torrent::Object::create_list(); - auto& result_list = result.as_list(); - auto parent_elements = element_access(doc->RootElement(), {"params", "param", "value", "array", "data"}); + result = torrent::Object::create_list(); + auto& result_list = result.as_list(); + auto parent_elements = element_access(doc->RootElement(), {"params", "param", "value", "array", "data"}); for (auto child = parent_elements->FirstChildElement("value"); child; child = child->NextSiblingElement("value")) { auto sub_method_name = element_access(child, {"struct", "member", "value", "string"})->GetText(); // If sub_params ends up a nullptr at the end of this if-chian, @@ -309,14 +274,14 @@ process_document(const tinyxml2::XMLDocument* doc, tinyxml2::XMLPrinter* printer sub_result.as_list().push_back(execute_command(sub_method_name, sub_params)); result_list.push_back(sub_result); } catch (xmlrpc_error& e) { - auto fault = torrent::Object::create_map(); + auto fault = torrent::Object::create_map(); fault.as_map()["faultString"] = e.what(); - fault.as_map()["faultCode"] = e.type(); + fault.as_map()["faultCode"] = e.type(); result_list.push_back(fault); } catch (torrent::local_error& e) { - auto fault = torrent::Object::create_map(); + auto fault = torrent::Object::create_map(); fault.as_map()["faultString"] = e.what(); - fault.as_map()["faultCode"] = XMLRPC_INTERNAL_ERROR; + fault.as_map()["faultCode"] = XMLRPC_INTERNAL_ERROR; result_list.push_back(fault); } } @@ -338,7 +303,6 @@ process_document(const tinyxml2::XMLDocument* doc, tinyxml2::XMLPrinter* printer printer->CloseElement(true); } - void print_xmlrpc_fault(int faultCode, std::string faultString, tinyxml2::XMLPrinter* printer) { printer->PushHeader(false, true); @@ -379,7 +343,7 @@ XmlRpc::process(const char* inBuffer, uint32_t length, slot_write slotWrite) { if (length > m_sizeLimit) { tinyxml2::XMLPrinter printer(nullptr, true, 0); print_xmlrpc_fault(XMLRPC_LIMIT_EXCEEDED_ERROR, "Content size exceeds maximum XML-RPC limit", &printer); - return slotWrite(printer.CStr(), printer.CStrSize()-1); + return slotWrite(printer.CStr(), printer.CStrSize() - 1); } tinyxml2::XMLDocument doc; doc.Parse(inBuffer, length); @@ -389,29 +353,36 @@ XmlRpc::process(const char* inBuffer, uint32_t length, slot_write slotWrite) { // remains. tinyxml2::XMLPrinter printer(nullptr, true, 0); process_document(&doc, &printer); - return slotWrite(printer.CStr(), printer.CStrSize()-1); + return slotWrite(printer.CStr(), printer.CStrSize() - 1); } catch (xmlrpc_error& e) { tinyxml2::XMLPrinter printer(nullptr, true, 0); print_xmlrpc_fault(e.type(), e.what(), &printer); - return slotWrite(printer.CStr(), printer.CStrSize()-1); + return slotWrite(printer.CStr(), printer.CStrSize() - 1); } catch (torrent::local_error& e) { tinyxml2::XMLPrinter printer(nullptr, true, 0); print_xmlrpc_fault(XMLRPC_INTERNAL_ERROR, e.what(), &printer); - return slotWrite(printer.CStr(), printer.CStrSize()-1); + return slotWrite(printer.CStr(), printer.CStrSize() - 1); } } -void XmlRpc::initialize() { m_isValid = true; } -void XmlRpc::cleanup() {} +void +XmlRpc::initialize() { m_isValid = true; } +void +XmlRpc::cleanup() {} -void XmlRpc::insert_command(const char*, const char*, const char*) {} -void XmlRpc::set_dialect(int) {} +void +XmlRpc::insert_command(const char*, const char*, const char*) {} +void +XmlRpc::set_dialect(int) {} -int64_t XmlRpc::size_limit() { return static_cast(m_sizeLimit); } -void XmlRpc::set_size_limit(uint64_t size) { m_sizeLimit = size; } +int64_t +XmlRpc::size_limit() { return static_cast(m_sizeLimit); } +void +XmlRpc::set_size_limit(uint64_t size) { m_sizeLimit = size; } -bool XmlRpc::is_valid() const { return m_isValid; } +bool +XmlRpc::is_valid() const { return m_isValid; } -} +} // namespace rpc #endif