From e3429df9c44cceb1f0777cfc05b5b8e3f2de1e86 Mon Sep 17 00:00:00 2001 From: Fred Morcos Date: Wed, 13 Dec 2023 15:35:35 +0100 Subject: [PATCH] Delint ws-api.cc --- pdns/ws-api.cc | 112 ++++++++++++++++++++++++++++--------------------- pdns/ws-api.hh | 2 +- 2 files changed, 66 insertions(+), 48 deletions(-) diff --git a/pdns/ws-api.cc b/pdns/ws-api.cc index 1484826415431..7bfae3f3b7c98 100644 --- a/pdns/ws-api.cc +++ b/pdns/ws-api.cc @@ -38,9 +38,9 @@ #include "responsestats.hh" #include "statbag.hh" #endif -#include -#include -#include +#include +#include +#include #include #include @@ -112,8 +112,9 @@ static Json getServerDetail() */ void apiDiscovery(HttpRequest* req, HttpResponse* resp) { - if (req->method != "GET") + if (req->method != "GET") { throw HttpMethodNotAllowedException(); + } Json version1 = Json::object{ {"version", 1}, @@ -125,8 +126,9 @@ void apiDiscovery(HttpRequest* req, HttpResponse* resp) void apiDiscoveryV1(HttpRequest* req, HttpResponse* resp) { - if (req->method != "GET") + if (req->method != "GET") { throw HttpMethodNotAllowedException(); + } Json version1 = Json::object{ {"server_url", "/api/v1/servers{/server}"}, @@ -138,8 +140,9 @@ void apiDiscoveryV1(HttpRequest* req, HttpResponse* resp) void apiServer(HttpRequest* req, HttpResponse* resp) { - if (req->method != "GET") + if (req->method != "GET") { throw HttpMethodNotAllowedException(); + } Json doc = Json::array{getServerDetail()}; resp->setJsonBody(doc); @@ -147,25 +150,29 @@ void apiServer(HttpRequest* req, HttpResponse* resp) void apiServerDetail(HttpRequest* req, HttpResponse* resp) { - if (req->method != "GET") + if (req->method != "GET") { throw HttpMethodNotAllowedException(); + } resp->setJsonBody(getServerDetail()); } void apiServerConfig(HttpRequest* req, HttpResponse* resp) { - if (req->method != "GET") + if (req->method != "GET") { throw HttpMethodNotAllowedException(); + } vector items = ::arg().list(); string value; Json::array doc; for (const string& item : items) { - if (item.find("password") != string::npos || item.find("api-key") != string::npos) + if (item.find("password") != string::npos || item.find("api-key") != string::npos) { value = "***"; - else + } + else { value = ::arg()[item]; + } doc.push_back(Json::object{ {"type", "ConfigSetting"}, @@ -178,8 +185,9 @@ void apiServerConfig(HttpRequest* req, HttpResponse* resp) void apiServerStatistics(HttpRequest* req, HttpResponse* resp) { - if (req->method != "GET") + if (req->method != "GET") { throw HttpMethodNotAllowedException(); + } Json::array doc; string name = req->getvars["statistic"]; @@ -225,8 +233,9 @@ void apiServerStatistics(HttpRequest* req, HttpResponse* resp) { Json::array values; for (const auto& item : resp_qtype_stats) { - if (item.second == 0) + if (item.second == 0) { continue; + } values.push_back(Json::object{ {"name", DNSRecordContent::NumberToType(item.first)}, {"value", std::to_string(item.second)}, @@ -243,8 +252,9 @@ void apiServerStatistics(HttpRequest* req, HttpResponse* resp) { Json::array values; for (const auto& item : resp_size_stats) { - if (item.second == 0) + if (item.second == 0) { continue; + } values.push_back(Json::object{ {"name", std::to_string(item.first)}, @@ -262,8 +272,10 @@ void apiServerStatistics(HttpRequest* req, HttpResponse* resp) { Json::array values; for (const auto& item : resp_rcode_stats) { - if (item.second == 0) + if (item.second == 0) { continue; + } + values.push_back(Json::object{ {"name", RCode::to_s(item.first)}, {"value", std::to_string(item.second)}, @@ -278,13 +290,14 @@ void apiServerStatistics(HttpRequest* req, HttpResponse* resp) } #ifndef RECURSOR - if (!req->getvars.count("includerings") || req->getvars["includerings"] != "false") { + if ((req->getvars.count("includerings") == 0) || req->getvars["includerings"] != "false") { for (const auto& ringName : S.listRings()) { Json::array values; const auto& ring = S.getRing(ringName); for (const auto& item : ring) { - if (item.second == 0) + if (item.second == 0) { continue; + } values.push_back(Json::object{ {"name", item.first}, @@ -318,50 +331,52 @@ DNSName apiNameToDNSName(const string& name) } } -DNSName apiZoneIdToName(const string& id) +DNSName apiZoneIdToName(const string& identifier) { string zonename; - ostringstream ss; + ostringstream outputStringStream; - if (id.empty()) + if (identifier.empty()) { throw HttpBadRequestException(); + } - std::size_t lastpos = 0, pos = 0; - while ((pos = id.find('=', lastpos)) != string::npos) { - ss << id.substr(lastpos, pos - lastpos); - char c; + std::size_t lastpos = 0; + std::size_t pos = 0; + while ((pos = identifier.find('=', lastpos)) != string::npos) { + outputStringStream << identifier.substr(lastpos, pos - lastpos); + char currentChar{}; // decode tens - if (id[pos + 1] >= '0' && id[pos + 1] <= '9') { - c = id[pos + 1] - '0'; + if (identifier[pos + 1] >= '0' && identifier[pos + 1] <= '9') { + currentChar = static_cast(identifier[pos + 1] - '0'); } - else if (id[pos + 1] >= 'A' && id[pos + 1] <= 'F') { - c = id[pos + 1] - 'A' + 10; + else if (identifier[pos + 1] >= 'A' && identifier[pos + 1] <= 'F') { + currentChar = static_cast(identifier[pos + 1] - 'A' + 10); } else { throw HttpBadRequestException(); } - c = c * 16; + currentChar = static_cast(currentChar * 16); // decode unit place - if (id[pos + 2] >= '0' && id[pos + 2] <= '9') { - c += id[pos + 2] - '0'; + if (identifier[pos + 2] >= '0' && identifier[pos + 2] <= '9') { + currentChar = static_cast(currentChar + identifier[pos + 2] - '0'); } - else if (id[pos + 2] >= 'A' && id[pos + 2] <= 'F') { - c += id[pos + 2] - 'A' + 10; + else if (identifier[pos + 2] >= 'A' && identifier[pos + 2] <= 'F') { + currentChar = static_cast(currentChar + identifier[pos + 2] - 'A' + 10); } else { throw HttpBadRequestException(); } - ss << c; + outputStringStream << currentChar; lastpos = pos + 3; } if (lastpos < pos) { - ss << id.substr(lastpos, pos - lastpos); + outputStringStream << identifier.substr(lastpos, pos - lastpos); } - zonename = ss.str(); + zonename = outputStringStream.str(); try { return DNSName(zonename); @@ -374,42 +389,45 @@ DNSName apiZoneIdToName(const string& id) string apiZoneNameToId(const DNSName& dname) { string name = dname.toString(); - ostringstream ss; + ostringstream outputStringStream; for (char iter : name) { if ((iter >= 'A' && iter <= 'Z') || (iter >= 'a' && iter <= 'z') || (iter >= '0' && iter <= '9') || (iter == '.') || (iter == '-')) { - ss << iter; + outputStringStream << iter; } else { - ss << (boost::format("=%02X") % (int)iter); + outputStringStream << (boost::format("=%02X") % (int)iter); } } - string id = ss.str(); + string identifier = outputStringStream.str(); // add trailing dot - if (id.size() == 0 || id.substr(id.size() - 1) != ".") { - id += "."; + if (identifier.empty() || identifier.substr(identifier.size() - 1) != ".") { + identifier += "."; } // special handling for the root zone, as a dot on it's own doesn't work // everywhere. - if (id == ".") { - id = (boost::format("=%02X") % (int)('.')).str(); + if (identifier == ".") { + identifier = (boost::format("=%02X") % (int)('.')).str(); } - return id; + return identifier; } void apiCheckNameAllowedCharacters(const string& name) { - if (name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_/.-") != std::string::npos) + if (name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_/.-") != std::string::npos) { throw ApiException("Name '" + name + "' contains unsupported characters"); + } } void apiCheckQNameAllowedCharacters(const string& qname) { - if (qname.compare(0, 2, "*.") == 0) + if (qname.compare(0, 2, "*.") == 0) { apiCheckNameAllowedCharacters(qname.substr(2)); - else + } + else { apiCheckNameAllowedCharacters(qname); + } } diff --git a/pdns/ws-api.hh b/pdns/ws-api.hh index 9f95f50b24319..7c3059382c021 100644 --- a/pdns/ws-api.hh +++ b/pdns/ws-api.hh @@ -32,7 +32,7 @@ void apiServerConfig(HttpRequest* req, HttpResponse* resp); void apiServerStatistics(HttpRequest* req, HttpResponse* resp); // helpers -DNSName apiZoneIdToName(const string& id); +DNSName apiZoneIdToName(const string& identifier); string apiZoneNameToId(const DNSName& name); void apiCheckNameAllowedCharacters(const string& name); void apiCheckQNameAllowedCharacters(const string& name);