From 670efadc983c837221ca60e1d7c8952e1e86e86e Mon Sep 17 00:00:00 2001 From: Marko Budiselic <mbudiselicbuda@gmail.com> Date: Fri, 16 Oct 2015 00:21:26 +0200 Subject: [PATCH 1/2] RapidJSON as dependency. rapidjson_middleware is responsible for parsing http::request::body into speedy::request::json --- .gitmodules | 3 +++ Makefile | 2 +- speedy/http/request.hpp | 4 ---- speedy/rapidjson | 1 + speedy/rapidjson_middleware.hpp | 29 +++++++++++++++++++++++++++++ speedy/request.hpp | 3 ++- speedy/speedy.hpp | 6 +++++- 7 files changed, 41 insertions(+), 7 deletions(-) create mode 160000 speedy/rapidjson create mode 100644 speedy/rapidjson_middleware.hpp diff --git a/.gitmodules b/.gitmodules index f1aef23f0..85fbffc8a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "speedy/r3"] path = speedy/r3 url = https://github.com/c9s/r3.git +[submodule "speedy/rapidjson"] + path = speedy/rapidjson + url = https://github.com/miloyip/rapidjson.git diff --git a/Makefile b/Makefile index 3c9f33f58..2f57419c7 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CXX=clang++ CFLAGS=-std=c++1y -O2 -Wall -Wno-unknown-pragmas LDFLAGS=-luv -lhttp_parser speedy/r3/.libs/libr3.a -L/usr/local/lib -lpcre -pthread -INC=-I./ +INC=-I./ -I./speedy/rapidjson/include/ SOURCES=memgraph.cpp EXECUTABLE=memgraph diff --git a/speedy/http/request.hpp b/speedy/http/request.hpp index 77020663c..a056f7f93 100644 --- a/speedy/http/request.hpp +++ b/speedy/http/request.hpp @@ -14,12 +14,8 @@ struct Request { Version version; Method method; - std::string url; - std::map<std::string, std::string> headers; - - // todo rename this body std::string body; }; diff --git a/speedy/rapidjson b/speedy/rapidjson new file mode 160000 index 000000000..a5d9971a0 --- /dev/null +++ b/speedy/rapidjson @@ -0,0 +1 @@ +Subproject commit a5d9971a06d83288d1a41f9465dcdadacb562d15 diff --git a/speedy/rapidjson_middleware.hpp b/speedy/rapidjson_middleware.hpp new file mode 100644 index 000000000..0ccb4e23d --- /dev/null +++ b/speedy/rapidjson_middleware.hpp @@ -0,0 +1,29 @@ +#ifndef MEMGRAPH_SPEEDY_RAPIDJSON_MIDDLEWARE_HPP +#define MEMGRAPH_SPEEDY_RAPIDJSON_MIDDLEWARE_HPP + +#include <iostream> + +#include "request.hpp" +#include "response.hpp" +#include "http/status_codes.hpp" +#include "rapidjson/document.h" +#include "rapidjson/error/en.h" + +namespace sp +{ + +bool rapidjson_middleware(sp::Request& req, sp::Response& res) +{ + if (req.json.Parse(req.body.c_str()).HasParseError()) { + const char *errorCode = rapidjson::GetParseError_En(req.json.GetParseError()); + std::string parseError = "JSON parse error: " + std::string(errorCode); + res.send(http::Status::BadRequest, parseError); + return false; + } + + return true; +} + +} + +#endif diff --git a/speedy/request.hpp b/speedy/request.hpp index 9c84bb2b9..424267929 100644 --- a/speedy/request.hpp +++ b/speedy/request.hpp @@ -2,6 +2,7 @@ #define MEMGRAPH_SPEEDY_REQUEST_HPP #include <vector> +#include "rapidjson/document.h" #include "http/request.hpp" @@ -13,8 +14,8 @@ class Request : public http::Request public: using http::Request::Request; - // todo json body insitu parsing // http://rapidjson.org/md_doc_dom.html + rapidjson::Document json; std::vector<std::string> params; }; diff --git a/speedy/speedy.hpp b/speedy/speedy.hpp index 6eb0049cf..1b3bac009 100644 --- a/speedy/speedy.hpp +++ b/speedy/speedy.hpp @@ -18,6 +18,7 @@ #include "response.hpp" #include "middleware.hpp" +#include "rapidjson_middleware.hpp" namespace sp { @@ -32,7 +33,10 @@ public: Speedy(uv::UvLoop::sptr loop, const std::string& prefix = "", size_t capacity = 100) - : server(*loop), prefix(std::move(prefix)), router(capacity) {} + : server(*loop), prefix(std::move(prefix)), router(capacity) + { + middlewares.push_back(rapidjson_middleware); + } Speedy(Speedy&) = delete; Speedy(Speedy&&) = delete; From 634c44c72474612f4aa79754361e2bee4b3c6c80 Mon Sep 17 00:00:00 2001 From: Marko Budiselic <mbudiselicbuda@gmail.com> Date: Fri, 16 Oct 2015 00:34:02 +0200 Subject: [PATCH 2/2] rapidjson middleware small modification --- speedy/speedy.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedy/speedy.hpp b/speedy/speedy.hpp index 1b3bac009..10ae22bd3 100644 --- a/speedy/speedy.hpp +++ b/speedy/speedy.hpp @@ -35,7 +35,7 @@ public: size_t capacity = 100) : server(*loop), prefix(std::move(prefix)), router(capacity) { - middlewares.push_back(rapidjson_middleware); + use(rapidjson_middleware); } Speedy(Speedy&) = delete;