osrm-backend/src/server/service/match_service.cpp
Michael Krasnyk 5186b9490d Fix syntax error position indicators in parameters queries
To fix #2193 prefix_length member variable has been added to ParsedURL
that is set to the length of "/service/version/profile/" prefix
when the prefix is accepted by the parser.
Also BOOST_FUSION_ADAPT_STRUCT for osrm::server::api::ParsedURL
has been moved from header to url_parser.cpp to speed up compilation
of CUs that do not use the fusion adaption.
2016-04-27 19:52:23 +02:00

74 lines
2.5 KiB
C++

#include "server/service/match_service.hpp"
#include "engine/api/match_parameters.hpp"
#include "server/api/parameters_parser.hpp"
#include "server/service/utils.hpp"
#include "util/json_container.hpp"
#include <boost/format.hpp>
namespace osrm
{
namespace server
{
namespace service
{
namespace
{
std::string getWrongOptionHelp(const engine::api::MatchParameters &parameters)
{
std::string help;
const auto coord_size = parameters.coordinates.size();
const bool param_size_mismatch = constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "hints",
parameters.hints, coord_size, help) ||
constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "bearings",
parameters.bearings, coord_size, help) ||
constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "radiuses",
parameters.radiuses, coord_size, help) ||
constrainParamSize(PARAMETER_SIZE_MISMATCH_MSG, "timestamps",
parameters.timestamps, coord_size, help);
if (!param_size_mismatch && parameters.coordinates.size() < 2)
{
help = "Number of coordinates needs to be at least two.";
}
return help;
}
} // anon. ns
engine::Status MatchService::RunQuery(std::size_t prefix_length, std::string &query, ResultT &result)
{
result = util::json::Object();
auto &json_result = result.get<util::json::Object>();
auto query_iterator = query.begin();
auto parameters =
api::parseParameters<engine::api::MatchParameters>(query_iterator, query.end());
if (!parameters || query_iterator != query.end())
{
const auto position = std::distance(query.begin(), query_iterator);
json_result.values["code"] = "InvalidQuery";
json_result.values["message"] =
"Query string malformed close to position " + std::to_string(prefix_length + position);
return engine::Status::Error;
}
BOOST_ASSERT(parameters);
if (!parameters->IsValid())
{
json_result.values["code"] = "InvalidOptions";
json_result.values["message"] = getWrongOptionHelp(*parameters);
return engine::Status::Error;
}
BOOST_ASSERT(parameters->IsValid());
return BaseService::routing_machine.Match(*parameters, json_result);
}
}
}
}