fix it (again)

pull/300/head
Jeff Becker 5 years ago
parent 66753430ad
commit e1fceb3636
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -3,12 +3,17 @@
#include <memory>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/ostreamwrapper.h>
#include <iostream>
namespace abyss
{
namespace json
{
using Document = rapidjson::Document;
using Value = rapidjson::Value;
using Writer = rapidjson::PrettyWriter< rapidjson::OStreamWrapper >;
} // namespace json
#if __cplusplus >= 201703L
@ -46,7 +51,7 @@ namespace abyss
MakeParser(size_t contentSize);
void
ToString(const json::Document &obj, std::string &out);
ToString(const json::Document &obj, std::ostream &out);
} // namespace json
} // namespace abyss

@ -22,7 +22,7 @@ namespace abyss
{
using Method_t = std::string;
using Params = json::Value;
using Response = json::Document;
using Response = json::Writer;
IRPCHandler(ConnImpl* impl);

@ -16,8 +16,8 @@ struct DemoHandler : public abyss::httpd::IRPCHandler
Response& resp) override
{
llarp::LogInfo("method: ", method);
resp.AddMember("result", abyss::json::Value().SetInt(1),
resp.GetAllocator());
resp.StartObject();
resp.EndObject();
return true;
}
};
@ -32,9 +32,7 @@ struct DemoCall : public abyss::http::IRPCClientHandler
bool
HandleResponse(abyss::http::RPC_Response resp) override
{
std::string body;
abyss::json::ToString(resp, body);
llarp::LogInfo("got response body: ", body);
abyss::json::ToString(resp, std::cout);
return true;
}

@ -46,7 +46,11 @@ namespace abyss
auto& alloc = m_RequestBody.GetAllocator();
m_RequestBody.AddMember("jsonrpc", json::Value().SetString("2.0"),
alloc);
m_RequestBody.AddMember("id", json::Value(abs(rand())), alloc);
llarp::AlignedBuffer< 8 > p;
p.Randomize();
std::string str = p.ToHex();
m_RequestBody.AddMember(
"id", json::Value().SetString(str.c_str(), alloc), alloc);
m_RequestBody.AddMember(
"method", json::Value().SetString(method.c_str(), alloc), alloc);
m_RequestBody.AddMember("params", params, alloc);
@ -212,7 +216,9 @@ namespace abyss
handler->PopulateReqHeaders(m_SendHeaders);
// create request body
std::string body;
json::ToString(m_RequestBody, body);
std::stringstream ss;
json::ToString(m_RequestBody, ss);
body = ss.str();
// request base
char buf[512] = {0};
int sz = snprintf(buf, sizeof(buf),

@ -1,5 +1,5 @@
#include <abyss/json.hpp>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/writer.h>
#include <util/string_view.hpp>
@ -50,12 +50,11 @@ namespace abyss
}
void
ToString(const json::Document& val, std::string& out)
ToString(const json::Document& val, std::ostream& out)
{
rapidjson::StringBuffer s;
rapidjson::Writer< rapidjson::StringBuffer > writer(s);
rapidjson::OStreamWrapper s(out);
rapidjson::Writer< rapidjson::OStreamWrapper > writer(s);
val.Accept(writer);
out = s.GetString();
}
} // namespace json

@ -24,7 +24,9 @@ namespace abyss
bool m_Bad;
std::unique_ptr< abyss::json::IParser > m_BodyParser;
json::Document m_Request;
json::Document m_Response;
std::stringstream m_ResponseBuffer;
rapidjson::OStreamWrapper m_ResponseStream;
abyss::json::Writer m_Response;
enum HTTPState
{
@ -40,7 +42,10 @@ namespace abyss
HTTPState m_State;
ConnImpl(BaseReqHandler* p, llarp_tcp_conn* c, llarp_time_t readtimeout)
: _conn(c), _parent(p)
: _conn(c)
, _parent(p)
, m_ResponseStream(m_ResponseBuffer)
, m_Response(m_ResponseStream)
{
handler = nullptr;
m_LastActive = p->now();
@ -181,19 +186,19 @@ namespace abyss
case json::IParser::eDone:
if(m_Request.IsObject() && m_Request.HasMember("params")
&& m_Request.HasMember("method") && m_Request.HasMember("id")
&& (m_Request["id"].IsString() || m_Request["id"].IsNumber())
&& m_Request["method"].IsString()
&& m_Request["id"].IsString() && m_Request["method"].IsString()
&& m_Request["params"].IsObject())
{
m_Response.SetObject();
m_Response.AddMember("jsonrpc",
abyss::json::Value().SetString("2.0"),
m_Response.GetAllocator());
m_Response.AddMember("id", m_Request["id"],
m_Response.GetAllocator());
m_Response.StartObject();
m_Response.Key("jsonrpc");
m_Response.String("2.0");
m_Response.Key("id");
m_Response.String(m_Request["id"].GetString());
m_Response.Key("result");
if(handler->HandleJSONRPC(m_Request["method"].GetString(),
m_Request["params"], m_Response))
{
m_Response.EndObject();
return WriteResponseJSON();
}
}
@ -208,7 +213,8 @@ namespace abyss
WriteResponseJSON()
{
std::string response;
json::ToString(m_Response, response);
m_ResponseStream.Flush();
response = m_ResponseBuffer.str();
return WriteResponseSimple(200, "OK", "application/json",
response.c_str());
}

@ -224,7 +224,7 @@ namespace llarp
router->ExtractStatus(dump);
if(!dump.Impl.IsObject())
return false;
resp.AddMember("result", dump.Impl.GetObject(), resp.GetAllocator());
dump.Impl.Accept(resp);
return true;
}
@ -234,48 +234,43 @@ namespace llarp
{
exit::Context::TrafficStats stats;
router->exitContext.CalculateExitTraffic(stats);
auto& alloc = resp.GetAllocator();
abyss::json::Value exits;
exits.SetArray();
resp.StartArray();
auto itr = stats.begin();
while(itr != stats.end())
{
abyss::json::Value info, ident;
info.SetObject();
ident.SetString(itr->first.ToHex().c_str(), alloc);
info.AddMember("ident", ident, alloc);
info.AddMember("tx", abyss::json::Value(itr->second.first), alloc);
info.AddMember("rx", abyss::json::Value(itr->second.second), alloc);
exits.PushBack(info, alloc);
resp.StartObject();
resp.Key("ident");
resp.String(itr->first.ToHex().c_str());
resp.Key("tx");
resp.Uint64(itr->second.first);
resp.Key("rx");
resp.Uint64(itr->second.second);
resp.EndObject();
++itr;
}
resp.AddMember("result", exits, alloc);
resp.EndArray();
return true;
}
bool
ListNeighboors(Response& resp) const
{
auto& alloc = resp.GetAllocator();
abyss::json::Value peers;
peers.SetArray();
resp.StartArray();
router->ForEachPeer([&](const ILinkSession* session, bool outbound) {
abyss::json::Value peer;
peer.SetObject();
abyss::json::Value ident_val, addr_val;
resp.StartObject();
auto ident = RouterID(session->GetPubKey()).ToString();
ident_val.SetString(ident.c_str(), alloc);
resp.Key("ident");
resp.String(ident.c_str());
auto addr = session->GetRemoteEndpoint().ToString();
addr_val.SetString(addr.c_str(), alloc);
resp.Key("addr");
resp.String(addr.c_str());
peer.AddMember("addr", addr_val, alloc);
peer.AddMember("ident", ident_val, alloc);
peer.AddMember("outbound", abyss::json::Value(outbound), alloc);
peers.PushBack(peer, alloc);
resp.Key("outbound");
resp.Bool(outbound);
resp.EndObject();
});
resp.AddMember("result", peers, alloc);
resp.EndArray();
return true;
}

@ -13,7 +13,9 @@ namespace llarp
StatusObject::~StatusObject()
{
#ifdef USE_ABYSS
Impl.RemoveAllMembers();
#endif
}
void

@ -4,6 +4,7 @@
#include <abyss/json.hpp>
#endif
#include <vector>
#include <string>
namespace llarp
{
@ -18,7 +19,7 @@ namespace llarp
};
struct Value_t
{
}
};
#endif
struct StatusObject

@ -26,7 +26,7 @@ struct AbyssTestBase : public ::testing::Test
void
AssertMethod(const std::string& meth) const
{
ASSERT_TRUE(meth == method);
ASSERT_EQ(meth, method);
}
void
@ -136,10 +136,12 @@ struct ServerHandler : public abyss::httpd::IRPCHandler
bool
HandleJSONRPC(Method_t method, __attribute__((unused)) const Params& params,
__attribute__((unused)) Response& response)
Response& response)
{
test->AssertMethod(method);
test->called = true;
response.StartObject();
response.EndObject();
return true;
}
};

Loading…
Cancel
Save