Add max values for viaroute and trip and reorganize return code handling
"status" is now always: - 200 if the request was successful - 207 if the result is empty (no path found) - 400 if the request is invalid viaroute and trip now have a maximum of 500 and 100 locations respectively. Override with the --max-viaroute-size and --max-trip-size parameters.
This commit is contained in:
+33
-32
@@ -52,6 +52,8 @@ RequestHandler::RequestHandler() : routing_machine(nullptr) {}
|
||||
void RequestHandler::handle_request(const http::request ¤t_request,
|
||||
http::reply ¤t_reply)
|
||||
{
|
||||
osrm::json::Object json_result;
|
||||
|
||||
// parse command
|
||||
try
|
||||
{
|
||||
@@ -93,40 +95,40 @@ void RequestHandler::handle_request(const http::request ¤t_request,
|
||||
const bool result =
|
||||
boost::spirit::qi::parse(api_iterator, request_string.end(), api_parser);
|
||||
|
||||
osrm::json::Object json_result;
|
||||
// check if the was an error with the request
|
||||
if (!result || (api_iterator != request_string.end()))
|
||||
if (result && api_iterator == request_string.end())
|
||||
{
|
||||
// parsing done, lets call the right plugin to handle the request
|
||||
BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed");
|
||||
|
||||
if (!route_parameters.jsonp_parameter.empty())
|
||||
{ // prepend response with jsonp parameter
|
||||
const std::string json_p = (route_parameters.jsonp_parameter + "(");
|
||||
current_reply.content.insert(current_reply.content.end(), json_p.begin(), json_p.end());
|
||||
}
|
||||
|
||||
const int return_code = routing_machine->RunQuery(route_parameters, json_result);
|
||||
json_result.values["status"] = return_code;
|
||||
// 4xx bad request return code
|
||||
if (return_code / 100 == 4)
|
||||
{
|
||||
current_reply.status = http::reply::bad_request;
|
||||
current_reply.content.clear();
|
||||
route_parameters.output_format.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 2xx valid request
|
||||
BOOST_ASSERT(return_code / 100 == 2);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
current_reply = http::reply::stock_reply(http::reply::bad_request);
|
||||
current_reply.content.clear();
|
||||
const auto position = std::distance(request_string.begin(), api_iterator);
|
||||
|
||||
json_result.values["status"] = 400;
|
||||
std::string message = "Query string malformed close to position ";
|
||||
message += std::to_string(position);
|
||||
json_result.values["status_message"] = message;
|
||||
osrm::json::render(current_reply.content, json_result);
|
||||
return;
|
||||
}
|
||||
|
||||
// parsing done, lets call the right plugin to handle the request
|
||||
BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed");
|
||||
|
||||
if (!route_parameters.jsonp_parameter.empty())
|
||||
{ // prepend response with jsonp parameter
|
||||
const std::string json_p = (route_parameters.jsonp_parameter + "(");
|
||||
current_reply.content.insert(current_reply.content.end(), json_p.begin(), json_p.end());
|
||||
}
|
||||
const auto return_code = routing_machine->RunQuery(route_parameters, json_result);
|
||||
if (200 != return_code)
|
||||
{
|
||||
current_reply = http::reply::stock_reply(http::reply::bad_request);
|
||||
current_reply.content.clear();
|
||||
json_result.values["status"] = 400;
|
||||
std::string message = "Bad Request";
|
||||
json_result.values["status_message"] = message;
|
||||
osrm::json::render(current_reply.content, json_result);
|
||||
return;
|
||||
current_reply.status = http::reply::bad_request;
|
||||
json_result.values["status"] = http::reply::bad_request;
|
||||
json_result.values["status_message"] = "Query string malformed close to position " + std::to_string(position);
|
||||
}
|
||||
|
||||
current_reply.headers.emplace_back("Access-Control-Allow-Origin", "*");
|
||||
@@ -166,10 +168,9 @@ void RequestHandler::handle_request(const http::request ¤t_request,
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
current_reply = http::reply::stock_reply(http::reply::internal_server_error);
|
||||
current_reply = http::reply::stock_reply(http::reply::internal_server_error);;
|
||||
SimpleLogger().Write(logWARNING) << "[server error] code: " << e.what()
|
||||
<< ", uri: " << current_request.uri;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user