reformatting RequestHandler

This commit is contained in:
Dennis Luxen 2014-05-11 18:03:05 +02:00
parent 24a61dc650
commit 584ba10726
2 changed files with 49 additions and 54 deletions

View File

@ -40,14 +40,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ctime> #include <ctime>
#include <algorithm> #include <algorithm>
// #include <iomanip>
#include <iostream> #include <iostream>
RequestHandler::RequestHandler() : routing_machine(NULL) { } RequestHandler::RequestHandler() : routing_machine(nullptr) {}
void RequestHandler::handle_request(const http::Request& req, http::Reply& rep){ void RequestHandler::handle_request(const http::Request &req, http::Reply &reply)
//parse command {
try { // parse command
try
{
std::string request; std::string request;
URIDecode(req.uri, request); URIDecode(req.uri, request);
@ -61,57 +62,51 @@ void RequestHandler::handle_request(const http::Request& req, http::Reply& rep){
time_t ltime; time_t ltime;
struct tm *Tm; struct tm *Tm;
ltime=time(NULL); ltime = time(nullptr);
Tm=localtime(&ltime); Tm = localtime(&ltime);
SimpleLogger().Write() << SimpleLogger().Write() << (Tm->tm_mday < 10 ? "0" : "") << Tm->tm_mday << "-"
(Tm->tm_mday < 10 ? "0" : "" ) << Tm->tm_mday << "-" << << (Tm->tm_mon + 1 < 10 ? "0" : "") << (Tm->tm_mon + 1) << "-"
(Tm->tm_mon+1 < 10 ? "0" : "" ) << (Tm->tm_mon+1) << "-" << << 1900 + Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "")
1900+Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "" ) << << Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "") << Tm->tm_min
Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "" ) << << ":" << (Tm->tm_sec < 10 ? "0" : "") << Tm->tm_sec << " "
Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "" ) << << req.endpoint.to_string() << " " << req.referrer
Tm->tm_sec << " " << req.endpoint.to_string() << " " << << (0 == req.referrer.length() ? "- " : " ") << req.agent
req.referrer << ( 0 == req.referrer.length() ? "- " :" ") << << (0 == req.agent.length() ? "- " : " ") << request;
req.agent << ( 0 == req.agent.length() ? "- " :" ") << request;
RouteParameters route_parameters; RouteParameters route_parameters;
APIGrammarParser api_parser(&route_parameters); APIGrammarParser api_parser(&route_parameters);
std::string::iterator it = request.begin(); auto it = request.begin();
const bool result = boost::spirit::qi::parse( const bool result = boost::spirit::qi::parse(it, request.end(), api_parser);
it,
request.end(),
api_parser
);
if ( !result || (it != request.end()) ) { if (!result || (it != request.end()))
rep = http::Reply::StockReply(http::Reply::badRequest); {
rep.content.clear(); reply = http::Reply::StockReply(http::Reply::badRequest);
reply.content.clear();
const int position = std::distance(request.begin(), it); const int position = std::distance(request.begin(), it);
rep.content.push_back( reply.content.push_back(
"{\"status\":400,\"status_message\":\"Query string malformed close to position " "{\"status\":400,\"status_message\":\"Query string malformed close to position ");
);
std::string tmp_position_string; std::string tmp_position_string;
intToString(position, tmp_position_string); intToString(position, tmp_position_string);
rep.content.push_back(tmp_position_string); reply.content.push_back(tmp_position_string);
rep.content.push_back("\"}"); reply.content.push_back("\"}");
} else { }
//parsing done, lets call the right plugin to handle the request else
BOOST_ASSERT_MSG( {
routing_machine != NULL, // parsing done, lets call the right plugin to handle the request
"pointer not init'ed" BOOST_ASSERT_MSG(routing_machine != nullptr, "pointer not init'ed");
); routing_machine->RunQuery(route_parameters, reply);
routing_machine->RunQuery(route_parameters, rep);
return; return;
} }
} catch(const std::exception& e) { }
rep = http::Reply::StockReply(http::Reply::internalServerError); catch (const std::exception &e)
SimpleLogger().Write(logWARNING) << {
"[server error] code: " << e.what() << ", uri: " << req.uri; reply = http::Reply::StockReply(http::Reply::internalServerError);
SimpleLogger().Write(logWARNING) << "[server error] code: " << e.what()
<< ", uri: " << req.uri;
return; return;
} }
} }
void RequestHandler::RegisterRoutingMachine(OSRM * osrm) { void RequestHandler::RegisterRoutingMachine(OSRM *osrm) { routing_machine = osrm; }
routing_machine = osrm;
}

View File

@ -30,30 +30,30 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string> #include <string>
template <typename Iterator, class HandlerT> template <typename Iterator, class HandlerT> struct APIGrammar;
struct APIGrammar;
struct RouteParameters; struct RouteParameters;
class OSRM; class OSRM;
namespace http { namespace http
class Reply; {
struct Request; class Reply;
struct Request;
} }
class RequestHandler class RequestHandler
{ {
public: public:
typedef APIGrammar<std::string::iterator, RouteParameters> APIGrammarParser; typedef APIGrammar<std::string::iterator, RouteParameters> APIGrammarParser;
RequestHandler(); RequestHandler();
RequestHandler(const RequestHandler &) = delete; RequestHandler(const RequestHandler &) = delete;
void handle_request(const http::Request& req, http::Reply& rep); void handle_request(const http::Request &req, http::Reply &rep);
void RegisterRoutingMachine(OSRM * osrm); void RegisterRoutingMachine(OSRM *osrm);
private: private:
OSRM * routing_machine; OSRM *routing_machine;
}; };
#endif // REQUEST_HANDLER_H #endif // REQUEST_HANDLER_H