From: Aki Tuomi Date: Wed, 23 Aug 2023 12:59:52 +0000 (+0300) Subject: ws-recursor.cc: Split apiServerZones to GET and POST variant X-Git-Tag: auth-4.9.0-alpha1~42^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e15a061881306bfcfdecaef01c2cfcf013aec0f5;p=thirdparty%2Fpdns.git ws-recursor.cc: Split apiServerZones to GET and POST variant --- diff --git a/pdns/recursordist/ws-recursor.cc b/pdns/recursordist/ws-recursor.cc index 41bcdd7702..89d86238c2 100644 --- a/pdns/recursordist/ws-recursor.cc +++ b/pdns/recursordist/ws-recursor.cc @@ -346,38 +346,34 @@ static bool doDeleteZone(const DNSName& zonename) return true; } -static void apiServerZones(HttpRequest* req, HttpResponse* resp) +static void apiServerZonesPOST(HttpRequest* req, HttpResponse* resp) { - if (req->method == "POST") { - if (::arg()["api-config-dir"].empty()) { - throw ApiException("Config Option \"api-config-dir\" must be set"); - } - - Json document = req->json(); + if (::arg()["api-config-dir"].empty()) { + throw ApiException("Config Option \"api-config-dir\" must be set"); + } - DNSName zonename = apiNameToDNSName(stringFromJson(document, "name")); + Json document = req->json(); - auto iter = SyncRes::t_sstorage.domainmap->find(zonename); - if (iter != SyncRes::t_sstorage.domainmap->end()) { - throw ApiException("Zone already exists"); - } + DNSName zonename = apiNameToDNSName(stringFromJson(document, "name")); - doCreateZone(document); - reloadZoneConfiguration(g_yamlSettings); - fillZone(zonename, resp); - resp->status = 201; - return; + const auto& iter = SyncRes::t_sstorage.domainmap->find(zonename); + if (iter != SyncRes::t_sstorage.domainmap->cend()) { + throw ApiException("Zone already exists"); } - if (req->method != "GET") { - throw HttpMethodNotAllowedException(); - } + doCreateZone(document); + reloadZoneConfiguration(g_yamlSettings); + fillZone(zonename, resp); + resp->status = 201; +} +static void apiServerZonesGET(HttpRequest* /* req */, HttpResponse* resp) +{ Json::array doc; - for (const SyncRes::domainmap_t::value_type& val : *SyncRes::t_sstorage.domainmap) { + for (const auto& val : *SyncRes::t_sstorage.domainmap) { const SyncRes::AuthDomain& zone = val.second; Json::array servers; - for (const ComboAddress& server : zone.d_servers) { + for (const auto& server : zone.d_servers) { servers.emplace_back(server.toStringWithPort()); } // id is the canonical lookup key, which doesn't actually match the name (in some cases) @@ -1325,8 +1321,8 @@ RecursorWebServer::RecursorWebServer(FDMultiplexer* fdm) d_ws->registerApiHandler("/api/v1/servers/localhost/zones/", apiServerZoneDetail, "GET"); d_ws->registerApiHandler("/api/v1/servers/localhost/zones/", apiServerZoneDetail, "PUT"); d_ws->registerApiHandler("/api/v1/servers/localhost/zones/", apiServerZoneDetail, "DELETE"); - d_ws->registerApiHandler("/api/v1/servers/localhost/zones", apiServerZones, "GET"); - d_ws->registerApiHandler("/api/v1/servers/localhost/zones", apiServerZones, "POST"); + d_ws->registerApiHandler("/api/v1/servers/localhost/zones", apiServerZonesGET, "GET"); + d_ws->registerApiHandler("/api/v1/servers/localhost/zones", apiServerZonesPOST, "POST"); d_ws->registerApiHandler("/api/v1/servers/localhost", apiServerDetail, "GET", true); d_ws->registerApiHandler("/api/v1/servers", apiServer, "GET"); d_ws->registerApiHandler("/api/v1", apiDiscoveryV1, "GET");