From a2c5e343d0a3be2ea49c71e40f6b6199bd04a2f5 Mon Sep 17 00:00:00 2001 From: nekobit Date: Sun, 9 Oct 2022 18:53:37 +0000 Subject: [PATCH] JSON helpers FossilOrigin-Name: 234c0b083e3993333b564795a008e660fccff1a4e6832f67d4480f7eba21367c --- CMakeLists.txt | 1 + src/jsonhelper.cpp | 28 +++++++++++++++++++ src/jsonhelper.h | 41 +++++++++++++++++++++++++++ src/protocol/webfinger/webfinger.cpp | 42 +++++++++++----------------- 4 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 src/jsonhelper.cpp create mode 100644 src/jsonhelper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b95c3c0..9795e01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ include(Module) # files add_executable(wormhole src/main.cpp + src/jsonhelper.cpp src/database/database.cpp src/database/sqlite/sqlite.cpp src/http/httpserver.cpp diff --git a/src/jsonhelper.cpp b/src/jsonhelper.cpp new file mode 100644 index 0000000..e4046d4 --- /dev/null +++ b/src/jsonhelper.cpp @@ -0,0 +1,28 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "jsonhelper.h" + +std::string rjson::to_string(rjson::Document& doc) +{ + rjson::StringBuffer buf; + rjson::Writer writer(buf); + doc.Accept(writer); + // Make copy into string on return + return buf.GetString(); +} diff --git a/src/jsonhelper.h b/src/jsonhelper.h new file mode 100644 index 0000000..99969fe --- /dev/null +++ b/src/jsonhelper.h @@ -0,0 +1,41 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once +#define RAPIDJSON_HAS_STDSTRING 1 +#define RAPIDJSON_NAMESPACE rjson +#define RAPIDJSON_NAMESPACE_BEGIN namespace rjson { +#define RAPIDJSON_NAMESPACE_END } + +#include +#include +#include +#include + +#define RJSON_NOT_EMPTY(ne, o) if (!(ne).empty()) { o; } + +namespace rjson +{ + std::string to_string(rjson::Document& doc); + + // Bad, ignore this + /* inline Value& move(std::string str, Document::AllocatorType& a) */ + /* { */ + /* return Value(str.c_str(), a).Move(); */ + /* } */ +} diff --git a/src/protocol/webfinger/webfinger.cpp b/src/protocol/webfinger/webfinger.cpp index 919836c..a08ed08 100644 --- a/src/protocol/webfinger/webfinger.cpp +++ b/src/protocol/webfinger/webfinger.cpp @@ -16,10 +16,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include +#include "jsonhelper.h" #include #include "http/mime.h" #include "http/response.h" @@ -34,21 +31,19 @@ using namespace Protocol; namespace { struct Resource { - std::string href; - std::string rel; - std::string type; - std::string tmpl; + std::string&& href; + std::string&& rel; + std::string&& type; + std::string&& tmpl; }; - rapidjson::Value make_resource(Resource&& res, rapidjson::Document::AllocatorType& a) + rjson::Value make_resource(Resource&& res, rjson::Document::AllocatorType& a) { - using Value = rapidjson::Value; - rapidjson::Value resource(rapidjson::kObjectType); - // TODO make helper macro - if (!res.href.empty()) resource.AddMember("href", Value(res.href.c_str(), a).Move(), a); - if (!res.rel.empty()) resource.AddMember("rel", Value(res.rel.c_str(), a).Move(), a); - if (!res.type.empty()) resource.AddMember("type", Value(res.type.c_str(), a).Move(), a); - if (!res.tmpl.empty()) resource.AddMember("template", Value(res.tmpl.c_str(), a).Move(), a); + rjson::Value resource(rjson::kObjectType); + RJSON_NOT_EMPTY(res.href, resource.AddMember("href", res.href, a)); + RJSON_NOT_EMPTY(res.rel, resource.AddMember("rel", res.rel, a)); + RJSON_NOT_EMPTY(res.type, resource.AddMember("type", res.type, a)); + RJSON_NOT_EMPTY(res.tmpl, resource.AddMember("template", res.tmpl, a)); return resource; } } @@ -57,9 +52,9 @@ namespace HTTP::Response Route::webfinger(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg) { using namespace std::string_literals; - rapidjson::Document root(rapidjson::kObjectType); - rapidjson::Document::AllocatorType& a = root.GetAllocator(); - rapidjson::Value links(rapidjson::kArrayType); + rjson::Document root(rjson::kObjectType); + rjson::Document::AllocatorType& a = root.GetAllocator(); + rjson::Value links(rjson::kArrayType); const std::string& resource = req.get.at("resource").string(); @@ -80,14 +75,9 @@ HTTP::Response Route::webfinger(std::any& args, const HTTP::Request& req, const root.AddMember("links", links.Move(), root.GetAllocator()); - root.AddMember("subject", rapidjson::Value(std::string("acct:"s + resource).c_str(), a).Move(), a); - - // Stringify TODO write helper! - rapidjson::StringBuffer buf; - rapidjson::Writer writer(buf); - root.Accept(writer); + root.AddMember("subject", "acct:"s + resource, a); - return HTTP::Response( buf.GetString(), HTTP::MIME::JSON ); + return HTTP::Response( rjson::to_string(root), HTTP::MIME::JSON ); } void Webfinger::init_webfinger(HTTP::Server* server)