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
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
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 \
-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 > $@
+JSON11_LIBS = -L$(top_srcdir)/ext/json11 -ljson11
+
AM_CPPFLAGS += \
-I$(top_srcdir)/ext/json11 \
-I$(top_srcdir)/ext/rapidjson/include \
@modulelibs@ \
$(LIBDL) \
$(MBEDTLS_LIBS) \
- $(YAHTTP_LIBS)
+ $(YAHTTP_LIBS) \
+ $(JSON11_LIBS)
if BOTAN110
pdns_server_SOURCES += botan110signers.cc botansigners.cc
$(LIBDL) \
$(MBEDTLS_LIBS) \
$(BOOST_PROGRAM_OPTIONS_LIBS) \
- $(YAHTTP_LIBS)
+ $(YAHTTP_LIBS) \
+ $(JSON11_LIBS)
if BOTAN110
pdnsutil_SOURCES += botan110signers.cc botansigners.cc
zone2sql.cc \
zoneparser-tng.cc
-zone2sql_LDADD = $(MBEDTLS_LIBS)
+zone2sql_LDADD = $(MBEDTLS_LIBS) $(JSON11_LIBS)
zone2json_SOURCES = \
arguments.cc \
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
pdns_recursor_LDADD = \
$(MBEDTLS_LIBS) \
- $(YAHTTP_LIBS)
+ $(YAHTTP_LIBS) \
+ $(JSON11_LIBS)
if PKCS11
pdns_recursor_SOURCES += pkcs11signers.cc pkcs11signers.hh
#include "rapidjson/writer.h"
using namespace rapidjson;
+using json11::Json;
int intFromJson(const Value& container, const char* key)
{
}
}
+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()) {
}
}
+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()) {
}
}
+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;
#include <map>
#include <stdexcept>
#include "rapidjson/document.h"
+#include "json11.hpp"
std::string returnJsonObject(const std::map<std::string, std::string>& items);
std::string returnJsonError(const std::string& error);
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
{
}
}
+json11::Json HttpRequest::json()
+{
+ string err;
+ if(this->body.empty()) {
+ L<<Logger::Debug<<"HTTP: JSON document expected in request body, but body was empty" << endl;
+ throw HttpBadRequestException();
+ }
+ json11::Json doc = json11::Json::parse(this->body, err);
+ if (doc.is_null()) {
+ L<<Logger::Debug<<"HTTP: parsing of JSON document failed:" << err << endl;
+ throw HttpBadRequestException();
+ }
+ return doc;
+}
+
bool HttpRequest::compareAuthorization(const string &expected_password)
{
// validate password
this->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
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
+#include "json11.hpp"
#include "namespaces.hh"
#include "sstuff.hh"
bool accept_html;
bool complete;
void json(rapidjson::Document& document);
+ json11::Json json();
// checks password _only_.
bool compareAuthorization(const string &expected_password);
HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { };
void setBody(rapidjson::Document& document);
+ void setBody(const json11::Json& document);
};