From: Christian Hofstaedtler Date: Mon, 28 Dec 2015 12:09:46 +0000 (+0100) Subject: API: move returnJsonMessage, returnJsonError to HttpResponse X-Git-Tag: dnsdist-1.0.0-alpha2~123^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=692829aa1ff872d38c9328642ca5285da45a3c72;p=pdns API: move returnJsonMessage, returnJsonError to HttpResponse --- diff --git a/pdns/json.cc b/pdns/json.cc index 0e58c0a86..1a94536ca 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -179,23 +179,3 @@ string makeStringFromDocument(const Document& doc) doc.Accept(w); return string(output.GetString(), output.Size()); } - -string returnJsonError(const string& error) -{ - Document doc; - doc.SetObject(); - Value jerror(error.c_str(), doc.GetAllocator()); // copy - doc.AddMember("error", jerror, doc.GetAllocator()); - return makeStringFromDocument(doc); -} - -/* success response */ -string returnJsonMessage(const string& message) -{ - Document doc; - doc.SetObject(); - Value jmessage; - jmessage.SetString(message.c_str()); - doc.AddMember("result", jmessage, doc.GetAllocator()); - return makeStringFromDocument(doc); -} diff --git a/pdns/json.hh b/pdns/json.hh index 72df62841..6da395f1b 100644 --- a/pdns/json.hh +++ b/pdns/json.hh @@ -27,8 +27,6 @@ #include "rapidjson/document.h" #include "json11.hpp" -std::string returnJsonError(const std::string& error); -std::string returnJsonMessage(const std::string& message); std::string makeStringFromDocument(const rapidjson::Document& doc); int intFromJson(const rapidjson::Value& container, const char* key); int intFromJson(const rapidjson::Value& container, const char* key, const int default_value); diff --git a/pdns/webserver.cc b/pdns/webserver.cc index a6c4ffe7b..3d6cfdd73 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -107,6 +107,18 @@ void HttpResponse::setBody(const json11::Json& document) document.dump(this->body); } +void HttpResponse::setErrorResult(const std::string& message, const int status) +{ + setBody(json11::Json::object { { "error", message } }); + this->status = status; +} + +void HttpResponse::setSuccessResult(const std::string& message, const int status) +{ + setBody(json11::Json::object { { "result", message } }); + this->status = status; +} + static void bareHandlerWrapper(WebServer::HandlerFunction handler, YaHTTP::Request* req, YaHTTP::Response* resp) { // wrapper to convert from YaHTTP::* to our subclasses @@ -166,12 +178,10 @@ static void apiWrapper(WebServer::HandlerFunction handler, HttpRequest* req, Htt resp->status = 200; handler(req, resp); } catch (ApiException &e) { - resp->body = returnJsonError(e.what()); - resp->status = 422; + resp->setErrorResult(e.what(), 422); return; } catch (JsonException &e) { - resp->body = returnJsonError(e.what()); - resp->status = 422; + resp->setErrorResult(e.what(), 422); return; } @@ -281,7 +291,7 @@ HttpResponse WebServer::handleRequest(HttpRequest req) resp.body = "" + what + "

" + what + "

"; } else if (req.accept_json) { resp.headers["Content-Type"] = "application/json"; - resp.body = returnJsonError(what); + resp.setErrorResult(what, resp.status); } else { resp.headers["Content-Type"] = "text/plain; charset=utf-8"; resp.body = what; diff --git a/pdns/webserver.hh b/pdns/webserver.hh index c02489cbd..7038b0d83 100644 --- a/pdns/webserver.hh +++ b/pdns/webserver.hh @@ -57,6 +57,8 @@ public: void setBody(rapidjson::Document& document); void setBody(const json11::Json& document); + void setErrorResult(const std::string& message, const int status); + void setSuccessResult(const std::string& message, const int status = 200); }; diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index b9781aefa..ba3f2c880 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -798,7 +798,7 @@ static void apiServerZoneAxfrRetrieve(HttpRequest* req, HttpResponse* resp) { random_shuffle(di.masters.begin(), di.masters.end()); Communicator.addSuckRequest(zonename, di.masters.front()); - resp->body = returnJsonMessage("Added retrieval request for '"+zonename.toString()+"' from master "+di.masters.front()); + resp->setSuccessResult("Added retrieval request for '"+zonename.toString()+"' from master "+di.masters.front()); } static void apiServerZoneNotify(HttpRequest* req, HttpResponse* resp) { @@ -815,7 +815,7 @@ static void apiServerZoneNotify(HttpRequest* req, HttpResponse* resp) { if(!Communicator.notifyDomain(zonename)) throw ApiException("Failed to add to the queue - see server log"); - resp->body = returnJsonMessage("Notification queued"); + resp->setSuccessResult("Notification queued"); } static void makePtr(const DNSResourceRecord& rr, DNSResourceRecord* ptr) { diff --git a/pdns/ws-recursor.cc b/pdns/ws-recursor.cc index c5665031c..cc3221355 100644 --- a/pdns/ws-recursor.cc +++ b/pdns/ws-recursor.cc @@ -499,8 +499,7 @@ void RecursorWebServer::jsonstat(HttpRequest* req, HttpResponse *resp) resp->setBody(Json::object { { "entries", entries } }); return; } else { - resp->status = 404; - resp->body = returnJsonError("Command '"+command+"' not found"); + resp->setErrorResult("Command '"+command+"' not found", 404); } }