Apply clang-format

This commit is contained in:
kannibalox
2024-12-06 21:16:44 -05:00
committed by Jari Sundell
parent 8cfc71b936
commit 5792ed1ae2
+56 -85
View File
@@ -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 <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h" #include "config.h"
#ifdef HAVE_XMLRPC_TINYXML2 #ifdef HAVE_XMLRPC_TINYXML2
@@ -42,46 +6,47 @@
#endif #endif
#include <cctype> #include <cctype>
#include <string>
#include <initializer_list> #include <initializer_list>
#include <string>
#include <stdlib.h> #include <stdlib.h>
#include <rak/string_manip.h> #include <rak/string_manip.h>
#include <torrent/object.h>
#include <torrent/exceptions.h> #include <torrent/exceptions.h>
#include <torrent/object.h>
#include "parse_commands.h"
#include "rpc/tinyxml2/tinyxml2.h" #include "rpc/tinyxml2/tinyxml2.h"
#include "utils/base64.h" #include "utils/base64.h"
#include "xmlrpc.h" #include "xmlrpc.h"
#include "parse_commands.h"
namespace rpc { namespace rpc {
// Taken from xmlrpc-c // Taken from xmlrpc-c
const int XMLRPC_INTERNAL_ERROR = -500; const int XMLRPC_INTERNAL_ERROR = -500;
const int XMLRPC_TYPE_ERROR = -501; const int XMLRPC_TYPE_ERROR = -501;
const int XMLRPC_INDEX_ERROR = -502; const int XMLRPC_INDEX_ERROR = -502;
const int XMLRPC_PARSE_ERROR = -503; const int XMLRPC_PARSE_ERROR = -503;
const int XMLRPC_NETWORK_ERROR = -504; const int XMLRPC_NETWORK_ERROR = -504;
const int XMLRPC_TIMEOUT_ERROR = -505; const int XMLRPC_TIMEOUT_ERROR = -505;
const int XMLRPC_NO_SUCH_METHOD_ERROR = -506; const int XMLRPC_NO_SUCH_METHOD_ERROR = -506;
const int XMLRPC_REQUEST_REFUSED_ERROR = -507; const int XMLRPC_REQUEST_REFUSED_ERROR = -507;
const int XMLRPC_INTROSPECTION_DISABLED_ERROR = -508; const int XMLRPC_INTROSPECTION_DISABLED_ERROR = -508;
const int XMLRPC_LIMIT_EXCEEDED_ERROR = -509; const int XMLRPC_LIMIT_EXCEEDED_ERROR = -509;
const int XMLRPC_INVALID_UTF8_ERROR = -510; const int XMLRPC_INVALID_UTF8_ERROR = -510;
class xmlrpc_error : public torrent::base_error { class xmlrpc_error : public torrent::base_error {
public: 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 ~xmlrpc_error() throw() {}
virtual int type() const throw() { return m_type; } virtual int type() const throw() { return m_type; }
virtual const char* what() const throw() { return m_msg.c_str(); } virtual const char* what() const throw() { return m_msg.c_str(); }
private: private:
int m_type; int m_type;
std::string m_msg; std::string m_msg;
}; };
const tinyxml2::XMLElement* const tinyxml2::XMLElement*
@@ -103,7 +68,7 @@ element_to_int(const tinyxml2::XMLNode* elem) {
if (elem->FirstChild() == nullptr) { if (elem->FirstChild() == nullptr) {
throw xmlrpc_error(XMLRPC_TYPE_ERROR, "unable to parse empty integer"); 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); auto result = std::strtoll(str, &pos, 10);
if (pos == str || *pos != '\0') if (pos == str || *pos != '\0')
throw xmlrpc_error(XMLRPC_TYPE_ERROR, "unable to parse integer value"); 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"); throw xmlrpc_error(XMLRPC_INTERNAL_ERROR, "received non-value element to convert");
} }
auto root_element = elem->FirstChild(); 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) { if (std::strncmp(root_type, "string", sizeof("string")) == 0) {
auto child_element = root_element->FirstChild(); auto child_element = root_element->FirstChild();
if (child_element == nullptr) { 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); throw xmlrpc_error(XMLRPC_TYPE_ERROR, "unknown boolean value: " + boolean_text);
} else if (std::strncmp(root_type, "array", sizeof("array")) == 0) { } else if (std::strncmp(root_type, "array", sizeof("array")) == 0) {
auto array_raw = torrent::Object::create_list(); auto array_raw = torrent::Object::create_list();
auto& array = array_raw.as_list(); auto& array = array_raw.as_list();
auto data_element = root_element->ToElement()->FirstChildElement("data"); auto data_element = root_element->ToElement()->FirstChildElement("data");
if (data_element == nullptr) if (data_element == nullptr)
throw xmlrpc_error(XMLRPC_PARSE_ERROR, "could not find expected data element in array"); 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")) { 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; return array_raw;
} else if (std::strncmp(root_type, "struct", sizeof("struct")) == 0) { } else if (std::strncmp(root_type, "struct", sizeof("struct")) == 0) {
auto map_raw = torrent::Object::create_map(); auto map_raw = torrent::Object::create_map();
auto& map = map_raw.as_map(); auto& map = map_raw.as_map();
for (auto child = root_element->FirstChildElement("member"); child; child = child->NextSiblingElement("member")) { for (auto child = root_element->FirstChildElement("member"); child; child = child->NextSiblingElement("member")) {
auto key = child->FirstChildElement("name")->GetText(); auto key = child->FirstChildElement("name")->GetText();
map[key] = std::move(xml_value_to_object(child->FirstChildElement("value"))); 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
torrent::Object execute_command(std::string method_name, const tinyxml2::XMLElement* params_element) { execute_command(std::string method_name, const tinyxml2::XMLElement* params_element) {
CommandMap::iterator cmd_itr = commands.find(method_name.c_str()); CommandMap::iterator cmd_itr = commands.find(method_name.c_str());
if (cmd_itr == commands.end() || !(cmd_itr->second.m_flags & CommandMap::flag_public_xmlrpc)) { 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"); throw xmlrpc_error(XMLRPC_NO_SUCH_METHOD_ERROR, "method '" + method_name + "' not defined");
} }
torrent::Object params_raw = torrent::Object::create_list(); torrent::Object params_raw = torrent::Object::create_list();
torrent::Object::list_type& params = params_raw.as_list(); torrent::Object::list_type& params = params_raw.as_list();
rpc::target_type target = rpc::make_target(); rpc::target_type target = rpc::make_target();
if (params_element != nullptr) { if (params_element != nullptr) {
if (std::strncmp(params_element->Name(), "params", sizeof("params")) == 0) { if (std::strncmp(params_element->Name(), "params", sizeof("params")) == 0) {
// Parse out the target if available // 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"); throw xmlrpc_error(XMLRPC_PARSE_ERROR, "methodCall element not found");
if (doc->FirstChildElement("methodCall")->FirstChildElement("methodName") == nullptr) if (doc->FirstChildElement("methodCall")->FirstChildElement("methodName") == nullptr)
throw xmlrpc_error(XMLRPC_PARSE_ERROR, "methodName element not found"); 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; torrent::Object result;
// Add a shim here for system.multicall to allow better code reuse, and // 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 // because system.multicall is one of the few methods that doesn't take a target
if (method_name == std::string("system.multicall")) { if (method_name == std::string("system.multicall")) {
result = torrent::Object::create_list(); result = torrent::Object::create_list();
auto& result_list = result.as_list(); auto& result_list = result.as_list();
auto parent_elements = element_access(doc->RootElement(), {"params", "param", "value", "array", "data"}); auto parent_elements = element_access(doc->RootElement(), {"params", "param", "value", "array", "data"});
for (auto child = parent_elements->FirstChildElement("value"); child; child = child->NextSiblingElement("value")) { for (auto child = parent_elements->FirstChildElement("value"); child; child = child->NextSiblingElement("value")) {
auto sub_method_name = element_access(child, {"struct", "member", "value", "string"})->GetText(); 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, // 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)); sub_result.as_list().push_back(execute_command(sub_method_name, sub_params));
result_list.push_back(sub_result); result_list.push_back(sub_result);
} catch (xmlrpc_error& e) { } 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()["faultString"] = e.what();
fault.as_map()["faultCode"] = e.type(); fault.as_map()["faultCode"] = e.type();
result_list.push_back(fault); result_list.push_back(fault);
} catch (torrent::local_error& e) { } 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()["faultString"] = e.what();
fault.as_map()["faultCode"] = XMLRPC_INTERNAL_ERROR; fault.as_map()["faultCode"] = XMLRPC_INTERNAL_ERROR;
result_list.push_back(fault); result_list.push_back(fault);
} }
} }
@@ -338,7 +303,6 @@ process_document(const tinyxml2::XMLDocument* doc, tinyxml2::XMLPrinter* printer
printer->CloseElement(true); printer->CloseElement(true);
} }
void void
print_xmlrpc_fault(int faultCode, std::string faultString, tinyxml2::XMLPrinter* printer) { print_xmlrpc_fault(int faultCode, std::string faultString, tinyxml2::XMLPrinter* printer) {
printer->PushHeader(false, true); printer->PushHeader(false, true);
@@ -379,7 +343,7 @@ XmlRpc::process(const char* inBuffer, uint32_t length, slot_write slotWrite) {
if (length > m_sizeLimit) { if (length > m_sizeLimit) {
tinyxml2::XMLPrinter printer(nullptr, true, 0); tinyxml2::XMLPrinter printer(nullptr, true, 0);
print_xmlrpc_fault(XMLRPC_LIMIT_EXCEEDED_ERROR, "Content size exceeds maximum XML-RPC limit", &printer); 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; tinyxml2::XMLDocument doc;
doc.Parse(inBuffer, length); doc.Parse(inBuffer, length);
@@ -389,29 +353,36 @@ XmlRpc::process(const char* inBuffer, uint32_t length, slot_write slotWrite) {
// remains. // remains.
tinyxml2::XMLPrinter printer(nullptr, true, 0); tinyxml2::XMLPrinter printer(nullptr, true, 0);
process_document(&doc, &printer); process_document(&doc, &printer);
return slotWrite(printer.CStr(), printer.CStrSize()-1); return slotWrite(printer.CStr(), printer.CStrSize() - 1);
} catch (xmlrpc_error& e) { } catch (xmlrpc_error& e) {
tinyxml2::XMLPrinter printer(nullptr, true, 0); tinyxml2::XMLPrinter printer(nullptr, true, 0);
print_xmlrpc_fault(e.type(), e.what(), &printer); 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) { } catch (torrent::local_error& e) {
tinyxml2::XMLPrinter printer(nullptr, true, 0); tinyxml2::XMLPrinter printer(nullptr, true, 0);
print_xmlrpc_fault(XMLRPC_INTERNAL_ERROR, e.what(), &printer); 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
void XmlRpc::cleanup() {} XmlRpc::initialize() { m_isValid = true; }
void
XmlRpc::cleanup() {}
void XmlRpc::insert_command(const char*, const char*, const char*) {} void
void XmlRpc::set_dialect(int) {} XmlRpc::insert_command(const char*, const char*, const char*) {}
void
XmlRpc::set_dialect(int) {}
int64_t XmlRpc::size_limit() { return static_cast<int64_t>(m_sizeLimit); } int64_t
void XmlRpc::set_size_limit(uint64_t size) { m_sizeLimit = size; } XmlRpc::size_limit() { return static_cast<int64_t>(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 #endif