2015-10-16 06:21:26 +08:00
|
|
|
#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)
|
|
|
|
{
|
2015-10-17 04:55:07 +08:00
|
|
|
// the body is empty and json parsing isn't necessary
|
|
|
|
if (req.body.empty())
|
|
|
|
return true;
|
|
|
|
|
2015-10-18 01:34:00 +08:00
|
|
|
// the body is successfuly parsed
|
2015-10-17 18:53:46 +08:00
|
|
|
if(!req.json.Parse(req.body.c_str()).HasParseError())
|
|
|
|
return true;
|
|
|
|
|
2015-10-18 01:34:00 +08:00
|
|
|
// some kind of parse error occurred
|
|
|
|
// return the error message to the client
|
2015-10-17 18:53:46 +08:00
|
|
|
auto error_str = rapidjson::GetParseError_En(req.json.GetParseError());
|
|
|
|
std::string parse_error = "JSON parse error: " + std::string(error_str);
|
|
|
|
res.send(http::Status::BadRequest, parse_error);
|
2015-10-18 01:34:00 +08:00
|
|
|
|
|
|
|
// stop further execution
|
2015-10-17 18:53:46 +08:00
|
|
|
return false;
|
2015-10-16 06:21:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|