]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ws-recursor.cc: Split apiServerZones to GET and POST variant
authorAki Tuomi <cmouse@cmouse.fi>
Wed, 23 Aug 2023 12:59:52 +0000 (15:59 +0300)
committerAki Tuomi <cmouse@cmouse.fi>
Fri, 15 Dec 2023 09:48:00 +0000 (11:48 +0200)
pdns/recursordist/ws-recursor.cc

index 41bcdd770297d16aff7bb7cd97524945d587b145..89d86238c2b2edaa9dc1b3fa405e345032e238d1 100644 (file)
@@ -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/<id>", apiServerZoneDetail, "GET");
   d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>", apiServerZoneDetail, "PUT");
   d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>", 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");