RapidJSON as dependency. rapidjson_middleware is responsible for parsing http::request::body into speedy::request::json

This commit is contained in:
Marko Budiselic 2015-10-16 00:21:26 +02:00
parent e71d302251
commit 670efadc98
7 changed files with 41 additions and 7 deletions

3
.gitmodules vendored
View File

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

View File

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

View File

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

1
speedy/rapidjson Submodule

@ -0,0 +1 @@
Subproject commit a5d9971a06d83288d1a41f9465dcdadacb562d15

View File

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

View File

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

View File

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