From: Christian Hofstaedtler Date: Mon, 28 Dec 2015 00:54:13 +0000 (+0100) Subject: API: Lay infra for using json11 X-Git-Tag: dnsdist-1.0.0-alpha2~123^2~16 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5938c49f693267ceebe30e72b05357c876b3806b;p=pdns API: Lay infra for using json11 --- diff --git a/build-scripts/dist-recursor b/build-scripts/dist-recursor index b066ddc9d..5da313f60 100755 --- a/build-scripts/dist-recursor +++ b/build-scripts/dist-recursor @@ -83,6 +83,7 @@ cp -a ../ext/mbedtls/library/{aes.c,base64.c,md.c,md_wrap.c,md5.c,sha1.c,sha256. cp -a ../ext/mbedtls/library/{rsa.c,bignum.c,oid.c,asn1parse.c,ctr_drbg.c,entropy.c,entropy_poll.c,timing.c,ecp.c,ecdsa.c,ecp_curves.c,hmac_drbg.c,asn1write.c} $DIRNAME/ext/mbedtls/library cp -a ../ext/yahttp/ $DIRNAME/ext/yahttp +cp -a ../ext/json11/ $DIRNAME/ext/json11 mkdir -p $DIRNAME/ext/luawrapper/include cp ../ext/luawrapper/include/LuaContext.hpp $DIRNAME/ext/luawrapper/include diff --git a/pdns/Makefile-recursor b/pdns/Makefile-recursor index 2a57156d1..48ceb9238 100644 --- a/pdns/Makefile-recursor +++ b/pdns/Makefile-recursor @@ -4,7 +4,7 @@ BINDIR=/usr/bin/ SYSCONFDIR=/etc/powerdns/ LOCALSTATEDIR=/var/run/ OPTFLAGS?=-O3 -CXXFLAGS:= $(CXXFLAGS) -Iext/rapidjson/include -I$(CURDIR)/ext/mbedtls/include -Wall @CF_PIE@ @CF_FORTIFY@ @CF_STACK@ $(OPTFLAGS) $(PROFILEFLAGS) $(ARCHFLAGS) -pthread -Iext/yahttp -DHAVE_CONFIG_H +CXXFLAGS:= $(CXXFLAGS) -Iext/rapidjson/include -I$(CURDIR)/ext/mbedtls/include -I$(CURDIR)/ext/json11 -Wall @CF_PIE@ @CF_FORTIFY@ @CF_STACK@ $(OPTFLAGS) $(PROFILEFLAGS) $(ARCHFLAGS) -pthread -Iext/yahttp -DHAVE_CONFIG_H CFLAGS:=$(CFLAGS) -Wall $(OPTFLAGS) @CF_PIE@ @CF_FORTIFY@ @CF_STACK@ $(PROFILEFLAGS) $(ARCHFLAGS) -I$(CURDIR)/ext/mbedtls/include -pthread -DHAVE_CONFIG_H LDFLAGS:=$(LDFLAGS) $(ARCHFLAGS) -pthread @LD_RELRO@ @CF_STACK@ @LD_PIE@ STRIP_BINARIES?=1 @@ -29,6 +29,7 @@ ext/mbedtls/library/hmac_drbg.o ext/mbedtls/library/asn1write.o \ ext/mbedtls/library/bignum.o ext/mbedtls/library/oid.o ext/mbedtls/library/asn1parse.o \ ext/mbedtls/library/ctr_drbg.o ext/mbedtls/library/entropy.o ext/mbedtls/library/entropy_poll.o\ ext/mbedtls/library/timing.o \ +ext/json11/json11.o \ lua-recursor4.o randomhelper.o recpacketcache.o dns.o \ reczones.o base32.o nsecrecords.o json.o ws-recursor.o ws-api.o \ version.o responsestats.o webserver.o ext/yahttp/yahttp/reqresp.o ext/yahttp/yahttp/router.o \ @@ -119,7 +120,7 @@ clean: binclean -rm -f dep *~ *.gcda *.gcno optional/*.gcda optional/*.gcno binclean: - -rm -f *.o pdns_hw pdns_recursor rec_control optional/*.o build-stamp ext/library/*.o ext/yahttp/yahttp/*.o + -rm -f *.o pdns_hw pdns_recursor rec_control optional/*.o build-stamp ext/library/*.o ext/yahttp/yahttp/*.o ext/json11/*.o dep: $(CXX) $(CXXFLAGS) -MM -MG *.cc *.hh > $@ diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 662e71534..a2af6b769 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1,3 +1,5 @@ +JSON11_LIBS = -L$(top_srcdir)/ext/json11 -ljson11 + AM_CPPFLAGS += \ -I$(top_srcdir)/ext/json11 \ -I$(top_srcdir)/ext/rapidjson/include \ @@ -207,7 +209,8 @@ pdns_server_LDADD = \ @modulelibs@ \ $(LIBDL) \ $(MBEDTLS_LIBS) \ - $(YAHTTP_LIBS) + $(YAHTTP_LIBS) \ + $(JSON11_LIBS) if BOTAN110 pdns_server_SOURCES += botan110signers.cc botansigners.cc @@ -307,7 +310,8 @@ pdnsutil_LDADD = \ $(LIBDL) \ $(MBEDTLS_LIBS) \ $(BOOST_PROGRAM_OPTIONS_LIBS) \ - $(YAHTTP_LIBS) + $(YAHTTP_LIBS) \ + $(JSON11_LIBS) if BOTAN110 pdnsutil_SOURCES += botan110signers.cc botansigners.cc @@ -374,7 +378,7 @@ zone2sql_SOURCES = \ zone2sql.cc \ zoneparser-tng.cc -zone2sql_LDADD = $(MBEDTLS_LIBS) +zone2sql_LDADD = $(MBEDTLS_LIBS) $(JSON11_LIBS) zone2json_SOURCES = \ arguments.cc \ @@ -400,7 +404,7 @@ zone2json_SOURCES = \ zone2json.cc \ zoneparser-tng.cc -zone2json_LDADD = $(MBEDTLS_LIBS) -L$(top_srcdir)/ext/json11 -ljson11 +zone2json_LDADD = $(MBEDTLS_LIBS) $(JSON11_LIBS) # pkglib_LTLIBRARIES = iputils.la # iputils_la_SOURCES = lua-iputils.cc @@ -1157,7 +1161,8 @@ pdns_recursor_SOURCES = \ pdns_recursor_LDADD = \ $(MBEDTLS_LIBS) \ - $(YAHTTP_LIBS) + $(YAHTTP_LIBS) \ + $(JSON11_LIBS) if PKCS11 pdns_recursor_SOURCES += pkcs11signers.cc pkcs11signers.hh diff --git a/pdns/json.cc b/pdns/json.cc index ee6022afe..b0bd9e513 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -31,6 +31,7 @@ #include "rapidjson/writer.h" using namespace rapidjson; +using json11::Json; int intFromJson(const Value& container, const char* key) { @@ -63,6 +64,31 @@ int intFromJson(const Value& container, const char* key, const int default_value } } +int intFromJson(const Json container, const std::string& key) +{ + auto val = container[key]; + if (val.is_number()) { + return val.int_value(); + } else if (val.is_string()) { + return std::stoi(val.string_value()); + } else { + throw JsonException("Key '" + string(key) + "' not an Integer or not present"); + } +} + +int intFromJson(const Json container, const std::string& key, const int default_value) +{ + auto val = container[key]; + if (val.is_number()) { + return val.int_value(); + } else if (val.is_string()) { + return std::stoi(val.string_value()); + } else { + // TODO: check if value really isn't present + return default_value; + } +} + string stringFromJson(const Value& container, const char* key) { if (!container.IsObject()) { @@ -76,6 +102,16 @@ string stringFromJson(const Value& container, const char* key) } } +string stringFromJson(const Json container, const std::string &key) +{ + const Json val = container[key]; + if (val.is_string()) { + return val.string_value(); + } else { + throw JsonException("Key '" + string(key) + "' not present or not a String"); + } +} + string stringFromJson(const Value& container, const char* key, const string& default_value) { if (!container.IsObject()) { @@ -116,6 +152,26 @@ bool boolFromJson(const rapidjson::Value& container, const char* key, const bool } } +bool boolFromJson(const json11::Json container, const std::string& key) +{ + auto val = container[key]; + if (val.is_bool()) { + return val.bool_value(); + } else { + throw JsonException("Key '" + string(key) + "' not present or not a Bool"); + } +} + +bool boolFromJson(const json11::Json container, const std::string& key, const bool default_value) +{ + auto val = container[key]; + if (val.is_bool()) { + return val.bool_value(); + } else { + return default_value; + } +} + string makeStringFromDocument(const Document& doc) { StringBuffer output; diff --git a/pdns/json.hh b/pdns/json.hh index 774631d8e..726f8de45 100644 --- a/pdns/json.hh +++ b/pdns/json.hh @@ -25,6 +25,7 @@ #include #include #include "rapidjson/document.h" +#include "json11.hpp" std::string returnJsonObject(const std::map& items); std::string returnJsonError(const std::string& error); @@ -32,10 +33,15 @@ 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); +int intFromJson(const json11::Json container, const std::string& key); +int intFromJson(const json11::Json container, const std::string& key, const int default_value); std::string stringFromJson(const rapidjson::Value& container, const char* key); std::string stringFromJson(const rapidjson::Value& container, const char* key, const std::string& default_value); +std::string stringFromJson(const json11::Json container, const std::string &key); bool boolFromJson(const rapidjson::Value& container, const char* key); bool boolFromJson(const rapidjson::Value& container, const char* key, const bool default_value); +bool boolFromJson(const json11::Json container, const std::string& key); +bool boolFromJson(const json11::Json container, const std::string& key, const bool default_value); class JsonException : public std::runtime_error { diff --git a/pdns/webserver.cc b/pdns/webserver.cc index 81b2cd140..a6c4ffe7b 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -51,6 +51,21 @@ void HttpRequest::json(rapidjson::Document& document) } } +json11::Json HttpRequest::json() +{ + string err; + if(this->body.empty()) { + L<body, err); + if (doc.is_null()) { + L<body = makeStringFromDocument(document); } +void HttpResponse::setBody(const json11::Json& document) +{ + document.dump(this->body); +} + static void bareHandlerWrapper(WebServer::HandlerFunction handler, YaHTTP::Request* req, YaHTTP::Response* resp) { // wrapper to convert from YaHTTP::* to our subclasses diff --git a/pdns/webserver.hh b/pdns/webserver.hh index 7eee5e266..c02489cbd 100644 --- a/pdns/webserver.hh +++ b/pdns/webserver.hh @@ -29,6 +29,7 @@ #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" +#include "json11.hpp" #include "namespaces.hh" #include "sstuff.hh" @@ -42,6 +43,7 @@ public: bool accept_html; bool complete; void json(rapidjson::Document& document); + json11::Json json(); // checks password _only_. bool compareAuthorization(const string &expected_password); @@ -54,6 +56,7 @@ public: HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { }; void setBody(rapidjson::Document& document); + void setBody(const json11::Json& document); };