diff --git a/Server/RequestHandler.cpp b/Server/RequestHandler.cpp index 80cc5786d..a6a1052ae 100644 --- a/Server/RequestHandler.cpp +++ b/Server/RequestHandler.cpp @@ -66,6 +66,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply ltime = time(nullptr); Tm = localtime(<ime); + // log timestamp SimpleLogger().Write() << (Tm->tm_mday < 10 ? "0" : "") << Tm->tm_mday << "-" << (Tm->tm_mon + 1 < 10 ? "0" : "") << (Tm->tm_mon + 1) << "-" << 1900 + Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "") @@ -81,6 +82,7 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply auto it = request.begin(); const bool result = boost::spirit::qi::parse(it, request.end(), api_parser); + // check if the was an error with the request if (!result || (it != request.end())) { reply = http::Reply::StockReply(http::Reply::badRequest); @@ -91,46 +93,39 @@ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply std::string message = ("Query string malformed close to position " + IntToString(position)); json_result.values["status_message"] = message; JSON::render(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 + "("); + reply.content.insert(reply.content.end(), json_p.begin(), json_p.end()); + } + routing_machine->RunQuery(route_parameters, reply); + if (!route_parameters.jsonp_parameter.empty()) + { // append brace to jsonp response + reply.content.push_back(')'); + } + + // set headers + reply.headers.emplace_back("Content-Length", UintToString(reply.content.size())); + if ("gpx" == route_parameters.output_format) + { // gpx file + reply.headers.emplace_back("Content-Type", "application/gpx+xml; charset=UTF-8"); + reply.headers.emplace_back("Content-Disposition", "attachment; filename=\"route.gpx\""); + } + else if (route_parameters.jsonp_parameter.empty()) + { // json file + reply.headers.emplace_back("Content-Type", "application/x-javascript; charset=UTF-8"); + reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.json\""); } else - { - // 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()) - { - const std::string json_p = (route_parameters.jsonp_parameter + "("); - reply.content.insert(reply.content.end(), json_p.begin(), json_p.end()); - } - routing_machine->RunQuery(route_parameters, reply); - - // set headers, still ugly and should be reworked - reply.headers.resize(3); - if ("gpx" == route_parameters.output_format) - { - reply.headers[1].name = "Content-Type"; - reply.headers[1].value = "application/gpx+xml; charset=UTF-8"; - reply.headers[2].name = "Content-Disposition"; - reply.headers[2].value = "attachment; filename=\"route.gpx\""; - } - else if (route_parameters.jsonp_parameter.empty()) - { - reply.headers[1].name = "Content-Type"; - reply.headers[1].value = "application/x-javascript; charset=UTF-8"; - reply.headers[2].name = "Content-Disposition"; - reply.headers[2].value = "inline; filename=\"response.json\""; - } - else - { - reply.content.push_back(')'); - reply.headers[1].name = "Content-Type"; - reply.headers[1].value = "text/javascript; charset=UTF-8"; - reply.headers[2].name = "Content-Disposition"; - reply.headers[2].value = "inline; filename=\"response.js\""; - } - reply.headers[0].name = "Content-Length"; - reply.headers[0].value = IntToString(reply.content.size()); - return; + { // jsonp + reply.headers.emplace_back("Content-Type", "text/javascript; charset=UTF-8"); + reply.headers.emplace_back("Content-Disposition", "inline; filename=\"response.js\""); } } catch (const std::exception &e)