JSON helpers

FossilOrigin-Name: 234c0b083e3993333b564795a008e660fccff1a4e6832f67d4480f7eba21367c
This commit is contained in:
nekobit 2022-10-09 18:53:37 +00:00
parent 6174968fc5
commit a2c5e343d0
4 changed files with 86 additions and 26 deletions

View file

@ -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

28
src/jsonhelper.cpp Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include "jsonhelper.h"
std::string rjson::to_string(rjson::Document& doc)
{
rjson::StringBuffer buf;
rjson::Writer<rjson::StringBuffer> writer(buf);
doc.Accept(writer);
// Make copy into string on return
return buf.GetString();
}

41
src/jsonhelper.h Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#define RAPIDJSON_HAS_STDSTRING 1
#define RAPIDJSON_NAMESPACE rjson
#define RAPIDJSON_NAMESPACE_BEGIN namespace rjson {
#define RAPIDJSON_NAMESPACE_END }
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#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(); */
/* } */
}

View file

@ -16,10 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "jsonhelper.h"
#include <utility>
#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<rapidjson::StringBuffer> 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)