From 584ba10726ea88ee3938040746018a90088dd659 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Sun, 11 May 2014 18:03:05 +0200 Subject: [PATCH] reformatting RequestHandler --- Server/RequestHandler.cpp | 83 ++++++++++++++++++--------------------- Server/RequestHandler.h | 20 +++++----- 2 files changed, 49 insertions(+), 54 deletions(-) diff --git a/Server/RequestHandler.cpp b/Server/RequestHandler.cpp index 3b1938736..181032ec4 100644 --- a/Server/RequestHandler.cpp +++ b/Server/RequestHandler.cpp @@ -40,14 +40,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -// #include #include -RequestHandler::RequestHandler() : routing_machine(NULL) { } +RequestHandler::RequestHandler() : routing_machine(nullptr) {} -void RequestHandler::handle_request(const http::Request& req, http::Reply& rep){ - //parse command - try { +void RequestHandler::handle_request(const http::Request &req, http::Reply &reply) +{ + // parse command + try + { std::string request; URIDecode(req.uri, request); @@ -61,57 +62,51 @@ void RequestHandler::handle_request(const http::Request& req, http::Reply& rep){ time_t ltime; struct tm *Tm; - ltime=time(NULL); - Tm=localtime(<ime); + ltime = time(nullptr); + Tm = localtime(<ime); - 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" : "" ) << - Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "" ) << - Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "" ) << - Tm->tm_sec << " " << req.endpoint.to_string() << " " << - req.referrer << ( 0 == req.referrer.length() ? "- " :" ") << - req.agent << ( 0 == req.agent.length() ? "- " :" ") << request; + 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" : "") + << Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "") << Tm->tm_min + << ":" << (Tm->tm_sec < 10 ? "0" : "") << Tm->tm_sec << " " + << req.endpoint.to_string() << " " << req.referrer + << (0 == req.referrer.length() ? "- " : " ") << req.agent + << (0 == req.agent.length() ? "- " : " ") << request; RouteParameters route_parameters; APIGrammarParser api_parser(&route_parameters); - std::string::iterator it = request.begin(); - const bool result = boost::spirit::qi::parse( - it, - request.end(), - api_parser - ); + auto it = request.begin(); + const bool result = boost::spirit::qi::parse(it, request.end(), api_parser); - if ( !result || (it != request.end()) ) { - rep = http::Reply::StockReply(http::Reply::badRequest); - rep.content.clear(); + if (!result || (it != request.end())) + { + reply = http::Reply::StockReply(http::Reply::badRequest); + reply.content.clear(); const int position = std::distance(request.begin(), it); - rep.content.push_back( - "{\"status\":400,\"status_message\":\"Query string malformed close to position " - ); + reply.content.push_back( + "{\"status\":400,\"status_message\":\"Query string malformed close to position "); std::string tmp_position_string; intToString(position, tmp_position_string); - rep.content.push_back(tmp_position_string); - rep.content.push_back("\"}"); - } else { - //parsing done, lets call the right plugin to handle the request - BOOST_ASSERT_MSG( - routing_machine != NULL, - "pointer not init'ed" - ); - routing_machine->RunQuery(route_parameters, rep); + reply.content.push_back(tmp_position_string); + reply.content.push_back("\"}"); + } + else + { + // parsing done, lets call the right plugin to handle the request + BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed"); + routing_machine->RunQuery(route_parameters, reply); return; } - } catch(const std::exception& e) { - rep = http::Reply::StockReply(http::Reply::internalServerError); - SimpleLogger().Write(logWARNING) << - "[server error] code: " << e.what() << ", uri: " << req.uri; + } + catch (const std::exception &e) + { + reply = http::Reply::StockReply(http::Reply::internalServerError); + SimpleLogger().Write(logWARNING) << "[server error] code: " << e.what() + << ", uri: " << req.uri; return; } } -void RequestHandler::RegisterRoutingMachine(OSRM * osrm) { - routing_machine = osrm; -} +void RequestHandler::RegisterRoutingMachine(OSRM *osrm) { routing_machine = osrm; } diff --git a/Server/RequestHandler.h b/Server/RequestHandler.h index 4d89decc5..462c34ffc 100644 --- a/Server/RequestHandler.h +++ b/Server/RequestHandler.h @@ -30,30 +30,30 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -template -struct APIGrammar; +template struct APIGrammar; struct RouteParameters; class OSRM; -namespace http { - class Reply; - struct Request; +namespace http +{ +class Reply; +struct Request; } class RequestHandler { -public: + public: typedef APIGrammar APIGrammarParser; RequestHandler(); RequestHandler(const RequestHandler &) = delete; - void handle_request(const http::Request& req, http::Reply& rep); - void RegisterRoutingMachine(OSRM * osrm); + void handle_request(const http::Request &req, http::Reply &rep); + void RegisterRoutingMachine(OSRM *osrm); -private: - OSRM * routing_machine; + private: + OSRM *routing_machine; }; #endif // REQUEST_HANDLER_H