diff --git a/api/resources/node.hpp b/api/resources/node.hpp index 60e6874f3..4800fc176 100644 --- a/api/resources/node.hpp +++ b/api/resources/node.hpp @@ -33,8 +33,9 @@ public: // [Atom id=k] k {1, 2, ...} // auto node = atom->first(); - - // TODO read the JSON body and store the properties in the + + // the json body is parsed (req.json) + // TODO store the properties in the // first version // // for(key, value in body) diff --git a/speedy/commands.txt b/speedy/commands.txt new file mode 100644 index 000000000..c8f593814 --- /dev/null +++ b/speedy/commands.txt @@ -0,0 +1,2 @@ +# dump of all commands used in development +clang++ -std=c++1y -g test_2.cpp -o test_2 -luv -lhttp_parser r3/.libs/libr3.a -lpcre -L/usr/local/lib -I../ -I./rapidjson/include diff --git a/speedy/rapidjson_middleware.hpp b/speedy/rapidjson_middleware.hpp index 0ccb4e23d..26caa59e5 100644 --- a/speedy/rapidjson_middleware.hpp +++ b/speedy/rapidjson_middleware.hpp @@ -14,8 +14,12 @@ namespace sp bool rapidjson_middleware(sp::Request& req, sp::Response& res) { + // the body is empty and json parsing isn't necessary + if (req.body.empty()) + return true; + if (req.json.Parse(req.body.c_str()).HasParseError()) { - const char *errorCode = rapidjson::GetParseError_En(req.json.GetParseError()); + auto errorCode = rapidjson::GetParseError_En(req.json.GetParseError()); std::string parseError = "JSON parse error: " + std::string(errorCode); res.send(http::Status::BadRequest, parseError); return false; diff --git a/speedy/response.hpp b/speedy/response.hpp index e56ca3e9e..0b1033228 100644 --- a/speedy/response.hpp +++ b/speedy/response.hpp @@ -4,6 +4,10 @@ #include "request.hpp" #include "http/response.hpp" +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" + namespace sp { @@ -11,6 +15,28 @@ class Response : public http::Response { public: using http::Response::Response; + + std::string json_string(const rapidjson::Document& document) + { + rapidjson::StringBuffer strbuf; + rapidjson::Writer writer(strbuf); + document.Accept(writer); + // TODO: error handling + auto str = strbuf.GetString(); + return str; + } + + void json(http::Status code, const rapidjson::Document& document) + { + auto str = json_string(document); + this->send(code, str); + } + + void json(const rapidjson::Document& document) + { + auto str = json_string(document); + this->send(str); + } }; using request_cb_t = std::function; diff --git a/speedy/test.cpp b/speedy/test_speedy_1.cpp similarity index 100% rename from speedy/test.cpp rename to speedy/test_speedy_1.cpp diff --git a/speedy/test_speedy_2_rapidjson.cpp b/speedy/test_speedy_2_rapidjson.cpp new file mode 100644 index 000000000..db366598c --- /dev/null +++ b/speedy/test_speedy_2_rapidjson.cpp @@ -0,0 +1,23 @@ +#include "speedy.hpp" + +#include "request.hpp" +#include "response.hpp" +#include "rapidjson/document.h" + +int main() { + + uv::UvLoop::sptr loop(new uv::UvLoop()); + http::Ipv4 ip("0.0.0.0", 8765); + sp::Speedy app(loop); + + app.get("/bla", [](sp::Request& req, sp::Response& res) { + rapidjson::Document document; + document.Parse("{ \"test\": \"test\" }"); + res.json(http::Status::Ok, document); + }); + + app.listen(ip); + loop->run(uv::UvLoop::Mode::Default); + + return 1; +}