From: Christian Hofstaedtler Date: Wed, 12 Feb 2014 15:32:18 +0000 (+0100) Subject: Fix JSON API to return an object for /config/allow-from X-Git-Tag: rec-3.6.0-rc1~181^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bd0320fe616f38d9cc900877e96a3fbf037cba6c;p=pdns Fix JSON API to return an object for /config/allow-from Can't return arrays for stuff that isn't a collection. --- diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index fc262d622..98109ca4a 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -66,9 +66,14 @@ static void apiServerConfigAllowFrom(HttpRequest* req, HttpResponse* resp) if (req->method == "PUT") { Document document; req->json(document); + const Value &jlist = document["value"]; - if (!document.IsArray()) { - throw ApiException("Supplied JSON must be an array"); + if (!document.IsObject()) { + throw ApiException("Supplied JSON must be an object"); + } + + if (!jlist.IsArray()) { + throw ApiException("'value' must be an array"); } ostringstream ss; @@ -78,8 +83,8 @@ static void apiServerConfigAllowFrom(HttpRequest* req, HttpResponse* resp) // Clear allow-from, and provide a "parent" value ss << "allow-from=" << endl; - for (SizeType i = 0; i < document.Size(); ++i) { - ss << "allow-from+=" << document[i].GetString() << endl; + for (SizeType i = 0; i < jlist.Size(); ++i) { + ss << "allow-from+=" << jlist[i].GetString() << endl; } apiWriteConfigFile("allow-from", ss.str()); @@ -93,16 +98,22 @@ static void apiServerConfigAllowFrom(HttpRequest* req, HttpResponse* resp) // Return currently configured ACLs Document document; - document.SetArray(); + document.SetObject(); + + Value jlist; + jlist.SetArray(); vector entries; t_allowFrom->toStringVector(&entries); BOOST_FOREACH(const string& entry, entries) { Value jentry(entry.c_str(), document.GetAllocator()); // copy - document.PushBack(jentry, document.GetAllocator()); + jlist.PushBack(jentry, document.GetAllocator()); } + document.AddMember("name", "allow-from", document.GetAllocator()); + document.AddMember("value", jlist, document.GetAllocator()); + resp->setBody(document); } diff --git a/regression-tests.api/test_RecursorConfig.py b/regression-tests.api/test_RecursorConfig.py index 7e10b66e8..30ec07169 100644 --- a/regression-tests.api/test_RecursorConfig.py +++ b/regression-tests.api/test_RecursorConfig.py @@ -12,11 +12,11 @@ class RecursorConfig(ApiTestCase): self.assertSuccessJson(r) def test_ConfigAllowFromReplace(self): - payload = ["127.0.0.1"] + payload = {'value': ["127.0.0.1"]} r = self.session.put( self.url("/servers/localhost/config/allow-from"), data=json.dumps(payload), headers={'content-type': 'application/json'}) self.assertSuccessJson(r) data = r.json() - self.assertEquals("127.0.0.1/32", data[0]) + self.assertEquals("127.0.0.1/32", data["value"][0])