From: Christian Hofstaedtler Date: Fri, 31 Jan 2014 00:27:34 +0000 (+0100) Subject: webserver: add HttpResponse.setBody(json document) X-Git-Tag: rec-3.6.0-rc1~211^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=669822d0b25c47e235a4538875987f1c050acf63;p=thirdparty%2Fpdns.git webserver: add HttpResponse.setBody(json document) Convenience method. --- diff --git a/pdns/webserver.cc b/pdns/webserver.cc index 20bf17b510..0c18cd2ccb 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -45,6 +45,11 @@ void HttpRequest::json(rapidjson::Document& document) } } +void HttpResponse::setBody(rapidjson::Document& document) +{ + this->body = makeStringFromDocument(document); +} + int WebServer::B64Decode(const std::string& strInput, std::string& strOutput) { return ::B64Decode(strInput, strOutput); diff --git a/pdns/webserver.hh b/pdns/webserver.hh index 8aef0c47bf..d56c1120d7 100644 --- a/pdns/webserver.hh +++ b/pdns/webserver.hh @@ -49,6 +49,8 @@ public: HttpResponse() : YaHTTP::Response() { }; HttpResponse(const YaHTTP::Request &req) : YaHTTP::Response(req) { }; HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { }; + + void setBody(rapidjson::Document& document); }; diff --git a/pdns/ws-api.cc b/pdns/ws-api.cc index da14b57492..b0e097b3c2 100644 --- a/pdns/ws-api.cc +++ b/pdns/ws-api.cc @@ -97,7 +97,7 @@ void apiServer(HttpRequest* req, HttpResponse* resp) { Value server; fillServerDetail(server, doc.GetAllocator()); doc.PushBack(server, doc.GetAllocator()); - resp->body = makeStringFromDocument(doc); + resp->setBody(doc); } void apiServerDetail(HttpRequest* req, HttpResponse* resp) { @@ -106,7 +106,7 @@ void apiServerDetail(HttpRequest* req, HttpResponse* resp) { Document doc; fillServerDetail(doc, doc.GetAllocator()); - resp->body = makeStringFromDocument(doc); + resp->setBody(doc); } void apiServerConfig(HttpRequest* req, HttpResponse* resp) { @@ -135,7 +135,7 @@ void apiServerConfig(HttpRequest* req, HttpResponse* resp) { doc.PushBack(jitem, doc.GetAllocator()); } - resp->body = makeStringFromDocument(doc); + resp->setBody(doc); } static string logGrep(const string& q, const string& fname, const string& prefix) @@ -220,5 +220,5 @@ void apiServerStatistics(HttpRequest* req, HttpResponse* resp) { doc.PushBack(jitem, doc.GetAllocator()); } - resp->body = makeStringFromDocument(doc); + resp->setBody(doc); } diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index d4a2f4871a..1db1a30f63 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -271,11 +271,11 @@ void AuthWebServer::indexfunction(HttpRequest* req, HttpResponse* resp) resp->body = ret.str(); } -static string getZone(const string& zonename) { +static void fillZone(const string& zonename, HttpResponse* resp) { UeberBackend B; DomainInfo di; if(!B.getDomainInfo(zonename, di)) - return returnJsonError("Could not find domain '"+zonename+"'"); + throw ApiException("Could not find domain '"+zonename+"'"); Document doc; doc.SetObject(); @@ -321,7 +321,7 @@ static string getZone(const string& zonename) { } doc.AddMember("records", records, doc.GetAllocator()); - return makeStringFromDocument(doc); + resp->setBody(doc); } void productServerStatisticsFetch(map& out) @@ -404,7 +404,7 @@ static void apiServerZones(HttpRequest* req, HttpResponse* resp) { di.backend->setKind(zonename, DomainInfo::stringToKind(kind)); di.backend->setMaster(zonename, master); - resp->body = getZone(zonename); + fillZone(zonename, resp); return; } @@ -439,7 +439,7 @@ static void apiServerZones(HttpRequest* req, HttpResponse* resp) { jdi.AddMember("last_check", (unsigned int) di.last_check, doc.GetAllocator()); doc.PushBack(jdi, doc.GetAllocator()); } - resp->body = makeStringFromDocument(doc); + resp->setBody(doc); } static void apiServerZoneDetail(HttpRequest* req, HttpResponse* resp) { @@ -466,7 +466,7 @@ static void apiServerZoneDetail(HttpRequest* req, HttpResponse* resp) { di.backend->setKind(zonename, DomainInfo::stringToKind(stringFromJson(document, "kind"))); di.backend->setMaster(zonename, master); - resp->body = getZone(zonename); + fillZone(zonename, resp); return; } else if(req->method == "DELETE") { @@ -487,7 +487,7 @@ static void apiServerZoneDetail(HttpRequest* req, HttpResponse* resp) { if(req->method != "GET") throw HttpMethodNotAllowedException(); - resp->body = getZone(zonename); + fillZone(zonename, resp); } static void apiServerZoneRRset(HttpRequest* req, HttpResponse* resp) { diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index 657a049060..53b4f10089 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -98,7 +98,7 @@ void RecursorWebServer::jsonstat(HttpRequest* req, HttpResponse *resp) doc.PushBack(jzone, doc.GetAllocator()); } - resp->body = makeStringFromDocument(doc); + resp->setBody(doc); return; } else if(command == "zone") { @@ -143,7 +143,7 @@ void RecursorWebServer::jsonstat(HttpRequest* req, HttpResponse *resp) root.AddMember("records", records, doc.GetAllocator()); doc.AddMember("zone", root, doc.GetAllocator()); - resp->body = makeStringFromDocument(doc); + resp->setBody(doc); return; } else { resp->body = returnJsonError("Could not find domain '"+arg_zone+"'");